键盘0~9一共10个键,还有一个确认键,一个修改键。我照着书比葫芦画瓢自己写了一个,怎么用protues仿真不成功呢,大佬们帮帮忙。
#include<reg52.h>
#include<intrins.h>
#include<stdlib.h>
#define uchar unsigned char
#define uint unsigned int
sbit dula=P3^2; //修改
sbit wela=P3^3;
#define LCD_data P0 //12864显示用P0口
sbit LCD_RS=P3^5;
sbit LCD_RW=P3^6;
sbit LCD_EN=P3^4;
sbit LCD_PSB=P3^7;
uchar code dis1[]={"欢迎通行"};
uchar code dis2[]={"车牌号:"};
uchar code dis3[]={"每公里5元"};
void delay(uint xms)
{
uint i,j;
for(i=xms;i>0;i--)
for(j=110;j>0;j--)
}
void write_cmd(uchar cmd) //写命令
{
LCD_RS=0;
LCD_RW=0;
LCD_EN=0;
P0=cmd;
delay(5);
LCD_EN=1;
delay(5);
LCD_EN=0;
}
void write_dat(uchar dat) //写数据
{
LCD_RS=1;
LCD_RW=0;
LCD_EN=0;
P0=dat;
delay(5);
LCD_EN=1;
delay(5);
LCD_EN=0;
}
void lcd_pos(uchar X,uchar Y)
{
uchar pos;
if(X==0)
{X=0x80;}
if(X==1)
{X=0x90;}
if(X==2)
{X=0x88;}
if(X==3)
{X=0x98;}
pos=X+Y;
write_cmd(pos);
}
void lcd_init() //LCD初始化设定
{
LCD_PSB=1;
write_cmd(0x30);
delay(5);
write_cmd(0x0C);
delay(5);
write_cmd(0x01);
delay(5);
write_cmd(0x06);
delay(5);
lcd_pos(0,0);
uchar i=0;
while(dis2[i]!='\0')
{
write_dat(dis2[i]);
i++;
}
lcd_pos(1,0);
i=0;
while(dis3[i]!='\0')
{
write_dat(dis3[i]);
i++;
}
}
void matrixkeyscan() //键盘扫描程序,使用P2口
{
uchar temp,key;
P2=0xfe;
temp=p2;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(10);
temp=P2;
temp=temp&0xf0;
if(temp!=0xf0)
{
temp=P2;
switch(temp)
{
case 0xee:
key=0;
break;
case 0xde:
key=1;
break;
case 0xbe:
key=2;
break;
case 0x7e:
key=3;
break;
}
while(temp!=0xf0)
{
temp=P2;
temp=temp&0xf0;
}
write_dat(key);
}
}
P2=0xfd;
temp=p2;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(10);
temp=P2;
temp=temp&0xf0;
if(temp!=0xf0)
{
temp=P2;
switch(temp)
{
case 0xed:
key=4;
break;
case 0xdd:
key=5;
break;
case 0xbd:
key=6;
break;
case 0x7d:
key=7;
break;
}
while(temp!=0xf0)
{
temp=P2;
temp=temp&0xf0;
}
write_dat(key);
}
}
P2=0xfb;
temp=p2;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(10);
temp=P2;
temp=temp&0xf0;
if(temp!=0xf0)
{
temp=P2;
switch(temp)
{
case 0xeb:
key=8;
break;
case 0xdb:
key=9;
break;
}
while(temp!=0xf0)
{
temp=P2;
temp=temp&0xf0;
}
write_dat(key);
}
}
P2=0xfb;
temp=p2;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(10);
temp=P2;
temp=temp&0xf0;
if(temp!=0xf0)
{
temp=P2;
switch(temp)
{
case 0xbb: //此键位为数字确认按键
break;
}
while(temp!=0xf0)
{
temp=P2;
temp=temp&0xf0;
}
write_cmd(0x01);
delay(5);
lcd_pos(1,0);
uchar i=0;
while(dis1[i]!='\0')
{
write_dat(dis1[i]);
i++;
}
}
}
P2=0xfb;
temp=p2;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(10);
temp=P2;
temp=temp&0xf0;
if(temp!=0xf0)
{
temp=P2;
switch(temp)
{
case 0x7b: //此键位为数字修改按键
break;
}
while(temp!=0xf0)
{
temp=P2;
temp=temp&0xf0;
}
lcd_init();
lcd_pos(0,8);
}
}
}
void main() //主函数
{
wela=0;
dula=0;
uchar i;
delay(10);
lcd_init();
lcd_pos(0,8);
while(1)
{
matrixkeyscan();
}
}
|