基于单片机设计
单片机源程序如下:
- #include<reg51.h>
- #include<intrins.h>
- #include<stdlib.h>
- #include<zimo.H>
- #include <ctype.h>
- #include <string.h>
- #include <stdio.h>
- #include <math.h>
- #include<ds1820.H>
- #include<ds1302.H>
- #include<at2402.H>
- #define uchar unsigned char
- #define uint unsigned int
- #define lcd_data P1
- uchar a1[6];
- uchar t;
- uchar h;
- uchar k;
- uchar f=0;
- uchar b1[6];
- uchar a1[6]={9,6,3,1,0,2};
- void delay(uint x)
- {
- uint i,j;
- for(i=x;i>0;i--)
- for(j=110;j>0;j--);
- }
- void write_cmd(uchar cmd)
- {
- lcd_rs=0;
- lcd_rw=0;
- lcd_en=0;
- P1=cmd;
- delay(0);
- lcd_en=1;
- delay(0);
- lcd_en=0;
- }
- void write_dat(uchar dat)
- {
- lcd_rs=1;
- lcd_rw=0;
- lcd_en=0;
- P1=dat;
- delay(0);
- lcd_en=1;
- delay(0);
- lcd_en=0;
- }
- void hz_disp16(unsigned char pag,unsigned char col, unsigned char code *hzk)
- {
- unsigned char j=0,i=0;
- for(j=0;j<2;j++)
- {
- write_cmd(0xb8+pag+j);
- write_cmd(0x40+col);
- for(i=0;i<16;i++)
- write_dat(hzk[16*j+i]);
- }
- }
- void hz_disp161(unsigned char pag,unsigned char col, unsigned char code *hzk)
- {
- unsigned char j=0,i=0;
- for(j=0;j<2;j++)
- {
- write_cmd(0xb8+pag+j);
- write_cmd(0x40+col);
- for(i=0;i<8;i++)
- write_dat(hzk[8*j+i]);
- }
- }
- void StartMsg(void)
- {
- CS2=1;CS1=0;
- hz_disp16(0,32,huan);
- delay(10);
- hz_disp16(0,48,ying);
- delay(10);
- CS2=0;CS1=1;
- hz_disp16(0,0,shi);
- delay(10);
- hz_disp16(0,16,yong);
- delay(10);
- CS2=1;CS1=0;
- hz_disp16(2,16,xin1);
- delay(10);
- hz_disp16(2,32,dian);
- delay(10);
- hz_disp16(2,48,zi);
- delay(10);
- CS2=0;CS1=1;
- hz_disp16(2,0,mi);
- delay(10);
- hz_disp16(2,16,ma);
- delay(10);
- hz_disp16(2,32,suo);
- delay(10);
- CS2=1;CS1=0;
- hz_disp16(4,16,zhi);
- delay(10);
- hz_disp16(4,32,zuo);
- delay(10);
- hz_disp16(4,48,maohao);
- delay(10);
- CS2=0;CS1=1;
- hz_disp16(4,0,liu);
- delay(10);
- hz_disp16(4,16,li);
- delay(10);
- hz_disp16(4,32,guo);
- delay(10);
- CS2=1;CS1=0;
- hz_disp16(6,0,wu);
- delay(10);
- hz_disp16(6,16,li1);
- delay(10);
- hz_disp16(6,32,yuan);
- hz_disp161(6,48,gg);
- delay(10);
- hz_disp161(6,56,hh);
- delay(10);
- CS2=0;CS1=1;
- hz_disp16(6,0,tong);
- delay(10);
- hz_disp16(6,16,xin);
- delay(10);
- hz_disp16(6,32,gong);
- delay(10);
- hz_disp16(6,48,cheng);
- delay(10);
- }
- void Disp_Img(unsigned char code *img)
- {
- unsigned char j=0,k=0;
- for(k=0;k<8;k++)
- {
- CS2=1;
- CS1=0;
- delay(10);
- write_cmd(0xb8+k);
- write_cmd(0x40+0);
- for(j=0;j<64;j++)
- write_dat(img[k*128+j]);
- CS2=0;
- CS1=1;
- delay(10);
- write_cmd(0xb8+k);
- write_cmd(0x40+0);
- for(j=64;j<128;j++)
- write_dat(img[k*128+j]);
- }
- }
- void Mima(void)
- {
- CS2=1;CS1=0;
- hz_disp16(6,0,mi);
- hz_disp16(6,16,ma);
- hz_disp16(6,32,weixiao);
- hz_disp16(6,48,maohao);
- }
- void Wrong(void)
- {
- CS2=1;CS1=0;
- hz_disp16(0,32,dui);
- hz_disp16(2,32,bu);
- hz_disp16(4,32,qi1);
- hz_disp16(6,32,douhao);
- hz_disp16(0,48,mi);
- hz_disp16(2,48,ma);
- hz_disp16(4,48,cuo);
- hz_disp16(6,48,wu1);
- CS2=0;CS1=1;
- hz_disp16(0,0,qing);
- hz_disp16(2,0,chong);
- hz_disp16(4,0,xin1);
- hz_disp16(6,0,shu);
- hz_disp16(0,16,ru);
- hz_disp16(2,16,douhao);
- hz_disp16(4,16,xie);
- hz_disp16(6,16,xie);
- }
- void Xinmima(void)
- {
- CS2=1;CS1=0;
- hz_disp16(4,16,qing);
- hz_disp16(4,32,shu);
- hz_disp16(4,48,ru);
- CS2=0;CS1=1;
- hz_disp16(4,0,xin1);
- hz_disp16(4,16,mi);
- hz_disp16(4,32,ma);
- }
- void xiugai(void)
- {
- CS2=1;CS1=0;
- hz_disp16(3,16,mi1);
- hz_disp16(3,32,ma1);
- hz_disp16(3,48,xiu);
- CS2=0;CS1=1;
- hz_disp16(3,0,gai);
- hz_disp16(3,16,cheng1);
- hz_disp16(3,32,gong1);
- }
- matrixkey()
- {
- void Wrong(void);
- void Xinmima(void);
- uchar m;
- uchar k=0;
- uchar temp,key;
- void Disp_Img(unsigned char code *img);
- Mima();
- P2=0xfe;
- temp=P2;
- temp=temp&0xf0;
- if(temp!=0xf0)
- {
- delay(10);
- temp=P2;
- temp=temp&0xf0;
- if(temp!=0xf0)
- {
- temp=P2;
- switch(temp)
- {
- case 0xee:
- key=7;
- b1[t]=key;
- if(h==1){a1[t]=key;}
- t++;
- break;
- case 0xde:
- key=8;
- b1[t]=key;
- if(h==1){a1[t]=key;}
- t++;
- break;
- case 0xbe:
- key=9;
- b1[t]=key;
- if(h==1){a1[t]=key;}
- t++;
- break;
- case 0x7e:
- init();
- for(k=0;k<6;k++)
- {
- write_add(k,a1[k]);
- delayms(20);
- }
- break;
- }
- while(temp!=0xf0)
- {
- temp=P2;
- temp=temp&0xf0;
- }
- if(t==1)
- {
- CS2=0;CS1=1;
- hz_disp161(6,0,shuzi[10]);
- delay(5);
- if(h==1) {hz_disp161(6,0,shuzi[key]);delay(5);}
- }
- if(t==2)
- {
- CS2=0;CS1=1;
- hz_disp161(6,8,shuzi[10]);
- delay(5);
- if(h==1) {hz_disp161(6,8,shuzi[key]);delay(5);}
- }
- if(t==3)
- {
- CS2=0;CS1=1;
- hz_disp161(6,16,shuzi[10]);
- delay(5);
- if(h==1) {hz_disp161(6,16,shuzi[key]);delay(5);}
- }
- if(t==4)
- {
- CS2=0;CS1=1;
- hz_disp161(6,24,shuzi[10]);
- delay(5);
- if(h==1) {hz_disp161(6,24,shuzi[key]);delay(5);}
- }
- if(t==5)
- {
- CS2=0;CS1=1;
- hz_disp161(6,32,shuzi[10]);
- delay(5);
- if(h==1) {hz_disp161(6,32,shuzi[key]);delay(5);}
- }
- if(t==6)
- {
- CS2=0;CS1=1;
- hz_disp161(6,40,shuzi[10]);
- delay(5);
- if(h==1) {hz_disp161(6,40,shuzi[key]);delay(5);}
- }
- }
- }
- P2=0xfd;
- temp=P2;
- temp=temp&0xf0;
- if(temp!=0xf0)
- {
- delay(10);
- temp=P2;
- temp=temp&0xf0;
- if(temp!=0xf0)
- {
- temp=P2;
- switch(temp)
- {
- case 0xed:
- key=4;
- b1[t]=key;
- if(h==1){a1[t]=key;}
- t++;
- break;
- case 0xdd:
- key=5;
- b1[t]=key;
- if(h==1){a1[t]=key;}
- t++;
- break;
- case 0xbd:
- key=6;
- b1[t]=key;
- if(h==1){a1[t]=key;}
- t++;
- break;
- }
- while(temp!=0xf0)
- {
- temp=P2;
- temp=temp&0xf0;
- }
- if(t==1)
- {
- CS2=0;CS1=1;
- hz_disp161(6,0,shuzi[10]);
- delay(5);
- if(h==1) {hz_disp161(6,0,shuzi[key]);delay(5);}
- }
- if(t==2)
- {
- CS2=0;CS1=1;
- hz_disp161(6,8,shuzi[10]);
- delay(5);
- if(h==1) {hz_disp161(6,8,shuzi[key]);delay(5);}
- }
- if(t==3)
- {
- CS2=0;CS1=1;
- hz_disp161(6,16,shuzi[10]);
- delay(5);
- if(h==1) {hz_disp161(6,16,shuzi[key]);delay(5);}
- }
- if(t==4)
- {
- CS2=0;CS1=1;
- hz_disp161(6,24,shuzi[10]);
- delay(5);
- if(h==1) {hz_disp161(6,24,shuzi[key]);delay(5);}
- }
- if(t==5)
- {
- CS2=0;CS1=1;
- hz_disp161(6,32,shuzi[10]);
- delay(5);
- if(h==1) {hz_disp161(6,32,shuzi[key]);delay(5);}
- }
- if(t==6)
- {
- CS2=0;CS1=1;
- hz_disp161(6,40,shuzi[10]);
- delay(5);
- if(h==1) {hz_disp161(6,40,shuzi[key]);delay(5);}
- }
- }
- }
- P2=0xfb;
- temp=P2;
- temp=temp&0xf0;
- if(temp!=0xf0)
- {
- delay(10);
- temp=P2;
- temp=temp&0xf0;
- if(temp!=0xf0)
- {
- temp=P2;
- switch(temp)
- {
- case 0xeb:
- key=1;
- b1[t]=key;
- if(h==1){a1[t]=key;}
- t++;
- break;
- case 0xdb:
- key=2;
- b1[t]=key;
- if(h==1){a1[t]=key;}
- t++;
- break;
- case 0xbb:
- key=3;
- b1[t]=key;
- if(h==1){a1[t]=key;}
- t++;
- break;
-
- }
- while(temp!=0xf0)
- {
- temp=P2;
- temp=temp&0xf0;
- }
- if(t==1)
- {
- CS2=0;CS1=1;
- hz_disp161(6,0,shuzi[10]);
- delay(5);
- if(h==1) {hz_disp161(6,0,shuzi[key]);delay(5);}
- }
- if(t==2)
- {
- CS2=0;CS1=1;
- hz_disp161(6,8,shuzi[10]);
- delay(5);
- if(h==1) {hz_disp161(6,8,shuzi[key]);delay(5);}
- }
- if(t==3)
- {
- CS2=0;CS1=1;
- hz_disp161(6,16,shuzi[10]);
- delay(5);
- if(h==1) {hz_disp161(6,16,shuzi[key]);delay(5);}
- }
- if(t==4)
- {
- CS2=0;CS1=1;
- hz_disp161(6,24,shuzi[10]);
- delay(5);
- if(h==1) {hz_disp161(6,24,shuzi[key]);delay(5);}
- }
- if(t==5)
- {
- CS2=0;CS1=1;
- hz_disp161(6,32,shuzi[10]);
- delay(5);
- if(h==1) {hz_disp161(6,0,shuzi[key]);delay(5);}
- }
- if(t==6)
- {
- CS2=0;CS1=1;
- hz_disp161(6,40,shuzi[10]);
- delay(5);
- if(h==1) {hz_disp161(6,40,shuzi[key]);delay(5);}
- }
- }
- }
-
- P2=0xf7;
- temp=P2;
- temp=temp&0xf0;
- if(temp!=0xf0)
- {
- delay(10);
- temp=P2;
- temp=temp&0xf0;
- if(temp!=0xf0)
- {
- temp=P2;
- switch(temp)
- {
- case 0xe7: //改密码/
- init();
- for(k=0;k<6;k++)
- {
- a1[k]=read_add(k);
- }
- if(a1[0]==b1[0]) { f++;}
- if(a1[1]==b1[1]) {f++;}
- if(a1[2]==b1[2]) {f++;}
- if(a1[3]==b1[3]) {f++;}
- if(a1[4]==b1[4]) {f++;}
- if(a1[5]==b1[5]) {f++;}
- if(f==6)
- {
- f=0;
- Clr_Scr();
- h=1;
- t=0;
- ClockMsg();
- Xinmima();
- }
- break;
- case 0xd7:
- key=0;
- b1[t]=key;
- if(h==1){a1[t]=key;}
- t++;
- break;
- case 0xb7:
- if(t==1)
- {
- t--;
- CS2=0;CS1=1;
- hz_disp161(6,0,kongbai);
- delay(5);
- }
- if(t==2)
- {
- t--;
- CS2=0;CS1=1;
- hz_disp161(6,8,kongbai);
- delay(5);
- }
- if(t==3)
- {
- t--;
- CS2=0;CS1=1;
- hz_disp161(6,16,kongbai);
- delay(5);
- }
- if(t==4)
- {
- t--;
- CS2=0;CS1=1;
- hz_disp161(6,24,kongbai);
- delay(5);
- }
- if(t==5)
- {
- t--;
- CS2=0;CS1=1;
- hz_disp161(6,32,kongbai);
- delay(5);
- }
- if(t==6)
- {
- t--;
- CS2=0;CS1=1;
- hz_disp161(6,40,kongbai);
- delay(5);
- }
- break;
- case 0x77: //确认//
- if(h==1)
- {
- init();
- for(k=0;k<6;k++)
- {
- write_add(k,b1[k]);
- delayms(20);
- }
- Clr_Scr();
- xiugai();
- delay(1000);
- Clr_Scr();
- f=0;t=0;
- h=0;
- }
- else
- {
- init();
- for(k=0;k<6;k++)
- {
- a1[k]=read_add(k);
- }
- if(a1[0]==b1[0]) {f++;}
- if(a1[1]==b1[1]) {f++;}
- if(a1[2]==b1[2]) {f++;}
- if(a1[3]==b1[3]) {f++;}
- if(a1[4]==b1[4]) {f++;}
- if(a1[5]==b1[5]) {f++;}
- if(f==6)
- { Clr_Scr();
- Disp_Img(img2);
- delay(100);
- f=0;
- while(1);
- }
- else
- {
- Clr_Scr();
- delay(50);
- Wrong();
- delay(500);
- Clr_Scr();
- Clr_Scr();
- f=0;
- m++;
- if(m==3)
- {
-
- while(1)
- {
- speak=0;
- delay(1);
- speak=1;
- }
- }
- }
- }
- break;
- }
- while(temp!=0xf0)
- {
- temp=P2;
- temp=temp&0xf0;
- }
- if(t==1)
- {
- CS2=0;CS1=1;
- hz_disp161(6,0,shuzi[10]);
- delay(5);
-
- }
- if(t==2)
- {
- CS2=0;CS1=1;
- hz_disp161(6,8,shuzi[10]);
- delay(5);
-
- }
- if(t==3)
- {
- CS2=0;CS1=1;
- hz_disp161(6,16,shuzi[10]);
- delay(5);
-
- }
- if(t==4)
- {
- CS2=0;CS1=1;
- hz_disp161(6,24,shuzi[10]);
- delay(5);
-
- }
- if(t==5)
- {
- CS2=0;CS1=1;
- hz_disp161(6,32,shuzi[10]);
- delay(5);
-
- }
- if(t==6)
- {
- CS2=0;CS1=1;
- hz_disp161(6,40,shuzi[10]);
- delay(5);
-
- }
- }
- }
-
- }
- void main()
- {
- lcd_init();
-
- Clr_Scr();
- ClockMsg();
- ds1302();
- ds18b20();
- ds18b20();
- ds18b20();
- Mima();
- while(1)
- {
-
- Refresh();
- matrixkey();
-
- }
- }
复制代码
所有资料51hei提供下载:
SPI总线之电子密码锁.rar
(222.48 KB, 下载次数: 19)
|