Thursday, March 17, 2016

8051 Serial Port

  • 8051 has integrated Universal Asynchronous Receiver Transmitter (UART) or Serial Port
  • Serial port mode of operation and baud rate can be configured
Registers:
SCON
BitNameFunction
7SM0Serial port mode bit 0 (see below)
6SM1Serial port mode bit 1 (see below)
5SM2Mutliprocessor Communications Enable. When set the "RI" flag will only be triggered if the 9th bit received was a "1"
4RENReceiver Enable. This bit must be set in order to receive characters
3TB8Transmit bit 8. The 9th bit to transmit in mode 2 and 3
2RB8Receive bit 8. The 9th bit received in mode 2 and 3
1TITransmit Flag. Set when a byte has been completely transmitted
0RIReceive Flag. Set when a byte has been completely received

SM1SM0ModeMode DescriptionBaud Rate
0008-bit Shift RegisterOscillator / 12
0118-bit UARTSet by Timer 1 (*)
1029-bit UARTOscillator / 64 (*)
1139-bit UARTSet by Timer 1 (*)
(*) Baud rate is doubled if PCON.7 (SMOD) is set.

Serial Port Initialization:
  • Set mode in SCON register
  • Set 9th bit for mode 2 and 3
Set Baud Rate:
  • 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.
The following equation is used to determine the TH1 value.
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
Receive Data:
  • 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