22
CoE3DJ4 Digital Systems Design Chapter 5: Serial Port Operation

CoE3DJ4 Digital Systems Design

  • Upload
    lizina

  • View
    51

  • Download
    0

Embed Size (px)

DESCRIPTION

CoE3DJ4 Digital Systems Design. Chapter 5: Serial Port Operation. Serial port. 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) - PowerPoint PPT Presentation

Citation preview

CoE3DJ4Digital Systems Design

Chapter 5: Serial Port Operation

Serial port• 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

The SCON RegisterMSB LSB

SM0 SM1 SM2 REN TB8 RB8 TI RI

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 EnableSet to enable reception. CLR to disable reception.

SCON.3 TB8 Bit 8 of message to transmitUsed to transmit optional parity bit

SCON.2 RB8 Bit 8 of received messageReceives optional parity bit

SCON.1 TI Transmit Interrupt FlagSet when Byte in SBUF is completely transmitted.

SCON.0 RI Receive Interrupt FlagSet when a valid byte is received into SBUF

The SCON Register• SMO, SM1, SM2 Serial Mode Control Bits

SM0 SM1 Mode Baud Rate Description0 0 0 fosc/12 8-bit shift register0 1 1 variable 8-bit UART1 0 2 fosc/32 or fosc/64 9-bit UART1 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.

Modes• 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.

Mode 1• 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.

Framing• 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 (10), LSB, …, MSB, <parity bit>, Stop bit (1),

Time

Start 0 1 2 3 4 5 6 7 <P> Stop <St>

Mode 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.

Mode 1• 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:1. Ninth bit (stop bit) is clocked into RB8 in SCON2. SBUF is loaded with eight data bits3. Receiver interrupt flag (RI) is set

• These only occur if following conditions exist:1. RI=0 (ensures software has read the previous character)2. SM2=0 (applies in multiprocessor communications)

Modes 2 & 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• 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

Initialization and Accessing• 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,PMOV TB8,CMOV SBUF,A

– Odd parityMOV C,PCPL CMOV TB8,CMOV SBUF, A

Initialization and Accessing• 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, WAITCLR RIMOV A, SBUF

Initialization and Accessing• 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, WAITCLR TIMOV SBUF, A

Baud rate• Baud rate is fixed in modes 0 and 2• In mode 0 it is always on-chip oscillator frequency divided by

12• Default baud rate for mode 2 (after system reset) is oscillator

frequency divided by 64• Bit 7 of PCON is the SMOD bit• Setting SMOD doubles the baud rate in mode 2 to 1/32nd of

oscillator frequency • Since PCON is not bit-addressable, setting SMOD has to be

done using the following approach:MOV A, PCONSETB ACC.7MOV PCON,A

Baud rate• The baud rates in Modes 1 and 3 are determined by

Timer1overflow rate• Since timer operates at a relatively high frequency, it is

further divided by 32 (or 16 if SMOD=1) before driving the serial port

• The usual technique for baud rate generation is to initialize TMOD for 8-bit auto-reload mode (mode 3) and put the correct reload value in TH1 to yield proper overflow rate for baud rate

Serial port• Example: 1200 baud operation:

BAUD RATE=TIMER 1 OVERFLOW RATE / 32TIMER 1 OVERFLOW RATE =1200*32=38.4kHz– If a 12 MHz oscillator is used, timer 1 is clocked at 1 MHZ (1000

kHz). – Since the timer must overflow at a rate of 38.4 kHz and the timer is

clocked at 1000 kHz, an overflow is required every 1000/38.4=26.04 clocks.

– Since timer counts up and overflows on FF to 00 transition, 26 counts less than 0 is the required reload value for TH1: MOV TH1, #-26

• Table 5-3 gives reload values for different baud rates

Steps to Transmit a Byte1. Program T1 for Mode2 (TMOD 0x20)2. Load TH1 and TL1 with the initial value (baud rate dependant) (TH1

FD / FA / F4 / E8)3. Program SCON for Mode1 (SCON 0x50)4. Start Timer1 (SETB TR1)5. Clear TI6. Load SBUF with the byte to be transferred (SBUF byte)7. Wait until TI becomes 1 (a wait loop using: JNB TI)8. 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 mode2MOV TH1, #-7 ;9600 baudMOV SCON, #50H ;8b, 1stop, 1start, REN enabledSETB TR1 ;start timer T1

AGAIN: CLR TI ;ready to transmitMOV SBUF, #’A’ ;letter A is to be transmitted

HERE: JNB TI, HERE ;poll TI until all the bits are transmittedSJMP AGAIN ;loop (forever loop)

Steps to Receive a Byte1. Program T1 for Mode2 (TMOD 0x20)2. Load TH1 and TL1 with the initial value (baud rate dependant)

(TH1 FD / FA / F4 / E8)3. Program SCON for Mode1 (SCON 0x50)4. Start Timer1 (SETB TR1)5. Clear RI6. Wait until RI becomes 1 (a wait loop using JNB RI)7. Store SBUF (A SBUF)8. Go back to Step5 for next byte

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

START: MOV TMOD, #20H ;T1 in mode 2MOV TH1, #-7 ;9600 baudMOV SCON, #50H ;8b, 1start, 1stopSETB TR1 ;start T1

AGAIN: CLR RI ;ready to receive a byteHERE: JNB RI, HERE ;wait until one byte is Rx-ed

MOV A, SBUF ;read the received byte from SBUFMOV P1, A ;display on P1SJMP AGAIN ;loop forever