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.
;uart.asm
;
;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
loop:
rcall serin
rcall serout
rjmp loop ;do forever
;serial output routine - output byte in r16 to UART
serout:
out udr,r16 ;load UART data register
serout1:
sbis usr,udre ;if UART data register empty bit is clear
rjmp serout1 ; loop back
;else return
ret
;serial input routine - input byte from UART to r16
serin:
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