Wednesday, March 23, 2016

8051 Programming Serial Interrupt

TI (transfer interrupt) is raised when the last bit of the framed data, the stop bit, is transferred, indicating that the SBUF register is ready to transfer the next byte. RI (received interrupt), is raised when the entire frame of data, including the stop bit, is received. In other words, when the SBUF register has a byte, RI is raised to indicate that the received byte needs to be picked up before it is lost (overrun) by new incoming serial data. In the 8051 only one interrupt is set aside for serial communication. This interrupt is used to both send and receive data. If the interrupt bit in the IE register (IE.4) is enabled, when RI or TI is raised the 8051 gets interrupted and jumps to memory address location 0023H to execute the ISR. In that ISR we must examine the TI and RI flags to see which one caused the interrupt and respond accordingly.

; wake up and go to main, avoid using memory space allocated for ISR
ORG 0H
LJMP MAIN; bypass ISR

; ISR for timer 0
ORG 0023H
LJMP ISR_SERIAL

; the main program for initialization
ORG 30H
MAIN : MOV R1, #30H ; start of serial buffer
MOV R2, #0H ; clear serial byte counter
MOV TMOD, #20H; timer 1, auto re-load mode
MOV TH1, #-6 ; 4800 baud rate
MOV SCON, #50H ; 8-bit, 1 stop, REN enabled
MOV IE, #10010000B ; enable serial interrupt
SETB TR1; start timer 1
HERE :MOV A, R2
JZ HERE
DEC R2
DEC R1
MOV SBUF, @R1 ; send the received byte
SJMP HERE ; keep doing it

; Serial port ISR
ORG 100H
ISR_SERIAL : JB TI, TRANSMIT ; jump if TI is high
MOV @R1, SBUF; otherwise due to receive
; store the received byte in RAM
INC R1; point to next location in buffer
INC R2; increment byte counter
CLR RI
RETI; return from interrupt
TRANSMIT :CLR TI
RETI; return from interrupt
END



Related topics:
8051 Loop   |   8051 Delay   |   8051 IO Port Programming   |   8051 Timer Programming   |   8051 Counter   |   8051 Serial Port Programming   |   8051 Programming Timer Interrupt   |   8051 Programming External Interrupt

List of topics: 8051

No comments:

Post a Comment