//主函数
#include <reg52.h> //用52头文件编程
#include <iic.h> //iic头文件
sfr P4=0xc0; //定义P4管脚
sbit C1=P3^0; //矩阵按键相关
sbit C2=P3^1;
sbit C3=P3^2;
sbit C4=P3^3;
sbit R1=P3^4;
sbit R2=P3^5;
sbit R3=P4^2;
sbit R4=P4^4;
unsigned char s16=0; //按键s16输入按键
unsigned char s12=0; //按键s12密码修改按键
unsigned char s8=0; //按键s8清除按键
unsigned char a1; //修改密码时的参数
unsigned char a2;
unsigned char a3;
unsigned char a4;
unsigned char a5;
unsigned char a6;
unsigned char b1=0; //
unsigned char b2=0; //输入完成的标志
unsigned char b3=0; //密码输入正确的标志
unsigned char b4=0; //密码输入错误的标志
unsigned char b5=0; //输入密码达到6位时的标志
unsigned char b6=0; //密码输入正确时,修改密码的标志
unsigned char c1=8; //读取上次存储密码第一位
unsigned char c2=8; //读取上次存储密码第二位
unsigned char c3=8; //读取上次存储密码第三位
unsigned char c4=8; //读取上次存储密码第四位
unsigned char c5=8; //读取上次存储密码第五位
unsigned char c6=8; //读取上次存储密码第六位
unsigned char c7; //首次上电的密码存储相关
unsigned char moshi=0; //显示的模式
unsigned char count_1; //中断计数
unsigned long mima=888888;
unsigned long mima_1=21; //密码第一位
unsigned long mima_2=21; //密码第二位
unsigned long mima_3=21; //密码第三位
unsigned long mima_4=21; //密码第四位
unsigned long mima_5=21; //密码第五位
unsigned long mima_6=21; //密码第六位
unsigned long mima_7=0; //密码错误时显示 原数
unsigned char num; //按键代表的数
unsigned char i; //选择第几位密码输入
unsigned char dat; //AT24C02写入的数据
unsigned char smg_buff[8]; //数码管缓存
unsigned char code SMG_Duan[]={
0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,
0x88,0x80,0xc6,0xc0,0x86,0x8e,0xbf,0x7f,0x8c,0x86,0xc8,0xff};
//数码管段选0 1 2 3 4 5 6 7 8 9 a b c d e f - . P E N 关闭
void Read_mima();
void Delay(unsigned int t) //延时函数
{
while(t--);
}
void Init_system() //系统初始化函数
{
P2=(P2 & 0x1f)|0x80; //关闭LED灯
P0=0xff;
P2=(P2 & 0x1f)|0xa0; //关闭继电器和蜂鸣器
P0=0X00;
P2=0x00;
}
//***********************************数码管显示基础函数***************************************
void Display_SMG(unsigned char value,unsigned char pos)
{
P2=(P2 & 0x1f)|0xe0;
P0=value;
P2=(P2 & 0x1f)|0xc0;
P0=0x01<<pos;
P2=0x00;
}
void ALL_smg() //关闭所有数码管以防最后一个和其他亮度不一样
{
P2=(P2 & 0x1f)|0xe0;
P0=0xff;
P2=(P2 & 0x1f)|0xc0;
P0=0xff;
P2=0x00;
}
void SMG_Display()
{
Display_SMG(SMG_Duan[smg_buff[0]],0);
Delay(500);
Display_SMG(SMG_Duan[smg_buff[1]],1);
Delay(500);
Display_SMG(SMG_Duan[smg_buff[2]],2);
Delay(500);
Display_SMG(SMG_Duan[smg_buff[3]],3);
Delay(500);
Display_SMG(SMG_Duan[smg_buff[4]],4);
Delay(500);
Display_SMG(SMG_Duan[smg_buff[5]],5);
Delay(500);
Display_SMG(SMG_Duan[smg_buff[6]],6);
Delay(500);
Display_SMG(SMG_Duan[smg_buff[7]],7);
Delay(500);
ALL_smg();
//Display_SMG(0xff,0&1&2&3&4&5&6&7);
Delay(500);
}
//***********************************************************************************
//********************************AT24C02写函数************************************
void Write_AT24C02Aadd(unsigned char addr,unsigned char dat)
{
IIC_Start();
Delay(