51单片机——电子密码锁

51单片机——电子密码锁

链接: link.

图片: !protues仿真

,下面展示同样高亮的 代码片.
#include<reg51.h>
unsigned char zt=1;
unsigned char password[8];
unsigned char table[8];
unsigned char smg[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
unsigned char i=0;
unsigned char wrong=0;
unsigned char right=0;
unsigned char input=0;
unsigned char temp;
unsigned char j;
unsigned char m=0;
unsigned char k;
unsigned char q;
sbit red=P2^3;
sbit yellow=P2^0;
sbit green=P2^1;
sbit blue=P2^2;
sbit fm=P2^6;
sbit full=P2^5;
sbit igreen=P2^4;
sbit P34=P3^4;
sbit P35=P3^5;
sbit P36=P3^6;
sbit P37=P3^7;
void delay();
void btn(unsigned char a);
void checkpassword();
void put();
void submit();
void lock();
void check_C1();
void check_C2();
void check_C3();
void check_C4();
void check_button();
void first();
void open();
void close();
void beep();
void ddelay();
void main(){
P2=0x00;
P0=0Xff;
while(1){

if(zt==1){
  first();
}
if(zt==2){
  open();
}
if(zt==3){
  close();
}

}
}
void beep(){
unsigned int c;
unsigned int d;
for(d=0;d<200;d++){
for(c=0;c<200;c++){
fm=1;
ddelay();
fm=0;
}
}
}
void ddelay(){
unsigned int b;
for(b=0;b<=30;b++);
}
void delay(){
unsigned int n;
TMOD=0X00;
for(n=0;n<=100;n++){
TH0=0X63;
TL0=0X18;
TR0=1;
while(!TF0);
TF0=0;
}
}
void btn(unsigned char a){
if(i<8&&input1){
table[i++]=a;
blue=1;
P0=smg[a];
delay();
blue=0;
P0=0xff;
}
if(i
8&&input1){
igreen=0;
full=1;
}
}
void checkpassword(){
m=0;
for(k=0;k<8;k++){
m++;
if(password[k]!=table[k]){
break;
}
}
if(m
8){
right=1;
}else{
full=0;
red=1;
delay();
red=0;
wrong++;
}
if(wrong>3){
red=1;
full=0;
beep();
}
}
void put(){
if(input0){
igreen=1;
input=1;
i=0;
}
}
void submit(){
if(zt
1||zt2){
if(i
8){
for(j=0;j<8;j++)
password[j]=table[j];
if(zt2)
i=0;
full=0;
input=0;
}
}
if(zt
3&&i==8){
checkpassword();
full=0;
input=0;
i=0;
}

if(zt==1&&i==8){
  zt=2;
  yellow=0;
  full=0;
  input=0;
  i=0;
}

}
void lock(){
if(zt2&&input0){
zt=3;
green=0;
}
}
void check_C1(){
P3=0xff;
P34=0;
temp=P3;
temp=temp&0x0f;
if(temp!=0x0f){
temp=P3;
temp=temp&0x0f;
switch(temp){
case 0x0e:btn(0);break;
case 0x0d:btn(4);break;
case 0x0b:btn(8);break;
case 0x07:break;
}
temp=P3;
temp=temp&0x0f;
while(temp!=0x0f)
{
temp=P3;
temp=temp&0x0f;
}
}
}
void check_C2(){
P3=0xff;
P35=0;
temp=P3;
temp=temp&0x0f;
if(temp!=0x0f){
temp=P3;
temp=temp&0x0f;
switch(temp){
case 0x0e:btn(1);break;
case 0x0d:btn(5);break;
case 0x0b:btn(9);break;
case 0x07:lock();break;
}
temp=P3;
temp=temp&0x0f;
while(temp!=0x0f)
{
temp=P3;
temp=temp&0x0f;
}
}
}
void check_C3(){
P3=0xff;
P36=0;
temp=P3;
temp=temp&0x0f;
if(temp!=0x0f){
temp=P3;
temp=temp&0x0f;
switch(temp){
case 0x0e:btn(2);break;
case 0x0d:btn(6);break;
case 0x0b:break;
case 0x07:put();break;
}
temp=P3;
temp=temp&0x0f;
while(temp!=0x0f)
{
temp=P3;
temp=temp&0x0f;
}
}
}
void check_C4(){
P3=0xff;
P37=0;
temp=P3;
temp=temp&0x0f;
if(temp!=0x0f){
temp=P3;
temp=temp&0x0f;
switch(temp){
case 0x0e:btn(3);break;
case 0x0d:btn(7);break;
case 0x0b:break;
case 0x07:submit();break;
}
temp=P3;
temp=temp&0x0f;
while(temp!=0x0f)
{
temp=P3;
temp=temp&0x0f;
}
}
}
void check_button(){
check_C1();
check_C2();
check_C3();
check_C4();
}

void first(){
yellow=1;
check_button();
while(input1){
check_button();
}
}
void open(){
green=1;
check_button();
while(input
1){
check_button();
}
}
void close(){
yellow=1;
check_button();
while(input1){
check_button();
}
if(right
1){
yellow=0;
red=0;
wrong=0;
right=0;
zt=2;
fm=0;
}
}

  • 15
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值