【文件属性】:
文件名称:c语言手机键盘
文件大小:3KB
文件格式:C
更新时间:2014-11-10 07:42:53
c语言、手机键盘
#include
#include
#define uchar unsigned char
#define uint unsigned int
#define delaynop() {_nop_();_nop_();_nop_();_nop_();}
sbit RS=P3^5;
sbit RW=P3^6;
sbit EN=P3^7;
sbit spk=P2^7;
uchar code keypad_chars[12][6]={"1 ","2abc","3def","4ghi","5jkl","6mno","7pqrs","8tuv","9wxyz","*","0+","#"};
uchar code line1[]=" mobile keypad ";
uchar code line2[]="key_char: ";
uchar keyno=-1;
uchar inner_idx=0;
uchar tspan=0;
void delayms(uchar i)
{
uchar x;
while(i--)
{
for(x=0;x<120;x++);
}
}
//uchar get_lcd_status()
uchar busy_waiting()
{
uchar lcd_status;
RS=0;
RW=1;
EN=1;
delayms(1);
lcd_status=P0;
EN=0;
return lcd_status;
}
void write_lcd_data(uchar dat)
{
while((busy_waiting()&&0x80)==0x80);
RS=1;RW=0;EN=0;P0=dat;EN=1;delayms(1);EN=0;
}
void write_lcd_command(uchar cmd)
{
while((busy_waiting()&0x80)==0x80);
RS=0;
RW=0;
EN=0;
P0=cmd;EN=1;delayms(1);EN=0;
}
void init_lcd()
{
write_lcd_command(0x38);
delayms(1);
write_lcd_command(0x01);
delayms(1);
write_lcd_command(0x06);
delayms(1);
write_lcd_command(0x0c);
delayms(1);
}
void showstring(uchar x,uchar y,uchar *str)
{
uchar i;
if(y==0)write_lcd_command(0x80|x);
if(y==1)write_lcd_command(0xc0|x);
for(i=0;i<16;i++)
{
write_lcd_data(str[i]);
}
}
void set_lcd_pos(uchar position)
{
write_lcd_command(position|0x80);
}
void key_scan()
{
P1=0x0f;delayms(1);
switch(P1)
{
case 0x0e:keyno=0;break;
case 0x0d:keyno=1;break;
case 0x0b:keyno=2;break;
}
P1=0xf0;delayms(1);
switch(P1)
{
case 0xe0:keyno+=0;break;
case 0xd0:keyno+=3;break;
case 0xb0:keyno+=6;break;
case 0x70:keyno+=9;
}
}
void beep()
{
uchar i;
for(i=0;i<60;i++)
{
spk=!spk;delayms(1);
}
spk=1;
delayms(100);
}
void to_int() interrupt 1
{
TH0=-50000/256;
TL0=-50000%256;
if(tspan<50)tspan++;else TR0=0;
}
void main()
{
uchar i,pre_keyno=-1;
IE=0x82;
TMOD=0x01;
TH0=-50000/256;
TL0=-50000%256;
P0=P2=0xff;
delayms(10);
init_lcd();
set_lcd_pos(0);
for(i=0;i<16;i++)
write_lcd_data(line1[i]);
set_lcd_pos(0x40);
for(i=0;i<16;i++)
write_lcd_data(line2[i]);
while(1)
{
P1=0xf0;
if(P1!=0xf0)key_scan();else continue;
if(pre_keyno!=keyno)
{
pre_keyno=keyno;
tspan=0;
inner_idx=0;
TR0=1;
}
else
{
if(tspan<=40)
inner_idx=(inner_idx+1)%strlen(keypad_chars[keyno]);
else
inner_idx=0;
tspan=0;
}
set_lcd_pos(0x4b);
write_lcd_data(keypad_chars[keyno][inner_idx]);
beep();
delayms(400);
}
}