制作密码锁,在51黑里受益颇多;这次设计结束,反馈给大家
制作出来的实物图如下:
单片机源程序如下:
- #include<reg51.h>
- #include<OLED.h>
- #include<keypad.h>
- #include<intrins.h>
- #include<eeprom.h>
- #define uchar unsigned char
- #define uint unsigned int
-
- sbit LED1=P1^7;
- sbit BEEP=P3^7;
- uchar x=11,count,j;
- void Delay() //@11.0592MHz
- {
-
- unsigned char i, t, k;
- _nop_();
- _nop_();
- i = 20;
- t = 3;
- k = 203;
- do
- {
- do
- {
- while (--k);
- } while (--t);
- } while (--i);
-
- }
- void Timer_init (void)
- {
- EA = 1; //中断总开关
- TMOD=0x01; //设置定时器0工作方式1后面运行TR位启动
- TH0=(65536-50000)/256;
- TL0=(65536-50000)%256;
- ET0=1;
-
- // IT0 = 1; //1:下沿触发 0:低电平触发
-
- }
- void Tenseconds()
- {
- if(x==10)
- OLED_DisplayText(1,3,"10",OLED_NOT_INVERT_COLOR);
- if(x==9)
- OLED_DisplayText(1,3," 9",OLED_NOT_INVERT_COLOR);
- if(x==8)
- OLED_DisplayText(1,3," 8",OLED_NOT_INVERT_COLOR);
- if(x==7)
- OLED_DisplayText(1,3," 7",OLED_NOT_INVERT_COLOR);
- if(x==6)
- OLED_DisplayText(1,3," 6",OLED_NOT_INVERT_COLOR);
- if(x==5)
- OLED_DisplayText(1,3," 5",OLED_NOT_INVERT_COLOR);
- if(x==4)
- OLED_DisplayText(1,3," 4",OLED_NOT_INVERT_COLOR);
- if(x==3)
- OLED_DisplayText(1,3," 3",OLED_NOT_INVERT_COLOR);
- if(x==2)
- OLED_DisplayText(1,3," 2",OLED_NOT_INVERT_COLOR);
- if(x==1)
- OLED_DisplayText(1,3," 1",OLED_NOT_INVERT_COLOR);
- if(x==0)
- {
- OLED_Clear();
- OLED_DisplayText(1,1,"请按返回键返回",OLED_NOT_INVERT_COLOR);
- OLED_DisplayText(3,0,"返回",OLED_NOT_INVERT_COLOR);
- j=0;
- }
- }
-
- void Enter() //确定键,只有按下s13才可以继续进行
- { uchar dat;
- do{P2=0xf0;
- while(P2==0xf0);
- dat=key_scan();
- }while(dat!=0x0a);
- }
- void System()
- {
- OLED_DisplayText(0,2,"淮北师范大学",OLED_NOT_INVERT_COLOR);
- OLED_DisplayText(1,2,"电子信息工程班",OLED_NOT_INVERT_COLOR);
- OLED_DisplayText(2,2,"20191304032",OLED_NOT_INVERT_COLOR);
- Delay();
- }
- void press(uchar *s,uchar x) //按键输入
- {
- uchar dat;
- int num;
- for(num=0;num<=4;num++)
- {
- //**************************************************
- if(num>=4)
- { P2=0xf0;
- while(P2==0xf0);
- dat=key_scan();
- if((dat!=0x0a) && (dat!=0x0b)&& (dat!=0x0c))
- {
- num--;
- }
- if(dat==0x0a)
- {
- OLED_DisplayText(x,num," ",OLED_NOT_INVERT_COLOR);
- }
- if(dat==0x0b)
- {
- OLED_DisplayText(x,num-1,"-",OLED_NOT_INVERT_COLOR);
- s--;
- num=num-2;
- }
- if(dat==0x0c)
- {
- s=s-num;
- num=-1;
- OLED_DisplayText(x,0,"----",OLED_NOT_INVERT_COLOR);
- }
-
- }
- else
- {
- P2=0xf0;
- while(P2==0xf0);
- dat=key_scan();
- if((dat!=0x0a)&&(dat!=0x0b)&&(dat!=0x0c))
- { *s=dat;
- OLED_DisplayText(x,num,"*",OLED_NOT_INVERT_COLOR);
- s++;
-
- }
- if(dat==0x0b)
- {
- OLED_DisplayText(x,num-1,"-",OLED_NOT_INVERT_COLOR);
- s--;
- num=num-2;
- }
- if(dat==0x0c)
- {
- s=s-num;
- num=-1;
- OLED_DisplayText(x,0,"----",OLED_NOT_INVERT_COLOR);
- }
- }
- }
-
- }
-
- void main()
- { int dat,x;
- OLED_Init();
- Timer_init ();
- System();
-
- read();//第一次输入烧录时,请将这里改为 “write();”!!!!
-
- do{
- LED1=1;
- BEEP=1;
- OLED_Clear();
- OLED_DisplayText(0,0,"请输入密码:",OLED_NOT_INVERT_COLOR);//OLED显示文本(行(0到3行), 列(0到15列),文本, 是否反色)
- OLED_DisplayText(1,0,"----",OLED_NOT_INVERT_COLOR);//OLED显示文本(行(0到3行), 列(0到15列),文本, 是否反色)
- OLED_DisplayText(3,0,"确定",OLED_NOT_INVERT_COLOR);//OLED显示文本(行(0到3行), 列(0到15列),文本, 是否反色)
- OLED_DisplayText(3,12,"删除",OLED_NOT_INVERT_COLOR);//OLED显示文本(行(0到3行), 列(0到15列),文本, 是否反色)
- press(key,1);
- if((key[0]==iic[0])&&(key[1]==iic[1])&&(key[2]==iic[2])&&(key[3]==iic[3]))
- {
- j=0;
- OLED_Clear();
- OLED_DisplayText(3,12," ",OLED_NOT_INVERT_COLOR);
- OLED_DisplayText(0,0,"直接开锁请按1",OLED_NOT_INVERT_COLOR);
- OLED_DisplayText(1,0,"修改密码请按2",OLED_NOT_INVERT_COLOR);
- do
- {
- P2=0xf0; //键入1或2继续执行下面语句,否则等待
- while(P2==0xf0);
- dat=key_scan();
- }while(dat!=0x01&&dat!=0x02);
- if(dat==1) //开锁
- {
- OLED_Clear();
- LED1=0;
- OLED_DisplayText(1,4,"开锁成功!",OLED_INVERT_COLOR );
- OLED_DisplayText(3,0,"返回",OLED_NOT_INVERT_COLOR );
- Enter();
-
- }
- if(dat==2) //修改密码
- { OLED_Clear();
- do{
- OLED_Clear();
- OLED_DisplayText(0,0,"输入修改密码:",OLED_NOT_INVERT_COLOR);
- OLED_DisplayText(1,0,"----",OLED_NOT_INVERT_COLOR);
- press(key,1);
- OLED_DisplayText(2,0,"确定密码:",OLED_NOT_INVERT_COLOR); //再次输入密码
- OLED_DisplayText(3,0,"----",OLED_NOT_INVERT_COLOR);
- press(iic,3);
- if((key[0]==iic[0])&&(key[1]==iic[1])&&(key[2]==iic[2])&&(key[3]==iic[3]))
- {
- OLED_Clear();
- OLED_DisplayText(1,4,"修改成功!",OLED_INVERT_COLOR);
- OLED_DisplayText(3,0,"返回",OLED_NOT_INVERT_COLOR );
-
- Erase();//第一次输入烧录时,请将这里注释掉!!!!
- write();//第一次输入烧录时,请将这里注释掉!!!!
-
- Enter();break; //修改密码成功
- }
- else //修改密码不成功,重新修改
- {
- OLED_Clear();
- OLED_DisplayText(3,12," ",OLED_NOT_INVERT_COLOR);
- OLED_DisplayText(1,2,"两次密码不一致",OLED_INVERT_COLOR);
- OLED_DisplayText(2,4,"请重新输入",OLED_INVERT_COLOR);
- OLED_DisplayText(3,0,"返回",OLED_NOT_INVERT_COLOR );
- Enter();
- }
- }while(1);
- }
- }
-
- else //密码不正确,重新输入密码
- {
-
- j++;
- if(j==3) //如果输错超过3次,开启定时器中断,键盘锁住,10s内无法操作
- {
- TR0=1;
-
- }
- OLED_Clear();
-
- if (j<=2)
- {
- BEEP=0;
- }
- else
- {BEEP=1;}
-
- OLED_DisplayText(3,12," ",OLED_NOT_INVERT_COLOR);
- OLED_DisplayText(1,4,"密码错误",OLED_INVERT_COLOR);
- OLED_DisplayText(3,0,"返回",OLED_NOT_INVERT_COLOR );
- Enter();
- }
- }
- while(1);
- }
- void TimeInterrupt() interrupt 1 //定时器中断服务程序
- {
-
- TH0=(65536-50000)/256;
- TL0=(65536-50000)%256;
- count++;
- if (count>=20)
- {
- count=0;
- x--;
- OLED_DisplayText(1,5,"秒后解锁",OLED_NOT_INVERT_COLOR);
- Tenseconds();
- if(x<=0)
- {
- TR0=0;
- x=11;
- }
- }
- }
复制代码
附件是Keil5代码下载(1个文件):
oled12864(IIC) 掉电储存密码锁.zip
(84.67 KB, 下载次数: 85)
|