Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

8051 Serial Port

EE4380 Fall02 Class 10

Pari vallal Kannan


Center for Integrated Circuits and Systems University of Texas at Dallas

Serial Comm. - Introduction


l l l l

Serial Vs Parallel Transfer of data Simplex, Duplex and half-Duplex modes Synchronous, Asynchronous, UART, USART Framing

Start bit, Stop bit, mark (1 no signal), space (0 no signal) Start bit (0), LSB, MSB, Stop bit (1) Optional parity bit Stop bit can be one or two bits Bps, baud Non TTL compatible logic levels (-3 to 25 for 1 and +3 to +25 for 0)
2

l l

Data transfer rate

RS232 protocol

26-Sep-02

RS232 Pins
l l

Too many signals, but most are unused in the 8051 UART In non-handshaking mode, only three signals

Pin2 : RxD received data Pin3 : TxD Transmitted data Pin5 : GND

For 8051 to PC serial port (COMx) connection, use null-modem connection


RxD of 8051 system to TxD of PC TxD of 8051 system to RxD of PC GND to GND Need to set transfer mode to use software flow control (XON/XOF)
3

26-Sep-02

RS232 Line driver


l l l

RS232 uses TLL-incompatible logic levels Need Line drivers to interface 8051 to Rs232 protocol MAX232, MAX233 most commonly used line 8051 MAX233 drivers
Dual channels Single supply, +5V operation MAX233 needs no external capacitors

TxD 11 5 2 3 RxD 10 4 5 DB-9

26-Sep-02

8051 Serial Port


l l

8051 has an internal UART

Baud rate is set by Timer1 SBUF : Serial Buffer Register


l l

Control Registers

Data moved to SBUF is Transmitted serially Serial data Rx-ed is stored by 8051 in SBUF Program the mode (start bit, stop bit, data bits length)

SCON : Serial Control Register


l l l

Only Mode 1 (8, 1, 1) is of interest, as others are obsolete

Receive enable REN RI and TI Receive & Transmit Interrupt flags

26-Sep-02

Setting the Baud rate


l l l

Timer 1is the timing controller for serial port in 8051 Clock for the Timer1 in the UART is

XTAL /12 /32 = 28,800Hz (for XTAL = 11.0592MHz)

Set SMOD bit (PCON.7) to program 8051 to use 1/16 multiplier


l l

XTAL / 12 / 16 = 56,700Hz Effectively doubles the baud rate

Timer1 has to be programmed in


Mode 2, 8bit auto reload mode Load TH1 with the required value

26-Sep-02

Setting the Baud rate


l

TH values (XTAL = 11.0592MHz)


Baud Rate: 9600 = 28800/3 TH1 = -3 = 0xFD Baud Rate: 4800 = 28800/6 TH1 = -6 = 0xFA Baud Rate: 2400 = 28800/12 TH1 = -12 = 0xF4 Baud Rate: 1200 = 28800/24 TH1 = -24 = 0xE8

If SMOD (PCON.7) is set then the same values for TH1 will give

19200 etc

26-Sep-02

SCON Register
l l

SCON.0 = RI

Receive interrupt flag. Valid byte is received in SBUF Transmit interrupt flag. Byte in SBUF was completely transmitted. Receive enable. Set to enable reception. Clr for transmit only Serial mode setting 01 = Mode 1 is the widely used mode
l

SCON.1 = TI

l l

SCON.4 = REN

SCON.7:SCON.6 = SM0:SM1

8bit data, 1start bit and 1 stop bit

All other bits to be set to 0


8

26-Sep-02

Steps to Transmit a Byte


1. 2.

3. 4. 5. 6.

7. 8.

Program T1 for Mode2 (TMOD 0x20) Load TH1 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 (jnb TI, not_done) Go back to Step5 for next byte

26-Sep-02

Examples: Transmit a character


l

Transfer ASCII A serially at 9600 baud continuously


START: mov TMOD, #20H mov TH1, #-3 mov SCON, #50H setb TR1 AGAIN: clr TI mov SBUF, #A HERE: jnb TI, HERE sjmp AGAIN ;T1 is mode2 ;9600 baud ;8b, 1stop, 1start, REN enabled ;start timer T1 ;ready to transmit ;letter A is to be transmitted ;poll TI until all the bits are transmitted ;while(1) loop (forever loop)

26-Sep-02

10

Steps to Receive a Byte


1. 2.

3. 4. 5. 6. 7. 8.

Program T1 for Mode2 (TMOD 0x20) Load TH1 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 (jnb RI, not_done) Store SBUF (A SBUF) Go back to Step5 for next byte

26-Sep-02

11

Example: Receive Data


l

Receive bytes serially and display them on P1, continuously.


START: mov TMOD, #20H mov TH1, #-3 mov SCON, #50H setb TR1 AGAIN: HERE: clr RI jnb RI, HERE mov A, SBUF mov P1, A sjmp AGAIN ;T1 in mode 2 ;9600 baud ;8b, 1start, 1stop ;start T1 ;ready to receive a byte ;wait until one byte is Rx-ed ;read the received byte from SBUF ;display on P1 ;while (1)

26-Sep-02

12

Serial Ports with Interrupts


l l

Using serial port with interrupts is THE way it was intended to be used. Both the RI and TI flags raise the Serial interrupt (S0), if S0 is enabled in IE.

ISR for S0 is at 0x0023 Transmit is polling based (Poll TI flag) and Receive is interrupt driven Transmit is interrupt driven and Receive is polling based (Poll RI flag)

Simple Case

In these cases, the ISR of S0 will check for the appropriate flag and either copy data to or from SBUF

26-Sep-02

13

Serial Ports with Interrupts


l

General Case

8051 is in full duplex mode, I.e. receives and transmits data continuously Both Transmit and Receive is interrupt driven ISR must first check which one of RI and TI raised the S0 interrupt If RI is set, then read data from SBUF to a safe place and clear RI If TI is set, then copy the next character to be transmitted onto SBUF and clear TI.

Write the ISR for S0 such that


26-Sep-02

14

Example : Simple case


l

8051 gets data from P1 and sends it to P2 continuously while receiving from Serial port. Serial port data is to be displayed on P0 org 0 ljmp MAIN org 23H ljmp SERIAL org 30H ;avoid the IVT ;serial port ISR SERIAL: org 100H jb TI, TRANS mov A, SBUF ;copy received data mov P0, A clr RI ;P1 as input port ;T1 in mode 2 ;9600 baud ; 8b, 1start, 1stop TRANS: RETI clr TI RETI end ;do nothing ;ISR does not handle TX ;display it on P0 ;clear RI

MAIN:

mov P1, #0FFH mov TMOD, #20 mov TH1, #-3 mov SCON, #50H

mov IE, #10010000B ;enable S0 interrupt setb TR1 BACK: mov A, P1 mov P2, A sjmp BACK
26-Sep-02

;enable T1

15

8051 Dev System Survey


l

Pauls 8051 Dev Board [http://www.pjrc.com]

87C52, 22MHz, 2 8255 devices, 50 I/O lines, LCD header, 30K Flash, 32K SRAM, proto area, 2 serial ports, ~80$ assembled S80C552-12 CPU, 32K each of EPROM, EEPROM and SRAM, 8 A/D inputs, 2 PWM outputs, LCD and keypad interface, 20 I/O lines, proto area, ~120$ Many products, all have proto area, Cost from 40$ to 180$ Many products, very small, no proto area, uses atmel devices with onboard Flash and SRAM, ISP, LCD and keypad interface, 40 to 70$ Many products. Very small, upto 32K SRAM and EEPROM, many I/O lines, No proto area, $40 to $70. T1 bare board is 12$ !
16

Axman CME552 and CME562 [http://www.axman.com/]

l l

New Micros [http://www.newmicros.com/ ]

Bipom [http://www.bipom.com/ ]

Tecel [ http://www.tecel.com/t1/]

26-Sep-02

Next Class
l l l

Interfacing analog sensors Interfacing Stepper Motors Embedded System Diagnostics

26-Sep-02

17

You might also like