|
这是一个电子密码锁的程序,初始密码输入后选择1在输入密码,这时P2.3却无法变为低电平,请大神看看有什么问题
#include<reg51.h>
#include<intrins.h.>
#define uchar unsigned char
#define uint unsigned int
sbit RS = P2^0;
sbit RW = P2^1;
sbit E = P2^2;
sbit P2_3 = P2^3;
sbit P2_4 = P2^4;
sbit P2_5 = P2^5;
sbit reset = P2^6;
sbit Sound = P2^7;
sbit scl = P3^6;
sbit sda = P3^7;
sbit led = P3^0;
uchar scan_1[6];
uchar password_1[7];
uchar newpassword_1[6];
uchar password_4[7]={0,0,0,0,0,0,1};
uchar code scan[6]={"******"};
uchar code table[]={0x7b,0xed,0xeb,0xe7,0xdd,0xdb,0xd7,0xbd,0xbb,0xb7,0x7d,0x77};
uchar code table1[]={"Input Code"};
uchar code table2[]={"Function Select"};
uchar code table3[]={"Input Code Again"};
uchar code table4[]={"[1]Open [2]Reset"};
uchar code table5[]={"Input New Code"};
uchar code table6[]={"Comfirm New Code"};
uchar code table7[]={"Success!"};
uchar code table8[]={"Failure!"};
uchar code table9[]={"Warning!"};
uchar code Array1[]={ 0x1C, 0x20,0x20, 0x20,0x1C,0x20,0x18,0x20, 0x20,0x80,0xFF,0x20};
void Read_Code(uchar password[],uchar address,uchar num);
void Write_Code(uchar Data[],uchar address,uchar num);
void Start();
void stop();
void Ack();
void NoAck();
uchar read_byte();
void write_byte(uchar date);
void lcd_init();
void write_com(uchar com);
void write_data(uchar date);
void delay();
void delay_ms(uint z);
void print(uchar tab[],uchar len,uchar loc,bit res);
void num9();
void num10();
void num11();
void Test1();
void Test2();
void Sound_Delay(uchar n);
void Music();
void Alarm();
void Music1();
void errorMusic(uint t);
char scan_num=-1; //键盘号数
uchar scan_len=0; //输入数的长度
uchar yes,key,flag;
uchar Sound_Signal;//定义音符大小
uchar Sound_Pace;//定义节拍大小
uchar Counter1;
uchar error;
void main()
{
P2_3 = 1; //密码锁关
P2_4 = 1;
P2_5 = 1;
Sound = 1;
EA=1; //开总中断
EX0=1; //开外部中断0
IT0=1; //中断0下降沿触发
TMOD=0x01; //定时器0工作方式1
TH0=0xff;
TL0=0xff;
ET0=1; //开定时器0中断
P1=0xf0;
led = 1;
if( reset == 0) //如果键按下,则密码清零,恢复出厂设置
Write_Code(password_4,0,6);
Read_Code(password_1,0,7); //从24C02中读密码
if(password_1[6] == 1) //之前对各卡的第七位分别写入1,2,3
flag = 1;
if(password_1[6] == 2)
flag = 2;
if(password_1[6] == 3)
flag = 3;
lcd_init(); //LCD初始化
while(1)
{
if(scan_num!=-1)
{
if(scan_num<=9)
{
num9(); //数字0~9处理
}
if(scan_num==10)
{
num10(); //删除
}
if(scan_num==11)
{
if(scan_len == 6)
{
num11(); //确定
Music();
if(key == 2) //再次输入新密码后确认键
{
key = 0;
Test2();
return;
}
Test1(); //提示"Input Code"输入的密码确认后判断是否正确
}
}
}
}
}
void Read_Code(uchar password[],uchar address,uchar num) //从24C02中读数据
{
uchar i;
for(i=0;i<num;i++)
{
Start(); //开始信号
write_byte(0xa0); //写控制字
Ack(); //应答信号
write_byte(address+i); //读地址
Ack(); //应答信号
Start(); //开始信号
write_byte(0xa1); //读控制字
Ack(); //应答信号
password[i] = read_byte(); //读数据
NoAck(); //非应答信号
stop(); //停止信号
}
}
void Write_Code(uchar Data[],uchar address,uchar num) //把数据写到24C02
{
uchar i;
for(i=0;i<num;i++)
{
Start(); //开始信号
write_byte(0xa0); //写控制字
Ack(); //应答信号
write_byte(address+i); //写地址
Ack(); //应答信号
write_byte(Data[i]); //写数据
Ack(); //应答信号
stop(); //停止信号
delay_ms(10);
}
}
void Start() //开始信号
{
sda = 1;
delay();
scl = 1;
delay();
sda = 0;
delay();
}
void stop() //停止信号
{
sda = 0;
delay();
scl = 1;
delay();
sda = 1;
delay();
}
void Ack() //应答信号
{
uchar i;
scl = 1;
delay();
while((sda == 1)&&(i < 250))
i++;
scl = 0;
delay();
}
void NoAck() //非应答信号
{
sda=1;
delay();
scl=1;
delay();
scl=0;
delay();
}
uchar read_byte() //读字节数据
{
uchar i,k;
scl = 0;
delay();
sda = 1;
delay();
for(i = 0;i < 8;i++)
{
scl = 1;
delay();
k = (k<<1)|sda;
scl = 0;
delay();
}
return k;
}
void write_byte(uchar date) //写字节数据
{
uchar i,temp;
temp = date;
for(i = 0;i < 8;i++)
{
temp = temp<<1;
scl = 0;
delay();
sda = CY;
delay();
scl = 1;
delay();
}
scl = 0;
delay();
sda = 1;
delay();
}
void lcd_init()
{
//uchar i;
write_com(0x38); //设置16*2显示,5*7点阵,8位数据接口
write_com(0x0c); //设置开显示,不显示光标
write_com(0x06); //写一个字符后地址指针加1
write_com(0x01); //显示清0,数据指针清0
write_com(0x80 + 0x03); //设置数据地址指针位置
/*for(i = 0;i <10;i++)
{
write_data(table1[i]);
delay_ms(5);
}与下句语句等价 */
print(table1,10,0x83,1); //LCD显示“Input Code”
}
/*LCD显示,输出函数*/
void print(uchar tab[],uchar len,uchar loc,bit res)
{
uchar i,n;
if(res==1)
{
write_com(0x01); //光标复位
}
write_com(loc); //设置显示起始位置
for(n=0;n<len;n++)
{
i=tab[n];
write_data(i);
}
for(n=len;n<6;n++)
{
write_data(32); //空格
}
}
void write_com(uchar com) //向LCD写命令
{
RS = 0;
RW = 0;
P0 = com;
delay_ms(5);
E = 1;
delay_ms(5);
E = 0;
}
void write_data(uchar date) //向LCD写数据
{
RS = 1;
RW = 0;
P0 = date;
delay_ms(5);
E = 1;
delay_ms(5);
E = 0;
}
/*按下键盘产生中断,中断程序*/
void EX_INT0() interrupt 0
{
uchar aa,bb,i,j;
EA = 0; //关总中断
P1 = 0xf0;
if((P1 & 0xf0) != 0xf0)
{
delay_ms(5);
if((P1 & 0xf0) != 0xf0)
{
aa = 0xfe;
for(i = 0;i < 4;i++)
{
P1 = aa;
if((P1 & 0xf0) != 0xf0)
{
bb = P1;
for(j=0;j<16;j++)
{
if(bb == table[j])
{
scan_num = j;
}
}
}
else
aa = _crol_(aa,1);
}
}
}
P1 = 0xf0;
EA=1; //开总中断
}
/*输入数字0~9处理函数*/
void num9()
{
uchar i;
if(scan_len>5)
return;
if(yes == 1) // 提示“Input Code”输入正确密码后
{
if(scan_num == 1) //键1开锁键
{
Music();
yes = 0;
scan_num=-1;
if(flag == 1)
{
flag = 0;
P2_3 = 0;
}
if(flag == 2)
{
flag = 0;
P2_3 = 0;
P2_4 = 0;
}
if(flag == 3)
{
flag = 0;
P2_3 = 0;
P2_4 = 0;
P2_5 = 0;
}
print(table1,10,0x83,1); //LCD显示“Input Code”
}
if(scan_num == 2) //键2设置新密码
{
Music();
yes = 0;
print(table5,14,0x81,1); //LCD显示“Input New Code”
scan_num=-1;
key = 1;
}return;
}
Music();
scan_1[scan_len] = scan_num;
//scan[scan_len] = scan_num + 48;
scan_len++;
print(scan,scan_len,0xc5,0); //显示密码值
scan_num=-1;
if(key == 1) //输入6位新密码后,提示“Comfirm New Code”
{
if(scan_len == 6)
{
scan_len = 0;
for(i = 0;i < 6;i++)
newpassword_1[i] = scan_1[i];
key = 2;
print(table6,16,0x80,1); //LCD显示“Comfirm New Code”
}
}
}
/*删除键*/
void num10()
{
if(scan_len>0)
{
Music();
scan_len=scan_len-1;//字符串的长度减少一位
print(scan,scan_len,0xc5,0); //显示输入值删除一位后的新值
scan_num=-1;
}
}
/*确认键*/
void num11()
{
// if(scan_len == 0)
// {
// return;
// }
scan_num=-1;
scan_len=0;
}
void Test1() //输入的密码是否正确
{
uchar i;
if((password_1[0]==scan_1[0])&&(password_1[1]==scan_1[1])&&(password_1[2]==scan_1[2])&&(password_1[3]==scan_1[3])&&(password_1[4]==scan_1[4])&&(password_1[5]==scan_1[5]))
{
yes=1;
print(table2,15,0x81,1);
print(table4,16,0xc0,0);
}
else
{
yes=0;
print(table3,16,0x80,1); //LCD显示“Input Code Again”
error++;
if(error == 3)
{
error = 0;
print(table9,8,0x84,1); //LCD显示“Warning!”
for(i = 0;i < 10;i++ )
{
errorMusic(90);
errorMusic(120);
led = ~led;
}
delay_ms(1000);
led = 1;
print(table1,10,0x83,1); //LCD显示“Input Code”
}
}
}
void Test2() //修改密码时两次输入的密码是否相同
{
if((newpassword_1[0]==scan_1[0])&&(newpassword_1[1]==scan_1[1])&&(newpassword_1[2]==scan_1[2])&&(newpassword_1[3]==scan_1[3])&&(newpassword_1[4]==scan_1[4])&&(newpassword_1[5]==scan_1[5]))
{
Write_Code(newpassword_1,0,6); //向24c02写入新密码
print(table7,8,0x84,1); //LCD显示“Success!”
Alarm();
delay_ms(1000);
Read_Code(password_1,0,6);
print(table1,10,0x83,1); //LCD显示“Input Code”
}
else
{
print(table8,8,0x84,1); //LCD显示“Failure!”
Music1();
delay_ms(1000);
print(table1,10,0x83,1); //LCD显示“Input Code”
}
}
void timer0(void) interrupt 1 using 1
{
Counter1++; //节拍次数计数
TH0=0xd8; //定义单位节拍的延时大小
TL0=0xef;
}
void Music() //按键声
{
Sound_Signal=Array1[0];
Sound_Pace=Array1[1];
TR0=1;
//当节拍数未达到时候,继续循环,产生该音调的声音
while(Counter1!=Sound_Pace)
{
Sound=~Sound;
Sound_Delay(Sound_Signal);
}
Counter1=0;//节拍计数器置0,进入下一个音调
TR0=0;
}
void Alarm() //设置密码成功后的声音
{
Sound_Signal=Array1[2];
Sound_Pace=Array1[3];
TR0=1;
//当节拍数未达到时候,继续循环,产生该音调的声音
while(Counter1!=Sound_Pace)
{
Sound=~Sound;
Sound_Delay(Sound_Signal);
}
// i++;
Counter1=0;//节拍计数器置0,进入下一个音调
TR0=0;
}
void Music1() //设置密码失败后的声音
{
uchar i=0;
while(i<14)
{
Sound_Signal=Array1[i];
Sound_Pace=Array1[++i];
TR0=1; //开定时器T0
while(Counter1!=Sound_Pace)
{
Sound=~Sound;
Sound_Delay(Sound_Signal);
}
Counter1=0;
TR0=0;
i++;
}
}
void errorMusic(uint t)
{
uchar i,j;
for(i=0;i<100;i++)
{
Sound=~Sound;
for(j=0;j<t;j++);
}
}
void Sound_Delay(uchar n)
{
uchar i;
while(n--)
{
for(i=0;i<2;i++);
}
}
void delay()
{
uchar i,j;
for(i=1;i>0;i--)
for(j=20;j>0;j--);
}
void delay_ms(uint z)
{
uint x,y;
for(x = z;x > 0;x--)
for(y = 110;y > 0;y--);
}
|
-
12.png
(48.08 KB, 下载次数: 90)
p2.3应产生跳变与1N4001连接使二极管亮起来
|