电子密码锁设计
时间:10-02
整理:3721RD
点击:
#include <REG51.H>
#define KEY_Prot P3
unsigned char Pre_KeyNO,i = 0,flag = 0;
unsigned char code duan_tab[]={0xC0,0xF9,0xA4,0xB0,0x99,
0x92,0x82,0xF8,0x80,0x90,0xBF};
unsigned char code wei_tab[]={0xFE,0XFD,0XFB,0XF7,0XEF,0XDF,0XBF,0X7F};
#define Wei P0
#define Duan P1
unsigned char mima[]={1,1,1,1,1,1};
void delay_ms(unsigned int ms)
{
unsigned int i,j;
for(i = 0;i<80;i++)
for(j = 0;j<ms;j++);
}
void led_show(unsigned char a,unsigned char b,unsigned char c)
{
unsigned char i;
unsigned char date[8];
date[0] = a/10;
date[1] = a%10;
date[2] = 10;
date[3] = b/10;
date[4] = b%10;
date[5] = 10;
date[6] = c/10;
date[7] = c%10;
for(i = 0 ;i < 8;i++)
{
Wei = wei_tab[i];
Duan = duan_tab[date[i]];
delay_ms(1);
}
}
unsigned char Keys_Scan()
{
unsigned char Tmp,KeyNO = 0;
KEY_Prot = 0x0f;
delay_ms(1);
Tmp = KEY_Prot ^ 0x0f;
switch(Tmp)
{
case 1: KeyNO = 0; break;
case 2: KeyNO = 1; break;
case 4: KeyNO = 2; break;
case 8: KeyNO = 3; break;
default: KeyNO = 16;
}
KEY_Prot = 0xf0;
delay_ms(1);
Tmp = KEY_Prot >> 4 ^ 0x0f;
switch(Tmp)
{
case 1: KeyNO += 0; break;
case 2: KeyNO += 4; break;
case 4: KeyNO += 8; break;
case 8: KeyNO += 12;
}
return KeyNO ;
}
void main()
{
TMOD = 0X01;
TH0 = (65535 - 10000)/256;
TL0 = (65535 - 10000)%256;
ET0 = 1;
TR0 = 1;
EA = 1;
while(1)
{
P3 = 0xf0; //初始化按键状态
if(P3 != 0xf0)//按键按下判断
{
flag++;
Pre_KeyNO = Keys_Scan();//返回按键键值
while(!(P3 == 0xf0)); //扫描抬键判断
if(Pre_KeyNO>=0&&Pre_KeyNO<=9)//有效数据判断
{
if(Pre_KeyNO ==mima[i] )//数据匹配
{
i++; //如果正确,i++匹配下一位数据
}else
{
i = 0;//不正确返回初始化状态
}
}
if(i == 6&&Pre_KeyNO == 10)//确认键按下时
{
P2 =~P2;
i = 0;
}
}
}
}
void Timer_0()interrupt 1
{
TH0 = (65535 - 20000)/256;
TL0 = (65535 - 20000)%256;
led_show(0,i,Pre_KeyNO);
}
为什么我设置密码是6个1的,但是通过按键只能显示3个1而已,这是为什么啊?请各位大神指导。
#define KEY_Prot P3
unsigned char Pre_KeyNO,i = 0,flag = 0;
unsigned char code duan_tab[]={0xC0,0xF9,0xA4,0xB0,0x99,
0x92,0x82,0xF8,0x80,0x90,0xBF};
unsigned char code wei_tab[]={0xFE,0XFD,0XFB,0XF7,0XEF,0XDF,0XBF,0X7F};
#define Wei P0
#define Duan P1
unsigned char mima[]={1,1,1,1,1,1};
void delay_ms(unsigned int ms)
{
unsigned int i,j;
for(i = 0;i<80;i++)
for(j = 0;j<ms;j++);
}
void led_show(unsigned char a,unsigned char b,unsigned char c)
{
unsigned char i;
unsigned char date[8];
date[0] = a/10;
date[1] = a%10;
date[2] = 10;
date[3] = b/10;
date[4] = b%10;
date[5] = 10;
date[6] = c/10;
date[7] = c%10;
for(i = 0 ;i < 8;i++)
{
Wei = wei_tab[i];
Duan = duan_tab[date[i]];
delay_ms(1);
}
}
unsigned char Keys_Scan()
{
unsigned char Tmp,KeyNO = 0;
KEY_Prot = 0x0f;
delay_ms(1);
Tmp = KEY_Prot ^ 0x0f;
switch(Tmp)
{
case 1: KeyNO = 0; break;
case 2: KeyNO = 1; break;
case 4: KeyNO = 2; break;
case 8: KeyNO = 3; break;
default: KeyNO = 16;
}
KEY_Prot = 0xf0;
delay_ms(1);
Tmp = KEY_Prot >> 4 ^ 0x0f;
switch(Tmp)
{
case 1: KeyNO += 0; break;
case 2: KeyNO += 4; break;
case 4: KeyNO += 8; break;
case 8: KeyNO += 12;
}
return KeyNO ;
}
void main()
{
TMOD = 0X01;
TH0 = (65535 - 10000)/256;
TL0 = (65535 - 10000)%256;
ET0 = 1;
TR0 = 1;
EA = 1;
while(1)
{
P3 = 0xf0; //初始化按键状态
if(P3 != 0xf0)//按键按下判断
{
flag++;
Pre_KeyNO = Keys_Scan();//返回按键键值
while(!(P3 == 0xf0)); //扫描抬键判断
if(Pre_KeyNO>=0&&Pre_KeyNO<=9)//有效数据判断
{
if(Pre_KeyNO ==mima[i] )//数据匹配
{
i++; //如果正确,i++匹配下一位数据
}else
{
i = 0;//不正确返回初始化状态
}
}
if(i == 6&&Pre_KeyNO == 10)//确认键按下时
{
P2 =~P2;
i = 0;
}
}
}
}
void Timer_0()interrupt 1
{
TH0 = (65535 - 20000)/256;
TL0 = (65535 - 20000)%256;
led_show(0,i,Pre_KeyNO);
}
为什么我设置密码是6个1的,但是通过按键只能显示3个1而已,这是为什么啊?请各位大神指导。
好的,,,,,,,,,,,,,,
好的 你看6个1能解锁不?用什么显示?
多谢分享,正在学习
好
好的 你看6个1能解锁不?用什么显示?