Activity 3. Simple calculator

This activity uses a 4x4 keypad and a LCD 16x2, with the aim of making the PIC18F4550 a simple calculator that can perform 4 basic operations between 2 single-digit numbers.

(75 minutes)


Activity 3

The PIC18F4550:

    • reads the first number
    • reads which mathematical operation will be performed (+, -, X, /)
    • reads the second number
    • displays the result on the LCD
    • waits for ‘C’ to be pressed to start the process from the beginning



Step 1. The circuit is drawn at the Proteus Design Suite.



Step 2. The program in C language is written.

Write in CCS C Compiler the program in C language

 #include <main.h> // the file main.h with the

                  // initial settings is included.

                  // This file must be placed in the same

                  // folder with the project.

                  // Also the 18F4550.h file must exist

                  // in the same folder with the project

               

#include <flex_lcd.h> // The h file of the lcd driver

                      // should be in the same folder where we will save our program.

                      // The #define LCD_DB4 PIN_B4 etc statements in flex_lcd.c

                      // should be checked and possibly modified.

                      // These statements determine the pins of the microcontroller

                      // that are connected to LCD 16x2.

                           

#include <keypad.h>   // The h file of the keypad driver

                      // should be in the same folder where we will save our program.

                      // The #define row0 PIN_B4 etc statements in keypad.h

                      // should be checked and possibly modified.

                      // These statements determine the pins of the microcontroller

                      // that are connected to the keypad 4x4.

 

#byte PORTB =0xF81

                     // We attribute to the memory position 0xF81 the name PORTB.

                     // This means that we define a 8 bit variable whose value

                     // will be stored to the memory position F81h.

                     // The memory position F81h is the PORTD data register.

 

#byte PORTD=0xF83    // F83h is the position or PORTD data register

                     // at the data memory of the microcontroller

                     // SFR Special Function Register

 

char key;         //variable to save keypad's characters

int num1=10;      //variable for the first number

int num2=10;      //variable for the second number

char operation;   //variable for the operation

float result;     //variable for the result

boolean flag=0;   //flag raised when the divider is 0

 

//this function converts keypad's character to integer

int convert_to_number(char c);

 

// ********* main program ************************

void main() {

   set_tris_d(0x00);    //PORTD is defined as output - LCD 16x2

   kbd_init();          //initialization routine for the keypad 4x4

   lcd_init();          //initialization routine for the LCD 16x2                

   lcd_putc("\f");      //clear the screen    

  

   while(TRUE){   

      //read the first number

      do{

         //wait until a key is pressed

         key=kbd_getc();

         if(key!=0){

            //call the "convert_to_number"

            num1=convert_to_number(key);

         }

      }

      while(num1>9);

      //print the first number

      printf(lcd_putc,"%d",num1);

       

      //read the operation

      do{

         //wait until a key is pressed

         operation=kbd_getc();

      }

      while(operation!='X' && operation!='/' && operation!='+' && operation!='-');

 

      //print the operation

      if(operation=='+'){

         printf(lcd_putc," + ");

      }

      else if(operation=='-'){

        printf(lcd_putc," - ");

      }

      if(operation=='X'){

         printf(lcd_putc," * ");

      }

      else if(operation=='/'){

         printf(lcd_putc," / ");

      }

     

      //read the second number

      do{

         //wait until a key is pressed

         key=kbd_getc();

         if(key!=0){

            //call the "convert_to_number"

            num2=convert_to_number(key);

         }

      }

      while(num2>9);

      //print the second number

      printf(lcd_putc,"%d = ",num2);

 

      //calculate the result

      if(operation=='+'){

          result=num1+num2;

       }

      else if(operation=='-'){

         if(num1>num2){

            result=num1-num2;

         }

         else{

            result=num2-num1;

            printf(lcd_putc,"-");

         }

      }

      else if(operation=='X'){

        result=num1*num2;

      } 

      else if(operation=='/'){

         if(num2==0){

            flag=1;

         }

         else{

            result=(float)(num1)/num2;

         }

      }   

 

      if(flag==1){ //divider = 0

         flag=0;

         printf(lcd_putc,"\nUndefined");

      }

      else{

         //print the result

         printf(lcd_putc,"%f",result);

      }

 

      //wait until the "C" is pressed

      do{

         key=kbd_getc();

      }

      while(key!='C');

 

      //clear the LCD

      lcd_putc("\f");

      num1=num2=10;

   }

}

 

//this function converts keypad's character to integer

int convert_to_number(char c){

  if(c=='0'){

    return 0;

  }

  else if(c=='1'){

    return 1;

  }

  else if(c=='2'){

    return 2;

  }

  else if(c=='3'){

    return 3;

  }

  else if(c=='4'){

    return 4;

  }

  else if(c=='5'){

    return 5;

  }

  else if(c=='6'){

    return 6;

  }

  else if(c=='7'){

    return 7;

  }

  else if(c=='8'){

    return 8;

  }

  else if(c=='9'){

    return 9;

  }

  else if(c=='/'){

    return 10;

  }

  else if(c=='X'){

    return 11;

  }

  else if(c=='-'){

    return 12;

  }

  else if(c=='+'){

    return 13;

  }

  else if(c=='='){

    return 14;

  }

  else if(c=='C'){

    return 15;

  }

  else{

   return 16;

  }

}


Step 3. Compile the program in order to create the hex.file (program in machine code). Load the program (hex.file) to the microcontroller.



Step 4. Run the simulation and check the correct operation of the circuit.



Step 5. Suggested modifications and discussion:

    • can other operations be added, such as power, or square root? 
      Tip. #include <math.h>, pow(), sqrt()