More PIC Microcontrollers:Serial Ports

Serial Ports

Serial communication ports allow the PIC to communicate with other MCUs, or exchange data with a master controller, via a single connection. Serial connections may also be made with external memory devices and sensors. There are several protocols available in PICs:

• USART (universal synchronous asynchronous receiver transmitter)

• SPI (serial peripheral interface)

• I2C (inter-integrated circuit)

• LIN (local interconnect network)

• CAN (controller area network)

• Ethernet

• USB (universal serial bus)

USART

RS232 is an asynchronous communication protocol previously used in the serial (COM) port of the PC for connecting peripherals such as the mouse, before USB was developed. It is low speed, but easy to understand, and has been used as the direct communication between computers, terminals and other systems for many years. It is also still used to download programs to the PIC MPSTART programmer module.

‘Asynchronous’ means that no separate clock signal is provided with the data, so correct reception of data relies on the sender and receiver operating at the same speed, with reception synchronized using a start bit for each byte. Serial data is sent and received as individual bytes using a pair of shift registers (Figure 12.7a). After each bit is shifted out of the send register onto the line, it must be shifted into the receiver register at the same time. In other words, the receiver must sample the line during the time that the transmitted bit is present. It must then take the next sample after the appropriate interval, which depends on the data rate.

PIC Microcontrollers-1310

The ‘baud rate’ sets this time interval, and there is a set of standard rates of between 300 and 115 200 bits per second. The sender and receiver must be initialized to operate at the same baud rate. At a typical rate of 19 200 baud (about 20 kbits/s), the bit time interval is about 50 ms. The signal is illustrated in Figure 12.7(b). The line is high when inactive; the start of a byte is indicated by the falling edge of a start bit. The receiver then samples the line at the required interval to read each bit, and the sampling is retriggered at the start of each byte.

When the USART is operated in asynchronous mode (Figure 12.8), there is a separate data path for send (TX) and receive (RX). One byte of data is transmitted at a time down the serial line, with start, stop and optional error check (parity) bits. If an error is detected, a retransmission can be requested. A synchronous mode is also available, when the TX pin is used instead to

PIC Microcontrollers-1311

carry a clock (CK) signal. This is sent alongside the data signal to clock the receiver, making the process more reliable. In this mode, the device can still send and receive, but only in one direction at a time.

The RS232 data signal produced by the PIC USART is output at TTL (transistoretransistor logic) levels. Most terminals, such as the PC, will produce a signal that is transmitted at higher bipolar voltage, typically ± 12 V, to allow the signal to travel further on the line (up to about 100 m). If the PIC is to communicate with such a terminal, the signal must be passed through a line driver, which will boost the voltage and shift the level as required.

SPI Bus

The SPI system uses three pins on each system device:

• Serial data out (SDO)

• Serial data in (SDI)

• Serial clock (SCK).

It is a single-master, multi-slave system, using hardware slave selection (Figure 12.9). To exchange data with a slave, the master selects it by taking the slave select input low (!SS). Synchronous 8-bit data is then exchanged via SDI or SDO, with a clock pulse to strobe in each bit to the destination register. Owing to the hardware selection requirements, this system is most suitable for communication between devices on the same board. Data can be transmitted and received at the same time, at a clock rate of up to 5 MHz with a 20 MHz chip clock.

I2C Bus

The I2C (pronounced eye squared see) system needs only two pins on each system device:

• Serial data (SDA)

• Serial clock (SCL).

This system also uses synchronous mastereslave communication, but with a software- rather than a hardware-based addressing system (Figure 12.10). As in a network, the destination address is transmitted on the same line (SDA) before the data. A 7- or 10-bit address can be used (up to 1023 slaves), which must be preprogrammed into an address register in each slave. The slave then only picks up the messages with its own address. The clock can operate at up to 1 MHz. I2C is suitable for communication between separate microcontroller boards, since no slave selection hardware connections are needed. Compare with SPI, the hardware is simpler, but the software is more complex. Note that in the hardware diagram, the lines are pulled up to þ5 V, giving active low, wired-OR operation on the serial bus and clock line.

PIC Microcontrollers-1312

LIN Bus

The LIN bus is a mixture of the I2C and RS232 protocols (Figure 12.11). The PIC interface is designated EUSART (extended USART) to indicate that it supports this additional bus option. It is a single-master, multi-slave protocol using a single bidirectional signal wire operating at 9e18 V (12 V) with open collector output bus transceivers and pull-up resistors. The transceivers are connected to the TX and RX pins as per RS232. Data and control bytes are transmitted in asynchronous mode with start and stop bits in the same way as RS232, but sent as message blocks with synchronization, identifier and up to 8 bytes of data, and terminated by an error check byte in the same way as a network data frame. It is primarily designed for automotive systems, where a reasonably simple and robust protocol is needed to integrate distributed controllers into a network. See Microchip Application Note AN729 for details.

CAN Bus

The CAN system (Figure 12.12) is also designed for transmitting signals in electrically noisy environments, such as motor vehicle control, using differential current drivers operating at 5 V. It is a multi-master system, meaning that any of the system nodes (electronic control units) can send a message at any time. This consists of a data frame containing identifier bits, up to 8 data

PIC Microcontrollers-1313

PIC Microcontrollers-1314

bytes, error checking and acknowledge bits. Collisions are resolved by each message having a priority code within its identifier code. CAN bus is only currently available in selected high-performance PIC 18 series devices.

Ethernet and USB

Ethernet and USB interfaces are now being added to some of the more powerful PIC MCUs, so they can be connected directly to standard peripherals. Some PIC32 (32-bit) devices support 100 Mb/s Ethernet, PIC24 (16-bit) chips do not, while some PIC18 (8-bit) chips offer 10 Mb/s Ethernet. All groups include chips with a USB 2.0 interface. Both interfaces require significant additional hardware and firmware capabilities to support these complex communication protocols.

More details on these communication interfaces is provided in Interfacing PIC Microcontrollers: Embedded Design by Interactive Simulation (Newnes 2006) by this author.

1. Summarize the key differences between the PIC 10, 12, 16 and 18 series of microcontrollers. (16)

2. State two advantages of in-circuit serial programming. (4)

3. From the table of PIC flash microcontrollers (Table 12.2), select for minimal cost and name:

(a) a device that has eight analogue inputs in the smallest package

(b) a device that could control two PWM motor outputs, has EEPROM, runs at 40 MHz and can be programmed in C. (4)

4. Explain the essential difference between capture and compare timer operations. (3)

5. Describe the essential difference between SPI and I2C addressing. Which has more complex hardware requirements? (3)

1. Download the data sheet and study the summary page for the PIC 12F675, 16F690 and 18F8720. Summarize the features of each, and suggest a typical application for each device.

2. A robot has four axes to be controlled by a PIC MCU. Each has a PWM speed controlled motor and an incremental encoder with three digital outputs providing the position feed- back. A block of EEPROM is needed to store up to 128 programmed positions, requiring

a 16-bit code for each axis for each position. Select the most suitable chip from the Microchip website that could be used as a controller for the robot positioning system. Download the data sheet and draw a block diagram for the system, identifying the pins that should be connected to the motors and encoders. Outline how the controller will move the robot between programmed positions. Refer back to Chapter 11 if necessary.

3. Sketch a block diagram for an alternative implementation of the robot controller in Activity 2 above, using a separate controller for each axis connected to a master SPI controller. Select suitable chips for the master and slave controllers, and list the connections required. Compare the cost of each system and suggest an advantage of the mastereslave system over the single controller solution proposed in Activity 2.

Leave a comment

Your email address will not be published. Required fields are marked *