Path: Home => AVR-Overview => Hardware => SIO

; Tests the Serial Communication Port

;
; Sends a text over the serial communication port of an AVR with
; 9k6 8N1 and echoes incoming characters on the SIO port of an AVR.
;

; Hardware: Connection between the serial interfaces

; Windows serial communication with HyperTerminal (see text)
; or equivalent terminal program with ANSI ;
.NOLIST
.INCLUDE "8515def.inc"
.LIST
;

; Constants

;
.EQU   fq=4000000 ; Xtal frequency
.EQU   baud=9600 ; Baudrate
.EQU   bdteiler=(fq/(16*baud))-1 ; Baud-Divider
;

; Registers

;
.DEF   mpr=R16 ; Universal register
.DEF   nc=R17 ; Counter
.DEF   c=R18 ; character
;

;Reset-/Interrupt-Vector

   RJMP   main
;
main:   LDI   mpr,bdteiler ; Baudgenerator
   OUT   UBRR,mpr ; Divider set
   LDI   mpr,0b00011000 ; Enable TX and RX
   OUT   UCR,mpr ; to UART Control Register
;

; Send all capital letters

;
   LDI   c,'A' ; first letter
   LDI   nc,90-65+1 ; number of letters
tloop:   SBIS   USR,UDRE ; Jump if transmit buffer empty
   RJMP   tloop ; Wait a bit
   OUT   UDR,c ; Letter to transmit buffer
   INC   c ; next letter
   DEC   nc ; Count letters to send
   BRNE   tloop ; next letter
;

; Wait until character comes in, echo back forever

;
rloop:   SBIS   USR,RXC ; Test RXC-bit for waiting char
   RJMP   rloop ; no character available
   IN   c,UDR ; Read char from UART
rwait:   SBIS   USR,UDRE ; Wait until TX ready
   RJMP   rwait ; TX not ready yet
   OUT   UDR,c ; Send char
   CPI   c,0x0D ; Return-char?
   BRNE   rloop ; No Return, go on
   LDI   c,0x0A ; Load Linefeed
   RJMP   rwait ; Send additional Linefeed
   


©2002 by http://www.avr-asm-tutorial.net