0% found this document useful (0 votes)
16 views

LAB 11 8051 Serial Programming

Uploaded by

tanveer1111110
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

LAB 11 8051 Serial Programming

Uploaded by

tanveer1111110
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

LAB 11

8051 SERIAL
PROGRAMMING
INTRODUCTION

In serial communication, the data is sent one bit at


a time, in contrast to parallel communication, in
which the data is sent a byte or more at a time.
The 8051 has serial communication capability
built into it.
TYPES OF COMMUNICATION

• Synchronous
• Asynchronous
SYNCHRONOUS COMMUNICATION
In synchronous communications, the two devices
initially synchronize themselves to each other, and then
continually send characters to stay in sync.
ASYNCHRONOUS COMMUNICATION
Asynchronous means "no synchronization". The
beginning and end of each byte of data is identified by
start and stop bits. The start bit indicates when the data
byte is about to begin and the stop bit signals when it
ends.
ASYNCHRONOUS COMMUNICATION
START AND STOP BIT
The data is transmitted in serial format with LSB of the byte to be
transmitted (shifted out) first among the data bits.
The general format for serial transmission is:

Stop Bit+ Parity Bit (Optional) + Data Bits + Start Bit.


SUMMARY -TYPES OF COMMUNICATION
 Synchronous
 Synchronized with clock.
 Additional wire is required for clock
 Asynchronous
 Data is send with regular interval
 There is an agreement that
 When data is started (start bit)
 Time rate at which each bit is sent (Baud rate)

 Stop bit

 Ideally line is always high, a start bit of low is


generated, which mark the start of data.
SERIAL COMMUNICATION – BAUD RATE
• The serial port baud rate can be fixed or variable.
Variable baud rate is supplied by the Timer 1.
SERIAL COMM
Two special function registers
provide software access to the serial
port, SBUF and SCON.
SERIAL CONTROL SCON (SFR)
Bit Name Bit Explanation of Function
Address
7 SM0 9Fh Serial port mode bit 0

6 SM1 9Eh Serial port mode bit 1.

5 SM2 9Dh Mutli-processor Communications Enable

4 REN 9Ch Receiver Enable. This bit must be set in order to receive
characters.
3 TB8 9Bh Transmit bit 8. The 9th bit to transmit in mode 2 and 3.

2 RB8 9Ah Receive bit 8. The 9th bit received in mode 2 and 3.

1 TI 99h Transmit Flag. Set when a byte has been completely


transmitted.
0 RI 98h Receive Flag. Set when a byte has been completely
received.
8051 SERIAL COMMUNICATION
 The 8051 has a single full duplex (means it can
transmit and receive data at a time) serial port that can
be used for either asynchronous or synchronous
communication.
 The port has 4 modes of operation:
 Mode 0: Synchronous at 1/12 oscillator frequency.
 Mode 1: 8 bit Asynchronous - variable baud rate.
 Mode 2: 9 bit Asynchronous -1/32 OR 1/64 of the oscillator
frequency.
 Mode 3: 9 bit Asynchronous - variable rate.
Modes Of Operation
SM0 SM1 Serial Mode Explanation Baud Rate

0 0 0 8-bit Shift Register Oscillator / 12


(fixed)

0 1 1 8-bit UART Set by Timer 1 (*)


(Variable)

1 0 2 9-bit UART Oscillator / 32 (*)


(fixed)

1 1 3 9-bit UART Set by Timer 1 (*)


(Variable)

(*) Note: The baud rate indicated in this table is doubled if PCON.7 (SMOD) is set.
Steps To Configure Serial Port for communication
1. Calculate the number for timer overflow to achieve correct baud
rate.
2. Configure Timer
a. Configure Timer 1 to mode 2 (8-bit auto-reload) (TCON).
b. Set TH1 to calculated value to reflect the correct frequency for baud rate
(from step 1).
c. Run timer (TR1).

3. Configure Serial Port to required mode and set the require bit to
activate the receive enable (SCON).
4. Set PCON.7 (SMOD) to double the baud rate (if required).
5. Start receiving or transmitting the data.
CALCULATE NUMBER FOR TIMER OVERFLOW
Baud Rate= Timer1 overflow / 32
Timer1 overflow = Baud rate x 32 ( if SMOD=1 then x by 16)
Timer1 overflow = Baud rate x 32
TH1 = (crystal freq (hz)/12) / (Baud rate * 32)

Th1= ((Crystal (Hz) / 384) / Baud)


(if SMOD=1 use 192 instead of 384)

Th1=256- (11.0592MHz / 384) / 4800 ) = 250 or -6


EXAMPLES
A program for the 8051 to transfer letter “Z” serially at 4800
baud rate continuously

MOV TMOD, #20H ;timer 1, mode 2 (auto


;reload)
MOV TH1, #-6 ;4800 baud rate
MOV SCON, #40H ;8-bit, 1 stop, REN disabled
SETB TR1 ;start timer 1
AGAIN:
MOV SBUF, #“Z" ;letter “Z" to be transferred
HERE:
JNB TI, HERE ;wait for the last bit
CLR TI ;clear TI for next char
SJMP AGAIN ;keep sending Z
EXAMPLES
A program to receive bytes of data serially, and put them in
P1. Set the baud rate at 4800, 8-bit data and 1 stop bit.

MOV TMOD, #20H ;timer1, mode 2 (auto reload)


MOV TH1, #-6 ;4800 baud
MOV SCON, #50H ;8-bit, 1 stop, REN enabled
SETB TR1 ;start timer 1
HERE:
JNB RI, HERE ;wait for char to come in
MOV A, SBUF ;save incoming byte in A
MOV P1, A ;send to port 1
CLR RI ;get ready to receive next byte
SJMP HERE ;keep getting data
EXERCISES
Write an 8051 program to transfer serially the
letter “M” continuously at a 1200 baud rate.
EXERCISES
Write an 8051 program to transfer serially the
message “We love our country” continuously at a
57,600 baud rate.
EXERCISES
Write a program to simultaneously transmit and
receive data at 9.6kbps baud rate.

You might also like