|
我的电路原理图能实现功能,但到了实物图就会出现蜂鸣器一直响,密码总是不正确。这究竟是什么原因?求求打什么指导
main函数:
#include <reg52.h>
#include "delay_ms.h"
#include "矩阵键盘.h"
#include "i2c.h"
#include "1602.h"
#include "delay_ms.h"
#include "mima.h"
#define uchar unsigned char
#define uint unsigned int
#define A 0x0a //输入密码
#define B 0x0b //修改密码
#define D 0x0d //关锁
#define enter 0x0c //确认
#define backspace 0x0f //退格键
sbit baojing = P3^7; //蜂鸣器接口
sbit lock = P3^3;//锁信号
sbit set = P3^2;
uchar idata mima_5[6] = {1,2,3,4,5,6}; //初始密码
uchar flag_change;
uchar aa;//存放密码错误的次数,够三次报警
uchar bb;
uchar flag_t0;
void baoj1()//报警函数
{
int i;
for(i = 0;i < 5; i++)
{
baojing = 1;
delay_ms(5);
baojing = 0;
delay_ms(5);
}
}
void baoj()
{
uchar i;
for(i = 0; i < 10; i++)
baoj1();
}
/*密码处理函数*/
void mimachuli()
{
uchar key,i;
uchar idata mima_1[6]={' ',' ',' ',' ',' ',' '};//存放密码缓冲区
uchar idata mima_2[6]={' ',' ',' ',' ',' ',' '};
uchar idata mima_3[6]={' ',' ',' ',' ',' ',' '};
key = step_choose();
if(key == A)//A被按下,接收输入密码,处理
{
read_n_add(mima_1,0x00,6);
write_com(0x06);//写一个字符后地址指针加 1
write_com(0x01);//显示清零,数据指针清零
write_com(0x0f);//显示光标
lcd_pos(0,0);
write_n_char(" press password ");
if(input_mima(mima_2)) //处理输入密码
{
if(mimaduibi(mima_2,mima_1)) //密码正确
{
lcd_pos(0,0);
write_com(0x0c);
write_com(0x06);//写一个字符后地址指针加 1
write_com(0x01);//显示清零,数据指针清零
write_n_char(" password right ");
aa = 0; //清除密码错误次数
lock = 0; //开锁
baojing = 1; //响一下
delay_ms(1000);
baojing = 0;
TR0 = 1;
flag_t0 = 1;
while(key != D && flag_t0) //D没按下一直开
{
key = keyscan();
}
TR0 = 0;
lock = 1;//D按下了关锁
}
else //密码 不正确
{
lcd_pos(0,0);
write_com(0x0c); //关光标
write_com(0x06);//写一个字符后地址指针加 1
write_com(0x01);//显示清零,数据指针清零
write_n_char(" password wrong");
delay_ms(1000);
aa++;
if(aa == 4)
{
aa = 0;
i = 20; //密码不正确报警
while(i--)
baoj();
}
}
}
else //密码没有输入正确或完成
{
lcd_pos(0,0);
write_com(0x0c); //关光标
write_com(0x06);//写一个字符后地址指针加 1
write_com(0x01);//显示清零,数据指针清零
write_n_char(" password wrong");
delay_ms(1000);
aa++;
if(aa == 4)
{
aa = 0;
i = 20; //密码不正确报警
while(i--)
baoj();
}
}
}
if(key == B)//B被按下,修改密码
{
read_n_add(mima_1,0x00,6);
write_com(0x06);//写一个字符后地址指针加 1
write_com(0x01);//显示清零,数据指针清零
write_com(0x0f);//显示光标
lcd_pos(0,0);
write_n_char(" input password");
write_com(0x0f);//显示光标
if(input_mima(mima_2)) //处理输入密码
{
if(mimaduibi(mima_2,mima_1)) //密码正确
{
lcd_pos(0,0);
write_com(0x0c);
write_com(0x06);//写一个字符后地址指针加 1
write_com(0x01);//显示清零,数据指针清零
write_n_char("password right ");
aa = 0; //清除密码错误次数
delay_ms(1500);
flag_change = 1;
while(flag_change) //下面开始修改密码
{
write_com(0x06);//写一个字符后地址指针加 1
write_com(0x01);//显示清零,数据指针清零
write_com(0x0f);//显示光标
lcd_pos(0,0);
write_n_char("In new password");
delay_ms(1500);
if(input_mima(mima_2)) //处理输入密码
{
lcd_pos(0,0);
write_com(0x0c);
write_com(0x06);//写一个字符后地址指针加 1
write_com(0x01);//显示清零,数据指针清零
write_n_char("Input new pass"); //确定新密码
lcd_pos(1,0);
write_n_char("word again");
delay_ms(2000);
lcd_pos(0,0);
write_com(0x0f);//显示光标
write_com(0x06);//写一个字符后地址指针加 1
write_com(0x01);//显示清零,数据指针清零
write_n_char("In new password");
if(input_mima(mima_3)) //处理输入密码
{
if(mimaduibi(mima_2,mima_3)) //密码正确
{
lcd_pos(0,0);
write_com(0x0c);
write_com(0x06);//写一个字符后地址指针加 1
write_com(0x01);//显示清零,数据指针清零
write_n_char("password has");
lcd_pos(1,0);
write_n_char("change already");
write_n_add(mima_3,0x00,6); //把修改的密码存进24C02
delay_ms(2000);
flag_change = 0;
}
else //密码 不正确
{
lcd_pos(0,0);
write_com(0x0c); //关光标
write_com(0x06);//写一个字符后地址指针加 1
write_com(0x01);//显示清零,数据指针清零
write_n_char("password wrong");
delay_ms(1000);
aa++;
if(aa == 4)
{
aa = 0;
i = 20; //3次输入密码不正确报警
while(i--)
baoj();
}
flag_change = 0;
}
}
else //密码没有输入正确或完成
{
lcd_pos(0,0);
write_com(0x0c); //关光标
write_com(0x06);//写一个字符后地址指针加 1
write_com(0x01);//显示清零,数据指针清零
write_n_char(" password wrong");
delay_ms(1000);
aa++;
if(aa == 4)
{
aa = 0;
i = 20; //3次输入密码不正确报警
while(i--)
baoj();
}
}
}
else //密码没有输入正确或完成
{
lcd_pos(0,0);
write_com(0x0c); //关光标
write_com(0x06);//写一个字符后地址指针加 1
write_com(0x01);//显示清零,数据指针清零
write_n_char("password wrong");
delay_ms(1000);
flag_change = 0;
aa++;
if(aa == 4)
{
aa = 0;
i = 20; //3次输入密码不正确报警
while(i--)
baoj();
}
}
}
}
else //密码 不正确
{
lcd_pos(0,0);
write_com(0x0c); //关光标
write_com(0x06);//写一个字符后地址指针加 1
write_com(0x01);//显示清零,数据指针清零
write_n_char("password wrong");
delay_ms(1000);
aa++;
if(aa == 4)
{
aa = 0;
i = 20; //3次输入密码不正确报警
while(i--)
baoj();
}
}
}
else //密码没有输入正确或完成
{
lcd_pos(0,0);
write_com(0x0c); //关光标
write_com(0x06);//写一个字符后地址指针加 1
write_com(0x01);//显示清零,数据指针清零
write_n_char("password wrong");
delay_ms(1000);
aa++;
if(aa == 4)
{
aa = 0;
i = 20; //3次输入密码不正确报警
while(i--)
baoj();
}
}
}
}
/*外部中断函数,恢复出厂设置,长按set键恢复*/
void int0() interrupt 0
{
delay_ms(2000);
if(!set)
{
lcd_pos(0,0);
write_com(0x0c); //关光标
write_com(0x06);//写一个字符后地址指针加 1
write_com(0x01);//显示清零,数据指针清零
i2cinit();//24C02总线初始化
write_n_add(mima_5,0x00,6);
write_n_char("password renew");
lcd_pos(1,0);
write_n_char("already");
delay_ms(1000);
lcd_pos(0,0);
write_com(0x0c); //关光标
write_com(0x06);//写一个字符后地址指针加 1
write_com(0x01);//显示清零,数据指针清零
write_n_char("Input password");
lcd_pos(1,0);
write_n_char("Press key A");
}// 引起了三个警告
}
void timer0() interrupt 1 //定时器0中断,50ms定时
{
TH0 = 0x4c;//50ms定时
TL0 = 0x00;
bb++;
if(bb == 200)//10秒时间到
{
bb = 0;
flag_t0 = 0;
}
}
void main()
{
yjinit();//液晶初始化
i2cinit();//24C02总线初始化
baojing = 0;
lock = 1; //关锁
TMOD = 0x01;//选择定时器0方式1
EA = 1;//打开总中断
ET0 = 1;//打开定时器0中断
EX0 = 1; //打开外部中断0
IT0 = 1;//下降沿触发
TR0 = 0;//关闭定时器
TH0 = 0x4c;//50ms装初值
TL0 = 0x00;
while(1)
{
mimachuli();
}
}
|
|