Full Version : RS232 I/O Routines (AVR ASM)
avr >>ASSMBLER ROUTINES >>RS232 I/O Routines (AVR ASM)


AVR_Admin- 05-07-2006
I used the RS232 serial communication protocol, so I bought MAX232 to generate RS232 logiv form (+8,5 for '1' and -8,5 for '0'), converted from AVR's output. The USART communication will be the good choice.

I've display the ASCII character saved in AVR's .db directive to the hyperterminal window.

I plan to build datalogger, visual interface. The graphic will show the AVR's ADC value for some sampled time, using Borland Delphi 6.

So, this is my code, in assembler :
CODE

.include "m8535def.inc"

.cseg
.org 0
rjmp main

main:
ldi tmp,low(ramend)
out spl,tmp
ldi tmp,high(ramend)
out sph,tmp

rcall init_usart

loop:
ldi zl,low(2*msg)
ldi zh,high(2*msg)
load:
lpm
mov txbyte,r0
cpi txbyte,0
breq done
rcall usart_tx
inc zl
rjmp load
done:
rcall usart_rx
mov txbyte,rxbyte
rcall usart_tx
rjmp done

.include "C:\Program Files\Atmel\AVR Tools\AvrStudio4\usart3\usart_lib.asm"

msg:
.db "***dicoba 1, 2, 3 ...***",13,10,0

and the included file :

;====================================
;USART Library for ATMEGA8535
;====================================
;=============
; usart init
;=============
;=====================================
; konfigurasi usart
;=====================================
.def tmp = r16
.def txbyte = r17
.def rxbyte = r18
.equ fclock = 11059200
.equ baud_rate = 9600
.equ ubbr_value = (fclock/(16*baud_rate)) - 1

init_usart:
; Set baud rate
ldi tmp,high(ubbr_value)
out UBRRH, tmp
ldi tmp,low(ubbr_value)
out UBRRL, tmp
; Enable Receiver and Transmitter
ldi tmp, 0b00011000
out UCSRB,tmp
; Set frame format: 8data, 1stop bit
ldi tmp, 0b10000110
out UCSRC,tmp
ret

;=====================
; usart transmit data
;=====================
usart_tx:
; Wait for empty transmit buffer
sbis UCSRA,UDRE
rjmp usart_tx
; Put data into buffer, sends the data
out UDR,txbyte
ret

;======================
; usart receive data
;======================
usart_rx:
; Wait for data to be received
sbis UCSRA, RXC
rjmp usart_rx
; Get and return received data from buffer
in rxbyte, UDR
ret

;=======================
; newline
;=======================
newline:
ldi txbyte,13
rcall usart_tx
ldi txbyte,10
rcall usart_tx
ret



Forumer™ is Voted #1 Free Forum Hosting provider
Build your own community today with the largest message board hosting company.