找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1730|回复: 2
收起左侧

百度的这个AT24C02程序,哪位大神给修改修改

[复制链接]
ID:268064 发表于 2018-5-23 13:44 | 显示全部楼层 |阅读模式
100黑币
AT24C02.C
#include <reg51.h>
#include "AT24C02.h"
sbit AT24C02_SDA = P3^6;        //定义数据线
sbit AT24C02_SCL = P3^7;        //定义时钟线
{   uchar i;
        for(i=0; i<200; i--);
}
void I2C_Start()
{   AT24C02_SDA = 1;   delay();
        AT24C02_SCL = 1;   delay();
        AT24C02_SDA = 0;   delay();
        AT24C02_SCL = 0;
}                      //钳位I2C总线,准备发送数据
void I2C_Stop()
{   AT24C02_SDA = 0;    delay();
        AT24C02_SCL = 1;    delay();
        AT24C02_SDA = 1;    delay();  
}
void Ack()
{   AT24C02_SDA = 0;    delay();
        AT24C02_SCL = 1;    delay();
        AT24C02_SCL = 0;    delay();
        AT24C02_SDA = 1;    delay();
}
void NoAck()
{   AT24C02_SDA = 1;    delay();
        AT24C02_SCL = 1;    delay();
        AT24C02_SCL = 0;    delay();  
}
uchar  Test_Ack()
{                 uchar flag;
        AT24C02_SDA=1;//读入数据
        delay();
        if(AT24C02_SDA==1)
                flag=0x01;
        else     flag=0x00;
                AT24C02_SCL=0;    delay();
        AT24C02_SCL=1;    delay();
        AT24C02_SCL=0;
        return(flag);   
}
void SendData(uchar  buffer)
{       uchar  BitCnt=8;//一字节8位
        uchar  temp=0;
        do
        {        temp=buffer;
                AT24C02_SCL=0;     delay();
                if((temp&0x80)==0)       //判断最高位是0还是1
                        AT24C02_SDA=0;
               else     AT24C02_SDA=1;   delay();
                       AT24C02_SCL=1;
                temp=buffer<<1;  //将buffer中的数据左移一位
                buffer=temp;
                BitCnt--;   
}
        while(BitCnt);
        AT24C02_SCL=0;
                AT24C02_SDA=1;   
}
uchar   ReceiveData()
{       uint BitCnt=8;
        uchar temp=0;
                uchar ucReceData;
        AT24C02_SDA=1;                           //读入数据
        do {     AT24C02_SCL=0;    delay();
                AT24C02_SCL=1;    delay();
                if(AT24C02_SDA==1)     ucReceData=ucReceData|0x01;//低位置1
                else                    ucReceData=ucReceData&0x0fe;//低位清0
                if(BitCnt-1)
                {       temp=ucReceData<<1;//数据左移一位
                        ucReceData=temp;
}
                BitCnt--;  
}
        while(BitCnt);
        AT24C02_SCL=0;
        return(ucReceData);  
}
uchar WriteNByte(uchar sla,uchar suba,uchar *s,uchar n)
{   uchar i;
        uchar flag;            
        I2C_Start();    //启动I2C                                                               
        SendData(sla);  //发送器件地址               
        flag = Test_Ack();        
        if(flag != 0x00)
        {   return 0;  
}
        SendData(suba);  //发送器件内部寄存器地址
        flag = Test_Ack();
        if(flag != 0x00)
        {   return 0;   
}
        for(i=0; i<n; i++) //写入n字节数据
        {   SendData(s);
                flag = Test_Ack();
                if(flag != 0x0   return 0;
}  }
        I2C_Stop();
        return 1;   
}
uchar ReadNByte(uchar sla,uchar suba,uchar *p,uchar n)
{    uchar i;
        uchar flag;   //测试n是否为有效值
        if(n > 8)
        {  return 0;  
}
        I2C_Start();                                //启动I2C
        SendData(sla);                    //发送器件地址
        flag = Test_Ack();
        if(flag != 0x00)
        {   return 0;   
}
        SendData(suba);                   //发送器件内部地址
        flag = Test_Ack();
    if(flag != 0x00)
        {   return 0;  
}
        I2C_Start();
        SendData(sla+1);
        flag = Test_Ack();
if(flag != 0x00){   return 0;
}
        for(i=0; i<n-1; i++)        //读取字节数据
        {   p = ReceiveData();        //读取数据
                Ack();  
}
        p[n-1] = ReceiveData();
        NoAck();
        I2C_Stop();
        return 1;   
}
KEY.H
#include"reg51.h"
#include"key.h"
#include "AT24C02.h"
#include"LCD_1602.h"
#define uchar unsigned char
#define uint unsigned int
#define        Key_in   P1=0x0f
#define  Key_pin  P1
sbit OUT2=P3^1;
sbit OUT1=P3^0;
uchar code        key_scan_tap[4]={0x1f,0x2f,0x4f,0x8f};
uchar code  Led_dsp_tap[6]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf};
uchar code  key_code_tap[16]=        {                0x1e,0x1d,0x1b,0x17,
                                                                    0x2e,0x2d,0x2b,0x27,
                                                                        0x4e,0x4d,0x4b,0x47,
0x8e,0x8d,0x8b,0x87  
};
uchar         new_key;
uchar         old_key;
uchar         key_cont;
uchar         key;
uchar   input_cont;
uchar   input_cont2;
uchar   input_step;
uchar   err_cont[8];          //各用户密码输入数错计数器
uchar   ID_stop_delay[8]; //用于保存各用户出错锁键盘时间
uchar   ID_keep_buf[8]={10,10,10,10,10,10,10,7};   //用户ID缓冲区
uchar   ID;
uchar          input_password[4]={0};
uchar          input_password2[4]={0};
uchar          input_password3[4]={0};
uchar          password[4];
uchar   keep_password[8][4];
uchar   OUT1_delay;
uint   moto_run_cont;
uchar   run_fx;
uchar   moto_cont;
uchar  POWER;
struct         key_flag
{          unsigned password_in:1;
          unsigned keep:1;
          unsigned open:1;
          unsigned redly_keep:1;
          unsigned call_open:1;
          unsigned ID_OK:1;
          unsigned control_en:1;
          unsigned control_input:1;
}
key_flag;
extern uchar sec;
extern uchar beef_cont;
extern uchar dsp_buf[1];
void moto_control(uchar cont,uchar fx)
{       moto_run_cont=cont*8;
                run_fx=fx;        
void key_control(void)
{                uchar i;      //以下为存物操作
                if(key==15)
                {       for(i=0;i<7;i++)
                                ID_keep_buf=10;
                                if(WriteNByte(0xa0,40,ID_keep_buf,7)==1)
                                {                beef_cont=0x81;        
                                                dsp_buf[0]=0xff;
                                                sec=3;
                                                OUT2=0;           
}
                                else  beef_cont=3;   
}
                if(key==14)
                {       POWER=POWER^0x01;
                                WRITE_ML(0x01);      //  清显示
                                if(POWER==1)
                                {    lcd_dsp(0,4,"Lockers",7);  
}
                                input_cont2=0;
                                key_flag.ID_OK=0;
                                key_flag.open=0;
                                key_flag.call_open=0;
                                input_step=0;
                                input_cont=0;
                                key_flag.keep=0;
                                key_flag.redly_keep=0;  
}
                if(POWER==1)
                {     if(key_flag.control_en==0)
                                {    if((key_flag.keep==1)&&(ID!=8))
                                                {     if(key<=9)
                                                                {                beef_cont=1;
                                                                                sec=0;
                                                                                if((input_cont<4)&&(input_step==0))
                                                                                {        input_password[input_cont]=key;
WRITE_ML(0x01);//  清显示
lcd_dsp(0,0,"save",4);
                                                    lcd_dsp(0,8,"on:",3);
lcd_no_write(0,11,ID,1);
                                                 lcd_dsp(1,0,"passwrod",8);
for(i=0;i<=input_cont;i++)    lcd_dsp(1,9+i,"*",1);
                                input_cont++;
}
        else  if((input_cont<4)&&(input_step==1))
{            input_password2[input_cont]=key;
                         WRITE_ML(0x01);//  清显示
                        lcd_dsp(0,0,"save",4);
                        lcd_dsp(0,8,"on:",3);
                        lcd_no_write(0,11,ID,1);
                        lcd_dsp(1,0,"passwrod",8);
                 for(i=0;i<=input_cont;i++)    lcd_dsp(1,9+i,"*",1);
                    input_cont++;
} }
else  if(key==13)        //取消键
                                                                {                beef_cont=1;
                                                                                sec=0;
                                                                                if((input_cont>0)&&(input_step==0))
                                    {                 input_cont-input_password[input_cont]=0;
WRITE_ML(0x01);//  清显示
                                             lcd_dsp(0,0,"save",4);
                                                      lcd_dsp(0,8,"on:",3);
                                                      lcd_no_write(0,11,ID,1);
                                             lcd_dsp(1,0,"passwrod",8);
                                if(input_cont>0)
                        {     for(i=0;i<=input_cont-1;i++)
                                        lcd_dsp(1,9+i,"*",1); } }
                        else   if((input_cont>0)&&(input_step==1))
                {  
input_password2[input_cont]=0;
                    input_cont--;
                        WRITE_ML(0x01);//  清显示
                        lcd_dsp(0,0,"save",4);
                        lcd_dsp(0,8,"on:",3);
                        lcd_no_write(0,11,ID,1);
                    lcd_dsp(1,0,"passwrod",8);
                        if(input_cont>0)
                {   
for(i=0;i<=input_cont-1;i++)
                   lcd_dsp(1,9+i,"*",1);
} } }
                else  if(key==10)
                {   
if((input_cont==4)&&(input_step==0))
                    {   input_step=1;
                                input_cont=0;  }
                           else  if((input_cont==4)&&(input_step==1))
{               
for(i=0;i<4;i++)
                        {         if(input_password2==input_password)
                                    {  ;  }
else   //密码核对匹配
        {        
beef_cont=0x03;
            input_step=0;
                input_cont=0;
i=5;
} }
if(i==4)        //密码核对成功
        {        
input_step=0;
                input_cont=0;
                key_flag.keep=0;
            key_flag.redly_keep=0;
for(i=0;i<4;i++)
keep_password[ID]=input_password2;   //保存用户密码
ID_keep_buf[ID]=ID;
if(WriteNByte(0xa0,40,ID_keep_buf,7)==1)
{               
beef_cont=0x81;
for(i=0;i<60;i++)   
delay();
if(WriteNByte(0xa0,ID*4,input_password2,4)==1)
{        beef_cont=0x81;
dsp_buf[0]=dsp_buf[0]&Led_dsp_tap[ID];//指示对应柜已占用
WRITE_ML(0x01);//  清显示
                lcd_dsp(0,0,"save",4);
                lcd_dsp(1,0,"successfully",12);        
                        sec=5;
                moto_control(8,1);
if(ID==1)
{
OUT2=1;
} } } } } }
else  if(key_flag.keep==0)
                                                {    if(key==11)
                                                                {                  input_cont2=0;
                                                                                key_flag.ID_OK=0;
                                                                                key_flag.open=0;
                                                                                key_flag.call_open=0;
                                                                                WRITE_ML(0x01);//  清显示
                                                                                lcd_dsp(0,0,"save",4);
                                                                                lcd_dsp(0,8,"on:",3);
                                                                                sec=0;
                                                                           for(i=0;i<6;i++)
                                                                           {   if(ID_keep_buf==10)
                                                                                        {       ID=i;
                                                                                                        i=6;
                                                                                                        key_flag.redly_keep=1;
}
                                                                                        else
                                                                                        {         ID=8;
} } }
                                                                else  if(key==10)
                                                                {           
        beef_cont=1;
                                                                                if(key_flag.redly_keep==1)
                                                                                {        
key_flag.keep=1;
                                                                                        WRITE_ML(0x01);//  清显示
                                                                                                lcd_dsp(0,0,"save",4);
                                                                                                lcd_dsp(0,8,"on:",3);
                                                                                                lcd_no_write(0,11,ID,1);
                                                                                                lcd_dsp(1,0,"passwrod",8);
} } }
                //取物
if(key_flag.open==1){  
if(key<=9)
        {                 beef_cont=1;
                        sec=0;        
                 if((input_cont2<4)&&(err_cont[ID]<3)){                  
input_password3[input_cont2]=key;
                                   WRITE_ML(0x01);//  清显示
                                         lcd_dsp(0,0,"open",4);
                                          lcd_dsp(0,8,"on:",3);
                                    lcd_no_write(0,11,ID,1);
                                       lcd_dsp(1,0,"passwrod",8);
for(i=0;i<=input_cont2;i++)      
lcd_dsp(1,9+i,"*",1);
                input_cont2++;  
}   }
                                else  if(key==13)        //取消键
                                                                {                beef_cont=1;
                                                                                sec=0;
                                                                                if((input_cont2>0)&&(input_step==0))
{                input_cont2--;
                input_password3[input_cont2]=0;
                WRITE_ML(0x01);//  清显示
                        lcd_dsp(0,0,"open",4);
                        lcd_dsp(0,8,"on:",3);
                        lcd_no_write(0,11,ID,1);
                    lcd_dsp(1,0,"passwrod",8);
if(input_cont2>0)
{   
for(i=0;i<=input_cont2-1;i++)
                lcd_dsp(1,9+i,"*",1);
} } }
else  if(key==10){
if(input_cont2==4){
                for(i=0;i<4;i++)  {         
if(keep_password[ID]==input_password3){
                                         ;  }
                        else {
i=5;
} }
                if(i==4)
{     
ID_keep_buf[ID]=10;
                input_cont2=0;
                key_flag.ID_OK=0;
            key_flag.open=0;
                key_flag.call_open=0;
if(WriteNByte(0xa0,40,ID_keep_buf,7)==1)   
beef_cont=0x81;
                err_cont[ID]=0;
if(ID<6)   dsp_buf[0]=dsp_buf[0]|(Led_dsp_tap[ID]^0xff);
                if(ID==6)
{                key_flag.control_en=1
}
                  WRITE_ML(0x01);//  清显示
lcd_dsp(0,0,"open",4);
lcd_dsp(1,0,"successfully",12);
sec=5;
moto_control(8,0);
if(ID==0){
        OUT1_delay=5;        }
        else  if(ID==1)
        {   OUT2=0;
}  }
else
{   err_cont[ID]++;
        if(err_cont[ID]>=3)
{        input_cont2=0;
        key_flag.ID_OK=0;
        key_flag.open=0;
        key_flag.call_open=0;
        ID_stop_delay[ID]=30;
}
        else
{        input_cont2=0; }
        beef_cont=3;
WRITE_ML(0x01);     //  清显示
        lcd_dsp(1,0,"Password Error",14);
        sec=0;
}
input_cont2=0;
} } }
else {      
if(key==12){                   beef_cont=1;
                                                                                key_flag.call_open=1;
                                                                                input_step=0;
                                                                                input_cont=0;
                                                                                key_flag.keep=0;
                                                                                key_flag.redly_keep=0;
                                                                                key_flag.ID_OK=0;
                                                                                key_flag.open=0;
                                                                                WRITE_ML(0x01);//  清显示
                                                                                lcd_dsp(0,0,"open",4);
                                                                                lcd_dsp(0,8,"on:",3);
                                                                                sec=0;  
}
                                        else  if(key<8){               
beef_cont=1;
                                                                                sec=0;
                                                                                if(key_flag.call_open==1)
                                                                                {                ID=key;
if((ID<=7)&&(ID_keep_buf[ID]==ID)&&(ID_stop_delay[ID]==0))
                                        {   key_flag.ID_OK=1;
}}}
                else if(key==10)
                        {
if(key_flag.ID_OK==1)
                                        {
key_flag.open=1;
                                                                                        WRITE_ML(0x01);//  清显示
                                                                                                lcd_dsp(0,0,"open",4);
                                                                                                lcd_dsp(0,8,"on:",3);
                                                                                                lcd_no_write(0,11,ID,1);
                                                                                                lcd_dsp(1,0,"passwrod",8);
}}}}
                                else {  if(key<6)
                                                {                ID=key;
                                                                beef_cont=1;
                                                                if(ID_keep_buf[ID]==ID)
                                                                {           key_flag.control_input=1;
}}
                                                else if((key==10)&&(key_flag.control_input==1))
                                                {               
beef_cont=1;
                                                                key_flag.control_en=0;
                                                                ID_keep_buf[ID]=10;
                                                                dsp_buf[0]=dsp_buf[0]|(Led_dsp_tap[ID]^0xff);
}}}}
void key_scan(void)
{                uchar i,j;
                uchar key_buf;         //暂存键值变量
                Key_in;                         //让读键引脚P10~P13为高电平
                old_key=new_key; //保存上次键值
                new_key=Key_pin&0x0f;//保存当前键值
                if(old_key==new_key)  //如果键状态没有发生变化,
                {                if(key_cont<100)  key_cont++;        //消抖动计数器加1
                                if(key_cont==5)        //消抖动成功,表示确实有键按下,或松键
                                {                    //以下进行循环扫描
                                                for(i=0;i<4;i++){  
Key_pin=key_scan_tap;         //逐行扫描
if((Key_pin&0x0f)!=new_key){                                        //逐行判断                               key_buf=(key_scan_tap&0xf0)|new_key;//得到键值
                                for(j=0;j<16;j++){
                        if(key_code_tap[j]==key_buf)         //  查内码
                {
key=j;//查得内码
                             key_control();                //调键值管理函数
                          j=17;  
}               
                        i=4;
}}}}
                else{               
key_cont=0;  
}}
LCD1602.C
#include"reg51.h"
#include"LCD_1602.h"
#define uchar unsigned char
#define uint unsigned int
sbit    RW=P3^2;
sbit    RS=P3^3;
sbit    E_LCD=P3^1;
sbit    BUSY=P2^7;
char code LCD_TAP[10]={"0123456789"};
void delay_1602(unsigned int i)         //延时函数
{  while(i--);  }
void PAN_BUSY(void)               //判忙子程序
{               
P2=0xFF;
                RS=0;
                RW=1;
                E_LCD=0;
                delay_1602(1);
                E_LCD=1;
                while(BUSY==1){; }}
void WRITE_ML(uchar m)    //写命令子程序
{                P2=m;
                RS=0;
                RW=0;
                E_LCD=0;
                PAN_BUSY();
                E_LCD=1;  
}
void WRITE_DAT(uchar dat)   //写数据子程序
{       P2=dat;
                RS=1;
                RW=0;
                E_LCD=0;
                PAN_BUSY();
                E_LCD=1;  
}

void  lcd_dsp(uchar l,uchar h,char a[],uchar n)
{           
uchar i;
                if(l==0){      
WRITE_ML(0x80+h);
                                for(i=0;i<n;i++)
                                WRITE_DAT(a);
}
                else if(l==1){
                                WRITE_ML(0xc0+h);
                                for(i=0;i<n;i++)
                                WRITE_DAT(a);  
}        }
void lcd_no_write(uchar l,uchar h,uchar cont,uchar n)
{               
uchar i;
                if(l==0)
                {      
WRITE_ML(0x80+h);
                                for(i=0;i<n;i++)
                                WRITE_DAT(LCD_TAP[cont]) ;  
}
                else if(l==1){      
WRITE_ML(0xc0+h);
                                for(i=0;i<n;i++)  WRITE_DAT(LCD_TAP[cont]);  
} }
void LCD_1602_init(void)
{        delay_1602(100);
        WRITE_ML(0x01);     //  清显示
    WRITE_ML(0x38);     //  8位点阵 允许双行显示
    WRITE_ML(0x0a);     //  开显示
    WRITE_ML(0x80);     //第一行显示
        delay_1602(100);
           WRITE_ML(0x01);     //  清显示
    WRITE_ML(0x38);     //  8位点阵 允许双行显示
    WRITE_ML(0x0C);     //  开显示
WRITE_ML(0x80);
}
//main程序
#include"reg51.h"
#include"key.h"
//#include"dsp.h"
#include "AT24C02.h"
#include"LCD_1602.h"
#define  uchar   unsigned char
#define  uint   unsigned int
#define THO_int   0xff
#define TL0_int   24
#define        TMOD_int   0x01
#define TCON_int     0x10
sbit    BUZ =P3^0;                  
uint          beef_delay;
uchar         beef_cont;
uchar         timecont_4ms;
uchar         timecont_10ms;
uchar         timecont_1s;
uchar   time_1000ms;
extern uint   moto_run_cont;
extern uchar   run_fx;
extern uchar   moto_cont;
uchar code  Led_dsp_tap2[6]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf};
uchar code  Led_tap[6]={0x01,0x02,0x04,0x08,0x10,0x20};
uchar code  moto_tap[8]={0x20,0x60,0x40,0x50,0x10,0x90,0x80,0xa0};
extern  uchar   err_cont[8];
extern  uchar   ID_stop_delay[8];
                 uchar         dsp_buf[1];
extern  uchar         password[4];
extern  uchar         ID_keep_buf[8];
extern  uchar   keep_password[8][4];
extern  uchar          password[4];
extern  uchar   OUT1_delay;
uchar   sec;
extern  uchar POWER;
uchar   SYS_clock;
struct flag
{          unsigned beef_en:1;
          unsigned dsp_control_en:1;
          unsigned out_time_en:1;  
}
flag1;   //蜂鸣器管理
void beef_control(void)
{         
if(beef_delay==0)
                {
if(flag1.beef_en==1){                flag1.beef_en=0;  //停止响蜂鸣器
                                                            beef_delay=20;          //蜂鸣器间隔时间  
}
                                else{         
  if(beef_cont&0x0f)//如果响蜂鸣次数还没有结束
                                                {        beef_cont--;
                                                                beef_delay=10; //响蜂鸣时间
                                                                if(beef_cont&0x80)  beef_delay=80;
                                                                flag1.beef_en=1;//开启响蜂鸣器
                                                } } } }
void out_time(void)
{         
if(flag1.out_time_en==1)
                {               
flag1.out_time_en=0;
                                if(sec){               
sec--;
                                                if(sec==0)  {         
WRITE_ML(0x01);//  清显示
                                                                lcd_dsp(0,4,"Lockers",7);        
} } } }
void T0_int(void)
{       TMOD=TMOD_int;
            TCON=TCON_int;
                TL0=TL0_int;
                TH0=THO_int;
                ET0=1;
            EA=1;
}
//单片机初始化
void MCU_init(void)
{                   P1=0xff;
                   P2=0xff;
                P3=0xff;         
}                        //响1声蜂鸣器
//定时器中断
void timer0_(void)interrupt 1 using 1        //定时器溢出周期为100us已加入16t
{
         uchar temp,i,P3_buf;
                 TR0=0;//
            TL0=TL0_int;
                TH0=THO_int;
                TR0=1;
                if(flag1.beef_en){  BUZ=!BUZ; }
                else     BUZ=1;
                timecont_4ms++;
                if(timecont_4ms>=16)
                {                  
SYS_clock=1;
                                timecont_4ms=0;
                                if(moto_run_cont)
                                {               
moto_run_cont--;
                                                moto_cont++;
                                                if(moto_cont>=8){  moto_cont=0; }
                                                P3_buf=P3&0x0f;
                                                if(run_fx)  
P3=P3_buf|moto_tap[7-moto_cont];
                                                else      
P3=P3_buf|moto_tap[moto_cont];  
}
                                else  {           P3=P3&0x0f; }
                                if(beef_delay)
beef_delay--;
                                temp=dsp_buf[0];
                                for(i=0;i<6;i++)
                                {                 
if(temp&0x01)  
P0=P0|Led_tap;
                                                 else   
P0=P0&Led_dsp_tap2;
                                                 temp=temp>>1;
}
                                time_1000ms++;
                                if(time_1000ms>=250)
                                {                time_1000ms=0;
                                                if(OUT1_delay){  OUT1_delay--;
                                                if(OUT1_delay==0);
} } }
                timecont_10ms++;
                if(timecont_10ms>=40){           
timecont_10ms=0;
                                                    timecont_1s++;
                                if((timecont_1s>=100)&&(POWER)){               
timecont_1s=0;
                                                flag1.out_time_en=1;
                                                if(ID_stop_delay[0]>0){               
ID_stop_delay[0]--;
                                                                if(ID_stop_delay[0]==0)
{
err_cont[0]=0;
} }
                                                if(ID_stop_delay[1]>0){               
ID_stop_delay[1]--;
                                                                 if(ID_stop_delay[1]==0)
{
err_cont[1]=0;
} }
                                                if(ID_stop_delay[2]>0){         
ID_stop_delay[2]--;
                                                                 if(ID_stop_delay[2]==0)
{
err_cont[2]=0;
} }
                                                if(ID_stop_delay[3]>0){
ID_stop_delay[3]--;
                                                                if(ID_stop_delay[3]==0)
{
err_cont[3]=0;
} }
                                                if(ID_stop_delay[4]>0){
ID_stop_delay[4]--;
                                                                 if(ID_stop_delay[4]==0)
{
err_cont[4]=0;
} }
                                                if(ID_stop_delay[5]>0){
ID_stop_delay[5]--;
                                                                        if(ID_stop_delay[5]==0)
{
err_cont[5]=0;
} }
                                                if(ID_stop_delay[6]>0){
ID_stop_delay[6]--;
                                                                        if(ID_stop_delay[6]==0)
{
err_cont[6]=0;
} } } } }
void main(void)
{            
        uchar i;
                POWER=1;
dsp_buf[0]=0xff;
MCU_init();
T0_int();
                for(i=0;i<7;i++)  ID_keep_buf=10;
if(ReadNByte(0xa0,0,password,4)==1)
                for(i=0;i<4;i++)  keep_password[0]=password;
                if(ReadNByte(0xa0,4,password,4)==1)
                for(i=0;i<4;i++)  keep_password[1]=password;
                if(ReadNByte(0xa0,8,password,4)==1)
                for(i=0;i<4;i++)  keep_password[2]=password;
                if(ReadNByte(0xa0,12,password,4)==1)
                for(i=0;i<4;i++)  keep_password[3]=password;
                if(ReadNByte(0xa0,16,password,4)==1)
                for(i=0;i<4;i++)  keep_password[4]=password;
                if(ReadNByte(0xa0,20,password,4)==1)
                for(i=0;i<4;i++)  keep_password[5]=password;
                 if(ReadNByte(0xa0,24,password,4)==1)
                for(i=0;i<4;i++)  keep_password[6]=password;
                if(ReadNByte(0xa0,40,ID_keep_buf,7)==1)
                beef_cont=3;
                for(i=0;i<6;i++)
                {       if(ID_keep_buf==i)
                                dsp_buf[0]=dsp_buf[0]&Led_dsp_tap2;
                                if(ID_keep_buf[1]==1){; }
                }
                LCD_1602_init();
                WRITE_ML(0x01);   //  清显示
                lcd_dsp(0,4,"Lockers",7);
for(;;) {        
                        if(SYS_clock)
                        {                SYS_clock=0;
                                        key_scan();               
                                        out_time();
                                           beef_control();
}
}
}

回复

使用道具 举报

ID:155507 发表于 2018-5-23 21:40 | 显示全部楼层
  1. //AT24C02.C
  2. #include <reg51.h>
  3. #include "AT24C02.h"
  4. sbit AT24C02_SDA = P3^6;        //定义数据线
  5. sbit AT24C02_SCL = P3^7;        //定义时钟线

  6. void delay()
  7. {
  8.         uchar i;
  9.         for(i=0; i<200; i--);
  10. }
  11. void I2C_Start()
  12. {
  13.         AT24C02_SDA = 1;   delay();
  14.         AT24C02_SCL = 1;   delay();
  15.         AT24C02_SDA = 0;   delay();
  16.         AT24C02_SCL = 0;
  17. }                      //钳位I2C总线,准备发送数据
  18. void I2C_Stop()
  19. {
  20.         AT24C02_SDA = 0;    delay();
  21.         AT24C02_SCL = 1;    delay();
  22.         AT24C02_SDA = 1;    delay();  
  23. }
  24. void Ack()
  25. {
  26.         AT24C02_SDA = 0;    delay();
  27.         AT24C02_SCL = 1;    delay();
  28.         AT24C02_SCL = 0;    delay();
  29.         AT24C02_SDA = 1;    delay();
  30. }
  31. void NoAck()
  32. {
  33.         AT24C02_SDA = 1;    delay();
  34.         AT24C02_SCL = 1;    delay();
  35.         AT24C02_SCL = 0;    delay();  
  36. }
  37. uchar  Test_Ack()
  38. {
  39.         uchar flag;
  40.         AT24C02_SDA=1;//读入数据
  41.         delay();
  42.         if(AT24C02_SDA==1)
  43.         flag=0x01;
  44.         else     flag=0x00;
  45.         AT24C02_SCL=0;    delay();
  46.         AT24C02_SCL=1;    delay();
  47.         AT24C02_SCL=0;
  48.         return(flag);   
  49. }
  50. void SendData(uchar  buffer)
  51. {
  52.         uchar  BitCnt=8;//一字节8位
  53.         uchar  temp=0;
  54.         do
  55.         {        temp=buffer;
  56.                 AT24C02_SCL=0;     
  57.                 delay();
  58.                 if((temp&0x80)==0)       //判断最高位是0还是1
  59.                 AT24C02_SDA=0;
  60.                 else     
  61.                 AT24C02_SDA=1;   
  62.                 delay();
  63.                 AT24C02_SCL=1;
  64.                 temp=buffer<<1;  //将buffer中的数据左移一位
  65.                 buffer=temp;
  66.                 BitCnt--;   
  67.         }
  68.         while(BitCnt);
  69.         AT24C02_SCL=0;
  70.         AT24C02_SDA=1;   
  71. }
  72. uchar   ReceiveData()
  73. {
  74.         uint BitCnt=8;
  75.         uchar temp=0;
  76.         uchar ucReceData;
  77.         AT24C02_SDA=1;                           //读入数据
  78.         do {
  79.                 AT24C02_SCL=0;   
  80.                 delay();
  81.                 AT24C02_SCL=1;   
  82.                 delay();
  83.                 if(AT24C02_SDA==1)     
  84.                 ucReceData=ucReceData|0x01;//低位置1
  85.                 else                    
  86.                 ucReceData=ucReceData&0x0fe;//低位清0
  87.                 if(BitCnt-1)
  88.                 {
  89.                         temp=ucReceData<<1;//数据左移一位
  90.                         ucReceData=temp;
  91.                 }
  92.                 BitCnt--;  
  93.         }
  94.         while(BitCnt);
  95.         AT24C02_SCL=0;
  96.         return(ucReceData);  
  97. }
  98. uchar WriteNByte(uchar sla,uchar suba,uchar *s,uchar n)
  99. {
  100.         uchar i;
  101.         uchar flag;            
  102.         I2C_Start();    //启动I2C                                                               
  103.         SendData(sla);  //发送器件地址               
  104.         flag = Test_Ack();        
  105.         if(flag != 0x00)
  106.         {
  107.                 return 0;  
  108.         }
  109.         SendData(suba);  //发送器件内部寄存器地址
  110.         flag = Test_Ack();
  111.         if(flag != 0x00)
  112.         {
  113.                 return 0;   
  114.         }
  115.         for(i=0; i<n; i++) //写入n字节数据
  116.         {
  117.                 SendData(s);
  118.                 flag = Test_Ack();
  119.                 if(flag != 0x0)
  120.                 return 0;
  121.         }  
  122.         I2C_Stop();
  123.         return 1;   
  124. }
  125. uchar ReadNByte(uchar sla,uchar suba,uchar *p,uchar n)
  126. {
  127.         uchar i;
  128.         uchar flag;   //测试n是否为有效值
  129.         if(n > 8)
  130.         {
  131.                 return 0;  
  132.         }
  133.         I2C_Start();                      //启动I2C
  134.         SendData(sla);                    //发送器件地址
  135.         flag = Test_Ack();
  136.         if(flag != 0x00)
  137.         {
  138.                 return 0;   
  139.         }
  140.         SendData(suba);                   //发送器件内部地址
  141.         flag = Test_Ack();
  142.         if(flag != 0x00)
  143.         {
  144.                 return 0;  
  145.         }
  146.         I2C_Start();
  147.         SendData(sla+1);
  148.         flag = Test_Ack();
  149.         if(flag != 0x00)
  150.         {
  151.                 return 0;
  152.         }
  153.         for(i=0; i<n-1; i++)        //读取字节数据
  154.         {
  155.                 p = ReceiveData();        //读取数据
  156.                 Ack();  
  157.         }
  158.         p[n-1] = ReceiveData();
  159.         NoAck();
  160.         I2C_Stop();
  161.         return 1;   
  162. }

  163. //KEY.H
  164. #include "reg51.h"
  165. #include "key.h"
  166. #include "AT24C02.h"
  167. #include "LCD_1602.h"
  168. #define uchar unsigned char
  169. #define uint unsigned int
  170. #define  Key_in   P1=0x0f
  171. #define  Key_pin  P1
  172. sbit OUT2=P3^1;
  173. sbit OUT1=P3^0;
  174. uchar code  key_scan_tap[4]={0x1f,0x2f,0x4f,0x8f};
  175. uchar code  Led_dsp_tap[6]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf};
  176. uchar code  key_code_tap[16]= {  0x1e,0x1d,0x1b,0x17,
  177.         0x2e,0x2d,0x2b,0x27,
  178.         0x4e,0x4d,0x4b,0x47,
  179.         0x8e,0x8d,0x8b,0x87  
  180. };
  181. uchar         new_key;
  182. uchar         old_key;
  183. uchar         key_cont;
  184. uchar         key;
  185. uchar   input_cont;
  186. uchar   input_cont2;
  187. uchar   input_step;
  188. uchar   err_cont[8];          //各用户密码输入数错计数器
  189. uchar   ID_stop_delay[8]; //用于保存各用户出错锁键盘时间
  190. uchar   ID_keep_buf[8]={10,10,10,10,10,10,10,7};   //用户ID缓冲区
  191. uchar   ID;
  192. uchar          input_password[4]={0};
  193. uchar          input_password2[4]={0};
  194. uchar          input_password3[4]={0};
  195. uchar          password[4];
  196. uchar   keep_password[8][4];
  197. uchar   OUT1_delay;
  198. uint   moto_run_cont;
  199. uchar   run_fx;
  200. uchar   moto_cont;
  201. uchar  POWER;
  202. struct    key_flag
  203. {
  204.         unsigned password_in:1;
  205.         unsigned keep:1;
  206.         unsigned open:1;
  207.         unsigned redly_keep:1;
  208.         unsigned call_open:1;
  209.         unsigned ID_OK:1;
  210.         unsigned control_en:1;
  211.         unsigned control_input:1;
  212. }
  213. key_flag;
  214. extern uchar sec;
  215. extern uchar beef_cont;
  216. extern uchar dsp_buf[1];
  217. void moto_control(uchar cont,uchar fx)
  218. {
  219.         moto_run_cont=cont*8;
  220.         run_fx=fx;  
  221. }                               
  222. void key_control(void)
  223. {
  224.         uchar i;      //以下为存物操作
  225.         if(key==15)
  226.         {
  227.                 for(i=0;i<7;i++)
  228.                 ID_keep_buf=10;
  229.                 if( WriteNByte(0xa0,40,ID_keep_buf,7)==1)
  230.                 {
  231.                         beef_cont=0x81;        
  232.                         dsp_buf[0]=0xff;
  233.                         sec=3;
  234.                         OUT2=0;           
  235.                 }
  236.                 else  beef_cont=3;   
  237.         }
  238.         if(key==14)
  239.         {
  240.                 POWER=POWER^0x01;
  241.                 WRITE_ML(0x01);      //  清显示
  242.                 if(POWER==1)
  243.                 {
  244.                         lcd_dsp(0,4,"Lockers",7);  
  245.                 }
  246.                 input_cont2=0;
  247.                 key_flag.ID_OK=0;
  248.                 key_flag.open=0;
  249.                 key_flag.call_open=0;
  250.                 input_step=0;
  251.                 input_cont=0;
  252.                 key_flag.keep=0;
  253.                 key_flag.redly_keep=0;  
  254.         }
  255.         if(POWER==1)
  256.         {     if(key_flag.control_en==0)
  257.                 {    if((key_flag.keep==1)&&(ID!=8))
  258.                         {
  259.                                 if(key<=9)
  260.                                 {
  261.                                         beef_cont=1;
  262.                                         sec=0;
  263.                                         if((input_cont<4)&&(input_step==0))
  264.                                         {
  265.                                                 input_password[input_cont]=key;
  266.                                                 WRITE_ML(0x01);//  清显示
  267.                                                 lcd_dsp(0,0,"save",4);
  268.                                                 lcd_dsp(0,8,"on:",3);
  269.                                                 lcd_no_write(0,11,ID,1);
  270.                                                 lcd_dsp(1,0,"passwrod",8);
  271.                                                 for(i=0;i<=input_cont;i++)    lcd_dsp(1,9+i,"*",1);
  272.                                                 input_cont++;
  273.                                         }
  274.                                         else  if((input_cont<4)&&(input_step==1))
  275.                                         {
  276.                                                 input_password2[input_cont]=key;
  277.                                                 WRITE_ML(0x01);//  清显示
  278.                                                 lcd_dsp(0,0,"save",4);
  279.                                                 lcd_dsp(0,8,"on:",3);
  280.                                                 lcd_no_write(0,11,ID,1);
  281.                                                 lcd_dsp(1,0,"passwrod",8);
  282.                                                 for(i=0;i<=input_cont;i++)    lcd_dsp(1,9+i,"*",1);
  283.                                                 input_cont++;
  284.                                         }
  285.                                 }
  286.                                 else  if(key==13)        //取消键
  287.                                 {                beef_cont=1;
  288.                                         sec=0;
  289.                                         if((input_cont>0)&&(input_step==0))
  290.                                         {
  291.                                                 input_cont-input_password[input_cont]=0;
  292.                                                 WRITE_ML(0x01);//  清显示
  293.                                                 lcd_dsp(0,0,"save",4);
  294.                                                 lcd_dsp(0,8,"on:",3);
  295.                                                 lcd_no_write(0,11,ID,1);
  296.                                                 lcd_dsp(1,0,"passwrod",8);
  297.                                                 if(input_cont>0)
  298.                                                 {     for(i=0;i<=input_cont-1;i++)
  299.                                                         lcd_dsp(1,9+i,"*",1);
  300.                                                 }
  301.                                         }
  302.                                         else   if((input_cont>0)&&(input_step==1))
  303.                                         {  
  304.                                                 input_password2[input_cont]=0;
  305.                                                 input_cont--;
  306.                                                 WRITE_ML(0x01);//  清显示
  307.                                                 lcd_dsp(0,0,"save",4);
  308.                                                 lcd_dsp(0,8,"on:",3);
  309.                                                 lcd_no_write(0,11,ID,1);
  310.                                                 lcd_dsp(1,0,"passwrod",8);
  311.                                                 if(input_cont>0)
  312.                                                 {   
  313.                                                         for(i=0;i<=input_cont-1;i++)
  314.                                                         lcd_dsp(1,9+i,"*",1);
  315.                                                 }
  316.                                         }
  317.                                 }
  318.                                 else  if(key==10)
  319.                                 {   
  320.                                         if((input_cont==4)&&(input_step==0))
  321.                                         {   input_step=1;
  322.                                                 input_cont=0;
  323.                                         }
  324.                                         else  if((input_cont==4)&&(input_step==1))
  325.                                         {               
  326.                                                 for(i=0;i<4;i++)
  327.                                                 {         if(input_password2==input_password)
  328.                                                         {  ;  }
  329.                                                         else   //密码核对匹配
  330.                                                         {        
  331.                                                                 beef_cont=0x03;
  332.                                                                 input_step=0;
  333.                                                                 input_cont=0;
  334.                                                                 i=5;
  335.                                                         }
  336.                                                 }
  337.                                                 if(i==4)        //密码核对成功
  338.                                                 {        
  339.                                                         input_step=0;
  340.                                                         input_cont=0;
  341.                                                         key_flag.keep=0;
  342.                                                         key_flag.redly_keep=0;
  343.                                                         for(i=0;i<4;i++)
  344.                                                         keep_password[ID]=input_password2;   //保存用户密码
  345.                                                         ID_keep_buf[ID]=ID;
  346.                                                         if(WriteNByte(0xa0,40,ID_keep_buf,7)==1)
  347.                                                         {               
  348.                                                                 beef_cont=0x81;
  349.                                                                 for(i=0;i<60;i++)
  350.                                                                 delay();
  351.                                                                 if(WriteNByte(0xa0,ID*4,input_password2,4)==1)
  352.                                                                 {        beef_cont=0x81;
  353.                                                                         dsp_buf[0]=dsp_buf[0]&Led_dsp_tap[ID];//指示对应柜已占用
  354.                                                                         WRITE_ML(0x01);//  清显示
  355.                                                                         lcd_dsp(0,0,"save",4);
  356.                                                                         lcd_dsp(1,0,"successfully",12);        
  357.                                                                         sec=5;
  358.                                                                         moto_control(8,1);
  359.                                                                         if(ID==1)
  360.                                                                         {
  361.                                                                                 OUT2=1;
  362.                                                                         }
  363.                                                                 }
  364.                                                                
  365.                                                         }
  366.                                                        
  367.                                                 }
  368.                                         }
  369.                                 }
  370.                                 else  if(key_flag.keep==0)
  371.                                 {    if(key==11)
  372.                                         {
  373.                                                 input_cont2=0;
  374.                                                 key_flag.ID_OK=0;
  375.                                                 key_flag.open=0;
  376.                                                 key_flag.call_open=0;
  377.                                                 WRITE_ML(0x01);//  清显示
  378.                                                 lcd_dsp(0,0,"save",4);
  379.                                                 lcd_dsp(0,8,"on:",3);
  380.                                                 sec=0;
  381.                                                 for(i=0;i<6;i++)
  382.                                                 {
  383.                                                         if(ID_keep_buf==10)
  384.                                                         {
  385.                                                                 ID=i;
  386.                                                                 i=6;
  387.                                                                 key_flag.redly_keep=1;
  388.                                                         }
  389.                                                         else
  390.                                                         {
  391.                                                                 ID=8;
  392.                                                         }
  393.                                                 }
  394.                                         }
  395.                                         else  if(key==10)
  396.                                         {           
  397.                                                 beef_cont=1;
  398.                                                 if(key_flag.redly_keep==1)
  399.                                                 {        
  400.                                                         key_flag.keep=1;
  401.                                                         WRITE_ML(0x01);//  清显示
  402.                                                         lcd_dsp(0,0,"save",4);
  403.                                                         lcd_dsp(0,8,"on:",3);
  404.                                                         lcd_no_write(0,11,ID,1);
  405.                                                         lcd_dsp(1,0,"passwrod",8);
  406.                                                 }
  407.                                         }
  408.                                 }
  409.                                 //取物
  410.                                 if(key_flag.open==1){  
  411.                                         if(key<=9)
  412.                                         {                 beef_cont=1;
  413.                                                 sec=0;        
  414.                                                 if((input_cont2<4)&&(err_cont[ID]<3)){                  
  415.                                                         input_password3[input_cont2]=key;
  416.                                                         WRITE_ML(0x01);//  清显示
  417.                                                         lcd_dsp(0,0,"open",4);
  418.                                                         lcd_dsp(0,8,"on:",3);
  419.                                                         lcd_no_write(0,11,ID,1);
  420.                                                         lcd_dsp(1,0,"passwrod",8);
  421.                                                         for(i=0;i<=input_cont2;i++)      
  422.                                                         lcd_dsp(1,9+i,"*",1);
  423.                                                         input_cont2++;  
  424.                                                 }   
  425.                                         }
  426.                                         else  if(key==13)        //取消键
  427.                                         {
  428.                                                 beef_cont=1;
  429.                                                 sec=0;
  430.                                                 if((input_cont2>0)&&(input_step==0))
  431.                                                 {
  432.                                                         input_cont2--;
  433.                                                         input_password3[input_cont2]=0;
  434.                                                         WRITE_ML(0x01);//  清显示
  435.                                                         lcd_dsp(0,0,"open",4);
  436.                                                         lcd_dsp(0,8,"on:",3);
  437.                                                         lcd_no_write(0,11,ID,1);
  438.                                                         lcd_dsp(1,0,"passwrod",8);
  439.                                                         if(input_cont2>0)
  440.                                                         {   
  441.                                                                 for(i=0;i<=input_cont2-1;i++)
  442.                                                                 lcd_dsp(1,9+i,"*",1);
  443.                                                         }
  444.                                                 }
  445.                                         }
  446.                                         else  if(key==10){
  447.                                                 if(input_cont2==4){
  448.                                                         for(i=0;i<4;i++)  {         
  449.                                                                 if(keep_password[ID]==input_password3){
  450.                                                                         ;  }
  451.                                                                 else {
  452.                                                                         i=5;
  453.                                                                 } }
  454.                                                         if(i==4)
  455.                                                         {     
  456.                                                                 ID_keep_buf[ID]=10;
  457.                                                                 input_cont2=0;
  458.                                                                 key_flag.ID_OK=0;
  459.                                                                 key_flag.open=0;
  460.                                                                 key_flag.call_open=0;
  461.                                                                 if(WriteNByte(0xa0,40,ID_keep_buf,7)==1)   
  462.                                                                 beef_cont=0x81;
  463.                                                                 err_cont[ID]=0;
  464.                                                                 if(ID<6)   dsp_buf[0]=dsp_buf[0]|(Led_dsp_tap[ID]^0xff);
  465.                                                                 if(ID==6)
  466.                                                                 {
  467.                                                                         key_flag.control_en=1 ;
  468.                                                                 }
  469.                                                                 WRITE_ML(0x01);//  清显示
  470.                                                                 lcd_dsp(0,0,"open",4);
  471.                                                                 lcd_dsp(1,0,"successfully",12);
  472.                                                                 sec=5;
  473.                                                                 moto_control(8,0);
  474.                                                                 if(ID==0){
  475.                                                                         OUT1_delay=5;
  476.                                                                 }
  477.                                                                 else  if(ID==1)
  478.                                                                 {
  479.                                                                         OUT2=0;
  480.                                                                 }
  481.                                                         }
  482.                                                         else
  483.                                                         {
  484.                                                                 err_cont[ID]++;
  485.                                                                 if(err_cont[ID]>=3)
  486.                                                                 {
  487.                                                                         input_cont2=0;
  488.                                                                         key_flag.ID_OK=0;
  489.                                                                         key_flag.open=0;
  490.                                                                         key_flag.call_open=0;
  491.                                                                         ID_stop_delay[ID]=30;
  492.                                                                 }
  493.                                                                 else
  494.                                                                 {
  495.                                                                         input_cont2=0;
  496.                                                                 }
  497.                                                                 beef_cont=3;
  498.                                                                 WRITE_ML(0x01);     //  清显示
  499.                                                                 lcd_dsp(1,0,"Password Error",14);
  500.                                                                 sec=0;
  501.                                                         }
  502.                                                         input_cont2=0;
  503.                                                 }
  504.                                         }
  505.                                 }
  506.                                 else {      
  507.                                         if(key==12){
  508.                                                 beef_cont=1;
  509.                                                 key_flag.call_open=1;
  510.                                                 input_step=0;
  511.                                                 input_cont=0;
  512.                                                 key_flag.keep=0;
  513.                                                 key_flag.redly_keep=0;
  514.                                                 key_flag.ID_OK=0;
  515.                                                 key_flag.open=0;
  516.                                                 WRITE_ML(0x01);//  清显示
  517.                                                 lcd_dsp(0,0,"open",4);
  518.                                                 lcd_dsp(0,8,"on:",3);
  519.                                                 sec=0;  
  520.                                         }
  521.                                         else  if(key<8){               
  522.                                                 beef_cont=1;
  523.                                                 sec=0;
  524.                                                 if(key_flag.call_open==1)
  525.                                                 {
  526.                                                         ID=key;
  527.                                                         if((ID<=7)&&(ID_keep_buf[ID]==ID)&&(ID_stop_delay[ID]==0))
  528.                                                         {
  529.                                                                 key_flag.ID_OK=1;
  530.                                                         }
  531.                                                 }
  532.                                         }
  533.                                         else if(key==10)
  534.                                         {
  535.                                                 if(key_flag.ID_OK==1)
  536.                                                 {
  537.                                                         key_flag.open=1;
  538.                                                         WRITE_ML(0x01);//  清显示
  539.                                                         lcd_dsp(0,0,"open",4);
  540.                                                         lcd_dsp(0,8,"on:",3);
  541.                                                         lcd_no_write(0,11,ID,1);
  542.                                                         lcd_dsp(1,0,"passwrod",8);
  543.                                                 }
  544.                                         }
  545.                                 }
  546.                         }
  547.                         else
  548.                         {  if(key<6)
  549.                                 {                ID=key;
  550.                                         beef_cont=1;
  551.                                         if(ID_keep_buf[ID]==ID)
  552.                                         {
  553.                                                 key_flag.control_input=1;
  554.                                         }
  555.                                 }
  556.                                 else if((key==10)&&(key_flag.control_input==1))
  557.                                 {               
  558.                                         beef_cont=1;
  559.                                         key_flag.control_en=0;
  560.                                         ID_keep_buf[ID]=10;
  561.                                         dsp_buf[0]=dsp_buf[0]|(Led_dsp_tap[ID]^0xff);
  562.                                 }
  563.                         }
  564.                 }
  565.         }
  566. }
  567. void key_scan(void)
  568. {
  569.         uchar i,j;
  570.         uchar key_buf;         //暂存键值变量
  571.         Key_in;               //让读键引脚P10~P13为高电平
  572.         old_key=new_key; //保存上次键值
  573.         new_key=Key_pin&0x0f;//保存当前键值
  574.         if(old_key==new_key)  //如果键状态没有发生变化,
  575.         {
  576.                 if(key_cont<100)  key_cont++;        //消抖动计数器加1
  577.                 if(key_cont==5)        //消抖动成功,表示确实有键按下,或松键
  578.                 {                    //以下进行循环扫描
  579.                         for(i=0;i<4;i++){  
  580.                                 Key_pin=key_scan_tap;         //逐行扫描
  581.                                 if((Key_pin&0x0f)!=new_key){    //逐行判断   
  582.                                         key_buf=(key_scan_tap&0xf0)|new_key;//得到键值
  583.                                         for(j=0;j<16;j++){
  584.                                                 if(key_code_tap[j]==key_buf)    //  查内码
  585.                                                 {
  586.                                                         key=j;//查得内码
  587.                                                         key_control();    //调键值管理函数
  588.                                                         j=17;  
  589.                                                 }               
  590.                                                 i=4;
  591.                                         }
  592.                                 }
  593.                         }
  594.                 }
  595.                 else{               
  596.                         key_cont=0;  
  597.                 }
  598.         }
  599. }

  600. //LCD1602.C
  601. #include"reg51.h"
  602. #include"LCD_1602.h"
  603. #define uchar unsigned char
  604. #define uint unsigned int
  605. sbit    RW=P3^2;
  606. sbit    RS=P3^3;
  607. sbit    E_LCD=P3^1;
  608. sbit    BUSY=P2^7;
  609. char code LCD_TAP[10]={"0123456789"};
  610. void delay_1602(unsigned int i)         //延时函数
  611. {
  612.         while(i--);
  613. }
  614. void PAN_BUSY(void)               //判忙子程序
  615. {               
  616.         P2=0xFF;
  617.         RS=0;
  618.         RW=1;
  619.         E_LCD=0;
  620.         delay_1602(1);
  621.         E_LCD=1;
  622.         while(BUSY==1){; }
  623. }
  624. void WRITE_ML(uchar m)    //写命令子程序
  625. {
  626.         P2=m;
  627.         RS=0;
  628.         RW=0;
  629.         E_LCD=0;
  630.         PAN_BUSY();
  631.         E_LCD=1;  
  632. }
  633. void WRITE_DAT(uchar dat)   //写数据子程序
  634. {
  635.         P2=dat;
  636.         RS=1;
  637.         RW=0;
  638.         E_LCD=0;
  639.         PAN_BUSY();
  640.         E_LCD=1;  
  641. }

  642. void  lcd_dsp(uchar l,uchar h,char a[],uchar n)
  643. {           
  644.         uchar i;
  645.         if(l==0){      
  646.                 WRITE_ML(0x80+h);
  647.                 for(i=0;i<n;i++)
  648.                 WRITE_DAT(a);
  649.         }
  650.         else if(l==1){
  651.                 WRITE_ML(0xc0+h);
  652.                 for(i=0;i<n;i++)
  653.                 WRITE_DAT(a);  
  654.         }        
  655. }
  656. void lcd_no_write(uchar l,uchar h,uchar cont,uchar n)
  657. {               
  658.         uchar i;
  659.         if(l==0)
  660.         {      
  661.                 WRITE_ML(0x80+h);
  662.                 for(i=0;i<n;i++)
  663.                 WRITE_DAT(LCD_TAP[cont]) ;  
  664.         }
  665.         else if(l==1)
  666.         {      
  667.                 WRITE_ML(0xc0+h);
  668.                 for(i=0;i<n;i++)  WRITE_DAT(LCD_TAP[cont]);  
  669.         }
  670. }
  671. void LCD_1602_init(void)
  672. {
  673.         delay_1602(100);
  674.         WRITE_ML(0x01);     //  清显示
  675.         WRITE_ML(0x38);     //  8位点阵 允许双行显示
  676.         WRITE_ML(0x0a);     //  开显示
  677.         WRITE_ML(0x80);     //第一行显示
  678.         delay_1602(100);
  679.         WRITE_ML(0x01);     //  清显示
  680.         WRITE_ML(0x38);     //  8位点阵 允许双行显示
  681.         WRITE_ML(0x0C);     //  开显示
  682.         WRITE_ML(0x80);
  683. }

  684. //main程序
  685. #include"reg51.h"
  686. #include"key.h"
  687. //#include"dsp.h"
  688. #include "AT24C02.h"
  689. #include"LCD_1602.h"
  690. #define  uchar   unsigned char
  691. #define  uint   unsigned int
  692. #define THO_int   0xff
  693. #define TL0_int   24
  694. #define        TMOD_int   0x01
  695. #define TCON_int     0x10
  696. sbit    BUZ =P3^0;                  
  697. uint          beef_delay;
  698. uchar         beef_cont;
  699. uchar         timecont_4ms;
  700. uchar         timecont_10ms;
  701. uchar         timecont_1s;
  702. uchar   time_1000ms;
  703. extern uint   moto_run_cont;
  704. extern uchar   run_fx;
  705. extern uchar   moto_cont;
  706. uchar code  Led_dsp_tap2[6]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf};
  707. uchar code  Led_tap[6]={0x01,0x02,0x04,0x08,0x10,0x20};
  708. uchar code  moto_tap[8]={0x20,0x60,0x40,0x50,0x10,0x90,0x80,0xa0};
  709. extern  uchar   err_cont[8];
  710. extern  uchar   ID_stop_delay[8];
  711. uchar         dsp_buf[1];
  712. extern  uchar         password[4];
  713. extern  uchar         ID_keep_buf[8];
  714. extern  uchar   keep_password[8][4];
  715. extern  uchar          password[4];
  716. extern  uchar   OUT1_delay;
  717. uchar   sec;
  718. extern  uchar POWER;
  719. uchar   SYS_clock;
  720. struct flag
  721. {
  722.         unsigned beef_en:1;
  723.         unsigned dsp_control_en:1;
  724.         unsigned out_time_en:1;  
  725. }
  726. flag1;   //蜂鸣器管理
  727. void beef_control(void)
  728. {         
  729.         if(beef_delay==0)
  730.         {
  731.                 if(flag1.beef_en==1){
  732.                         flag1.beef_en=0;  //停止响蜂鸣器
  733.                         beef_delay=20;          //蜂鸣器间隔时间  
  734.                 }
  735.                 else{         
  736.                         if(beef_cont&0x0f)//如果响蜂鸣次数还没有结束
  737.                         {
  738.                                 beef_cont--;
  739.                                 beef_delay=10; //响蜂鸣时间
  740.                                 if(beef_cont&0x80)  beef_delay=80;
  741.                                 flag1.beef_en=1;//开启响蜂鸣器
  742.                         }
  743.                 }
  744.         }
  745. }
  746. void out_time(void)
  747. {         
  748.         if(flag1.out_time_en==1)
  749.         {               
  750.                 flag1.out_time_en=0;
  751.                 if(sec){               
  752.                         sec--;
  753.                         if(sec==0)  {         
  754.                                 WRITE_ML(0x01);//  清显示
  755.                                 lcd_dsp(0,4,"Lockers",7);        
  756.                         }
  757.                 }
  758.         }
  759. }
  760. void T0_int(void)
  761. {
  762.         TMOD=TMOD_int;
  763.         TCON=TCON_int;
  764.         TL0=TL0_int;
  765.         TH0=THO_int;
  766.         ET0=1;
  767.         EA=1;
  768. }
  769. //单片机初始化
  770. void MCU_init(void)
  771. {
  772.         P1=0xff;
  773.         P2=0xff;
  774.         P3=0xff;         
  775. }                        //响1声蜂鸣器
  776. //定时器中断
  777. void timer0_(void)interrupt 1 using 1        //定时器溢出周期为100us已加入16t
  778. {
  779.         uchar temp,i,P3_buf;
  780.         TR0=0;//
  781.         TL0=TL0_int;
  782.         TH0=THO_int;
  783.         TR0=1;
  784.         if(flag1.beef_en){  BUZ=!BUZ; }
  785.         else     BUZ=1;
  786.         timecont_4ms++;
  787.         if(timecont_4ms>=16)
  788.         {                  
  789.                 SYS_clock=1;
  790.                 timecont_4ms=0;
  791.                 if(moto_run_cont)
  792.                 {               
  793.                         moto_run_cont--;
  794.                         moto_cont++;
  795.                         if(moto_cont>=8){  moto_cont=0; }
  796.                         P3_buf=P3&0x0f;
  797.                         if(run_fx)  
  798.                         P3=P3_buf|moto_tap[7-moto_cont];
  799.                         else      
  800.                         P3=P3_buf|moto_tap[moto_cont];  
  801.                 }
  802.                 else  {
  803.                         P3=P3&0x0f;
  804.                 }
  805.                 if(beef_delay)
  806.                 beef_delay--;
  807.                 temp=dsp_buf[0];
  808.                 for(i=0;i<6;i++)
  809.                 {                 
  810.                         if(temp&0x01)  
  811.                         P0=P0|Led_tap;
  812.                         else   
  813.                         P0=P0&Led_dsp_tap2;
  814.                         temp=temp>>1;
  815.                 }
  816.                 time_1000ms++;
  817.                 if(time_1000ms>=250)
  818.                 {                time_1000ms=0;
  819.                         if(OUT1_delay){  OUT1_delay--;
  820.                                 if(OUT1_delay==0);
  821.                         }
  822.                 }
  823.         }
  824.         timecont_10ms++;
  825.         if(timecont_10ms>=40){           
  826.                 timecont_10ms=0;
  827.                 timecont_1s++;
  828.                 if((timecont_1s>=100)&&(POWER)){               
  829.                         timecont_1s=0;
  830.                         flag1.out_time_en=1;
  831.                         if(ID_stop_delay[0]>0){               
  832.                                 ID_stop_delay[0]--;
  833.                                 if(ID_stop_delay[0]==0)
  834.                                 {
  835.                                         err_cont[0]=0;
  836.                                 }
  837.                         }
  838.                         if(ID_stop_delay[1]>0){               
  839.                                 ID_stop_delay[1]--;
  840.                                 if(ID_stop_delay[1]==0)
  841.                                 {
  842.                                         err_cont[1]=0;
  843.                                 }
  844.                         }
  845.                         if(ID_stop_delay[2]>0){         
  846.                                 ID_stop_delay[2]--;
  847.                                 if(ID_stop_delay[2]==0)
  848.                                 {
  849.                                         err_cont[2]=0;
  850.                                 }
  851.                         }
  852.                         if(ID_stop_delay[3]>0){
  853.                                 ID_stop_delay[3]--;
  854.                                 if(ID_stop_delay[3]==0)
  855.                                 {
  856.                                         err_cont[3]=0;
  857.                                 }
  858.                         }
  859.                         if(ID_stop_delay[4]>0){
  860.                                 ID_stop_delay[4]--;
  861.                                 if(ID_stop_delay[4]==0)
  862.                                 {
  863.                                         err_cont[4]=0;
  864.                                 }
  865.                         }
  866.                         if(ID_stop_delay[5]>0){
  867.                                 ID_stop_delay[5]--;
  868.                                 if(ID_stop_delay[5]==0)
  869.                                 {
  870.                                         err_cont[5]=0;
  871.                                 }
  872.                         }
  873.                         if(ID_stop_delay[6]>0){
  874.                                 ID_stop_delay[6]--;
  875.                                 if(ID_stop_delay[6]==0)
  876.                                 {
  877.                                         err_cont[6]=0;
  878.                                 }
  879.                         }
  880.                 }
  881.         }
  882. }
  883. void main(void)
  884. {            
  885.         uchar i;
  886.         POWER=1;
  887.         dsp_buf[0]=0xff;
  888.         MCU_init();
  889.         T0_int();
  890.         for(i=0;i<7;i++)  ID_keep_buf=10;
  891.         if(ReadNByte(0xa0,0,password,4)==1)
  892.         for(i=0;i<4;i++)  keep_password[0]=password;
  893.         if(ReadNByte(0xa0,4,password,4)==1)
  894.         for(i=0;i<4;i++)  keep_password[1]=password;
  895.         if(ReadNByte(0xa0,8,password,4)==1)
  896.         for(i=0;i<4;i++)  keep_password[2]=password;
  897.         if(ReadNByte(0xa0,12,password,4)==1)
  898.         for(i=0;i<4;i++)  keep_password[3]=password;
  899.         if(ReadNByte(0xa0,16,password,4)==1)
  900.         for(i=0;i<4;i++)  keep_password[4]=password;
  901.         if(ReadNByte(0xa0,20,password,4)==1)
  902.         for(i=0;i<4;i++)  keep_password[5]=password;
  903.         if(ReadNByte(0xa0,24,password,4)==1)
  904.         for(i=0;i<4;i++)  keep_password[6]=password;
  905.         if(ReadNByte(0xa0,40,ID_keep_buf,7)==1)
  906.         beef_cont=3;
  907.         for(i=0;i<6;i++)
  908.         {       if(ID_keep_buf==i)
  909.                 dsp_buf[0]=dsp_buf[0]&Led_dsp_tap2;
  910.                 if(ID_keep_buf[1]==1){; }
  911.         }
  912.         LCD_1602_init();
  913.         WRITE_ML(0x01);   //  清显示
  914.         lcd_dsp(0,4,"Lockers",7);
  915.         for(;;) {        
  916.                 if(SYS_clock)
  917.                 {                SYS_clock=0;
  918.                         key_scan();               
  919.                         out_time();
  920.                         beef_control();
  921.                 }
  922.         }
  923. }

复制代码
回复

使用道具 举报

ID:268717 发表于 2018-10-2 16:50 | 显示全部楼层
典型郭天祥风格程序  不过在函数的配合上面有一点问题。
详情加我QQ:2577647123!有偿回答!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表