Professional Documents
Culture Documents
Project overview
With matrix keypads 16 keys can function with 8 pins (4 rows and 4 columns)
of microcontrollers either same or different ports as convenient.
colum=(key_pin&0x0f);
}while(colum==0x0f);
while(1)
{
key_port=0xef;
colum=(key_pin&0x0f);
if(colum!=0x0f)
{
row=0;
break;
}
key_port=0xdf;
colum=(key_pin&0x0f);
if(colum!=0x0f)
{
row=1;
break;
}
key_port=0xbf;
colum=(key_pin&0x0f);
if(colum!=0x0f)
{
row=2;
break;
}
key_port=0x7f;
colum=(key_pin&0x0f);
row=3;
break;
}
if(colum==0x0e)
key=keypad[row][0];
else if(colum==0x0d)
key=keypad[row][1];
else if(colum==0x0b)
key=keypad[row][2];
else
key=keypad[row][3];
return(key);
}
_delay_ms(1);
lcd &=~(1<<en);
_delay_ms(1); // delay to get things executed
PORTA= ((data<<4)&0xf0);
lcd |=(1<<rs);
lcd &=~(1<<rw);
lcd |= (1<<en);
_delay_ms(1);
lcd &=~(1<<en);
return ;
}
void ini_lcd(void)
{
_delay_ms(5);
lcd_cmd(0x02);
_delay_ms(1);
lcd_cmd(0x28);
_delay_ms(1);
lcd_cmd(0x01); // clear LCD
_delay_ms(1);
lcd_cmd(0x0E); // cursor ON
_delay_ms(1);
lcd_cmd(0x84);
_delay_ms(1);
return;
}
Calculator
The functioning of calculator s as follows:
i.
ii.
iii.
iv.
Main program:
#include<avr/io.h>
#include<util/delay.h>
#include"4bitlcd.h"
#include"keypad.h"
#define key_port PORTA
#define key_ddr DDRA
#define key_pin PINA
void main()
{
unsigned char ch=0,op=0;
long o1=0,o2=0,o3=0,ch1=0;
PORTB=0XFF;
DDRB=0xff;
_delay_ms(5);
ini_lcd();
while(1)
{
lcd_data('k');
lcd_cmd(0x01);
_delay_ms(20);
ch=0;
op=0;
ch1=0;
o1=0;
o2=0;
o3=0;
lcd_cmd(0x80);
while((ch!='+')&(ch!='-')&(ch!='*')&(ch!='/'))
{
ch=takekey();
ch1=ch;
if((ch!='c')&(ch!='=')&(ch!='+')&(ch!='-')&(ch!='*')&(ch!='/'))
{
lcd_data(ch);
o1=((o1*10)+(ch1-0x30));
}
else
{
op=ch;
lcd_data(ch);
break;
}
}
ch=0;
ch1=0;
lcd_cmd(0xc0);
while((ch!='+')&(ch!='-')&(ch!='*')&(ch!='/'))
{
ch=takekey();
ch1=ch;
if((ch!='c')&(ch!='=')&(ch!='+')&(ch!='-')&(ch!='*')&(ch!='/'))
{
lcd_data(ch);
o2=((o2*10)+(ch1-0x30));
}
else
{
lcd_cmd(0x01);
break;
}
}
switch(op)
{
case '+':
o3=o1+o2;
lcd_data('a');
_delay_ms(5);
break;
case '-':
o3=o1-o2;
lcd_data('s');
_delay_ms(5);
break;
case '*':
o3=o1*o2;
//apply operation
lcd_data('m');
_delay_ms(5);
break;
case '/':
o3=o1/o2;
lcd_data('d');
_delay_ms(5);
break;
default:
lcd_cmd(0x01);
_delay_ms(5);
}
lcd_cmd(0xc2);
lcd_cmd(0x01);
lcd_cmd(0xce);
while(o3)
{
lcd_data((o3%10)+0x30);
lcd_cmd(0x10);
lcd_cmd(0x10);
o3=o3/10;
}
ch=takekey();
lcd_cmd(0x01);
_delay_ms(5);
}
return ;
}
//print result