8051 has a serial port to transfer and receive data serially. The 8051 transfers and receives data serially at many different baud rates. The baud rate in the 8051 is programmable. This is done with the help of Timer 1.
In the case of XTAL = 11.0592 MHz, the machine cycle frequency is 11.0592 MHz / 12 = 921.6 kHz 921.6 kHz . The 8051 ‘s serial communication UART circuitry divides the machine cycle frequency of 921.6 kHz by 32 once more before it is used by Timer 1 to set the baud rate. Therefore, 921.6 kHz divided by 32 gives 28,800 Hz. When Timer 1 is used to set the baud rate it must be programmed in mode 2, that is 8-bit, auto-reload.
For XTAL = 11.0592 MHz , Timer 1 TH1 register values for various Baud rates are listed below.
Serial Buffer Register:
SBUF is an 8-bit register used solely for serial communication in the 8051. For a byte of data to be transferred via the TxD line, it must be placed in the SBUF register. Similarly, SBUF holds the byte of data when it is received by the 8051 ‘s RxD line.
The moment a byte is written into SBUF, it is framed with the start and stop bits and transferred serially via the TxD pin. Similarly, when the bits are received serially via RxD, the 8051 deframes it by eliminating the stop and start bits, making a byte out of the data received, and then placing it in the SBUF.
Serial Control Register:
The SCON register is an 8-bit register used to program the start bit, stop bit, and data bits of data framing, serial modes and interrupt.
Programming 8051 to transfer data serially:
Programming 8051 to receive data serially:
Doubling Baud Rate:
There is a software way to double the baud rate of the 8051 while the crystal frequency is fixed. This is done using SMOD bit in PCON register. When the 8051 is powered up, D7 (SMOD bit) of the PCON register is zero. We can set it to high by software and thereby double the baud rate.
In the case of XTAL = 11.0592 MHz, the machine cycle frequency is 11.0592 MHz / 12 = 921.6 kHz 921.6 kHz . The 8051 ‘s serial communication UART circuitry divides the machine cycle frequency of 921.6 kHz by 32 once more before it is used by Timer 1 to set the baud rate. Therefore, 921.6 kHz divided by 32 gives 28,800 Hz. When Timer 1 is used to set the baud rate it must be programmed in mode 2, that is 8-bit, auto-reload.
For XTAL = 11.0592 MHz , Timer 1 TH1 register values for various Baud rates are listed below.
Baudrate | TH | |
---|---|---|
bps | Decimal | Hex |
9600 | -3 | FD |
4800 | -6 | FA |
2400 | -12 | F4 |
1200 | -24 | E8 |
Serial Buffer Register:
SBUF is an 8-bit register used solely for serial communication in the 8051. For a byte of data to be transferred via the TxD line, it must be placed in the SBUF register. Similarly, SBUF holds the byte of data when it is received by the 8051 ‘s RxD line.
MOV SBUF, #’D’ ; load SBUF = 44h, ASCII for ‘D’
MOV SBUF, A ; copy accumulator in to SBUF
MOV A, SBUF ; copy SBUF in to accumulator
The moment a byte is written into SBUF, it is framed with the start and stop bits and transferred serially via the TxD pin. Similarly, when the bits are received serially via RxD, the 8051 deframes it by eliminating the stop and start bits, making a byte out of the data received, and then placing it in the SBUF.
Serial Control Register:
The SCON register is an 8-bit register used to program the start bit, stop bit, and data bits of data framing, serial modes and interrupt.
Programming 8051 to transfer data serially:
- The TMOD register is loaded with the value 20H, indicating the use of Timer1 in mode 2 (8-bit auto-reload) to set the baud rate.
- The TH1 is loaded with one of the values in above table to set the 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 the “CLR TI” instruction.
- The character byte to be transferred serially is written into the SBUF register.
- The TI flag bit is monitored to see if the character has been transferred completely.
ORG 0H
MOV TMOD, #20H ; timer 1, auto re-load mode
MOV TH1, #-6 ; 4800 baud rate
MOV SCON, #50H ; 8-bit, 1 stop, REN enabled
SETB TR1 ; start timer 1
CLR TI
AGAIN : MOV SBUF, #’A’ ; letter ‘A’ to be transferred
HERE : JNB TI, HERE ; wait for transfer complete
CLR TI ; clear TI for next char
SJMP AGAIN ; keep sending A
END
Programming 8051 to receive data serially:
- The TMOD register is loaded with the value 20H, indicating the use of Timer1 in mode 2 (8-bit auto-reload) to set the baud rate.
- The TH1 is loaded with one of the values in above table to set the 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.
- RI is cleared by the “CLR RI” instruction.
- The character byte to be transferred serially is written into the SBUF register.
- The RI flag bit is monitored 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.
ORG 0H
MOV TMOD, #20H ; timer 1, auto re-load mode
MOV TH1, #-6 ; 4800 baud rate
MOV SCON, #50H ; 8-bit, 1 stop, REN enabled
SETB TR1 ; start timer 1
CLR RI
HERE : JNB RI, HERE ; wait for char to come in
MOV A, SBUF ; save byte in A
CLR RI ; clear RI to receive next char
MOV SBUF, A ; send the received byte
HERE1 : JNB TI, HERE1 ; wait for transfer complete
CLR TI ; clear TI for next char
SJMP HERE ; keep receiving
END
Doubling Baud Rate:
There is a software way to double the baud rate of the 8051 while the crystal frequency is fixed. This is done using SMOD bit in PCON register. When the 8051 is powered up, D7 (SMOD bit) of the PCON register is zero. We can set it to high by software and thereby double the baud rate.
MOV A, PCON
SETB ACC.7
MOV PCON, A
Baudrate | TH | ||
---|---|---|---|
SMOD = 0 | SMOD = 1 | Decimal | Hex |
9600 | 19200 | -3 | FD |
4800 | 9600 | -6 | FA |
2400 | 4800 | -12 | F4 |
1200 | 2400 | -24 | E8 |
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 Serial Interrupt | 8051 Programming External Interrupt
List of topics: 8051
No comments:
Post a Comment