PIC Microcontroller and Embedded Systems Muhammad Ali Mazidi
PIC Microcontroller and Embedded Systems Muhammad Ali Mazidi, Rolin Mc. Kinlay and Danny Causey Eng. Husam Alzaq The Islamic Uni. Of Gaza The PIC u. Cs 10 -1
Chapter 10: PIC 18 Serial Port Programming in Assembly. PIC Microcontroller and Embedded Systems Muhammad Ali Mazidi, Rolin Mc. Kinlay and Danny Causey, February 2007. The PIC u. Cs 10 -2
Objective r Explain serial communication protocol r Describe data transfer rate and bps rate r Interface the PIC 18 with an RS 232 connector r Describe the main registers used by serial communication of the PIC 18 r Program the PIC 18 serial port in Assembly The PIC u. Cs 10 -3
Outlines r Programming timers 0 and 1 r Counter Programming The PIC u. Cs 10 -4
Introduction r Computers transfer data in two ways: Parallel and Serial. r Parallel: Eight or more data lines, few feet only, short time r Serial: Single data line, long distance r The PIC 18 has serial communication capability built into it. The PIC u. Cs 10 -5
Basics of Serial Communication r The byte of data must be converted to serial bits using a parallel-in-serial-out shift register The PIC u. Cs Serial versus Parallel Data Transfer 10 -6
Basics of Serial Communication (cont’d) r The receiving end must be a serial-in- parallel-out shift register and pack them into a byte. r Two methods of serial data communication: Asynchronous and Synchronous Transfers a single byte at a time The PIC u. Cs Transfers a block of data at a time 10 -7
Half-and Full-Duplex Transmission The PICu. Cs 10 -8
Start and Stop Bits r In the asynchronous method, each character is placed between start and stop bits (framing) MSB LSB Framing ASCII ‘A’ (41 H) The PIC u. Cs 10 -9
Data Transfer Rate of data transfer: bps (bits per second) r Another widely used terminology for bps is baud rate r For Asynchronous serial data communication, the baud rate is generally limited to 100, 000 bps The PIC u. Cs 10 -10
RS 232 Standard r Standard for serial comm (COM port) 1: -3 V to -25 V; 0: +3 V to +25 V m Reason: for long distance wired line r Input-output voltage are not TTL compatible r So, we need MAX 232/233 for voltage converter. Commonly known as line drivers The PIC u. Cs 10 -11
RS 232 Pins Connectors: Minimally, 3 wires: Rx. D, Tx. D, GND Could have 9 -pin or 25 -pin The PICu. Cs DB-25 DB-9 25 -Pin Connector 9 -Pin Connector 10 -12
RS 232 Pins (cont’d) IBM PC DB-9 Signals Data in Data out Pin 1 – Data Carrier Detect (DCD) Pin 2 – Received Data (Rx. D) Pin 3 – Transmitted Data (Tx. D) 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 The PICu. Cs 10 -13
PIC 18 Connection to RS 232 Line driver (a) Inside MAX 232 The PICu. Cs (b) its Connection to the PIC 18 10 -14
Figure 10 -6. Null Modem Connection r Null modem is a communication method to connect two DTEs (computer, terminal, printer etc. ) directly using a RS-232 serial cable. r With a null modem connection the transmit and receive lines are crosslinked. r Depending on the purpose, sometimes also one or more handshake lines are crosslinked. The PIC u. Cs 10 -15
PIC 18 Connection to RS 232 Line driver (a) Inside MAX 232 The PICu. Cs (b) its Connection to the PIC 18 10 -16
PIC 18 Connection to RS 232 (Cont’d) Line driver (a) Inside MAX 233 The PICu. Cs (b) Its Connection to the PIC 18 10 -17
Section 10. 3: PIC 18 Serial Port Programming in Assembly r USART has both m Synchronous m Asynchronous Rx Port The PIC u. Cs r 6 registers m SPBRG m TXREG m RCREG m TXSTA m RCSTA m PIR 1 Tx Port 10 -18
SPBRG Register and Baud Rate in the PIC 18 r The baud rate in is programmable r loaded into the SPBRG decides the baud rate r Depend on crystal frequency m BR = The PIC u. Cs F Fosc 4*16*(X+1)) Baud Rate SPBRG (Hex Value) 38400 19200 9600 4800 2400 1200 3 7 F 20 40 81 *For XTAL = 10 MHz only! 10 -19
Baud rate Formula If Fosc = 10 MHz X = (156250/Desired Baud Rate) - 1 Example: Desired baud rate = 1200, Clock Frequency = 10 MHz X = (156250/1200) – 1 X = 129. 21 = 129 = 81 H The PICu. Cs 10 -20
TXREG Register r 8 -bit register used for serial communication in the PIC 18 r For a byte of data to be transferred via the Tx pin, it must be placed in the TXREG register first. r The moment a byte is written into TXREG, it is fetched into a non-accessible register TSR MOVFF PORTB, TXREG r The frame contains 10 bits The PIC u. Cs 10 -21
RCREG Register r 8 -bit register used for serial communication in the PIC 18 r When the bits are received serially via the Rx pin, the PIC 18 deframes them by eliminating the START and STOP bit, making a byte out of data received and then placing it in the RCREG register MOVFF RCREG, PORTB The PIC u. Cs 10 -22
TXSTA (Transmit Status and Control Register) The PICu. Cs 10 -23
TXSTA (Transmit Status and Control Register) (Cont’d) The PICu. Cs 10 -24
RCSTA (Receive Status and Control Register) The PICu. Cs 10 -25
RCSTA (Receive Status and Control Register) (Cont’d) The PICu. Cs 10 -26
PIR 1 (Peripheral Interrupt Request Register 1) The PICu. Cs 10 -27
Programming the PIC 18 to Transfer Data Serially 1. TXSTA register = 20 H: Indicating asynchronous mode with 8 -bit data frame, low baud rate and transmit enabled 2. Set Tx pin an output (RC 6) 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 TXREG 6. Keep Monitor TXIF bit 7. To transmit next character, go to step 5 The PIC u. Cs 10 -28
Example 10. 2 ; Write a program for the PIC 18 to transfer the letter 'G' serially ; at 9600 baud continuously. Assume XTAL = 10 MHz OVER S 1 The PICu. Cs MOVLW MOVWF BCF BSF MOVLW BTFSS PIR 1, BRA MOVWF BRA B'00100000' TXSTA D'15'; 9600 bps SPBRG TRISC, TX RCSTA, SPEN A'G' TXIF S 1 TXREG OVER 10 -29
TXSTA: Transmit Status and Control Register The PIC u. Cs 10 -30
Programming the PIC 18 to Receive Data Serially 1. RCSTA register = 90 H: To enable the continuous receive in addition to the 8 -bit data size option 2. The TXSTA register = 00 H: To choose the low baud rate option 3. Loaded SPBRG for baud rate 4. Set Rx pin an input 5. Keep Monitor RCIF bit 6. Move RCREG into a safe place 7. To receive next character, go to step 5 The PIC u. Cs 10 -31
Example 10. 4 ; Write a program for the PIC 18 to receive data serially and ; put them on PORTB. Set the baud rate at 9600, 8 -bit data ; and 1 stop bit R 1 The PICu. Cs MOVLW MOVWF BSF CLRF BTFSS PIR 1, BRA MOVFF BRA B'10010000' RCSTA D'15' SPBRG TRISC, RX TRISB RCIF R 1 RCREG, PORTB R 1 10 -32
Increasing the Baud Rate r Faster Crystal m May not be able to change crystal r TXSTA. BRGH bit m Normally used low m Can be set high m Quadruples rate when set high The PIC u. Cs
Baud Rate Error Calculation r ? ? ? Errors in the baud rate? Yep! m Caused by using integer division in rate generator The PIC u. Cs
Transmit and Receive r Please see program 10 -1: Page 412 The PIC u. Cs 10 -35
Figure 10 -12. Simplified USART Transmit Block Diagram The PIC u. Cs 10 -36
Chapter 10: Summary Next: the final exam The PIC u. Cs 10 -37
- Slides: 37