#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;
}
}
}
}
main函數結構錯誤:
k=coding();這句應該包含進一個死循環中,你這樣寫一上電沒等你操作就過去了,以下僅供參考:
void main()
{
uchar k;
light=0;
while(1){
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){ //已經輸入了6個密碼了,你想要干啥呢?如果是密碼鎖,是不是該確認一下密碼對不對,對了怎麼辦,不對又怎麼辦
//做個循環,判斷密碼
//前面再定義個標志變量,這個你自己加到開頭: bool flag = 0 ;
for(i=0;i<6;i++)
{
if(currentword[i]!=originalword[i]) //只要有一位密碼不對
{
count=0;//清空計數
flag = 0;
break;//跳出
}
flag = 1; //6位驗證都通過設為1
}
if(flag == 1)
{//這裡,密碼都對了,該怎麼做你自己改
flag =0 ;
light=1;
count=0;
}
}
}
}
}
PS:格式!格式!格式!