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

;
; Test of the Serial IO
;
; Receives charactern from the SIO with 9k6 8N1 and sends back
; their hex values as text
;
; Hardware: Serial connection between the board and a terminal
; Communication: Terminal program, e.g. HyperTerminal
;
.NOLIST
.INCLUDE "8515def.inc"
.LIST
;
; Constants
;
.EQU   fq=4000000 ; XTal-frequency
.EQU   baud=9600 ; Baudrate
.EQU   bdteiler=(fq/(16*baud))-1 ; Baud-Divider
.EQU   RamStart = 0x0060
;
; Register
;
.DEF   mpr=R16 ; Universal register
.DEF   cc=R17 ; Char copy
.DEF   h=R18 ; Various values
;
; XL/XH = R26/R27 are used as Pointer to the SRAM (input buffer position)
; YL/YH = R28/R29 are used as Pointer to the SRAM (output buffer position)
;
; Program code starts here
;
.CSEG
;
; Reset-Vector
;
   RJMP   main ; Reset-vector
;
main:
   LDI   XH,HIGH(RamStart)
   LDI   XL,LOW(RamStart)
   LDI   YH,HIGH(RamStart)
   LDI   YL,LOW(RamStart)
   LDI   mpr,0x0D ; Start with a new line
   ST   X+,mpr ; put to the SRAM buffer and
  inc input pointer
   LDI   mpr,0x0A ; additional linefeed
   ST   X+,mpr
   LDI   mpr,bdteiler ; Set baudrate generator
   OUT   UBRR,mpr ; to divider port
   LDI   mpr,0b00011000 ; Enable TX and RX
   OUT   UCR,mpr ; to UART Control Register
;
; Main program loop asks UART for characters and transmits buffered chars
  in SRAM
;
tloop:
   SBIC   USR,RXC ; Jump if receiver is empty
   RJMP   rx ; Receive the next char
   SBIC   USR,UDRE ; Jump if the transmitter is not ready to receive chars
   RJMP   tx ; Send next char
   RJMP   tloop ; All over again
;
; Receive a char and store it in the SRAM buffer
;
rx:
   LDI   mpr,' ' ; Transmits a blank as separator
   ST   X+,mpr ; Store it in the SRAM
  buffer and inc the pointer
   IN   mpr,UDR ; Get a char from the UART
  receiver port
   MOV   cc,mpr ; Make a copy of that char
   SWAP   mpr ; Swap upper and lower nibble
  of the char
   ANDI   mpr,0x0F ; Delete the upper nibble part
   CPI   mpr,10 ; Nibble > 9?
   BRCS   rx1 ; No
   LDI   h,7 ; Add 7 to get hex A to F
   ADD   mpr,h
rx1:
   LDI   h,'0' ; from 0 to '0'
   ADD   mpr,h
   ST   X+,mpr ; and copy to SRAM
   ANDI   cc,0x0F ; Same procedure with the lower nibble
   CPI   cc,10
   BRCS   rx2
   LDI   h,7
   ADD   cc,h
rx2:
   LDI   h,'0'
   ADD   cc,h
   ST   X+,cc
   LDI   cc,'h' ; Send 'h' to signal hex
   ST   X+,cc ; and copy to SRAM buffer
   RJMP   tloop ; and return to the main program loop
;
; Send characters stored in the SRAM-buffer, if there are such waiting
;
tx:
   CP   XL,YL ; Compare input and output position
   BREQ   tx1 ; No chars available
   LD   mpr,Y+ ; Get a char from the SRAM and
  inc the output pointer
   OUT   UDR,mpr ; Transfer this char to the transmitter
  port
   RJMP   tloop ; and return back to the main program loop
tx1:
   LDI   XH,HIGH(RamStart) ; All sent, set the
     pointer to the beginning of the SRAM
   LDI   XL,LOW(RamStart)
   LDI   YH,HIGH(RamStart)
   LDI   YL,LOW(RamStart)
   RJMP   tloop ; and return to the main program loop
;
; End Of Code
;


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