51单片机——密码锁

#include
#include
#include
sbit LSA=P2^2;
sbit LSB=P2^3;
sbit LSC=P2^4;
sbit p2_0 = P2^0;
sbit beep = P1^5;
#define led P2
#define KEY P1
#define DIG P0
#define EP_HEADER_ADDR 0x4000
char cnt = 0, cnt1 = 0,z,j=0;
char PasswordSet[20], Password[20];
char KeyValue,WrongTime=0;
char code smgduan[] =
{
0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71
};
char code table[] =
{
0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x06,0x06,0x06,0x06,0x06
};
void delay(int);
void Set(void);
void Use(void);
int Check(void);
void Alarm(void);
void LEDLights(void);
void Sing(void);
void UsartInit();
int main()
{
LSA=LSB=LSC=1;
DIG=0x00;
Set();
for(z=0;z<=8;z++)
{At24c02Write(z,PasswordSet[z]);
delay(1000);}
Use();
return 0;
}
//延时
void delay(int i)
{
while(i–);
}
//设置密码
void Set(void)
{
UsartInit();
}
//使用模式
void Use(void)
{
LSA = LSB = LSC = 1;
DIG = 0x00;
while(1)
{
DIG = 0x00;
if (Check())
{
Sing();
LEDLights();
Use();
}
else
{
Alarm();
Use();
}
}
}
//检查密码正误
int check(void)
{
int i;
for (i = 0; i < 8; i++)
if (PasswordSet[i]!= PasswordSet[8+i])
return 0;
return 1;
}
//蜂鸣器报警
void Alarm(void)
{
int i = 500;
while (i–)
{
beep = ~beep;
delay(1000);
}
}

//流水灯
void LEDLights(void)
{
char i;
led=0xfe;
delay(50000);
while(1)
{
for(i=0;i<7;i++)
{
led=crol(led,1);
delay(50000);
}
for(i=0;i<7;i++)
{
led=cror(led,1);
delay(50000);
}
}
}
//蜂鸣器
void Sing(void)
{
int i = 500;
while (i–)
{
beep = ~beep;
delay(100);
}
}
//串口
void UsartInit()
{
SCON=0X50; //设置为工作方式1
TMOD=0X20; //设置计数器工作方式2
PCON=0X80; //波特率加倍
TH1=0XF3; //计数器初始值设置,注意波特率是4800的
TL1=0XF3;
ES=1; //打开接收中断
EA=1; //打开总中断
TR1=1; //打开计数器
}
void Usart() interrupt 4
{
PasswordSet[j]=SBUF;//出去接收到的数据
RI = 0;//清除接收中断标志位
j++;
}

你可能感兴趣的:(51单片机)