- 8051 has integrated Universal Asynchronous Receiver Transmitter (UART) or Serial Port
- Serial port mode of operation and baud rate can be configured
SCON | ||
---|---|---|
Bit | Name | Function |
7 | SM0 | Serial port mode bit 0 (see below) |
6 | SM1 | Serial port mode bit 1 (see below) |
5 | SM2 | Mutliprocessor Communications Enable. When set the "RI" flag will only be triggered if the 9th bit received was a "1" |
4 | REN | Receiver Enable. This bit must be set in order to receive characters |
3 | TB8 | Transmit bit 8. The 9th bit to transmit in mode 2 and 3 |
2 | RB8 | Receive bit 8. The 9th bit received in mode 2 and 3 |
1 | TI | Transmit Flag. Set when a byte has been completely transmitted |
0 | RI | Receive Flag. Set when a byte has been completely received |
SM1 | SM0 | Mode | Mode Description | Baud Rate |
---|---|---|---|---|
0 | 0 | 0 | 8-bit Shift Register | Oscillator / 12 |
0 | 1 | 1 | 8-bit UART | Set by Timer 1 (*) |
1 | 0 | 2 | 9-bit UART | Oscillator / 64 (*) |
1 | 1 | 3 | 9-bit UART | Set by Timer 1 (*) |
Serial Port Initialization:
- Set mode in SCON register
- Set 9th bit for mode 2 and 3
- Mode 0: Oscillator / 12 = 11.059 MHz/12 = 921,583 baud
- Mode 2: Oscillator / 64 = 11.059 MHz/64 = 172,797 baud
- Mode 1 & 3: Baud rate is determined by how frequently timer 1 overflows. The most common method is to configure timer 1 in 8-bit auto reload mode and set a reload value (TH1) that causes Timer 1 to overflow at a frequency appropriate to generate a baud rate.
TH1 = 256 - ((Crystal / 384) / Baud)
If PCON.7 is set then TH1 = 256 - ((Crystal / 192) / Baud)
To achieve 19200,
TH1 = 256 - ((Crystal / 192) / Baud)
TH1 = 256 - ((11059000 / 192) / 19200)
TH1 = 256 - ((57699) / 19200)
TH1 = 256 - 3 = 253
Transmit Data:
- To send a byte to the serial port one must simply write the value to the SBUF (99h) SFR
- Wait until TI bit is set
- Wait for the 8051 to set the RI flag
- Read the value stored in the SBUF (99h) SFR
Related topics:
8051 Register Bank and Stack | 8051 Flags Bits | 8051 Program Flow | 8051 Timers | 8051 Interrupts
List of topics: 8051
No comments:
Post a Comment