SCON
SCON is an 8-bit register used to program the start bit, stop bit, and data bits of data framing, among other things
SM0 | SM1 | Mode |
---|---|---|
0 | 0 | Serial mode 0 |
0 | 1 | Serial mode 1, 8-bit data, 1 stop bit, 1 start bit |
1 | 0 | Serial mode 2 |
1 | 1 | Serial mode 3 |
Transmitting, 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
- TMOD register is loaded with the value
20H
, indicating the use of timer 1 in mode 2(8-bit auto-reload) to set baud rate - The TH1 is loaded with one of the values to set baud rate for serial data transfer
- The SCON register is loaded with the value
50H
, indicating serial mode 1, where an 8-bit data is framed with start and stop bits - TR1 is set to 1 to start timer 1
- TI is cleared by
CLR TI
instruction - The character byte to be transfered serially is written into SBUF register
- The TI flag bit is monitored with the used of instruction
JNB TI, xx
to see if the character has been transferred completely - To tranfer the next byte, go to step 5.
MOV TMOD,#20H ;timer 1,mode 2(auto reload)
MOV TH1,#-6 ;4800 baud rate
MOV SCON,#50H ;8-bit, 1 stop, REN enabled
SETB TR1 ;start timer 1
AGAIN: MOV SBUF,#”A” ;letter “A” to transfer
HERE: JNB TI,HERE ;wait for the last bit
CLR TI ;clear TI for next char
SJMP AGAIN ;keep sending A
Receiving, RI(received interrupt) is raised when the entire frame of data, including the stop bit, is received
- TMOD register is loaded with the value
20H
, indicating the use of timer 1 in mode 2 (8-bit auto-reload) to set baud rate - TH1 is loaded to set baud rate
- The SCON register is loaded with the value
50H
indicating serial mode 1, where an 8-bit data is framed with start and stop bits - TR1 is set to 1 to start timer 1
- RI is cleared by
CLR RI
instruction - The RI flag bit is monitored with the use of instruction
JNB RI, xx
to see if an entire character has been received yet - When RI is raised, SBUF has the byte, its contents are moved into a safe place
- To receive the next character, go to step 5
MOV TMOD,#20H ;timer 1,mode 2(auto reload)
MOV TH1,#-6 ;4800 baud rate
MOV SCON,#50H ;8-bit, 1 stop, REN enabled
SETB TR1 ;start timer 1
HERE: JNB RI,HERE ;wait for char to come in
MOV A,SBUF ;saving incoming byte in A
MOV P1,A ;send to port 1
CLR RI ;get ready to receive next
;byte
SJMP HERE ;keep getting data
Page Source