SERIAL DATA INPUT /OUTPUT AT89C51

SERIAL DATA INPUT /OUTPUT AT89C51

SERIAL DATA INPUT /OUTPUT


Computer must be able to communicate with other computers in modern multiprocessor distributed systems. One cost effective way to communication is to send and receive data bits serially. The 8051 has a serial data communication circuits that uses register SBUF to hold data. Register SCON control data communication, register PCON controls data rates, and pins RXD and TXD connect to the serial data network.

SBUF is physically two register. One is write only and is used to hold data to be transmitted out of the 8051 via TXD. The other is read only and hold received data from external source via RXD. Both mutually exclusive register use address 99h.

There are four programmable modes for serial data communication that are chosen by setting the SMX bits in SCON. Baud rates are determined by the mode chosen.

Data Transfer

transfer data in 2 ways:
1) Parallel- requires 8 or more lines
- used to transfer data with nearest devices
2) Serial -uses a single line (sending one bit at a time)
-can transfer data over larger distances
-cheaper.

serial communication can be of 2 types:
1) Synchronous- transmitter and receiver is synchronized with same clock frequencies
2) Asynchronous- start and stop bits are used along with data bits
UART-Special IC chip made for asynchronous communication.

Simplex-data is transmitted from Txr to Rxr only (one way communication)
Duplex communication-data can be transmitted and received by the same device.
Half duplex- data can either be transmitted or received at a time
Full duplex- data can be transmitted and received at same time.

Framing in Asynchronous Communication



An 8-bit message needs to be “framed” so that the receiver can detect correctly its beginning and end.
Standard framing:
Start bit – always 0, Stop bit – always 1.
Optional parity bit
The message now becomes:
Start bit (10), LSB, …, MSB, , Stop bit (1),



The rate of data transfer in serial communication
is stated in bps (bits per second)
Commonly used terminology to indicate data rate
is BAUD RATE
Unit of baud rate is also bps, but it actually indicates
number of signal changes per second .



Serial port in 8051

8051 includes an on-chip serial port.
Hardware access to the port is through TXD and RXD (Port 3 bits 1 and 0)
Serial port is full duplex (simultaneous transmission and reception)
Note: In Mode 0, it is half duplex .
Serial port has a receiving buffering allowing one character to be received and held in a buffer while a second character is received.
Two registers SBUF and SCON provide access to the serial port
Serial buffer (SBUF) is in fact two buffers one write only for transmission and one read-only for receiving .

SBUF

8-bit register used solely for serial communication.
Byte of data to be transferred via TXD or received through RXD.
When byte is written to SBUF it is framed with start and stop bits and transferred serially via TXD pin.
When the bits are received serially via RXD, the 8051 deframes it by eliminating start and stop bits, making a byte out of data received, then placing it in SBUF .


Bit :Name: Description
SCON.7 :SM0 :Serial Port Mode bit 0
SCON.6 :SM1 :Serial Port Mode bit 1
SCON.5 :SM2 :Multiprocessor Communication Enable
SCON.4 :REN :Receive Enable
Set to enable reception. CLR to disable reception.
SCON.3 :TB8 :Bit 8 of message to transmit
Used to transmit optional parity bit
SCON.2 :RB8 :Bit 8 of received message
Receives optional parity bit
SCON.1 :TI :Transmit Interrupt Flag
Set when Byte in SBUF is completely transmitted.
SCON.0 :RI :Receive Interrupt Flag
Set when a valid byte is received into SBUF.

SMO, SM1, SM2 Serial Mode Control Bits
SM0 SM1 Mode Baud Rate Description
0 0 0 fosc/12 8-bit shift register
0 1 1 variable 8-bit UART
1 0 2 fosc/32 or fosc/64 9-bit UART
1 1 3 variable 9-bit UART

SM2 Multiprocessor Mode Control Bit
1 = Multi-processor mode
0 = Normal mode
REN Receiver Enable Bit
1 = Receive Enable
0 = Receive Disabled
TB8 9th Transmit Bit Enabled only in modes 2 and 3
RB8 9th Bit Received Used in modes 2 and 3
RI, TI Serial Interrupts
RI is set to indicate receipt of a serial word and TI is set to indicate completion of a serial transmission.

MODE

Mode0
8 data bits, baud rate is 1/12 of osc.freq.
Mode1
10 bits: start bit(0), 8 data bits, stop bit(1).
Mode2
11bits: start bit(0), 8 data bits,programmable 9th bit, stop bit(0)
Baud rate is 1/64 or 1/32 of osc.freq.
Mode3
11bits: start bit(0), 8 data bits,programmable 9th bit, stop bit(0)
Baud rate is variable.

Mode of operation of serial port is set by writing to SCON.
Serial port has four modes of operation selected by writing 1s and 0s to SM0 and SM1 bits in SCON
Mode 0 puts the serial port in 8-bit shift register mode
Serial data enter and exit through RXD.
TXD serves as clock (outputs shift clock).
Eight bits are transmitted or received with the LSB first
Baud rate is fixed at 1/12th of on-chip oscillator.
Transmission is initiated by any instruction that writes data to SBUF
Data are shifted out on RXD line with clock pulses sent out the TXD.

MODE 0

Each transmitted bit is valid on RXD pin for one machine cycle
Reception is initiated as soon as REN bit is set to 1 and the receive interrupt (RI) bit is cleared.
Usually, REN is set at the beginning of the program to initialize the serial port, then RI is cleared to start a data input operation.
As soon as RI is cleared, the shift clock will be produced on the TxD pin.
At the beginning of the following machine cycle, data will be clocked in from the RxD line.
The clocking occurs on the rising edge of the TxD line.
After the 8th clocking cycle, the data is copied to SBUF and the RI bit is set.
It is up to the attached circuitry to provide data on RXD line synchronized by the clock signal on TXD.

MODE1

Mode 1 is an 8-bit UART with variable baud rate
UART (universal asynchronous receiver/transmitter): a device that receives and transmits serial data with each data character preceded by a start bit (low) and followed by a stop bit (high)
In mode 1, 10 bit are transmitted on TXD or received on RXD
These consist of a start bit (always 0), eight data bits (LSB first) and stop bit (always 1)
For receive operation, stop bit goes into RB8 in SCON
Baud rate is set by timer 1.
Transmission is initiated by writing to SBUF
Shifted data are outputted on the TXD line beginning with start bit and followed by eight bit data bits then stop bit
The transmit interrupt (TI) flag is set as soon as the stop bit appears on TxD.
Reception is initiated by a 1 to 0 transition on RXD (assuming REN is 1).
The start bit is skipped and eight data bits are clocked into the serial port shift register .
When all eight bits have been clocked in, the following occur:
Ninth bit (stop bit) is clocked into RB8 in SCON.
SBUF is loaded with eight data bits.
Receiver interrupt flag (RI) is set.
These only occur if following conditions exist:
RI=0 (ensures software has read the previous character).
SM2=0 (applies in multiprocessor communications) .

MODE 2 AND 3

Mode 2: 9-bit UART with fixed baud rate.
Eleven bits are transmitted or received: a start bit, eight data bits, a programmable ninth bit and a stop bit.
On transmission, the ninth bit is whatever has been put in TB8 in SCON.
On reception, the ninth bit received is placed in RB8.
Baud rate in mode 2 is either1/32nd or 1/64th on-chip oscillator.
Mode 3: 9-bit UART with variable baud rate.
Mode 3 is same as Mode 2 except baud rate is programmable and provided by Timer 1.

Initialization and Accessing.

Receiver enable bit REN in SCON must be set by software to enable reception of characters.
This is done at the beginning of a program when port is initialized .
SETB REN.
Or MOV SCON, #xxx1xxxxB.
Ninth data bit transmitted in modes 2 and 3 must be loaded into TB8 by software.
Ninth bit received is placed in RB8.
A common use for ninth data bit is to add parity to a character
P bit in PSW is set or cleared every machine cycle to establish even parity with bits in accumulator
Example: transmitting eight bits in accumulator with an even parity added in ninth bit:.
...........................................
MOV C,P
MOV TB8,C
MOV SBUF,A
-Odd parity
MOV C,P
CPL C
MOV TB8,C
MOV SBUF, A
........................................

RI and TI in SCON play an important role in 8051 serial communications
Both bits are set by hardware but must be cleared by software
Typically RI is set at the end of character reception and indicates “receive buffer full”
If software wishes to input a character from the device connected to serial port, it must wait until RI is set, then clear RI and read character from SBUF
WAIT: JNB RI, WAIT
CLR RI
MOV A, SBUF

TI is set at the end of character transmission and indicates “transmit buffer empty”.
If software wishes to send a character to the device connected to the serial port, it must first check that the serial port is ready.
WAIT: JNB TI, WAIT
CLR TI
MOV SBUF, A


Baud rate

Baud rate is fixed in modes 0 and 2
In mode 0, Baud Rate= Fosc/12
Default baud rate for mode 2 (after system reset)= Fosc/ 64
Bit 7 of PCON is the SMOD bit
Setting SMOD doubles the baud rate, in mode 2 to Fosc/32

Since PCON is not bit-addressable, setting SMOD has to be done using the following approach:
MOV A, PCON
SETB ACC.7
MOV PCON,A

The baud rates in Modes 1 and 3 are determined by Timer1overflow rate
When Timer 1 is the baud rate generator, the baud rates in Modes l and 3 are determined by the Timer 1 overflow rate and the value of SMOD according to the following equation.


The Timer 1 interrupt should be disabled in this application.
Here it is configured for timer operation in auto-reload mode (high nibble of TMOD = 0010B)
The baud rate is given by the following formula.

Example: 1200 baud operation:
Let oscillator frequency be 12MHz and SMOD=0, then from formula
256-TH1(decimal)= 12MHz / ( 1200*32*12)
= 26.04 = 26(appx.)
Then,
THI (dec)= 256-26
= 230
Hex value to be loaded in TH1= E6
i.e. MOV TH1,#E6H or MOV TH1,#-26

Steps to Transmit a Byte


Program T1 for Mode2 (TMOD  0x20)
Load TH1 and TL1 with the initial value (baud rate dependant) (TH1  FD / FA / F4 / E8)
Program SCON for Mode1 (SCON  0x50)
Start Timer1 (SETB TR1)
Clear TI
Load SBUF with the byte to be transferred (SBUF  byte)
Wait until TI becomes 1 (a wait loop using: JNB TI)
Go back to Step5 for next byte.

Examples: Transmit a character.

Transfer ASCII “A” serially at 9600 baud continuously
START: MOV TMOD, #20H ;Put T1 in mode2
MOV TH1, #-7 ;9600 baud
MOV SCON, #50H ;8b, 1stop, 1start, REN enabled
SETB TR1 ;start timer T1
AGAIN: CLR TI ;ready to transmit
MOV SBUF, #’A’ ;letter A is to be transmitted
HERE: JNB TI, HERE ;poll TI until all the bits are transmitted
SJMP AGAIN ;loop (forever loop)


Steps to Receive a Byte


Program T1 for Mode2 (TMOD  0x20)
Load TH1 and TL1 with the initial value (baud rate dependant) (TH1  FD / FA / F4 / E8)
Program SCON for Mode1 (SCON  0x50)
Start Timer1 (SETB TR1)
Clear RI
Wait until RI becomes 1 (a wait loop using JNB RI)
Store SBUF (A  SBUF)
Go back to Step5 for next byte

Receive bytes serially and display them on P1, continuously.
Example:

START: MOV TMOD, #20H ;T1 in mode 2
MOV TH1, #-7 ;9600 baud
MOV SCON, #50H ;8b, 1start, 1stop
SETB TR1 ;start T1
AGAIN: CLR RI ;ready to receive a byte
HERE: JNB RI, HERE ;wait until one byte is Rx-ed
MOV A, SBUF ;read the received byte from SBUF
MOV P1, A ;display on P1
SJMP AGAIN ;loop forever

Labels: