AVR Hardware UART Example

The following little program illustrates how to use the hardware UART provided in the larger AVR micro-controllers. It's been tried in the 2313, and should work in all the other members of the family.

;test program for Duplex UART

     	.include        "2313def.inc"

     	ser             r16                    	;port B all outputs
     	out            	ddrb,r16
	ldi             r16,RAMEND         	;initialise stack pointer
	out             spl,r16
	sbi             ucr,txen                ;enable UART transmitter
	sbi             ucr,rxen                ;enable UART receiver
	ldi             r16,5000000/(9600*16)-1 ;set baud rate
	out             ubrr,r16

     	rcall           serin
	rcall           serout
	rjmp            loop                    ;do forever

;serial output routine - output byte in r16 to UART

	out             udr,r16         	;load UART data register
	sbis            usr,udre        	;if UART data register empty bit is clear
	rjmp            serout1         	;       loop back
						;else return

;serial input routine - input byte from UART to r16

        sbis            usr,rxc     		;if UART Receive Complete bit is clear
        rjmp            serin       		;       loop back
        in              r16,udr     		;else get received byte in r16
        ret                         		;and return

Return to home page.

Hosting by WebRing.