I am studying serial communication in 8051 using UART and interrupts. Today I came across this code in which author says he is constantly transfering data coming on Port 0. The way transfer is occuring, I think is voilating the rules of serial communication in 8051.
org 00h
ljmp main
org 23h
ljmp serial_ISR
org 30h
main:
mov TMOD,#20h
mov TH1,#-03h
mov SCON,#50h
setb IE.7
setb IE.4
setb TR1
back:
mov A,P0
mov SBUF,A
sjmp back
serial_ISR:
jb TI,trans
mov R0,SBUF
clr RI
RETI
trans:
clr TI
RETI
The thing that is confusing me is, in back
label we constantly writing on SBUF register which is voilating the rule that we should not write on SBUF until the previous data has been sent (which is notified by the TI flag).
Is constantly writing data on SBUF register in above code valid? will UART send correct data?
Regards
You are definitely right, the code inside back label should be rewritten like this:
back:
jb TI,$
mov A,P0
mov SBUF,A
sjmp back
Coding back
label like I did before guarantee you that you are not going to move any data to SBUF until it finishes sending the last data.
There is one detail here to take into account, remember that serial port interrrupts (by receiving or transmitting) are not cleared automatically, so in the code before I am assuming that you cleared the TI interrupt flag manually.