0% found this document useful (0 votes)
85 views6 pages

Caluculator Key Pad

This code defines a key() function that reads the state of buttons on a 4x4 keypad and returns the corresponding number. It uses this to build a basic calculator that accepts two numbers from the keypad, performs an arithmetic operation selected from additional buttons, and displays the result on an LCD screen. The main program loop continuously waits for input, performs calculations, and displays output.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
85 views6 pages

Caluculator Key Pad

This code defines a key() function that reads the state of buttons on a 4x4 keypad and returns the corresponding number. It uses this to build a basic calculator that accepts two numbers from the keypad, performs an arithmetic operation selected from additional buttons, and displays the result on an LCD screen. The main program loop continuously waits for input, performs calculations, and displays output.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

#include<avr/io.

h>
#include<util/delay.h>
#include<user/lcd.h>

int key()
{
while(1)
{

PORTC=0XE0;

_delay_ms(10);
if(PINA==0X0E) { while(PINA==0X0E); return 1;}
if(PINA==0X0D) { while(PINA==0X0D); return 2;}
if(PINA==0x0B) { while(PINA==0x0B);return 3;}
if(PINA==0x07) { while(PINA==0x07);return 4;}

PORTC=0xD0;
_delay_ms(10);
if(PINA==0x0E){ while(PINA==0x0E);return 5;}
if(PINA==0x0D){ while(PINA==0x0D);return 6;}
if(PINA==0x0B){ while(PINA==0x0B);return 7;}
if(PINA==0x07){ while(PINA==0x07);return 8;}

PORTC=0xB0;
_delay_ms(10);

if(PINA==0x0E){ while(PINA==0x0E);return 9;}


if(PINA==0x0D){ while(PINA==0x0D);return 0;}
if(PINA==0x0B){ while(PINA==0x0B);return 11;}
if(PINA==0x07){ while(PINA==0x07);return 12;}

PORTC=0x70;
_delay_ms(10);

if(PINA==0x0E){ while(PINA==0x0E);return 13;}


if(PINA==0x0D){ while(PINA==0x0D);return 14;}
if(PINA==0x0B){ while(PINA==0x0B);return 15;}
if(PINA==0x07){ while(PINA==0x07);return 16;}

}
}

int x=0,y=0,k;
int sum,sub,mul,div;

void main()
{

DDRD=0xFF;
lcd_init();

DDRA=0xF0;
PORTA=0x0F;
DDRC=0xF7;

disp_cmd(0x88);
disp_cmd(0x06);
while(1)
{

do
{
k=key();

if(k==16){break;}

else
{
x=x*10+k;
disp_cmd(0x88);
lcd_number(x);
_delay_ms(300);
}

}while(k!=16);

do
{

k=key();

if(k==16){break;}

else
{
y=y*10+k;
disp_cmd(0x88);
lcd_number(y);
_delay_ms(300);
}

}while(k!=16);

do
{
k=key();

if(k==13)
{
_delay_ms(10);
sum=x+y;
disp_cmd(0xc8);
lcd_number(sum);
_delay_ms(10);
break;
}

else if(k==14)
{
_delay_ms(10);

if(x>y)
sub=x-y;
else
{
disp_cmd(0xc8);
disp_data('-');
sub=y-x;
}

disp_cmd(0xc9);
lcd_number(sub);
_delay_ms(10);break;
}

else if(k==12)
{
_delay_ms(10);
mul=x*y;
disp_cmd(0xc8);
lcd_number(mul);
_delay_ms(10);break;
}

else if(k==15)

{
_delay_ms(10);
div=x/y;
disp_cmd(0xc8);
lcd_number(div);
_delay_ms(10);break;}

else if(k==16){break;}

}while(k==16);

You might also like