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.
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.
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 1mF capacitors for its operation.
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.
A sample calculation is as follows:
CALCULATOR PROGRAM
Project Hardware
Figure 6.53 shows the block diagram of the project. The circuit diagram is given in Figure 6.54. This project uses a PIC18F452 microcontroller with a 4MHz resonator, and the built-in USART is used for serial communication. The serial communication lines of the microcontroller (RC6 and RC7) are connected to a MAX232 voltage translator chip and then to the serial input port (COM1) of a PC using a 9-pin connector.
Project PDL
The PDL of the project is shown in Figure 6.55. The project consists of a main program and two functions called Newline and Text_To_User. Function Newline sends a carriage-return and line-feed to the serial port. Function Text_To_User sends a text message to USART. The main program receives two numbers and the operation to be performed from the PC keyboard. The numbers are echoed on the PC monitor. The result of the operation is also displayed on the monitor.
Project Program
The program listing of the project is shown in Figure 6.56. The program consists of a main program and two functions called Newline and Text_To_Usart. Function Newline sends a carriage return and line feed to the USART to move the cursor to the next line. Function Text_To_Usart sends a text message to the USART.
At the beginning of the program various messages used in the program are defined as msg1 to msg5. The USART is then initialized to 9600 baud using the mikroC library routine Usart_Init. Then the heading “CALCULATOR PROGRAM” is displayed on the PC monitor. The program reads the first number from the keyboard using the library function Usart_Read. Function Usart_Data_Ready checks when a new data byte is ready before reading it. Variable Op1 stores the first number. Similarly, another loop is formed and the second number is read into variable Op2. The program then reads the operation to be performed (þ – * /). The required operation is performed inside a switch statement and the result is stored in variable Calc. The program then converts the result into string format by calling library function LongToStr. Leading blanks are
removed from this string, and the final result is stored in character array kbd and sent to the USART to display on the PC keyboard.
Testing the Program
The program can be tested using a terminal emulator software such as HyperTerminal, which is distributed free of charge with Windows operating systems. The steps to test the program follow (these steps assume serial port COM2 is used):
• Connect the RS232 output from the microcontroller to the serial input port of a PC (e.g., COM2)
• Start HyperTerminal terminal emulation software and give a name to the session
• Select File -> New connection -> Connect using and select COM2
• Select the baud rate as 9600, data bits as 8, no parity bits, and 1 stop bit
• Reset the microcontroller
An example output from the HyperTerminal screen is shown in Figure 6.57.
Using Software-Based Serial Communication
The preceding example made use of the microcontroller’s USART and thus its special serial I/O pins. Serial communication can also be handled entirely in software, without using the USART. In this method, any pin of the microcontroller can be used for serial communication.
The calculator program given in Project 10 can be reprogrammed using the mikroC software serial communications library functions known as the Software Uart Library.
The modified program listing is given in Figure 6.58. The circuit diagram of the project is same as in Figure 6.54 (i.e., RC6 and RC7 are used for serial TX and RX respectively), although any other port pins can also be used. At the beginning of the program the serial I/O port is configured by calling function Soft_Uart_Init. The serial port name, the pins used for TX and RX, the baud rate, and the mode are specified. The mode tells the microcontroller whether or not the data is inverted. Setting mode to 1 inverts the data. When a MAX232 chip is used, the data should be noninverted (i.e., mode ¼ 0).
Serial data is then output using function Soft_Uart_Write. Serial data is input using function Soft_Uart_Read. As the reading is a nonblocking function, it is necessary to check whether or not a data byte is available before attempting to read. This is done using the error argument of the function. The remaining parts of the program are