#include
#include
#define uchar unsigned char
#define uint unsigned int
sbit light=P2^5;
uchar originalword[6]={1,2,3,4,5,6};
uchar currentword[6];
uchar newword[6];
bit pwflag;
uchar count=0;
bit entryflag;
uchar key,n=0;
//定义变量
void delay(uint i) //延时函数
{
while(i--);
}
void keyscan()//键盘扫描函数,使用行列反转扫描法 反转法的原理:
// 反转法就是通过给单片机的端口赋值两次,最后得出所按键的值的一种算法。
{
uchar l,h; //定义局部变量,用l得出低4位的值,用h得出高4位的值
P0=0x0f; //给P1赋值00001111
l=P0&0x0f;
if(l!=0x0f)
{
delay(10);
if(l!=0x0f)
l=P0&0x0f; //若有键按下,得出低四位的值
P0=l|0xf0;
h=P0&0xf0;
// while((P0&0x0f)!=0x0f);
}
key=l+h; //高4位的值与低4位的值相加
}
uchar coding(){
uint keynum;
keyscan();
switch(key)
{
case(0xee): keynum=1;break;
case(0xde): keynum=2;break;
case(0xbe): keynum=3;break;
case(0x7e): keynum='A';break;
case(0xed): keynum=4;break;
case(0xdd): keynum=5;break;
case(0xbd): keynum=6;break;
case(0x7d): keynum='B';break;
case(0xeb): keynum=7;break;
case(0xdb): keynum=8;break;
case(0xbb): keynum=9;break;
case(0x7b): keynum='C';break;
case(0xe7): keynum='*';break;
case(0xd7): keynum=0;break;
case(0xb7): keynum='#';break;
case(0x77): keynum='S';break;
}
return(keynum);
}
void main()
{
uchar k;
light=0;
k=coding();
while(k>0){
switch(k){
case 1: if(count<6){
currentword[count]=1;
count++;
}
break;
case 2: if(count<6){
currentword[count]=2;
count++;
}
break;
case 3: if(count<6){
currentword[count]=3;
count++;
}
break;
case 4: if(count<6){
currentword[count]=4;
count++;
}
break;
case 5: if(count<6){
currentword[count]=5;
count++;
}
; break;
case 6: if(count<6){
currentword[count]=6;
count++;
}
break;
case 7: if(count<6){
currentword[count]=7;
count++;
}
break;
case 8: if(count<6){
currentword[count]=8;
count++;
}
break;
case 9: if(count<6){
currentword[count]=9;
count++;
}
break;
case 0: if(count<6){
currentword[count]=0;
count++;
}
break;
case 'A': light=1; break;
case 'B': light=1; break;
case 'C': light=1; break;
case 'D': light=1; break;
case 'S': light=1; break;
case '*': light=1; break;
case '#': light=1; break;
}
if(count==6){
while(1){
light=1;
count=0;
}
}
}
}