|
嗯,这个是属于学校社团做的一个任务吧,也是很简单的一个;
首先,密码锁离不开矩阵键盘(具体可看b站江科大视频来学习)
然后就是舵机,pwm控制传输信号,鸣声器,
断电保护离不开i2c和at89c52吗,
具体代码如下:
然后我在29行我对LCD_ShowNum(2,1,Key,4);这一段进行了定义修改
如果你取消了,密码就无法显示,当然密码忘了就对这个定义添加回来
第一次使用建议添加回来因为我也忘记最后一次密码修改成啥了(难受)
舵机电源,数据和接地的接口我放在芯片旁边了
其次,密码正确输出显示ok,舵机旋转
密码错误显示err,同时鸣声器报警
s1到s10为1,2,3,4......,0
s11为确认密码键
s12为密码清空键
s13为密码重置键
说难不难,说简单不简单,毕竟刚刚学51单片机才2个月:)
代码展示!
main.c文件如下
- #include <REGX52.H>
- #include "Delay.h"
- #include "LCD1602.h"
- #include "MatrixKey.h"
- #include "Buzzer.h"
- #include "timer0.h"
- #include "at24c02.h"
- unsigned char KeyNum,kount=0,PWM_kount=0;
- unsigned int PassWord,Count,NewKey,Ncount,Key,i,Data,Num;
- sbit SG_PWM=P2^0;
- sbit Buzzer=P2^5;
- void Delay_ms(unsigned int nms)
- {
- unsigned int i,j;
- for(i=nms;i>0;i--)
- for(j=113;j>0;j--);
- }
- void main()
- {
- Timer0_Init();
- LCD_Init();
- LCD_ShowString(1,1,"PassWord:");
- Key=AT24C02_ReadByte(2);
- Key=Key*100+AT24C02_ReadByte(1);
- // LCD_ShowNum(2,1,Key,4);
- while(1)
- {
- KeyNum=MatrixKey();
- if(KeyNum)
- {
- if (KeyNum<=10)
- {
- if(Count<=3)
- {
- PassWord*=10;
- PassWord+=KeyNum%10;
- }
- Count=Count+1;
- }
- if(Count==1)
- {
- LCD_ShowNum(2,1,PassWord,4);
- Delay(4);
- }
- if(Count==2)
- {
- LCD_ShowNum(2,1,PassWord,4);
- Delay(4);
- }
- if(Count==3)
- {
- LCD_ShowNum(2,1,PassWord,4);
- Delay(4);
- }
- if(Count==4)
- {
- LCD_ShowNum(2,1,PassWord,4);
- Delay(4);
- }
- if(KeyNum==11)
- {
- if(PassWord==Key)
- {
- LCD_ShowString(1,13,"ok ");
- PassWord=0;
- Count=0;
- LCD_ShowNum(2,1,PassWord,4);
- Delay(1000);
- LCD_ShowString(1,13," ");
- PWM_kount=1;
- Delay(1000);
- PWM_kount=2;
- Delay(1000);
- PWM_kount=3;
- Delay(1000);
- PWM_kount=4;
- Delay(1000);
- PWM_kount=5;
- Delay(1000);
- }
- else
- {
- LCD_ShowString(1,13,"err");
- for(i=0;i<200;i++)
- {
- Buzzer=!Buzzer;
- Delay(1);
- }
- PassWord=0;
- Count=0;
- LCD_ShowNum(2,1,PassWord,4);
- Delay(1000);
- LCD_ShowString(1,13," ");
-
- }
- }
- if(KeyNum==12)
- {
- PassWord=0;
- Count=0;
- LCD_ShowNum(2,1,PassWord,4);
- }
- if(KeyNum==13)
- {
- Count=0;
- LCD_ShowString(1,1,"OldPassWord:");
- while(1)
- {
- KeyNum=MatrixKey();
- if(KeyNum)
- {
- if (KeyNum<=10)
- {
- if(Count<=3)
- {
- PassWord*=10;
- Delay(8);
- PassWord+=KeyNum%10;
- }
- Count=Count+1;
- }
- if(Count==1)
- {
- LCD_ShowNum(2,1,PassWord,4);
- Delay(4);
- }
- if(Count==2)
- {
- LCD_ShowNum(2,1,PassWord,4);
- Delay(4);
- }
- if(Count==3)
- {
- LCD_ShowNum(2,1,PassWord,4);
- Delay(4);
- }
- if(Count==4)
- {
- LCD_ShowNum(2,1,PassWord,4);
- Delay(4);
- }
- }
- if(KeyNum==11)
- {
- if(PassWord==Key)
- {
- LCD_ShowString(1,1,"OldPassWord:OK");
- Delay(1000);
- LCD_Init();
- PassWord=0;
- LCD_ShowString(1,1,"NewPassWord:");
- while(1)
- {
- KeyNum=MatrixKey();
- if(KeyNum)
- {
- if (KeyNum<=10)
- {
- if(Ncount<=3)
- {
- PassWord*=10;
- PassWord+=KeyNum%10;
- NewKey=PassWord;
- }
- Ncount=Ncount+1;
- }
- LCD_ShowNum(2,1,PassWord,4);
- if(KeyNum==11)
- {
- AT24C02_WriteByte(1,NewKey%100);
- Delay(8);
- AT24C02_WriteByte(2,NewKey/100);
- Delay(8);
- Key=AT24C02_ReadByte(2);
- Key=Key*100+AT24C02_ReadByte(1);
- Count=0;
- PassWord=0;
- LCD_ShowNum(2,1,PassWord,4);
- LCD_Init();
- LCD_ShowString(1,1,"PassWord:");
- while(1)
- {
- KeyNum=MatrixKey();
- if(KeyNum)
- {
- if (KeyNum<=10)
- {
- if(Count<=3)
- {
- PassWord*=10;
- PassWord+=KeyNum%10;
- }
- Count=Count+1;
- }
- if(Count==1)
- {
- LCD_ShowNum(2,1,PassWord,4);
- Delay(4);
- }
- if(Count==2)
- {
- LCD_ShowNum(2,1,PassWord,4);
- Delay(4);
- }
- if(Count==3)
- {
- LCD_ShowNum(2,1,PassWord,4);
- Delay(4);
- }
- if(Count==4)
- {
- LCD_ShowNum(2,1,PassWord,4);
- Delay(4);
- }
- if(KeyNum==11)
- {
- if(PassWord==NewKey)
- {
- LCD_ShowString(1,13,"OK ");
- PassWord=0;
- Count=0;
- LCD_ShowNum(2,1,PassWord,4);
- Delay(1000);
- LCD_ShowString(1,13," ");
- PWM_kount=1;
- Delay(1000);
- PWM_kount=2;
- Delay(1000);
- PWM_kount=3;
- Delay(1000);
- PWM_kount=4;
- Delay(1000);
- PWM_kount=5;
- Delay(1000);
- }
- else
- {
- LCD_ShowString(1,13,"Err");
- for(i=0;i<200;i++)
- {
- Buzzer=!Buzzer;
- Delay(1);
- }
- PassWord=0;
- Count=0;
- LCD_ShowNum(2,1,PassWord,4);
- Delay(1000);
- LCD_ShowString(1,13," ");
- }
- }
- }
- if(KeyNum==12)
- {
- PassWord=0;
- Count=0;
- LCD_ShowNum(2,1,PassWord,4);
- }
-
- }
- }
- }
- }
- }
- }
- if(KeyNum==12)
- {
- PassWord=0;
- Count=0;
- LCD_ShowNum(2,1,PassWord,4);
- LCD_Init();
- LCD_ShowString(1,1,"PassWord:");
- }
- }
- }
- }
- }
- }
- void Timer0() interrupt 1
- {
-
- TL0 = 0x33;
- TH0 = 0xFE;
-
- kount++;
- kount%=40;
-
- if(kount<PWM_kount)
- {
- SG_PWM=1;
- }
- else
- {
- SG_PWM=0;
- }
- }
复制代码 然后就是附件
我用到了这些
仿真程序下载:
51单片机项目(考核).7z
(91.58 KB, 下载次数: 17)
|
评分
-
查看全部评分
|