Выбрать главу

  {

  case Plus:

   Calc = Op1 + Op2;  // If ADD

   break;

  case Minus:

   Calc = Op1 - Op2;  // If Subtract

   break;

  case Multiply:

   Calc = Op1 * Op2;  // If Multiply

   break;

  case Divide:

   Calc = Op1 / Op2;  // If Divide

   break;

  }

  LongToStr(Calc, op); // Convert to string

  //

  // Remove leading blanks

  //

  j=0;

  for(i=0;i<=11;i++) {

   if (op[i] != ' ') // If a blank

   {

    lcd[j]=op[i];

    j++;

   }

  }

  Lcd_Out_Cp(lcd); // Display result

  Delay_ms(5000);  // Wait 5 seconds

  Lcd_Cmd(LCD_CLEAR);

 }

}

Figure 6.49: Modified program listing

Before using the Keypad_Released function we have to call the Keypad_Init function to tell the microcontroller what the keypad is connected to. Keypad_Released detects when a key is pressed and then released. When released, the function returns a number between 1 and 16 corresponding to the key pressed. The remaining parts of the program are the same as in Figure 6.48.

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

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.

Figure 6.53: Block diagram of the project

Figure 6.54: Circuit diagram of the project

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.