為什么這個程式在盤片機上按鍵的功能只能顯示1,2,3,其他按鍵完全沒用,密碼鎖的功能也不行
#include <reg51.h>
#define uint unsigned int
#define uchar unsigned char
#define Segport P0;
sbit Du=P2^6;
sbit Wei=P2^7;
void delay(uint);
void Show_Pwd();
void Show_on();
void Show_err();
void Sure_on_err();
void Show_Sixty();
void Show_Time(uint);
void Init();
void Total_Show();
void Close_Init();
void Pwd_Modity();
void SetPwd();
char Key=-1;
uchar PwdRight=0;
char PwdDigit=-1;
uchar PwdErrTime=0;
uchar TimeCount=0;
uchar ShowSign=1;
uchar Keycount=0;
char Password[6]={-1,-1,-1,-1,-1,-1};
uchar Pwd_Now[6]={10,10,10,10,10,10};
uchar code Seg_Du[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40,0x37,0x7b,0x31}; //??0-9?'-'(10),'n'(11),'e'(12),'r'(13)
uchar code key_buf[]={0xd7,0xeb,0xdb,0xbb,0xed,0xdd,0xbd,0xee,
0xde,0xbe,0xb7,0x7e,0x7d,0x7b,0x77,0xe7};
uchar code Seg_wei[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf};
void delay(uint time)
{
uchar i=0;
for(;time>0;time--)
for(i=0;i<113;i++);
}
void Getkey(void) interrupt 0
{
uchar key_scan[]={0xef,0xdf,0xbf,0x7f};
uchar i=0,j=0;
for(i=0;i<4;i++)
{
P3=key_scan[i];
if((P3&0x0f)!=0x0f)
{
delay(10);
if((P3&0x0f)!=0x0f)
{
for(j=0;j<16;j++)
{
if(key_buf[j]==P3)
{
while(P3!=key_scan[i])
{
Total_Show();
}
Key=j;
if(j<10)
{
PwdDigit++;
if(PwdDigit<6)
Pwd_Now[PwdDigit]=j;
}
else if(j!=12&&j!=15)
Keycount++;
P3=0x0f;
return;
}
}
}
}
}
}
void Show_Pwd()
{
switch(PwdDigit)
{
case 5:P0=Seg_wei[0];
Wei=1;
Wei=0;
P0=Seg_Du[Pwd_Now[5]];
Du=1;
Du=0;
delay(10);
case 4:P0=Seg_wei[1];
Wei=1;
Wei=0;
P0=Seg_Du[Pwd_Now[4]];
Du=1;
Du=0;
delay(10);
case 3:P0=Seg_wei[2];
Wei=1;
Wei=0;
P0=Seg_Du[Pwd_Now[3]];
Du=1;
Du=0;
delay(10);
case 2:P0=Seg_wei[3];
Wei=1;
Wei=0;
P0=Seg_Du[Pwd_Now[2]];
Du=1;
Du=0;
delay(10);
case 1:P0=Seg_wei[4];
Wei=1;
Wei=0;
P0=Seg_Du[Pwd_Now[1]];
Du=1;
Du=0;
delay(10);
case 0:P0=Seg_wei[5];
Wei=1;
Wei=0;
P0=Seg_Du[Pwd_Now[0]];
Du=1;
Du=0;
delay(10);break;
default:P0=0x00;
Wei=1;
Wei=0;
P0=Seg_Du[10];
Du=1;
Du=0; PwdDigit=-1;delay(10);break;
}
}
void Total_Show()
{
switch(ShowSign)
{
case 1:Show_Pwd();break;
case 2:Show_on();break;
case 3:Show_err();break;
case 4:Show_Sixty();break;
}
}
void Show_on()
{
P0=Seg_wei[0];
Wei=1;
Wei=0;
P0=Seg_Du[0];
Du=1;
Du=0;
delay(15);
P0=Seg_wei[1];
Wei=1;
Wei=0;
P0=Seg_Du[11];
Du=1;
Du=0;
delay(15);
}
void Show_err()
{
P0=Seg_wei[0];
Wei=1;
Wei=0;
P0=Seg_Du[12];
Du=1;
Du=0;
delay(10);
P0=Seg_wei[1];
Wei=1;
Wei=0;
P0=Seg_Du[13];
Du=1;
Du=0;
delay(10);
P0=Seg_wei[2];
Wei=1;
Wei=0;
P0=Seg_Du[13];
Du=1;
Du=0;
delay(10);
}
void Sure_on_err()
{
uchar i=0;
ShowSign=3;
if(PwdRight==PwdDigit)
{
for(i=0;i<=PwdDigit;i++)
{
if(Password[i]!=Pwd_Now[i])
break;
}
if(i==PwdDigit+1)
{
ShowSign=2;
PwdErrTime=0;
}
else
PwdErrTime++;
}
else
PwdErrTime++;
if(PwdErrTime==3)
{
ShowSign=4;
}
}
void SetPwd()
{
uchar i=0;
uchar count=0; //?????????
uchar sign=0; //????????(0:???,1:???)
P3=0x7f; //?????????
while(P3!=key_buf[12]); //setpwd???????
P3=0xef; //?????????
while(P3!=key_buf[15]); //??????ready?
EX0=1; //????????
while(1)
{
Total_Show();
if(count!=Keycount&&Key==11) //??delete?????????????1(count!=Keycount???????delete?,??????????????)
{
count=Keycount; //??????????count
PwdDigit--; //??????--
}
if(PwdDigit>=0&&Key==10) //??1-4???????sure?????????
{
ShowSign=2; //?????on?????
Total_Show();
sign=1; //?????????
for(i=0;i<=PwdDigit;i++) //????
Password[i]=Pwd_Now[i];
PwdRight=PwdDigit; //??????????????????
}
if(Key==14&&sign==1) //?????????????close??
{
return;
}
}
}
void Show_Time(uint number)
{
P0=Seg_wei[0];
Wei=1;
Wei=0;
P0=Seg_Du[10];
Du=1;
Du=0;
delay(5);
P0=Seg_wei[1];
Wei=1;
Wei=0;
P0=Seg_Du[10];
Du=1;
Du=0;
delay(5);
P0=Seg_wei[2];
Wei=1;
Wei=0;
P0=Seg_Du[number/10];
Du=1;
Du=0;
delay(5);
P0=Seg_wei[3];
Wei=1;
Wei=0;
P0=Seg_Du[number%10];
Du=1;
Du=0;
delay(5);
P0=Seg_wei[4];
Wei=1;
Wei=0;
P0=Seg_Du[10];
Du=1;
Du=0;
delay(5);
P0=Seg_wei[5];
Wei=1;
Wei=0;
P0=Seg_Du[10];
Du=1;
Du=0;
delay(5);
}
void Show_Sixty()
{
uchar toal=60;
EX0=0;
TR0=1;
do
{
if(TimeCount==20)
{
TimeCount=0;
toal--;
}
Show_Time(toal); //??????
}while(toal>0); //????0???
TR0=0; //???????
EX0=1; //????????
PwdErrTime=0; //?????????0
PwdDigit=-1; //???????-1????
ShowSign=1; //?????????1,????
}
int0_srv() interrupt 1
{
TimeCount++;
}
void Init()
{
IT0=0; //????????
TMOD=0x01; //???0?????1
TH0=0x3c;
TL0=0xb0; //????50ms
ET0=1; //?????0?????
EA=1; //?????
//?????
// ALTER=0; //?????
}
void Close_Init()
{
uchar i=0;
P0=0xff;
Wei=1;
Wei=0;
EX0=0; //??????
PwdDigit=-1; //???????-1
P3=0xef; //?????????
while(P3!=key_buf[15]);//??????ready?
ShowSign=1; //???????
EX0=1; //??????
for(i=0;i<6;i++) //????????
Pwd_Now[i]=10;
}
void Pwd_Modity()
{
uchar count=0; //?????????
PwdDigit=-1; //???????-1
ShowSign=1; //???????
while(1)
{
Total_Show();
if(count!=Keycount&&Key==11) //??delete?????????????1(count!=Keycount???????delete?,??????????????)
{
count=Keycount; //??????????count
PwdDigit--; //??????--
}
if(PwdDigit>=0&&Key==10) //??1-4???????sure?????????
{
uchar i=0;
for(i=0;i<=PwdDigit;i++) //????
Password[i]=Pwd_Now[i];
for(i=PwdDigit+1;i<6;i++) //??????????-1
Password[i]=-1;
PwdRight=PwdDigit; //??????????????????
ShowSign=2; //?????on?????
Key=12; //??????????switch-case
return;
}
}
}
void main(void)
{
uchar count=0;//???????
Init(); //???
SetPwd(); //????
while(1)
{
if(count!=Keycount) //???????????switch-case(???????delete?,??????????????)
{
count=Keycount; //???????????count
switch(Key)
{
case 10:Sure_on_err();break; //????????
case 11:if(ShowSign==1){PwdDigit--;Pwd_Now[PwdDigit+1]=10;}break; //?????1,??????????????10
case 13:if(ShowSign==2)Pwd_Modity();break; //????????????
case 14:Close_Init();break; //close???????
}
}
Total_Show();
}
}
單片機原理圖是這樣的,







轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/150263.html
標籤:其他技術專區
