Implementing a Calculator Using Peripherals Like a Keypad

Introduction

In this lab, you will build a simple calculator using the keypad as an input and the LCD as an output peripheral. After debugging and testing your program, you will have to burn the compiled program to a standalone 8051 chip at the end of this lab.

Keypads are often used as a primary input device for embedded microcontrollers. The keypads actually consist of a number of switches, connected in a row/column arrangement as shown in Fig 1.

In order for the microcontroller to scan the keypad, it outputs a nibble to force one (only one) of the columns low and then reads the rows to see if any buttons in that column have been pressed. The rows are pulled up by the internal weak pull-ups in the 8051 ports. Consequently, as long as no buttons are pressed, the microcontroller sees a logic high on each of the pins attached to the keypad rows. The nibble driven onto the columns always contains only a single 0. The only way the microcontroller can find a 0 on any row pin is for the keypad button to be pressed that connects the column set to 0 to a row. The controller knows which column is at a 0-level and which row reads 0, allowing it to determine which key is pressed. For the keypad, the pins from left to right are: R1, R2, R3, R4, C1, C2, C3, C4.

Assignment

In this lab :

• You will design a integer calculator with a keypad input. Your design should be able to read the operands (integers 0 to 9) and the operator from the keypad. You can design any combination of the input sequence as you like. For example, you may wish to input in the way:
1. pick the first operand
2. pick the operator
3. pick the second operand

Apparatus Required:

1. 1k resistor(1)
3. LCD
4. 12MHz Crystal
5. 5V power supply
6. Philips PDS51 development board
7. Programmer LCPX5X40

Schematic:

Program:

```
/* To implement a integer calculator using a keypad and LCD */

#pragma SMALL DB OE
#include <reg51.h>
#include "io.h"

/* The functions to initialize and control the LCD are assumed to be in the file io.c */

/* Function to output the decimal value of the result on the LCD */
void PrintInt(int i) {
.
.
.
}

/* Routine to scan the key pressed */
unsigned char key_scan()
{
unsigned char i, j, temp1, temp2;

while( 1 ) /* keep waiting for a key to be pressed */

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

/* Set each row to 0 */
P1 = 0xff & ~(1<<i);

/* Scan each column to see which key was pressed */
for (j=4; j<8; j++) {

/* Code to determine the position of the
key which was pressed */
/* return(position) */
}
}
}

void main() {

/* You can have a conversion table to convert the key position into a
valid number which indicates the operator or operand value. For eg:
the numbers 0 to 9 are valid operands and 100 to 103 denote
addition, subtraction, multiplication and division respectively */

char conv_table[] = {

1, 2,  3, 100 /* add */,
4, 5,  6, 101 /* sub */,
7, 8,  9, 102 /* mul */,
-1, 0, -1, 103 /* div */
};
char num1, num2, op;
int result;

InitIO();

while(1) {

ClearScreen();

GotoXY(0, 0);
PrintString("num1 : ");
do {

num1 = conv_table[key_scan()];
}
while( num1 < 0 || num1 > 9 );

/* read a valid operation */
GotoXY(0, 0);
PrintString("op :   ");
do {

op = conv_table[key_scan()];
}
while( op < 100 );

GotoXY(0, 0);
PrintString("num2 : ");
do {

num2 = conv_table[key_scan()];
}
while( num2 < 0 || num2 > 9 );

/* compute result */
if( op == 100 ) {

/* Add numbers and display result on the LCD */
}
else if( op == 101 ) {
.
.
.
.
/* Continue similarly for other operations */

}
}
}
```

Procedure:

1. Wire up the circuit as shown in the schematic.
Note: Port 0 should not be used for output because it does cannot sufficiently drive the LCD.
2. Follow the instructions given in Lab 1 to map your network drive, edit, compile and run your program using PDS51.
3. After successfully emulation, translate .OMF file to .HEX file:
oh51 calc.out
4. Copy your hex file, eg: calc.hex onto a floppy disk and proceed to burn an 8051 chip.
Programmer: MP-51
• Connect to serial port
• Plug power in
• Run the programmer by using the executable file: ceibo.exe
• Place the chip to be programmed on the slot
• Use the arrow keys on the keyboard to choose options
• Choose Type ® 51Controller ® PC89C52U
• Choose File ® Load and enter the pathname of your hex file
A:\calc.hex
• Choose Command ® Program ® DeviceMemory ® Program Device ¢s Memory
• The chip will be programmed. Remove it from the socket.
5. Plug in the 8051 chip onto your breadboard. Make sure the following connections are made before you switch the power on.
• Pin 40: VCC (+5V)
• Pin 31: EA (+5V)
• Pin 9: Reset (0V) Pulse it to +5V to start.
• Pin 18-19: Connect the 12Mhz crystal between these pins.