SIMPLE CALCULATOR PROJECT

on Saturday, 9 November 2013


In our previous posts we have seen interface of matrix keypad and 16x2 Lcd display, in this post we will use both and we make a simple calculator project.
If you dint red my previous post then go back to home page and read hierarchically, Click here

AIM:Aim of the project is to make a simple calculator which can perform addition, subtraction, multiplication and division.

CIRCUIT DIAGRAM:
KEYPAD SCHEMATIC: 



















LCD SCHEMATIC:


OVERALL CIRCUIT:

All other connections are same i.e pin 13, 14 =crystal, 12,31=gnd, 1,11,32=power +5v.
EXPLANATION:

When we apply power, Lcd displays "CALCULATOR" for few sec, next  "No1:" is displayed on the LCD's first line and user is expected to type first number and then press ENTER key, next  "No2:" is displayed then user is expected to type the second number and press ENTER key. After user press Enter key, "op:" will displayed on the LCD and it is expected that user enter the operation key i.e for addition "+" for subtraction "-" for multiplication "x" for division "\" . As soon as the user press corresponding key, result will be generated and displayed on LCD. Again loop will be repeated.  
Example: 
CALCULATOR
No1: 10
No2: 3
Op: +
13

Note from the above circuit diagram that there are many unused keys. I have left it because if you want to make this calculator to perform more operations like sine, cos, tan , etc then include them by extending this project.
ALGORITHM:

1. Display "CALCULATOR"
2. Delay 2sec
3. Display "No1:"
4. Take input and save in variable op1 until user press Enter key
5. Display "No2:"
6. Take input and save in variable op2 until user press Enter key
7. Display "Op:"
8. Save in variable 'op'
9. Use 'switch' statement and perform corresponding operation
10.Convert 'result' to 'string' (os[12]) so that it can be displayed on Lcd
11. Left trim the string using ltrim() library function
12. Display result on the Lcd screen

PROGRAM:
//COMPILER: MIKROC FOR PIC
//MICRO CONTROLLER: PIC18F452
//CRYSTAL FREQ: 4MHZ


#define mask 0b00101111
#define mask 0b00101111
#define enter 10
#define plus 11
#define minus 12
#define multiply 13
#define divide 14
#define resett 15
//Lcd Module 
sbit LCD_RS at RC4_bit;
sbit LCD_EN at RC5_bit;
sbit LCD_D4 at RC0_bit;
sbit LCD_D5 at RC1_bit;
sbit LCD_D6 at RC2_bit;
sbit LCD_D7 at RC3_bit;

sbit LCD_RS_Direction at TRISC4_bit;
sbit LCD_EN_Direction at TRISC5_bit;
sbit LCD_D4_Direction at TRISC0_bit;
sbit LCD_D5_Direction at TRISC1_bit;
sbit LCD_D6_Direction at TRISC2_bit;
sbit LCD_D7_Direction at TRISC3_bit;
// End LCD module connections

//Function to detect pressed key

unsigned char getkeypad()
{
   unsigned char i,key=0,flag=0;

   PORTB=0x01;//First column=1
   while(1)//infinite loop
  {

      if((PORTA&mask)!=0)//if key is pressed
     {
                  flag=1;//set the flag if any key is pressed
                  if(PORTA.F0==1)break;//check first row
                  key=key+5;
                  if(PORTA.F1==1)break;//check 2nd row
                  key=key+5;
                  if(PORTA.F2==1)break;//check 3rd row
                  key=key+5;
                  if(PORTA.F3==1)break;//check 4th row
                  key=key+5;
                  if(PORTA.F5==1)break;//check 5th row
     }
     if(flag==1)break;//if key is detected break
     key++;// increment the variable 'key'
     PORTB<<=1;//left shift the portb
      if(key==5)
      {
         key=0;PORTB=0X01;
       }
  }

  //Below two lines are used to check whether user is still hold the pressed key
  PORTB=0b00011111;//make all columns one
  while((PORTA&mask)!=0);//check all rows, if not equal to 0 then enter into while loop and come out of the loop if rows is '0' i.e come out of loop if person released the pressed key.
  delay_ms(20);
  return(key);//return key number

}
//Main function

void main()
 {
      unsigned char mykey,op,os[12];

      long op1,op2,result;

      TRISC=0;TRISA=0b000101111;TRISB=0X0;

      ADCON1=7;//Disable analog of porta to use porta for digital use

      //configure LCD
      Lcd_Init();//Library function for LCD initiate

      Lcd_Cmd(_LCD_CLEAR);//function for LCD clear

      Lcd_Out(1,1,"CALCULATOR");

      while(1)
      {
      Delay_ms(2000);//delay 2sec

      Lcd_Cmd(_LCD_CLEAR);

      Lcd_Out(1,1,"No1:");

      while(1)
      {

         mykey=getkeypad();//take input from keypad
         if(mykey==enter)break;//if enter is pressed, come out of loop
         if(mykey==resett)asm RESET;//if reset is pressed then reset
         Lcd_Chr_Cp(mykey+'0');
         op1=10*op1+mykey;

       }
       Lcd_Cmd(_LCD_CLEAR);

       Lcd_Out(1,1,"No2:");

       while(1)
      {

         mykey=getkeypad();//take input from keypad
         if(mykey==enter)break;//if enter is pressed then break the loop
         if(mykey==resett)asm RESET;//if resett is pressed then 'reset'
         Lcd_Chr_Cp(mykey+'0');
         op2=10*op2+mykey;

       }
       Lcd_Cmd(_LCD_CLEAR);

       Lcd_Out(1,1,"op:");

       op=getkeypad();

       switch(op)//Do corresponding operation
       {
              case plus:
                        result=op1+op2;//adding 
                        break;
              case minus:
                         result=op1-op2;//subtracting
                         break;
              case multiply:
                         result=op1*op2;//multiplication
                         break;
              case divide:
                         result=op1/op2;//division
                         break;
        }
        longtostr(result,os);//convert to string

        Lcd_Cmd(_LCD_CLEAR);//clear lcd

        ltrim(os);// left trim 'os'

        Lcd_out(1,1,os);//display result

        result=op1=op2=op=mykey=0;//clear all previous data
        }
       
}

It is not necessary for me to explain code again because I have explained in my previous posts i.e Interfacing 16x2 Lcd to microcontroller and Tutorial on 5x5 matrix keypad, but one thing i need to clear.
Look at the line 33 in main program above, in which " asm reset" resets the microcontroller. Here 'reset' is an assembly instruction, we can use assembly language instructions in between MikroC language code using keyword 'asm'.
 
AUTHOR: YOUSTRON SIC


4 comments:

Anonymous said...

Is it possible with this circuit 7 segment display?

jaime medina said...

hello excuse me could pass such files and the simulation code ?

Aman Rustagi said...

Thanks a lot .. It will help us in our lab assignments. Here is another C program to make calculator using switch statement.

Anonymous said...

Can we have a code like this in assembly language?

Post a Comment