8051 – Serial Port


Download 8051 – Serial Port


Preview text

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 Serial Vs Parallel Transfer of data
l Simplex, Duplex and half-Duplex modes l Synchronous, Asynchronous, UART, USART l 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
l Data transfer rate
– Bps, baud
l RS232 protocol
– Non TTL compatible logic levels (-3 to –25 for 1 and +3 to +25 for 0)

26-Sep-02

2

RS232 Pins
l Too many signals, but most are unused in the 8051 UART
l In non-handshaking mode, only three signals
– Pin2 : RxD – received data – Pin3 : TxD – Transmitted data – Pin5 : GND
l 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)

26-Sep-02

3

RS232 Line driver

l RS232 uses TLL-incompatible logic levels

l Need Line drivers to interface 8051 to Rs232 protocol

l MAX232, MAX233 most commonly used line

drivers

8051

MAX233

– Dual channels

TxD 11 5

2

– Single supply, +5V operation

3

– MAX233 needs no external

RxD 10 4

5

capacitors DB-9

26-Sep-02

4

8051 Serial Port
l 8051 has an internal UART
– Baud rate is set by Timer1
l Control Registers
– SBUF : Serial Buffer Register
l Data moved to SBUF is Transmitted serially l Serial data Rx-ed is stored by 8051 in SBUF
– SCON : Serial Control Register
l Program the mode (start bit, stop bit, data bits length)
– Only Mode 1 (8, 1, 1) is of interest, as others are obsolete
l Receive enable REN l RI and TI – Receive & Transmit Interrupt flags

26-Sep-02

5

Setting the Baud rate
l Timer 1is the timing controller for serial port in 8051 l Clock for the Timer1 in the UART is
– XTAL /12 /32 = 28,800Hz (for XTAL = 11.0592MHz)
l Set SMOD bit (PCON.7) to program 8051 to use 1/16 multiplier
l XTAL / 12 / 16 = 56,700Hz l Effectively doubles the baud rate
l Timer1 has to be programmed in
– Mode 2, 8bit auto reload mode – Load TH1 with the required value

26-Sep-02

6

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
l If SMOD (PCON.7) is set then the same values for TH1 will give
– 19200 etc

26-Sep-02

7

SCON Register
l SCON.0 = RI
– Receive interrupt flag. Valid byte is received in SBUF
l SCON.1 = TI
– Transmit interrupt flag. Byte in SBUF was completely transmitted.
l SCON.4 = REN
– Receive enable. Set to enable reception. Clr for transmit only
l SCON.7:SCON.6 = SM0:SM1
– Serial mode setting – 01 = Mode 1 is the widely used mode
l 8bit data, 1start bit and 1 stop bit
l All other bits to be set to 0

26-Sep-02

8

Steps to Transmit a Byte
1. Program T1 for Mode2 (TMOD ß 0x20) 2. Load TH1 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 TI 6. Load SBUF with the byte to be transferred (SBUF ß
byte) 7. Wait until TI becomes 1 (jnb TI, not_done) 8. Go back to Step5 for next byte

26-Sep-02

9

Examples: Transmit a character

l Transfer ASCII “A” serially at 9600 baud continuously

START:
AGAIN: HERE:

mov TMOD, #20H mov TH1, #-3 mov SCON, #50H setb TR1 clr TI mov SBUF, #’A’ 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

Preparing to load PDF file. please wait...

0 of 0
100%
8051 – Serial Port