|
适合新手学习。
单片机源程序如下:
- #include<reg52.h>
- #include<intrins.h>
- #define uint unsigned int
- #define uchar unsigned char
- #define GPIO_KEY P2
- sbit LCD_E=P3^7;
- sbit LCD_RW=P3^6;
- sbit LCD_RS=P3^5;
- sbit beep=P3^4;
- uchar table[]="00:00:00";
- char keyword1[]={1,2,3,4,5,6};
- char table1[5]="pass!";
- char table2[6]="wrong!";
- char in[6]={-6,-6,-6,-6,-6,-6};
- uchar num,miao=0,fen=0,shi=0,x,y,n1=0,n2=0,cmiao=0,cshi=0,cfen=0,i,ge1=0,shi1=0,bai1=0,qian1=0;
- char KeyValue=-1,Key1=0,Key2=0;
- int s=0;
- char flag1=-1,flag2=-1;
- char keyword;
- uchar i=0,j,a=0;
- char flag_pass=-1,flag_beep=-1,c;
- void delay5ms();
- void write_com(uchar com);
- void write_data(uchar dat);
- void display(uchar b,uchar dat);
- void init1();
- void init2();
- void Keyscan();
- void input();
- void show1();
- void show2();
- char store();
- void unlock();
- void delay2s();
- void main()
- {
- init1();
- beep=1;
- while(1)
- {
- if(flag1==1)
- {
- if(KeyValue==1)
- {
- TR0=0;
- a++;
- KeyValue=-1;
- if(a==4)
- {
- TR0=1;
- a=0;
- }
-
- }
- if(a!=0)
- {
- if(KeyValue==2)
- {
- KeyValue=-1;
- switch(a)
- {
- case 1:miao=miao+1;display(0x86,miao);break;
- case 2:fen=fen+1;display(0x83,fen); break;
- case 3:shi=shi+1;display(0x80,shi); break;
- }
- }
- if(KeyValue==3)
- {
- KeyValue=-1;
- switch(a)
- {
- case 1:miao=miao-1;display(0x86,miao);break;
- case 2:fen=fen-1;display(0x83,fen); break;
- case 3:shi=shi-1;display(0x80,shi); break;
- }
- }
- }
- }
- Keyscan();
- input();
- unlock();
- if(flag_beep==1)
- {
- beep=0;
- }
- else
- {
- beep=1;
- }
- }
- }
- void delay5ms() //?? 0us
- {
- uchar a,b;
- for(b=19;b>0;b--)
- for(a=130;a>0;a--);
- }
- void write_com(uchar com)
- {
- LCD_E=0;
- LCD_RW= 0 ;
- LCD_RS=0;
- P1=com;
- delay5ms();
- LCD_E=1;
- delay5ms();
- LCD_E=0;
- }
- void write_data(uchar dat)
- {
- LCD_E=0;
- LCD_RW=0;
- LCD_RS=1;
- P1=dat;
- delay5ms();
- LCD_E=1;
- delay5ms();
- LCD_E=0;
- }
- void init1()
- {
- write_com(0x38);
- write_com(0x0c);
- write_com(0x06);
- write_com(0x01);
- }
- void init2()
- {
- write_com(0x80);
- for(num=0;num<8;num++)
- {
- write_data(table[num]);
- }
- write_com(0xc0);
- for(i=0;i<8;i++)
- write_data(table[i]);
- TMOD=0X11;
-
- TH0=(65536-50000)/256;
- TL0=(65536-50000)%256;
- TH1=(65536-50000)/256;
- TL1=(65536-50000)%256;
-
- ET0=1;//????0??
- TR0=1;//?????0
- ET1=1;//????0??
- TR1=1;
- EA=1;
- }
- void display(uchar b,uchar dat)
- {
- uchar shi,ge;
- shi=dat/10;
- ge=dat%10;
- write_com(b);
- delay5ms();
- write_data(0x30+shi);
- write_data(0x30+ge);
- }
- void Keyscan()
- {
- uchar i;
- GPIO_KEY=0X0f;
- if(GPIO_KEY!=0X0f)
- {
- delay5ms();
- if(GPIO_KEY!=0X0f)
- {
-
- switch(GPIO_KEY)
- {
- case 0x07:KeyValue=12; break ;
- case 0x0b:KeyValue=8; break ;
- case 0x0d:KeyValue=4; break ;
- case 0x0e:KeyValue=0; break ;
- }
-
- GPIO_KEY=0XF0;
- if(GPIO_KEY!=0XF0)
- {
- delay5ms();
- if(GPIO_KEY!=0XF0)
- switch(GPIO_KEY)
- {
- case 0x70:KeyValue=KeyValue+1;break;
- case 0xb0:KeyValue=KeyValue+2;break;
- case 0xd0:KeyValue=KeyValue+3;break;
- case 0xe0:KeyValue=KeyValue+4;break;
- }
-
- while((i<50)&&(GPIO_KEY!=0XF0))
- {
- delay5ms();
- i++;
- }
- i=0;
-
- }
- }
- }
- }
- void input()
- {
-
- if(flag_pass==1)
- {
- if(flag1==1)
- {
- if(KeyValue==5)
- {
- KeyValue=-1;
- Key2++;
- if(Key2==4)
- Key2=0;
- }
- if(Key2!=0)
- {
- if(KeyValue==6)
- {
- KeyValue=-1;
- switch(Key2)
- {
- case 1:cmiao=cmiao+1;display(0xc6,cmiao);break;
- case 2:cfen=cfen+1;display(0xc3,cfen);break;
- case 3:cshi=cshi+1;display(0xc0,cshi);break;
-
- }
- }
- if(KeyValue==7)
- {
- KeyValue=-1;
- switch(Key2)
- {
- case 1:cmiao=cmiao-1;display(0xc6,cmiao);break;
- case 2:cfen=cfen-1;display(0xc3,cfen);break;
- case 3:cshi=cshi-1;display(0xc0,cshi);break;
- }
- }
- }
- if(KeyValue==9)
- {
- KeyValue=-1;
- s++;
- }
- if(KeyValue==10)
- {
- KeyValue=-1;
- s--;
- }
- if(KeyValue==13)
- {
- flag2=1;
- }
- if(KeyValue==14)
- {
- flag2=2;
- }
- if(miao==cmiao&&fen==cfen&&shi==cshi&&Key2==0)
- {
- flag_beep=1;
- c=KeyValue;
- }
- if(c!=KeyValue)
- {
- flag_beep=-1;
- }
-
- show1();
- show2();
-
- }
- if(KeyValue==4)
- {
- KeyValue=-1;
- init1();
- init2();
- flag1=1;
- }
- if(KeyValue==8)
- {
- KeyValue=-1;
- TR1=1;
- }
- if(KeyValue==12)
- {
- KeyValue=-1;
- TR1=0;
- }
- }
- }
- void unlock()
- {
- uchar s1,m;
- char flag[6]={-1,-1,-1,-1,-1,-1};
- if(flag_pass==-1)
- {
- if(KeyValue==2&&flag!=1)
- {
- for(s1=0;s1<6;s1++)
- {
- if(in[s1]==keyword1[s1])
- flag[s1]=1;
- else
- flag[s1]=0;
- }
- if(flag[0]==1&&flag[1]==1&&flag[2]==1&&flag[3]==1&&flag[4]==1&&flag[5]==1)
- {
- flag_pass=1;
- write_com(0x80);
- for(m=0;m<5;m++)
- write_data(table1[m]);
- }
- else
- {
- write_com(0x80);
- flag_pass=-1;
- for(m=0;m<6;m++)
- write_data(table2[m]);
- }
- }
-
- if(KeyValue!=-1&&flag1!=1)
- {
- if(KeyValue!=1)
- {
- in[i]=store();
- i++;
- KeyValue=-1;
- }
- if(KeyValue==1)
- {
- i--;
- in[i]=-6;
- KeyValue=-1;
- }
- }
- write_com(0xc0);
- for(j=0;j<6;j++)
- {
- write_data(in[j]+0x30);
- }
- }
- else ;
-
- }
- void show1()
- {
- if(n1>19) //????????
- {
- n1=0;
- miao++;
- if(miao>59)
- {
- miao=0;
- fen++;
- if(fen>59)
- {
- fen=0;
- shi++;
- if(shi>23)
- {
- shi=0;
- }
- display(0x80,shi);
- }
- display(0x83,fen);
- }
- display(0x86,miao);
- }
- }
- void show2()
- {
-
- if(n2>19)
- {
- if(flag2==1)
- {
- s++;
- }
- if(flag2==2)
- {
- s--;
- }
- n2=0;
-
- }
- if(s<0)
- s=0;
- qian1=s/1000;
- bai1=s%1000/100;
- shi1=s%100/10;
- ge1=s%10;
- write_com(0xc9);
- write_data(qian1+0x30);
- write_com(0xca);
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
多功能密码锁.zip
(53.3 KB, 下载次数: 29)
|
|