Книга: Advanced PIC Microcontroller Projects in C

PROJECT 6.10 — Serial Communication–Based Calculator 

Project Description

Serial communication is a simple means of sending data long distances quickly and reliably. The most common serial communication method is based on the RS232 standard, in which standard data is sent over a single line from a transmitting device to a receiving device in bit serial format at a prespecified speed, also known as the baud rate, or the number of bits sent each second. Typical baud rates are 4800, 9600, 19200, 38400, etc.

RS232 serial communication is a form of asynchronous data transmission where data is sent character by character. Each character is preceded with a start bit, seven or eight data bits, an optional parity bit, and one or more stop bits. The most common format is eight data bits, no parity bit, and one stop bit. The least significant data bit is transmitted first, and the most significant bit is transmitted last.

A logic high is defined at –12V, and a logic 0 is at +12V. Figure 6.50 shows how character “A” (ASCII binary pattern 0010 0001) is transmitted over a serial line. The line is normally idle at –12V. The start bit is first sent by the line going from high to low. Then eight data bits are sent, starting from the least significant bit. Finally, the stop bit is sent by raising the line from low to high.


Figure 6.50: Sending character “A” in serial format

In a serial connection, a minimum of three lines is used for communication: transmit (TX), receive (RX), and ground (GND). Serial devices are connected to each other using two types of connectors: 9-way and 25-way. Table 6.11 shows the TX, RX, and GND pins of each type of connectors. The connectors used in RS232 serial communication are shown in Figure 6.51.

Table 6.11: Minimum required pins for serial communication

9-pin connector
Pin Function
2 Transmit (TX)
3 Receive (RX)
5 Ground (GND)
25-pin connector
Pin Function
2 Transmit (TX)
3 Receive (RX)
7 Ground (GND)

Figure 6.51: RS232 connectors

As just described, RS232 voltage levels are at ±12V. However, microcontroller input-output ports operate at 0 to +5V voltage levels, so the voltage levels must be translated before a microcontroller can be connected to a RS232 compatible device. Thus the output signal from the microcontroller has to be converted to ±12V, and the input from an RS232 device must be converted into 0 to +5V before it can be connected to a microcontroller. This voltage translation is normally done with special RS232 voltage converter chips. One such popular chip is the MAX232, a dual converter chip having the pin configuration shown in Figure 6.52. The device requires four external 1?F capacitors for its operation.


Figure 6.52: MAX232 pin configuration

In the PIC18 series of microcontrollers, serial communication can be handled either in hardware or in software. The hardware option is easy. PIC18 microcontrollers have built-in USART (universal synchronous asynchronous receiver transmitter) circuits providing special input-output pins for serial communication. For serial communication all the data transmission is handled by the USART, but the USART has to be configured before receiving and transmitting data. With the software option, all the serial bit timing is handled in software, and any input-output pin can be programmed and used for serial communication.

In this project a PC is connected to the microcontroller using an RS232 cable. The project operates as a simple integer calculator where data is sent to the microcontroller using the PC keyboard and displayed on the PC monitor.

CALCULATOR PROGRAM
Enter First Number: 12
Enter Second Number: 2
Enter Operation: +
Result = 14

Оглавление книги


Генерация: 1.068. Запросов К БД/Cache: 3 / 1
поделиться
Вверх Вниз