MTS 311 Week 6 UART
MTS 311 Week 6 UART
4
Serial Vs Parallel Data Transfer
Parallel Serial
Fast Slow
5
Serial Vs Parallel Data Transfer
Parallel Serial
6
Serial Vs Parallel in 8051
Parallel Serial
8 Bits can be sent at a time 1 bit can be sent at a time
Synchronous
Asynchronous
8
Synchronous Communication
Examples
Devices using I2C (IIC), SPI
9
Asynchronous Communication
Examples
Serial Port in PCs (UART)
10
Format of Asynchronous Transmission
Data is transmitted serially in the
following order:
11
Communication Modes
Simplex
Data is transmitted in only one pre-assigned direction
e-g keyboard, mouse with PC
Half Duplex
Data is transmitted in either direction, one direction at a time.
e-g walkie-talkies sets
Full Duplex
Data is transmitted in both directions at a time
e-g telephone
12
Baud Rate
13
Serial Communication
TTL Voltages
Input
LOW: 0 – 0.8V
HIGH: 2 – 5V
Output
LOW: 0 – 0.4V
HIGH: 2.4 – 5V
14
Serial Communication
RS 232 Standard
LOW: +3 to +25V
HIGH: - 3 to - 25V
Difference is increased
MAX232 or MAX233
ICs (and many others)
convert the TTL level
to RS 232 level
Ratings
50 ft
20 K bits / second
Assignment: Read about RS485
15
Hardware Access
Hardware access to serial port in 8051 is through pins:
RXD – P3.0
Receive Serial data
TXD – P3.1
Transmit Serial data
16
Software Access
Serial SFRs
SBUF
Serial Buffer
Stores the data received, or the data to be transmitted
SCON
Serial Control
Timer1
To set Baud Rate
17
SCON – Serial Control SFR
18
Serial Mode
19
PCON
20
Mode 0 – 8 Bit Shift Register Mode
Synchronous Communication
21
Mode 0 – Shift Register
Example
If we are using 12 Mhz clock, frequency of TXD is 1 Mhz
22
Configuration Bits of SCON
SM2
Flag for multiprocessor communication
SM2 bit allows the microcontroller to distinguish between
address and data bytes in a serial communication stream.
SM2 = 0: the UART module treats incoming data as regular
data bytes.
SM2 = 1: the UART module interprets the incoming data in
a specific way, making it useful for multiprocessor
communication.
When SM2 is set, the UART module looks for a specific
type of data frame: an address frame followed by a data
frame.
23
Configuration Bits of SCON
REN
Receiver Enable
25
Optional Bits of SCON
TB8
Transmit Bit 8
26
Optional Bits of SCON
RB8
Receive Bit 8
Example
IF ninth bit of received data were Set, RB8 will have value
1
27
SCON – Serial Communication Control
TI
Transmit Interrupt
28
SCON – Serial Communication Control
RI
Receive Interrupt
29
Steps to Configure Serial Port
1. Calculate number for Timer to achieve correct Baud rate
2. Configure timer
Configure Timer1 to Mode 2 (8 Bit Auto Reload Mode)
Set TH1 and TL1 to calculated values
Run Timer 1 (TR1)
3. Configure serial port to the required mode
4. Set SMOD to double the baud rate, if required
5. Start receiving or transmitting the data
30
Calculating Timer Value
31
Calculating Timer Value
32
Calculating Timer Value
Mode 1 and 3 – Baud Rate By Timer 1
When SMOD = 0
TH1 = ( Crystal Freq. /12 ) / (Baud Rate * 32)
When SMOD = 1
TH1 = ( Crystal Freq. /12 ) / (Baud Rate * 16)
33
Calculating Timer Value
Mode 2 – Baud Rate By Oscillator
When SMOD = 0
Baud Rate = Oscillator / 64
When SMOD = 1
Baud Rate = Oscillator / 32
34
Baud Rate Generation
Calculate TH1 value to generate baud rate of 9600 using
crystal of 12 MHz. Consider SMOD = 1
35
Baud Rate Generation
Calculate TH1 value to generate baud rate of 9600 using
crystal of 12 MHz. Consider SMOD = 1
When SMOD = 1
TH1 = ( Crystal Freq. /12 ) / (Baud Rate * 16)
36
Baud Rate Generation
Calculate TH1 value to generate baud rate of 9600 using crystal
of 12 MHz. Consider SMOD = 1
37
Baud Rate Generation
38
Baudrate Error Effect
case1
case2
CLR TI
MOV SBUF, #F6h
JNB TI, $
40
Reading the Serial Port
Code to read the serial Port
JNB RI, $
MOV A, SBUF
CLR RI
41
Transmit “A” Continuously at 4800 Baud Rate
1. Calculate value of T1 to achieve correct baud rate
2. Configure timer
Configure Timer 1 to Mode 2 (8 bit Auto Reload)
Set TH1 and TL1 to the calculated value
Run Timer (TR1 = 1)
3. Configure serial port to mode 1
4. Clear or Set PCON.7 (SMOD)
5. Load ‘A’ in SBUF
6. Wait for TI to be High
7. Clear TI
8. Go to step 5
42
The Parity Bit
Parity may be
EVEN or
ODD
43
Even Parity
Example
For 1100 0011
Parity Bit: 0
44
Odd Parity
In Odd Parity, number of 1’s in the byte are odd (i.e. data
bits + parity bit)
Example
For 1100 0011
Parity Bit: 1
45
The Parity Bit
46
Program to Send Even Parity (9 Bit Mode)
MOV A, #data
MOV SBUF, A
47
Testing of Serial Communication
You can use following packages on PC to check serial port
functionality
HyperTerminal
Real Term
Tera Term
C/C++
MATLAB
48