1. You can now buy finished microcontroller project from us, Check out the Store for the complete list of projects.
  2. Need a custom project, Send us some details about your project. So that we can quote the price for it.

Implementing a Calculator Using Peripherals Like a Keypad and LCD 1.0

Implementing a Calculator Using Peripherals Like a Keypad and LCD

  1. Binu
    In this Project, 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.



    Description:
    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.

    [​IMG]
    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.
    [​IMG]
    Code (Text):
    1. /* To implement a integer calculator using a keypad and LCD */
    2.  
    3. #pragma SMALL DB OE
    4. #include <reg51.h>
    5. #include "io.h"
    6.  
    7. /* The functions to initialize and control the LCD are assumed to be in the file io.c */
    8.  
    9. /* Function to output the decimal value of the result on the LCD */
    10. void PrintInt(int i) {
    11.     .
    12.     .
    13.     .
    14. }
    15.  
    16. /* Routine to scan the key pressed */
    17. unsigned char key_scan()
    18. {
    19.       unsigned char i, j, temp1, temp2;
    20.  
    21.         while( 1 ) /* keep waiting for a key to be pressed */
    22.  
    23.                 for(i=0; i<4; i++) {
    24.  
    25.                         /* Set each row to 0 */
    26.                         P1 = 0xff & ~(1<<i);
    27.  
    28.                         /* Scan each column to see which key was pressed */
    29.                         for (j=4; j<8; j++) {
    30.  
    31.                               /* Code to determine the position of the
    32.                                 key which was pressed */
    33.                               /* return(position) */
    34.                         }
    35.                 }
    36. }
    37.  
    38. void main() {
    39.  
    40.         /* You can have a conversion table to convert the key position into a
    41.           valid number which indicates the operator or operand value. For eg:
    42.           the numbers 0 to 9 are valid operands and 100 to 103 denote
    43.           addition, subtraction, multiplication and division respectively */
    44.  
    45.         char conv_table[] = {
    46.  
    47.                 1, 2,  3, 100 /* add */,
    48.                 4, 5,  6, 101 /* sub */,
    49.                 7, 8,  9, 102 /* mul */,
    50.                 -1, 0, -1, 103 /* div */
    51.         };
    52.         char num1, num2, op;
    53.         int result;
    54.  
    55.         InitIO();
    56.  
    57.         while(1) {
    58.  
    59.                 ClearScreen();
    60.  
    61.                 /* read num1 */
    62.                 GotoXY(0, 0);
    63.                 PrintString("num1 : ");
    64.                 do {
    65.  
    66.                     num1 = conv_table[key_scan()];
    67.                 }
    68.                 while( num1 < 0 || num1 > 9 );
    69.  
    70.                 /* read a valid operation */
    71.                 GotoXY(0, 0);
    72.                 PrintString("op :  ");
    73.                 do {
    74.  
    75.                     op = conv_table[key_scan()];
    76.                 }
    77.                 while( op < 100 );
    78.  
    79.                 /* read num2 */
    80.                 GotoXY(0, 0);
    81.                 PrintString("num2 : ");
    82.                 do {
    83.  
    84.                     num2 = conv_table[key_scan()];
    85.                 }
    86.                 while( num2 < 0 || num2 > 9 );
    87.  
    88.                 /* compute result */
    89.                 if( op == 100 ) {
    90.  
    91.                     /* Add numbers and display result on the LCD */
    92.                 }
    93.                 else if( op == 101 ) {
    94.                       .
    95.                       .
    96.                       .
    97.                       .
    98.                 /* Continue similarly for other operations */
    99.  
    100.                 }
    101.         }
    102. }
Loading...