AVR Assembler Source Blog

Ремонт частотных преобразователей AVR Assembler Source Blog: Test_RS232_IO

Friday, October 4, 2013

Test_RS232_IO

                              ;
                              ; 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
                              ;

No comments:

Post a Comment