Chapter 10 Serial Port
Transcript of Chapter 10 Serial Port
8/10/2019 Chapter 10 Serial Port
http://slidepdf.com/reader/full/chapter-10-serial-port 1/37
The PIC uCs
PIC Microcontroller andEmbedded Systems
Muhammad Ali Mazidi, Rolin McKinlay and Danny Causey
10-1
8/10/2019 Chapter 10 Serial Port
http://slidepdf.com/reader/full/chapter-10-serial-port 2/37
The PIC uCs
Chapter 10: PIC18Serial PortProgramming inAssembly.
PIC Microcontrollerand Embedded SystemsMuhammad Ali Mazidi,Rolin McKinlay andDanny Causey , February2007.
10-2
8/10/2019 Chapter 10 Serial Port
http://slidepdf.com/reader/full/chapter-10-serial-port 3/37
The PIC uCs
Objective
Explain serial communication protocolDescribe data transfer rate and bps rate
Interface the PIC18 with an RS232connector
Describe the main registers used by serialcommunication of the PIC18
Program the PIC18 serial port in Assembly
10-3
8/10/2019 Chapter 10 Serial Port
http://slidepdf.com/reader/full/chapter-10-serial-port 4/37
The PIC uCs
Outlines
Programming timers 0 and 1
Counter Programming
10-4
8/10/2019 Chapter 10 Serial Port
http://slidepdf.com/reader/full/chapter-10-serial-port 5/37
The PIC uCs
Introduction
Computers transfer data in two ways:Parallel and Serial.
Parallel: Eight or more data lines, few feet
only, short timeSerial: Single data line, long distance
The PIC18 has serial communication
capability built into it.
10-5
8/10/2019 Chapter 10 Serial Port
http://slidepdf.com/reader/full/chapter-10-serial-port 6/37
The PIC uCs
Basics of Serial Communication
The byte of data must be converted toserial bits using a parallel-in-serial-outshift register
Serial versus Parallel Data Transfer 10-6
8/10/2019 Chapter 10 Serial Port
http://slidepdf.com/reader/full/chapter-10-serial-port 7/37
The PIC uCs
Basics of Serial Communication(cont’d)
The receiving end must be a serial-in-parallel-out shift register and pack theminto a byte.
Two methods of serial data communication:Asynchronous and Synchronous
Transfers ablock of data ata time
Transfers a singlebyte at a time
10-7
8/10/2019 Chapter 10 Serial Port
http://slidepdf.com/reader/full/chapter-10-serial-port 8/37
The PIC uCs
The PIC uCs
Half-and Full-DuplexTransmission
10-8
8/10/2019 Chapter 10 Serial Port
http://slidepdf.com/reader/full/chapter-10-serial-port 9/37
The PIC uCs
Start and Stop Bits
In the asynchronous method, eachcharacter is placed between start and stopbits (framing)
Framing ASCII ‘A’ (41H)
LSBMSB
10-9
8/10/2019 Chapter 10 Serial Port
http://slidepdf.com/reader/full/chapter-10-serial-port 10/37
The PIC uCs
Data Transfer Rate
Rate of data transfer: bps (bits persecond)
Another widely used terminology for bps is
baud rate For Asynchronous serial data
communication, the baud rate is generallylimited to 100,000bps
10-10
8/10/2019 Chapter 10 Serial Port
http://slidepdf.com/reader/full/chapter-10-serial-port 11/37
The PIC uCs
RS232 Standard
Standard for serial comm (COM port)1: -3V to -25V;0: +3V to +25V
Reason: for long distance wired line
Input-output voltage are not TTLcompatible
So, we need MAX232/233 for voltage
converter. Commonly known as line drivers
10-11
8/10/2019 Chapter 10 Serial Port
http://slidepdf.com/reader/full/chapter-10-serial-port 12/37
The PIC uCs
The PIC uCs
RS232 Pins
DB-25
25-Pin Connector
DB-9
9-Pin Connector
Connectors:Minimally, 3 wires: RxD, TxD, GND
Could have 9-pin or 25-pin
10-12
8/10/2019 Chapter 10 Serial Port
http://slidepdf.com/reader/full/chapter-10-serial-port 13/37
The PIC uCs
The PIC uCs
RS232 Pins (cont’d)
IBM PC DB-9 Signals
Pin 1 – Data Carrier Detect (DCD)Pin 2 – Received Data (RxD)
Pin 3 – Transmitted Data (TxD)Pin 4 – Data Terminal Ready (DTR)Pin 5 – Signal Ground (GND)Pin 6 – Data Set Ready (/DSR)Pin 7 – Request to Send (/RTS)Pin 8 – Clear to Send (/CTS)Pin 9 – Ring Indicator (RI)
DB-9
9-Pin Connector
Data in Data out
10-13
8/10/2019 Chapter 10 Serial Port
http://slidepdf.com/reader/full/chapter-10-serial-port 14/37
The PIC uCs
The PIC uCs
PIC18 Connection to RS232
Line driver
(a) Inside MAX232 (b) its Connection to the PIC18
10-14
8/10/2019 Chapter 10 Serial Port
http://slidepdf.com/reader/full/chapter-10-serial-port 15/37
The PIC uCs
Figure 10-6. Null ModemConnection
Null modem is a communication method toconnect two DTEs (computer, terminal, printeretc.) directly using a RS-232 serial cable.
With a null modem connection the transmit andreceive lines are crosslinked.
Depending on the purpose, sometimes also oneor more handshake lines are crosslinked.
10-15
8/10/2019 Chapter 10 Serial Port
http://slidepdf.com/reader/full/chapter-10-serial-port 16/37
The PIC uCs
The PIC uCs
PIC18 Connection to RS232
Line driver
(a) Inside MAX232 (b) its Connection to the PIC18
10-16
8/10/2019 Chapter 10 Serial Port
http://slidepdf.com/reader/full/chapter-10-serial-port 17/37
The PIC uCs
The PIC uCs
PIC18 Connection to RS232(Cont’d)
(a) Inside MAX233 (b) Its Connection to thePIC18
Line driver
10-17
8/10/2019 Chapter 10 Serial Port
http://slidepdf.com/reader/full/chapter-10-serial-port 18/37
The PIC uCs
Section10.3: PIC18 Serial PortProgramming in Assembly
USART has both Synchronous
Asynchronous
6 registers SPBRG
TXREG
RCREG
TXSTA
RCSTA
PIR1
10-18
Rx Port
Tx Port
8/10/2019 Chapter 10 Serial Port
http://slidepdf.com/reader/full/chapter-10-serial-port 19/37
The PIC uCs
SPBRG Register and Baud Ratein the PIC18
The baud rate inis programmable
loaded into theSPBRG decidesthe baud rate
Depend on crystalfrequency BR = F Fosc
4*16*(X+1)(
Baud
Rate
SPBRG
(Hex Value)
38400 319200 7
9600 F
4800 202400 40
1200 81
*For XTAL = 10MHz only! 10-19
8/10/2019 Chapter 10 Serial Port
http://slidepdf.com/reader/full/chapter-10-serial-port 20/37
The PIC uCs
The PIC uCs
Baud rate Formula
If Fosc = 10MHz
X = (156250/Desired Baud Rate) - 1
Example:
Desired baud rate = 1200, Clock Frequency =
10MHz
X = (156250/1200) – 1X = 129.21 = 129 = 81H
10-20
8/10/2019 Chapter 10 Serial Port
http://slidepdf.com/reader/full/chapter-10-serial-port 21/37
The PIC uCs
TXREG Register
8-bit register used for serialcommunication in the PIC18
For a byte of data to be transferred via
the Tx pin, it must be placed in the TXREGregister first.
The moment a byte is written into TXREG,it is fetched into a non-accessible registerTSR
MOVFF PORTB, TXREG
The frame contains 10 bits10-21
8/10/2019 Chapter 10 Serial Port
http://slidepdf.com/reader/full/chapter-10-serial-port 22/37
The PIC uCs
RCREG Register
8-bit register used for serialcommunication in the PIC18
When the bits are received serially via the
Rx pin, the PIC18 deframes them byeliminating the START and STOP bit,making a byte out of data received andthen placing it in the RCREG register
MOVFF RCREG, PORTB
10-22
8/10/2019 Chapter 10 Serial Port
http://slidepdf.com/reader/full/chapter-10-serial-port 23/37
The PIC uCs
The PIC uCs
TXSTA (Transmit Status andControl Register)
10-23
8/10/2019 Chapter 10 Serial Port
http://slidepdf.com/reader/full/chapter-10-serial-port 24/37
The PIC uCs
The PIC uCs
TXSTA (Transmit Status andControl Register) (Cont’d)
10-24
8/10/2019 Chapter 10 Serial Port
http://slidepdf.com/reader/full/chapter-10-serial-port 25/37
The PIC uCs
The PIC uCs
RCSTA (Receive Status andControl Register)
10-25
8/10/2019 Chapter 10 Serial Port
http://slidepdf.com/reader/full/chapter-10-serial-port 26/37
The PIC uCs
The PIC uCs
RCSTA (Receive Status andControl Register) (Cont’d)
10-26
8/10/2019 Chapter 10 Serial Port
http://slidepdf.com/reader/full/chapter-10-serial-port 27/37
The PIC uCs
The PIC uCs
PIR1 (Peripheral InterruptRequest Register 1)
10-27
8/10/2019 Chapter 10 Serial Port
http://slidepdf.com/reader/full/chapter-10-serial-port 28/37
The PIC uCs
Programming the PIC18 toTransfer Data Serially
1. TXSTA register = 20H: Indicatingasynchronous mode with 8-bit data frame, lowbaud rate and transmit enabled
2. Set Tx pin an output (RC6)3. Loaded SPBRG for baud rate
4. Enabled the serial port (SPEN = 1 in RCSTA)
5. The character byte to transmit must be
written into TXREG6. Keep Monitor TXIF bit
7. To transmit next character, go to step 5
10-28
8/10/2019 Chapter 10 Serial Port
http://slidepdf.com/reader/full/chapter-10-serial-port 29/37
The PIC uCs
The PIC uCs
Example 10.2
;Write a program for the PIC18 to transfer the letter 'G' serially
;at 9600 baud continuously. Assume XTAL = 10 MHz
MOVLW B'00100000'
MOVWF TXSTA
MOVLW D'15'; 9600 bps
MOVWF SPBRG
BCF TRISC, TX
BSF RCSTA, SPEN
OVER MOVLW A'G'S1 BTFSS PIR1, TXIF
BRA S1
MOVWF TXREG
BRA OVER
10-29
8/10/2019 Chapter 10 Serial Port
http://slidepdf.com/reader/full/chapter-10-serial-port 30/37
The PIC uCs
TXSTA: Transmit Status andControl Register
10-30
8/10/2019 Chapter 10 Serial Port
http://slidepdf.com/reader/full/chapter-10-serial-port 31/37
The PIC uCs
Programming the PIC18 toReceive Data Serially
1. RCSTA register = 90H: To enable thecontinuous receive in addition to the 8-bitdata size option
2. The TXSTA register = 00H: To choosethe low baud rate option
3. Loaded SPBRG for baud rate
4. Set Rx pin an input5. Keep Monitor RCIF bit
6. Move RCREG into a safe place
7. To receive next character, go to step 510-31
8/10/2019 Chapter 10 Serial Port
http://slidepdf.com/reader/full/chapter-10-serial-port 32/37
The PIC uCs
The PIC uCs
Example 10.4
;Write a program for the PIC18 to receive data serially and
;put them on PORTB. Set the baud rate at 9600, 8-bit data
;and 1 stop bit
MOVLW B'10010000'
MOVWF RCSTA
MOVLW D'15'
MOVWF SPBRG
BSF TRISC, RX
CLRF TRISBR1 BTFSS PIR1, RCIF
BRA R1
MOVFF RCREG, PORTB
BRA R1
10-32
8/10/2019 Chapter 10 Serial Port
http://slidepdf.com/reader/full/chapter-10-serial-port 33/37
The PIC uCs
Increasing the Baud Rate
Faster CrystalMay not be able to change crystal
TXSTA.BRGH bit
Normally used low Can be set high
Quadruples rate when set high
8/10/2019 Chapter 10 Serial Port
http://slidepdf.com/reader/full/chapter-10-serial-port 34/37
The PIC uCs
Baud Rate ErrorCalculation
??? Errors in the baud rate? Yep! Caused by using integer division in rate generator
)1(64
)
SPBRG
F Rate Baud Calculated
where
Rate Baud Desired
Rate Baud Desired ratebaud Calculated Error
OSC
8/10/2019 Chapter 10 Serial Port
http://slidepdf.com/reader/full/chapter-10-serial-port 35/37
The PIC uCs
Transmit and Receive
Please see program 10-1: Page 412
10-35
F 10 1 l f d
8/10/2019 Chapter 10 Serial Port
http://slidepdf.com/reader/full/chapter-10-serial-port 36/37
The PIC uCs
Figure 10-12. Simplified USARTTransmit Block Diagram
10-36
8/10/2019 Chapter 10 Serial Port
http://slidepdf.com/reader/full/chapter-10-serial-port 37/37
Th PIC C
Chapter 10: Summary
Next: the final exam
10 37