Thursday, November 18th, 2010

MikroC code for CALCULATOR WITH KEYPAD AND LCD
by

MikroC code for CALCULATOR WITH KEYPAD AND LCD:
In this project a 4 x 4 keypad is connected to PORTB of a PIC18F452 microcontroller. Also an LCD is connected to PORTC. The project is a simple calculator which can perform integer arithmetic.
The keys are organized as follows:
0 1 2 3
4 5 6 7
8 9 10 11
12 13 14 15
The keys are labeled as follows:
1 2 3 4
5 6 7 8
9 0  .  Enter
+ − * /
#define MASK 0xF0
#define Enter 11
#define Plus 12
#define Minus 13
#define Multiply 14
#define Divide 15
//// This function gets a key from the keypad//
unsigned char getkeypad()
{unsigned char i, Key = 0;
PORTB = 0×01; // Start with column 1
while((PORTB & MASK) == 0) // While no key pressed
{PORTB = (PORTB << 1); // next column
Key++; // column number
if(Key == 4){
PORTB = 0×01; // Back to column 1
Key = 0; }}
Delay_Ms(20); // Switch debounce
for(i = 0×10; i !=0; i <<=1) // Find the key pressed
{if((PORTB & i) != 0)break;
Key = Key + 4;}
PORTB=0x0F;
while((PORTB & MASK) != 0); // Wait until key released
Delay_Ms(20); // Switch debounce
return (Key); // Return key number
}//// Start of MAIN program//
void main()
{unsigned char MyKey, i,j,lcd[5],op[12];
unsigned long Calc, Op1, Op2;
TRISC = 0; // PORTC are outputs (LCD)
TRISB = 0xF0; // RB4-RB7 are inputs
//// Configure LCD//
Lcd_Init(&PORTC); // LCD is connected to PORTC
Lcd_Cmd(LCD_CLEAR);
Lcd_Out(1,1,”CALCULATOR”); // Display CALCULATOR
Delay_ms(2000); // Wait 2 seconds
Lcd_Cmd(LCD_CLEAR); // Clear display
//// Program loop//
for(;;) // Endless loop
{MyKey = 0;
Op1 = 0;
Op2 = 0;
Lcd_Out(1,1,”No1: “); // Display No1:
while(1) // Get first no
{MyKey = getkeypad();
if(MyKey == Enter)break; // If ENTER pressed
MyKey++;
if(MyKey == 10)MyKey = 0; // If 0 key pressed
Lcd_Chr_Cp(MyKey + ’0′);
Op1 = 10*Op1 + MyKey; // First number in Op1
}
Lcd_Out(2,1,”No2: “); // Display No2:
while(1) // Get second no
{MyKey = getkeypad();
if(MyKey == Enter)break; // If ENTER pressed
MyKey++;
if(MyKey == 10)MyKey = 0; // If 0 key pressed
Lcd_Chr_Cp(MyKey + ’0′);
Op2 = 10*Op2 + MyKey; // Second number in Op2
}
Lcd_Cmd(LCD_CLEAR); // Clear LCD
Lcd_Out(1,1,”Op: “); // Display Op:
MyKey = getkeypad(); // Get operation
Lcd_Cmd(LCD_CLEAR);
Lcd_Out(1,1,”Res=”); // Display Res=
switch(MyKey) // Perform the operation
{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 in op
//// 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);
}}

PIC 16F877, 18F452 and 18F4550,PIC16F877, PIC18F452,PIC18F4550,16f876,16f84,microcontroller projects,lm35,lm34,lm335,temperature sensor,MikroC Projects

5 Responses

05/15/2011
Keiv

Hi, i would like to ask about the formula Op1 = 10*Op1 + MyKey.
could you explain how it formed? is it Op1 an ASCII number for every single key??


01/25/2012
Aproniano

To Whom it may concern,

Will you please send me the circuit of this using proteus?

Thanks.


08/25/2012
ram

Will you please send me the circuit of this using proteus?


01/22/2013
boby

Hello.Thanks for your nice project of; MikroC code for CALCULATOR WITH KEYPAD AND LCD.

I made its PROTEUS as you wrote at your project and tested it but it does not work!!??

When I press each keys, there is indicate nothing on the LCD.

Would you please tell me what is wrong with my PROTEUS of your project to work correctly?

[I can send you that PROTEUS to your e-mail.].

Thanks,


05/22/2013
sina

Will you please send me the circuit of this using proteus?