UART on the 8051
Configuration
In order to configure the following registers must be initialized:
SCON (Serial port control register)
Configuration: 01010000
- SM0, SM1: 01
- Mode 1 (8-bit UART)
- SM2: 0
- Disable multiprocessor communication (modes 2/3 only)
- REN: 1
- Enable serial reception
- TB8: 0 - 9th data bit to be sent - not used for 8-bit UART
- RB8: 0
- 9th received data bit
- Not used for 8-bit UART
- TI: 0
- Transmit interrupt flag
- Set by hardware when byte has been transmitted
- Must be cleared by software
- RI: 0
- Receive interrupt flag
- Set by hardware when byte has been received
- Must be cleared by software
TMOD (Timer/counter mode control register)
Since UART uses Timer 1, we only need to configure the upper nibble.
Configuration: 0010XXXX
- Timer 1 configuration
- GATE: 0
- Disable gating control
- C/T#: 0
- Select timer operation
- M1, M0: 10
- Select mode 2 (auto reload)
- Timer 0 configuration
- Don't care
TCON
Configuration: 01XX0XXX
- TCON.7: 0
- Timer 1 overflow flag
- Set by hardware, should be cleared by software
- TCON.6: 1
- Timer 1 run control bit
- Set by software to start timer
- TCON.3: 0
- Interrupt 1 edge flag
- Set by hardware, should be cleared by software
- TCON.2: X
- Interrupt 1 type control bit - we are not using interrupts on timer 1.
TH1
$$ TH1 = 256 - \frac{K(f_{OSC})}{384f_{BAUD}} $$
- For $f_{BAUD}=9600$, $K=1$, and $f_{OSC}=11.0592MHz$, $TH1=253$.
Receiving
- To receive a byte through UART, move the contents of the SBUF register to another register.
mov A, SBUF ; Store the received byte in the accumulator.
Transmitting
- To transmit a byte through UART, move some data to the SBUF register.
mov SBUF, A ; Transmit the byte stored in the accmulator.