(protues高版本不兼容低版本真是不方便啊)
设计要求:
实现Proteus ISIS中的键盘矩阵与8051微控制器的接口。在上一个项目中,实现了LCD与8051微控制器的接口,可以在上面显示数据和不同的值,接下来的项目将从键盘上获取按键值,并在LCD上显示这些值。
#include
void cct_init(void);
void delay(int);
void lcdinit(void);
void writecmd(int);
void writedata(char);
void Return(void);
char READ_SWITCHES(void);
char get_key(void);
sbit RowA = P1^0;
sbit RowB = P1^1;
sbit RowC = P1^2;
sbit RowD = P1^3;
sbit C1 = P1^4;
sbit C2 = P1^5;
sbit C3 = P1^6;
sbit C4 = P1^7;
sbit E = P3^6;
sbit RS = P3^7;
int main(void)
{
char key;
cct_init();
lcdinit();
while(1)
{
key = get_key();
writedata(key);
}
}
void cct_init(void)
{
P0 = 0x00;
P1 = 0xf0;
P2 = 0x00;
P3 = 0x00;
}
void delay(int a)
{
int i;
for(i=0;i}
void writedata(char t)
{
RS = 1;
P2 = t;
E = 1;
delay(150);
E = 0;
delay(150);
}
void writecmd(int z)
{
RS = 0;
P2 = z;
E = 1;
delay(150);
E = 0;
delay(150);
}
void lcdinit(void)
{
delay(15000);
writecmd(0x30);
delay(4500);
writecmd(0x30);
delay(300);
writecmd(0x30);
delay(650);
writecmd(0x38);
writecmd(0x0c);
writecmd(0x01);
writecmd(0x06);
}
void Return(void)
{
writecmd(0x02);
delay(1500);
}
char READ_SWITCHES(void)
{
RowA = 0; RowB = 1; RowC = 1; RowD = 1;
if (C1 == 0) { delay(10000); while (C1==0); return '7'; }
if (C2 == 0) { delay(10000); while (C2==0); return '8'; }
if (C3 == 0) { delay(10000); while (C3==0); return '9'; }
if (C4 == 0) { delay(10000); while (C4==0); return '/'; }
RowA = 1; RowB = 0; RowC = 1; RowD = 1;
if (C1 == 0) { delay(10000); while (C1==0); return '4'; }
if (C2 == 0) { delay(10000); while (C2==0); return '5'; }
if (C3 == 0) { delay(10000); while (C3==0); return '6'; }
if (C4 == 0) { delay(10000); while (C4==0); return 'x'; }
RowA = 1; RowB = 1; RowC = 0; RowD = 1;
if (C1 == 0) { delay(10000); while (C1==0); return '1'; }
if (C2 == 0) { delay(10000); while (C2==0); return '2'; }
if (C3 == 0) { delay(10000); while (C3==0); return '3'; }
if (C4 == 0) { delay(10000); while (C4==0); return '-'; }
RowA = 1; RowB = 1; RowC = 1; RowD = 0;
if (C1 == 0) { delay(10000); while (C1==0); return 'C'; }
if (C2 == 0) { delay(10000); while (C2==0); return '0'; }
if (C3 == 0) { delay(10000); while (C3==0); return '='; }
if (C4 == 0) { delay(10000); while (C4==0); return '+'; }
return 'n';
}
char get_key(void)
{
char key = 'n';
while(key=='n')
key = READ_SWITCHES();
return key;
}
别的就没啥了,没太大问题的情况下是都可以正常运行的。