Serial Communication Interface Chapter 3 Objectives Upon completion

Serial Communication Interface Chapter 3

Objectives • Upon completion of this chapter, you will be able to: – Explain serial communication protocol – Describe the serial communication features of the PIC 18 – Program the PIC 18 serial port in C

Introduction • Computers transfer data in two ways: Parallel and Serial. • Parallel: Eight or more data lines, few feet only, short time • Serial: Single data line, long distance • The PIC 18 has serial communication capability built into it.

Basics of Serial Communication • The byte of data must be converted to serial bits using a parallel-in-serial-out shift register Serial versus Parallel Data Transfer

Basics of Serial Communication (cont’d) • The receiving end must be a serial-inparallel-out shift register and pack them into a byte. • Two methods of serial data communication: Synchronous and Asynchronous Transfers a single byte at a time Transfers a block of data at a time

Half-and Full-Duplex Transmission

Start and Stop Bits • In the asynchronous method, each character is placed between start and stop bits (framing) MSB LSB Framing ASCII ‘A’ (41 H)

Data Transfer Rate • Rate of data transfer: bps (bits per second) • Another widely used terminology for bps is baud rate • For Asynchronous serial data communication, the baud rate is generally limited to 100, 000 bps

RS 232 Standard • Standard for serial comm (COM port) 1: -3 V to -25 V; 0: +3 V to +25 V – Reason: for long distance wired line • Input-output voltage are not TTL compatible • So, we need MAX 232/233 for voltage converter. Commonly known as line drivers

RS 232 Pins Connectors: Minimally, 3 wires: Rx. D, Tx. D, GND Could have 9 -pin or 25 -pin DB-25 DB-9 25 -Pin Connector 9 -Pin Connector

RS 232 Pins (cont’d) IBM PC DB-9 Signals Data in Data out DB-9 9 -Pin Connector 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)

PIC 18 Connection to RS 232 Line driver (a) Inside MAX 232 (b) its Connection to the PIC 18

PIC 18 Connection to RS 232 (Cont’d) Line driver (a) Inside MAX 233 (b) Its Connection to the PIC 18

SPBRG (Serial Port Baud Rate Generator) Register and Baud Rate in the PIC 18 • The baud rate in the PIC 18 is programmable • The value loaded into the SPBRG decides the baud rate • Depend on crystal frequency SPBRG Baud Rate (Hex Value) 38400 19200 9600 4800 2400 1200 *For XTAL = 10 MHz only! 3 7 F 20 40 81

Baud rate Formula (pg. 397) X (SPBRG) = (156250/Desired Baud Rate) - 1 Example: Desired baud rate = 1200, Clock Frequency = 10 MHz X (SPBRG) = (156250/1200) – 1 X (SPBRG) = 129. 21 = 129 = 81 H

TXREG Register • 8 -bit register used for serial communication in the PIC 18 • For a byte of data to be transferred via the Tx pin, it must be placed in the TXREG register

RCREG Register • 8 -bit register used for serial communication in the PIC 18 • 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 in the RCREG register

TXSTA (Transmit Status and Control Register)

TXSTA (Transmit Status and Control Register) (Cont’d)

RCSTA (Receive Status and Control Register) _used to enable the serial port to receive data

RCSTA (Receive Status and Control Register) (Cont’d)

PIR 1 (Peripheral Interrupt Request Register 1)

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 3. Loaded SPBRG for baud rate 4. Enabled the serial port (SPEN = 1) 5. Character byte to transmit is write into TXREG 6. Keep Monitor TXIF bit 7. To transmit next character, go to step 5

Example 1 Write a C program to transfer the letter ‘G’ serially at 9600, continuously. Use 8 -bit data and 1 stop bit. Assume XTAL = 10 MHz.

Example 1 (cont’d)

Example 2 Write a C program to transfer the message “YES” serially at 9600, continuously. Use 8 -bit data and 1 stop bit. Assume XTAL = 10 MHz.

Example 2 (cont’d)

Example 3 Write a C program to receive bytes of data serially and put them on PORTB. Set the baud rate at 9600, 8 -bit data and 1 stop bit.

Example 3 (cont’d)

End of Chapter 3
- Slides: 30