8051 Serial Port
8051 Serial Port
8051 Serial Port
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
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
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 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
26-Sep-02
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)
26-Sep-02
Timer 1is the timing controller for serial port in 8051 Clock for the Timer1 in the UART is
Mode 2, 8bit auto reload mode Load TH1 with the required value
26-Sep-02
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
26-Sep-02
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
26-Sep-02
10
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
26-Sep-02
12
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
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.
26-Sep-02
14
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
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
l l
Bipom [http://www.bipom.com/ ]
Tecel [ http://www.tecel.com/t1/]
26-Sep-02
Next Class
l l l
26-Sep-02
17