0% found this document useful (0 votes)
21 views44 pages

MTS 311 Week 6 UART

Uploaded by

tanveer1111110
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views44 pages

MTS 311 Week 6 UART

Uploaded by

tanveer1111110
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 44

Serial Port

4
Serial Vs Parallel Data Transfer

Parallel Serial

 Sends data at a time  Sends data sequentially

 More wires needed  Less wires needed

 Fast  Slow

5
Serial Vs Parallel Data Transfer

Parallel Serial

Sender Receiver Sender Receiver

6
Serial Vs Parallel in 8051

Parallel Serial
 8 Bits can be sent at a time  1 bit can be sent at a time

 Fast data transfer  Slow data transfer

 8 wires for data  2 wires for data

 More components, more  Less components, less


chances of failure chances of failure

 More cost  Less cost


7
Types of Serial Communication

 Synchronous
 Asynchronous

8
Synchronous Communication

 The 2 devices initially synchronize themselves to each


other
 Both the sender and receiver share a common clock
signal to synchronize the data transmission.
 Data is transferred staying in synchronization.

 Examples
 Devices using I2C (IIC), SPI

9
Asynchronous Communication

 Asynchronous means “Not Synchronous”


 Does not use a shared clock signal. Instead, it relies on
the sender and receiver having agreed beforehand on the
transmission speed.
 Beginning and End of data is identified, e.g. by use of
start and stop bits

 Examples
 Serial Port in PCs (UART)

10
Format of Asynchronous Transmission
Data is transmitted serially in the
following order:

 First the start bit is transmitted

 Then 8 bits of data are


transmitted, starting from LSB

 Parity bit is transmitted


(Optional)

 Stop bit is transmitted in the end


of 1 byte transmission

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

 Named after Jean Maurice Emile


Baudot, who was an officer in the
French Telegraph Service.

 Baud Rate: Number of bits


transmitted per second

 Baud rate includes start and stop


bits

13
Serial Communication
 TTL Voltages
 Input
 LOW: 0 – 0.8V
 HIGH: 2 – 5V
 Output
 LOW: 0 – 0.4V
 HIGH: 2.4 – 5V

 Long wires act as antennas and pick up noise


 How to solve this problem?

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

 It has Full Duplex operation

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

o SMOD – Doubles the baud rate of Serial Port


when serial port is in Mode 1, 2 or 3

20
Mode 0 – 8 Bit Shift Register Mode

 Terms TXD and RXD are misleading in this mode

 TXD: serves as clock


 RXD: used for both receiving and transmitting data

 Half Duplex: either can be received or transmitted; only


one thing at a time

 Synchronous Communication

21
Mode 0 – Shift Register

 TXD runs at 1/12th the frequency of clock

Example
 If we are using 12 Mhz clock, frequency of TXD is 1 Mhz

 Cycle length will be 1 micro-sec

 Each bit will be active for 1micro-sec

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

 To receive data from serial port, this must be set High

25
Optional Bits of SCON
TB8
Transmit Bit 8

 Used in modes 2 and 3


 9 Bit modes

 TB8 is the ninth bit, added with every data byte

 IF TB8 is set, 9th bit will always be set


 IF TB8 is clear, 9th bit will always be clear

26
Optional Bits of SCON
RB8
Receive Bit 8

 Used in modes 2 and 3


 9 Bit modes

 Value of Ninth bit received is stored in RB8

Example
 IF ninth bit of received data were Set, RB8 will have value
1

27
SCON – Serial Communication Control
TI
 Transmit Interrupt

 When a data is sent using serial port, it will take some


time to transmit all bits

 TI is set high after all the bits have been transmitted

 When TI is set, it means data has been transferred and


the next data can be sent

28
SCON – Serial Communication Control
RI
 Receive Interrupt

 When data is received using serial port.

 RI is set high after a byte has been received

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

Mode 0 – Fixed Baud Rate

31
Calculating Timer Value

Mode 1 and 3 – Baud Rate By Timer 1

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

 TH1 = ( Crystal Freq. /12 ) / (Baud Rate * 16)

 Timer1 Overflow = Baud Rate * 16


= 9600 * 16
= 153600
 Timer Value = (12M / 12) / 153600
=1M / 153600 = 1000,000 / 153600
= 6.51 ≈ 7
TH1 = 7

37
Baud Rate Generation

38
Baudrate Error Effect

case1

case2

the last bit must be sampled at least 20% of


the bit period before the transition from last
data bit to stop bit.
39
Writing to Serial Port
 Code to transmit number F6h

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

Write the code yourself

42
The Parity Bit

 Allows some limited checking

 To detect corruption of data during transmission

 Parity may be
 EVEN or
 ODD

 Additional PARITY BIT added, to keep parity of the byte


fixed

43
Even Parity

 In Even Parity, number of 1’s in the byte are even (i.e.


data bits + parity bit)

 Example
 For 1100 0011
 Parity Bit: 0

 For 1101 0110


 Parity Bit: 1

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

 For 1101 0110


 Parity Bit: 0

45
The Parity Bit

 LSB of Program Status Word (PSW.0)

 Even Parity Flag (1 = Even Parity, 0 = Odd Parity)

 Mode 2 and 3 (9 bit) can be used to send Parity


 8 data bits
 9th bit is Parity

 Parity can be sent through TB8 and received through RB8

46
Program to Send Even Parity (9 Bit Mode)

MOV A, #data

MOV C,P ;the parity flag in PSW is moved to carry flag


MOV TB8, C

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

You might also like