#include
#include
#include "iic.h"
extern unsigned char cun[];
extern unsigned char cunmima[];
extern unsigned char qing,xiu,shu;
extern unsigned char key,i,k;
unsigned char display[]={0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
unsigned char smg[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x8c,0x86,0xc8};
unsigned char mima[]={8,8,8,8,8,8};
unsigned char open,error;
void Delay10ms();
void xiugai();
void panduan();
void Timer0Init();
void xianshi();
void main()
{
Timer0Init();EA=1;ET0=1;
P2=0XA0;P0=0X00;P2=0X1f;
cunmima[0]=EEPROM_read(0x11);Delay10ms();
cunmima[1]=EEPROM_read(0x12);Delay10ms();
cunmima[2]=EEPROM_read(0x13);Delay10ms();
cunmima[3]=EEPROM_read(0x14);Delay10ms();
cunmima[4]=EEPROM_read(0x15);Delay10ms();
cunmima[5]=EEPROM_read(0x16);Delay10ms();
while(1)
{
anjian();
xianshi();
panduan();
xiugai();
if(k==7)
{
EEPROM_write(0x11,mima[0]);Delay10ms();
EEPROM_write(0x12,mima[1]);Delay10ms();
EEPROM_write(0x13,mima[2]);Delay10ms();
EEPROM_write(0x14,mima[3]);Delay10ms();
EEPROM_write(0x15,mima[4]);Delay10ms();
EEPROM_write(0x16,mima[5]);Delay10ms();
}
}
}
void xianshi()//输入显示
{
if(shu==0)
{
display[0]=0xff;
display[1]=0xff;
display[2]=0xff;
display[3]=0xff;
display[4]=0xff;
display[5]=0xff;
display[6]=0xff;
display[7]=0xff;
}
if(shu==1)
{
if(i<=6)
{
P2=0X80;
P0=0XBF;
P2=0X1F;
}
if(i==10)
{
P2=0X80;
P0=0X7F;
P2=0X1F;
}
switch(i)
{
case 0:
display[0]=0xbf;
display[1]=0xff;
display[2]=0xff;
display[3]=0xff;
display[4]=0xff;
display[5]=0xff;
display[6]=0xff;
display[7]=0xff;
break;
case 1:
display[0]=0xbf;
display[1]=0xff;
display[2]=0xff;
display[3]=0xff;
display[4]=0xff;
display[5]=0xff;
display[6]=0xff;
display[7]=smg[cun[0]];
break;
case 2:
display[0]=0xbf;
display[1]=0xff;
display[2]=0xff;
display[3]=0xff;
display[4]=0xff;
display[5]=0xff;
display[6]=smg[cun[1]];
display[7]=smg[cun[0]];
break;
case 3:
display[0]=0xbf;
display[1]=0xff;
display[2]=0xff;
display[3]=0xff;
display[4]=0xff;
display[5]=smg[cun[2]];
display[6]=smg[cun[1]];
display[7]=smg[cun[0]];
break;
case 4:
display[0]=0xbf;
display[1]=0xff;
display[2]=0xff;
display[3]=0xff;
display[4]=smg[cun[3]];
display[5]=smg[cun[2]];
display[6]=smg[cun[1]];
display[7]=smg[cun[0]];
break;
case 5:
display[0]=0xbf;
display[1]=0xff;
display[2]=0xff;
display[3]=smg[cun[4]];
display[4]=smg[cun[3]];
display[5]=smg[cun[2]];
display[6]=smg[cun[1]];
display[7]=smg[cun[0]];
break;
case 6:
display[0]=0xbf;
display[1]=0xff;
display[2]=smg[cun[5]];
display[3]=smg[cun[4]];
display[4]=smg[cun[3]];
display[5]=smg[cun[2]];
display[6]=smg[cun[1]];
display[7]=smg[cun[0]];
break;
case 7:
i=6;
break;
case 8://密码正确
display[0]=smg[0];
display[1]=0xff;
display[2]=0xff;
display[3]=0xff;
display[4]=smg[0];
display[5]=smg[10];
display[6]=smg[11];
display[7]=smg[12];
open=1;
break;
case 9://密码错误
error=1;
break;
// case 10://修改密码
// i=0;
// break;
}
}
}
void panduan()//判断密码
{
if(i>=6)
{
if((mima[0]==cun[0])&(mima[1]==cun[1])&(mima[2]==cun[2])&(mima[3]==cun[3])&(mima[4]==cun[4])&(mima[5]==cun[5]))
{
i=8;//密码正确
}
else
{
i=9;//密码错误
}
}
}
void xiugai()//修改密码
{
unsigned char j;
if(xiu==1)
{
i=10;
switch(k)
{
case 0:
display[0]=0xc6;
display[1]=0xff;
display[2]=0xff;
display[3]=0xff;
display[4]=0xff;
display[5]=0xff;
display[6]=0xff;
display[7]=0xff;
break;
case 1:
display[0]=0xc6;
display[1]=0xff;
display[2]=0xff;
display[3]=0xff;
display[4]=0xff;
display[5]=0xff;
display[6]=0xff;
display[7]=smg[cunmima[0]];
break;
case 2:
display[0]=0xc6;
display[1]=0xff;
display[2]=0xff;
display[3]=0xff;
display[4]=0xff;
display[5]=0xff;
display[6]=smg[cunmima[1]];
display[7]=smg[cunmima[0]];
break;
case 3:
display[0]=0xc6;
display[1]=0xff;
display[2]=0xff;
display[3]=0xff;
display[4]=0xff;
display[5]=smg[cunmima[2]];
display[6]=smg[cunmima[1]];
display[7]=smg[cunmima[0]];
break;
case 4:
display[0]=0xc6;
display[1]=0xff;
display[2]=0xff;
display[3]=0xff;
display[4]=smg[cunmima[3]];
display[5]=smg[cunmima[2]];
display[6]=smg[cunmima[1]];
display[7]=smg[cunmima[0]];
break;
case 5:
display[0]=0xc6;
display[1]=0xff;
display[2]=0xff;
display[3]=smg[cunmima[4]];
display[4]=smg[cunmima[3]];
display[5]=smg[cunmima[2]];
display[6]=smg[cunmima[1]];
display[7]=smg[cunmima[0]];
break;
case 6:
display[0]=0xc6;
display[1]=0xff;
display[2]=smg[cunmima[5]];
display[3]=smg[cunmima[4]];
display[4]=smg[cunmima[3]];
display[5]=smg[cunmima[2]];
display[6]=smg[cunmima[1]];
display[7]=smg[cunmima[0]];
break;
case 7:
// EEPROM_write(1,cunmima[0]);Delay10ms();
// EEPROM_write(2,cunmima[1]);Delay10ms();
// EEPROM_write(3,cunmima[2]);Delay10ms();
// EEPROM_write(4,cunmima[3]);Delay10ms();
// EEPROM_write(5,cunmima[4]);Delay10ms();
// EEPROM_write(6,cunmima[5]);Delay10ms();
for(j=0;j<6;j++)
{
mima[j]=cunmima[j];
}
break;
}
}
if(xiu==2)
{
i=0;xiu=0;
// mima[0]=EEPROM_read(0);Delay10ms();
// mima[1]=EEPROM_read(1);Delay10ms();
// mima[2]=EEPROM_read(2);Delay10ms();
// mima[3]=EEPROM_read(3);Delay10ms();
// mima[4]=EEPROM_read(4);Delay10ms();
// mima[5]=EEPROM_read(5);Delay10ms();
}
}
void Timer0Init(void) //2毫秒@11.0592MHz
{
AUXR |= 0x80; //定时器时钟1T模式
TMOD &= 0xF0; //设置定时器模式
TL0 = 0x9A; //设置定时初值
TH0 = 0xA9; //设置定时初值
TF0 = 0; //清除TF0标志
TR0 = 1; //定时器0开始计时
}
void shuma() interrupt 1
{
unsigned char m;
unsigned int t,q;
P2=0XE0;
P0=0XFF;
P2=0X1f;
P2=0XC0;
P0=1<<m;
P2=0X1f;
P2=0XE0;
P0=display[m];
P2=0X1f;
m++;
if(m>=8)m=0;
if(open==1)
{
q++;
if(q<2500)
{
P2=0XA0;
P0=0X10;
P2=0X1F;
}
if(q==2500)
{
P2=0XA0;
P0=0X00;
P2=0X1F;
open=2;
i=0;k=0;
}
}
if(error==1)
{
t++;
if(t<2500)
{
P2=0X80;
P0=0Xfe;
P2=0X1F;
}
if(t==2500)
{
P2=0X80;
P0=0Xff;
P2=0X1F;
error=2;
i=0;k=0;
}
}
}
#include
#include
sbit h1=P3^0;
sbit h2=P3^1;
sbit h3=P3^2;
sbit h4=P3^3;
unsigned char key,i,k;
unsigned char cun[]={0xff,0xff,0xff,0xff,0xff,0xff};
unsigned char cunmima[]={0xff,0xff,0xff,0xff,0xff,0xff};
unsigned char xiu,shu;
sbit l1=P4^4;
sbit l2=P4^2;
sbit l3=P3^5;
sbit l4=P3^4;
void anjian()
{
l1=0;
l4=l2=l3=1;
h1=h2=h3=h4=1;
if(h1==0)
{
Delay10ms();
if(h1==0)
{key=0;if(xiu==0){cun[i]=key;i++;}if(xiu==1){cunmima[k]=key;k++;}}
while(!h1);
}
if(h2==0)
{
Delay10ms();
if(h2==0)
{key=4;if(xiu==0){cun[i]=key;i++;}if(xiu==1){cunmima[k]=key;k++;}}
while(!h2);
}
if(h3==0)
{
Delay10ms();
if(h3==0)
{key=8;if(xiu==0){cun[i]=key;i++;}if(xiu==1){cunmima[k]=key;k++;}}
while(!h3);
}
// if(h4==0)
// {
// Delay10ms();
// if(h4==0)
// {key=4;}
// while(!h4);
// }
l2=0;
l3=l1=l4=1;
h1=h2=h3=h4=1;
if(h1==0)
{
Delay10ms();
if(h1==0)
{key=1;if(xiu==0){cun[i]=key;i++;}if(xiu==1){cunmima[k]=key;k++;}}
while(!h1);
}
if(h2==0)
{
Delay10ms();
if(h2==0)
{key=5;if(xiu==0){cun[i]=key;i++;}if(xiu==1){cunmima[k]=key;k++;}}
while(!h2);
}
if(h3==0)
{
Delay10ms();
if(h3==0)
{key=9;if(xiu==0){cun[i]=key;i++;}if(xiu==1){cunmima[k]=key;k++;}}
while(!h3);
}
if(h4==0)
{
Delay10ms();
if(h4==0)
{if(i<7)i--;if(k<7)k--;}
while(!h4);
}
l3=0;
l1=l2=l4=1;
h1=h2=h3=h4=1;
if(h1==0)
{
Delay10ms();
if(h1==0)
{key=2;if(xiu==0){cun[i]=key;i++;}if(xiu==1){cunmima[k]=key;k++;}}
while(!h1);
}
if(h2==0)
{
Delay10ms();
if(h2==0)
{key=6;if(xiu==0){cun[i]=key;i++;}if(xiu==1){cunmima[k]=key;k++;}}
while(!h2);
}
// if(h3==0)
// {
// Delay10ms();
// if(h3==0)
// {key=11;}
// while(!h3);
// }
//
if(h4==0)
{
Delay10ms();
if(h4==0)
{
xiu++;if(xiu>3)xiu=0;
}
while(!h4);
}
l4=0;
l1=l2=l3=1;
h1=h2=h3=h4=1;
if(h1==0)
{
Delay10ms();
if(h1==0)
{key=3;if(xiu==0){cun[i]=key;i++;}if(xiu==1){cunmima[k]=key;k++;}}
while(!h1);
}
if(h2==0)
{
Delay10ms();
if(h2==0)
{key=7;if(xiu==0){cun[i]=key;i++;}if(xiu==1){cunmima[k]=key;k++;}}
while(!h2);
}
// if(h3==0)
// {
// Delay10ms();
// if(h3==0)
// {key=15;}
// while(!h3);
// }
if(h4==0)
{
Delay10ms();
if(h4==0)
{shu=1;}
while(!h4);
}
}
void Delay10ms() //@11.0592MHz
{
unsigned char i, j;
i = 108;
j = 145;
do
{
while (--j);
} while (--i);
}
juzhen.h
#ifndef __JUZHEN_H_
#define __JUZHEN_H_
void Delay10ms();
void anjian();
#endif
/*
程序说明: IIC总线驱动程序
软件环境: Keil uVision 4.10
硬件环境: CT107单片机综合实训平台 8051,12MHz
日 期: 2011-8-9
*/
#include
#include "intrins.h"
#define DELAY_TIME 5
#define SlaveAddrW 0xA0
#define SlaveAddrR 0xA1
//总线引脚定义
sbit SDA = P2^1; /* 数据线 */
sbit SCL = P2^0; /* 时钟线 */
void IIC_Delay(unsigned char i)
{
do{_nop_();}
while(i--);
}
//总线启动条件
void IIC_Start(void)
{
SDA = 1;
SCL = 1;
IIC_Delay(DELAY_TIME);
SDA = 0;
IIC_Delay(DELAY_TIME);
SCL = 0;
}
//总线停止条件
void IIC_Stop(void)
{
SDA = 0;
SCL = 1;
IIC_Delay(DELAY_TIME);
SDA = 1;
IIC_Delay(DELAY_TIME);
}
////发送应答
//void IIC_SendAck(bit ackbit)
//{
// SCL = 0;
// SDA = ackbit; // 0:应答,1:非应答
// IIC_Delay(DELAY_TIME);
// SCL = 1;
// IIC_Delay(DELAY_TIME);
// SCL = 0;
// SDA = 1;
// IIC_Delay(DELAY_TIME);
//}
//等待应答
bit IIC_WaitAck(void)
{
bit ackbit;
SCL = 1;
IIC_Delay(DELAY_TIME);
ackbit = SDA;
SCL = 0;
IIC_Delay(DELAY_TIME);
return ackbit;
}
//通过I2C总线发送数据
void IIC_SendByte(unsigned char byt)
{
unsigned char i;
for(i=0; i<8; i++)
{
SCL = 0;
IIC_Delay(DELAY_TIME);
if(byt & 0x80) SDA = 1;
else SDA = 0;
IIC_Delay(DELAY_TIME);
SCL = 1;
byt <<= 1;
IIC_Delay(DELAY_TIME);
}
SCL = 0;
}
//从I2C总线上接收数据
unsigned char IIC_RecByte(void)
{
unsigned char i, da;
for(i=0; i<8; i++)
{
SCL = 1;
IIC_Delay(DELAY_TIME);
da <<= 1;
if(SDA) da |= 1;
SCL = 0;
IIC_Delay(DELAY_TIME);
}
return da;
}
unsigned char EEPROM_read(unsigned char add)
{
unsigned char temp;
IIC_Start();
IIC_SendByte(0XA0);
IIC_WaitAck();
IIC_SendByte(add);
IIC_WaitAck();
IIC_Stop();
IIC_Start();
IIC_SendByte(0XA1);
IIC_WaitAck();
temp=IIC_RecByte();
IIC_Stop();
return temp;
}
void EEPROM_write(unsigned char add,unsigned char dat)
{
IIC_Start();
IIC_SendByte(0XA0);
IIC_WaitAck();
IIC_SendByte(add);
IIC_WaitAck();
IIC_SendByte(dat);
IIC_WaitAck();
IIC_Stop();
}
#ifndef _IIC_H
#define _IIC_H
void IIC_Start(void);
void IIC_Stop(void);
bit IIC_WaitAck(void);
void IIC_SendAck(bit ackbit);
void IIC_SendByte(unsigned char byt);
unsigned char IIC_RecByte(void);
unsigned char EEPROM_read(unsigned char add);
void EEPROM_write(unsigned char add,unsigned char dat);
#endif