ATmega128__UART1
Istvan Retaller
istvan.retaller at evoran.hu
Tue Apr 29 11:33:59 CEST 2003
2003.04.29. 1:59:41, "HWSW Famulus" <hwsw at famulus.hu> wrote:
Szia, KJ!
Itt van az RS232.inc mellekelve.
En azt talaltam ki, hogy ne legyen felesleges kod a programomban,
hogy macronak irok meg minden funkciot. Igy az egeszet include-
olva (milyen szep is ez a szo...) nulla a helyfoglalas, ha nem
hasznalok semmit. Ha pl. csak rx vagy csak tx kell az applikaciomba,
akkor csak arra a makrora hivatkozok, csak az fordul le es igy csak
a szukseges merteku a helyfoglalas.
Szoval ezert macro.
Egyebkent a dolog ugy mukodik, hogy minden vett byte ujrainditja
a timert es ha egy bizonyos ideig nincs bejovo RS232, azaz a string
veget ert, akkor bebillent egy flaget.
Udv, RI
Itt az include file:
;----------- --------------
InitUART macro
;----------- --------------
out UBRR,R16 ;UART Baud rate generator
ldi R16,11011000b ;UART Interrupt Enables and UART Settings
out UCR,R16 ;RX enabled
;TX enabled
;RX IT enabled
;TX IT enabled
endm
;----------- --------------
BaudTable macro
;----------- --------------
;UBRR = (f/16*baud)-1
if freq=4000
db 51 ;4800 Baud
db 25 ;9600 Baud
db 12 ;19200 Baud
db 6 ;38400 Baud
else
db 79 ;4800 Baud
db 39 ;9600 Baud
db 19 ;19200 Baud
db 9 ;38400 Baud
endif
endm
;----------- --------------
RStimer macro
;----------- --------------
local __RStoex, __RSto1
lds R18,RSTIMOVR
sbrc R18,7 ;The first received character will clear bit7.
rjmp __RStoex ;if bit7 is set, countdown is prohibited.
tst R18
breq __RStoex ;if 0, no countdown
dec R18
brne __RStoex ;if not reached zero, continue countdown
lds R16,FLAGS+1
ori R16,1 ;indicate event
sts FLAGS+1,R16
lds R16,ONRSREC
lds R17,ONRSREC+1
cp R16,efef
brne __RSto1
cp R17,efef
breq __RStoex
__RSto1
push R18
rcall StartTask
pop R18
__RStoex
sts RSTIMOVR,R18
endm
;----------- --------------
RX_INI macro
;----------- --------------
sts RXLEN,nulla
sts (RXLEN+1),nulla
sts RSTIMOVR,efef
lds R16,SYSFLAG
sbrc R16,HANDSHAKE
cbi PORTD,RTS
ret
endm
;----------- --------------
RXC_IT macro
;----------- --------------
_tout_ equ 40 ;about n*10 ms. N cannot reach 80h due to the special meaning of bit7.
local _rxc_ex
pushf
push XL
push XH
ldi R16,_tout_ ;this way bit7 is cleared and timeout is re-initialized
sts RSTIMOVR,R16
in R16,UDR
lds XL,RXLEN
cpi XL,0ffh-2
brcs _rxc_1
lds XH,SYSFLAG
sbrc XH,HANDSHAKE
sbi PORTD,RTS
_rxc_1:
cpi XL,0ffh ;comparise seems to be useless, however it is important if there is no
handshake
breq _rxc_ex
ldi XH,high RSRXBUF
st X+,R16
sts RXLEN,XL
_rxc_ex:
pop XH
pop XL
popf
reti
endm
;----------- --------------
TXC_IT macro
;----------- --------------
local _txc_ex
pushf
push R17
push XL
push XH
lds R16,SYSFLAG
sbrs R16,HANDSHAKE
rjmp _txc_1
sbic PIND,CTS
rjmp _txc_ex
_txc_1:
lds R16,TXLEN
tst R16
breq _txc_ex
lds XL,TXPTR
ldi XH,high RSTXBUF
ld R17,X+
out UDR,R17
sts TXPTR,XL
dec R16
sts TXLEN,R16
_txc_ex:
pop XH
pop XL
pop R17
popf
reti
endm
More information about the Elektro
mailing list