如何实现1602LCD 掉电不丢失数据,上电显示原计数值?

怎么能在原有程序基础上,按下K1既能计数又能写入EEPROM,掉电数据不丢失,上电显示掉电前计数值?

> /********************************************************
* 文    件: 1602显示屏按键计数
* 作    者: 
* 外部晶振: 11.0592MHZ    
* 编译环境: Keil μVisio4    
* 功    能: 数码管静态显示                                                      
* 注意事项: 
********************************************************/

#include<reg52.h>
unsigned char code table1[]="COUNT:";             //命名数组用来显示字符“COUNT:”
int count = 0;                                   //全局变量初始化
sbit RW = P2^5;
sbit RS = P2^6;                                    //数据/命令选择接口
sbit EN = P2^7;                                    //使能接口
sbit k1 = P3^2;                            //按键1接在P3^2接口,对应外部中断0
sbit k2 = P3^3;                            //按键2接在P3^3接口,对应外部中断1

/********************************************************
函数名:毫秒级CPU延时函数
调  用:delay_ms(?);
参  数:1~65535(参数不可为0)
返回值:无
结  果:占用CPU方式延时与参数数值相同的毫秒时间
备  注:应用于1T单片机时i<600,应用于12T单片机时i<125
/********************************************************/

void delay(unsigned int a)
{
    unsigned int i;
    while( --a != 0){
        for(i = 0; i < 125; i++);
    }
}

/********************************************************
函数名:写指令函数
调  用:writeinstruction(x);
参  数:无符号十六进制数0x??
返回值:无
结  果:将指令发送给1602控制器
/********************************************************/

void  writeinstruction(unsigned char x)
{   
    RS = 0;
    EN = 0;
    RW = 0;
    P0 = x;
    delay(5);
    EN = 1;
    delay(5);
    EN = 0;
}

/*********************************************************
函数名:写数据函数
调  用:writedata(y);
参  数:无符号十六进制数0x??
返回值:无
结  果:将数据发送至1602内部的显示内存中
/*********************************************************/

void  writedata(unsigned char y)
{   
    RS = 1;
    EN = 0;
    RW = 0;
    P0 = y;
    delay(5);
    EN = 1;
    delay(5);
    EN = 0;
}

/*********************************************************
函数名:写变量函数
调  用:writechar(z);
参  数:无符号十六进制数0x??
返回值:无
结  果:将数据发送至1602内部的显示内存中
备  注:不能将整数型变量count直接作为writedata的参数,需要将个、十、百位分别+48(0x30)转化成ascii码。
/*********************************************************/

void  writechar(unsigned char z)                         
{
    unsigned char A;
    unsigned char B;
    unsigned char C;
    RS = 1;
    EN = 0;
    RW = 0;
    A = z/1%10;                                    //取个位
    B = z/10%10;                                //取十位
    C = z/100%10;                                //取百位
    writedata(C+48);            
    delay(5);    
    writedata(B+48);                            //分离出变量的每一位数,再将每一位加上0x30,
    delay(5);    
    writedata(A+48);                            //这样就变成了ASCII码了,再送给LCD才能显示出来变量的每一位的值。
    delay(5);
}

/*********************************************************
函数名:1602设置初始化函数
调  用:init1602();
参  数:无
返回值:无
结  果:初始化。具体指令在手册中查询。
/*********************************************************/

void  init1602()
{ 
                                                       
    writeinstruction(0x38);                        //设置显示模式 指令码 00111000 => 0x38      
    delay(5);
    writeinstruction(0x0C);                        //[00001DCB]:D为显示开关,B为光标开关,C为光标闪烁开关。0x0C:开显示 不显示光标 不闪烁
    delay(5);    
    writeinstruction(0x06);                        //写一个字符后地址指针加一
    delay(5);    
    writeinstruction(0x01);                       //清屏       
    delay(5);    

}

/**********************************************************
函数名:主函数main
/**********************************************************/

void  main()
{  
    char i;

    init1602();
    writeinstruction(0x80);                      //设置数据地址指针从第一个开始
    delay(5);
    
    /* 初始化中断 */
    EA = 1;
    EX0 = 1;
    EX1 = 1;
    
    /* 写初始字符:COUNT:0 */
    for (i = 0; i < 6; ++i)                     //逐个写入第一行数据
    {
        writedata(table1[i]);
        delay(20);
    }

    writeinstruction(0x02);                     //第一行光标返回
    writeinstruction(0x80 + 0x40);                 //数据地址指针指向第二行第一个    
    writechar(count);
    
    /*砸瓦鲁多 */
    while(1);                                    //程序停在这里,等待中断到来
}

/*********************************************************
函数名:中断显示计数值函数
调  用:按下button1
参  数:无
返回值:无
结  果:显示当前计数值。具体指令在手册中查询。
/*********************************************************/

void Display()interrupt 0 using 1
{
    int i;
    if(k1==0)
    {
        delay(10);
        if(k1==0)
        {
            count++;                              //按键松开后,程序会运行到这里,计数值加1
            while(!k1);                     //如果按键没有松开,程序会停在这里
        }
        writeinstruction(0x01);                     //清屏   
        for (i = 0; i < 6; ++i)                  //逐个写入第一行数据
        {
               writedata(table1[i]);
            delay(20);
        }    
        writeinstruction(0x02);                 //回车
        writeinstruction(0x80 + 0x40);          //换行                    
        writechar(count);
    }

/*********************************************************
函数名:中断清零函数
调  用:按下button2;
参  数:无
返回值:无
结  果:计数值清零。具体指令在手册中查询。
/*********************************************************/
}

void Clear() interrupt 2 using 1
{
    int i;
    writeinstruction(0x01);
    count=0;                                     //count计数清零
    for (i = 0; i < 6; ++i)                     //逐个写入第一行数据
    {
           writedata(table1[i]);
        delay(20);
    }
    writeinstruction(0x02);                     //回车
    writeinstruction(0x80 + 0x40);                 //换行
    writechar(count);
}

> 
        #include"i2c.h"

/*******************************************************************************
* 函数名         : Delay10us()
* 函数功能           : 延时10us
* 输入           : 无
* 输出              : 无
*******************************************************************************/

void Delay10us()
{
    unsigned char a,b;
    for(b=1;b>0;b--)
        for(a=2;a>0;a--);

}
/*******************************************************************************
* 函数名         : I2cStart()
* 函数功能         : 起始信号:在SCL时钟信号在高电平期间SDA信号产生一个下降沿
* 输入           : 无
* 输出              : 无
* 备注           : 起始之后SDA和SCL都为0
*******************************************************************************/

void I2cStart()
{
    SDA=1;
    Delay10us();
    SCL=1;
    Delay10us();//建立时间是SDA保持时间>4.7us
    SDA=0;
    Delay10us();//保持时间是>4us
    SCL=0;            
    Delay10us();        
}
/*******************************************************************************
* 函数名         : I2cStop()
* 函数功能         : 终止信号:在SCL时钟信号高电平期间SDA信号产生一个上升沿
* 输入           : 无
* 输出              : 无
* 备注           : 结束之后保持SDA和SCL都为1;表示总线空闲
*******************************************************************************/

void I2cStop()
{
    SDA=0;
    Delay10us();
    SCL=1;
    Delay10us();//建立时间大于4.7us
    SDA=1;
    Delay10us();        
}
/*******************************************************************************
* 函数名         : I2cSendByte(unsigned char dat)
* 函数功能         : 通过I2C发送一个字节。在SCL时钟信号高电平期间,保持发送信号SDA保持稳定
* 输入           : num
* 输出              : 0或1。发送成功返回1,发送失败返回0
* 备注           : 发送完一个字节SCL=0,SDA=1
*******************************************************************************/

unsigned char I2cSendByte(unsigned char dat)
{
    unsigned char a=0,b=0;//最大255,一个机器周期为1us,最大延时255us。        
    for(a=0;a<8;a++)//要发送8位,从最高位开始
    {
        SDA=dat>>7;     //起始信号之后SCL=0,所以可以直接改变SDA信号
        dat=dat<<1;
        Delay10us();
        SCL=1;
        Delay10us();//建立时间>4.7us
        SCL=0;
        Delay10us();//时间大于4us        
    }
    SDA=1;
    Delay10us();
    SCL=1;
    while(SDA)//等待应答,也就是等待从设备把SDA拉低
    {
        b++;
        if(b>200)     //如果超过2000us没有应答发送失败,或者为非应答,表示接收结束
        {
            SCL=0;
            Delay10us();
            return 0;
        }
    }
    SCL=0;
    Delay10us();
     return 1;        
}
/*******************************************************************************
* 函数名         : I2cReadByte()
* 函数功能           : 使用I2c读取一个字节
* 输入           : 无
* 输出              : dat
* 备注           : 接收完一个字节SCL=0,SDA=1.
*******************************************************************************/

unsigned char I2cReadByte()
{
    unsigned char a=0,dat=0;
    SDA=1;            //起始和发送一个字节之后SCL都是0
    Delay10us();
    for(a=0;a<8;a++)//接收8个字节
    {
        SCL=1;
        Delay10us();
        dat<<=1;
        dat|=SDA;
        Delay10us();
        SCL=0;
        Delay10us();
    }
    return dat;        
}


/*******************************************************************************
* 函数名         : void At24c02Write(unsigned char addr,unsigned char dat)
* 函数功能           : 往24c02的一个地址写入一个数据
* 输入           : 无
* 输出              : 无
*******************************************************************************/

void At24c02Write(unsigned char addr,unsigned char dat)
{
    I2cStart();
    I2cSendByte(0xa0);//发送写器件地址
    I2cSendByte(addr);//发送要写入内存地址
    I2cSendByte(dat);    //发送数据
    I2cStop();
}
/*******************************************************************************
* 函数名         : unsigned char At24c02Read(unsigned char addr)
* 函数功能           : 读取24c02的一个地址的一个数据
* 输入           : 无
* 输出              : 无
*******************************************************************************/

unsigned char At24c02Read(unsigned char addr)
{
    unsigned char num;
    I2cStart();
    I2cSendByte(0xa0); //发送写器件地址
    I2cSendByte(addr); //发送要读取的地址
    I2cStart();
    I2cSendByte(0xa1); //发送读器件地址
    num=I2cReadByte(); //读取数据
    I2cStop();
    return num;    
}

#ifndef __I2C_H_
#define __I2C_H_

#include <reg52.h>

sbit SCL=P1^6;
sbit SDA=P1^7;

void I2cStart();
void I2cStop();
unsigned char I2cSendByte(unsigned char dat);
unsigned char I2cReadByte();
void At24c02Write(unsigned char addr,unsigned char dat);
unsigned char At24c02Read(unsigned char addr);

#endif
> 




> /********************************************************
* 文    件: 1602显示屏按键计数
* 作    者:
* 外部晶振: 11.0592MHZ
* 编译环境: Keil μVisio4
* 功    能: 数码管静态显示
* 注意事项:
********************************************************/
#include<reg52.h>
unsigned char code table1[] = "COUNT:";             //命名数组用来显示字符“COUNT:”
#include <intrins.h>
//定义类型,方便代码移植

typedef unsigned char UINT8;
typedef unsigned int  UINT16;
typedef unsigned long UINT32;

typedef char INT8;
typedef int  INT16;
typedef long INT32;
/*
定义寄存器 ISP
*/
sfr ISP_DATA = 0xe2;  // Flash数据寄存器
sfr ISP_ADDRH = 0xe3;// Flash高字节地址寄存器
sfr ISP_ADDRL = 0xe4;// Flash低字节地址寄存器
sfr ISP_CMD = 0xe5;// Flash命令模式寄存器
sfr ISP_TRIG = 0xe6;// Flash命令触发寄存器
sfr ISP_CONTR = 0xe7;// ISP/IAP 控制寄存器
                                  //全局变量初始化
sbit RW = P2 ^ 5;
sbit RS = P2 ^ 6;                                    //数据/命令选择接口
sbit EN = P2 ^ 7;                                    //使能接口
sbit k1 = P3 ^ 2;                            //按键1接在P3^2接口,对应外部中断0
sbit k2 = P3 ^ 3;                            //按键2接在P3^3接口,对应外部中断1

int count ;

//EEPROM使能
void EEPROMEnable(void)
{
    ISP_CONTR = 0x81;//使能并设置好等待时间 
}
//EEPROM禁用
void EEPROMDisable(void)
{
    ISP_CONTR = 0x00;//禁止EEPROM
    ISP_CMD = 0X00;//无ISP操作
    ISP_TRIG = 0X00;//清零
    ISP_ADDRH = 0X00;//清零
    ISP_ADDRL = 0X00;//清零
}

//eeprom 设置读写地址(相对地址)
void EEPROMSetAddress(UINT16 addr)
{
    addr += EEPROM_START_ADDRESS;//初始化地址
    ISP_ADDRH = (UINT8)(addr >> 8);//设置读写地址高字节
    ISP_ADDRL = (UINT8)addr;         //设置读写地址低字节
}

//EEPROM启动
void EEPROMStart(void)
{
    ISP_TRIG = 0x46;
    ISP_TRIG = 0xB9;
}

//EEPROM读取单个字节
UINT8 EEPROMReadByte(UINT16 addr)
{
    ISP_DATA = 0X00;
    ISP_CMD = 0X01;
    EEPROMEnable();
    EEPROMSetAddress(addr);
    EEPROMStart();
    DelayNus(10);//读取一个字节需要10us
    EEPROMDisable();
    return (ISP_DATA);
}


//EEPROM写入单个字节
UINT8 EEPROMWriteByte(UINT16 addr, UINT8 byte)
{

    EEPROMEnable();
    ISP_CMD = 0X02;
    EEPROMSetAddress(addr);
    ISP_DATA = byte;


    EEPROMStart();
    DelayNus(60);
    EEPROMDisable();

}
/********************************************************
函数名:毫秒级CPU延时函数
调  用:delay_ms(?);
参  数:1~65535(参数不可为0)
返回值:无
结  果:占用CPU方式延时与参数数值相同的毫秒时间
备  注:应用于1T单片机时i<600,应用于12T单片机时i<125
/********************************************************/
void delay(unsigned int a)
{
    unsigned int i;
    while (--a != 0) {
        for (i = 0; i < 125; i++);
    }
}
/********************************************************
函数名:写指令函数
调  用:writeinstruction(x);
参  数:无符号十六进制数0x??
返回值:无
结  果:将指令发送给1602控制器
/********************************************************/
void  writeinstruction(unsigned char x)
{
    RS = 0;
    EN = 0;
    RW = 0;
    P0 = x;
    delay(5);
    EN = 1;
    delay(5);
    EN = 0;
}
/*********************************************************
函数名:写数据函数
调  用:writedata(y);
参  数:无符号十六进制数0x??
返回值:无
结  果:将数据发送至1602内部的显示内存中
/*********************************************************/
void  writedata(unsigned char y)
{
    RS = 1;
    EN = 0;
    RW = 0;
    P0 = y;
    delay(5);
    EN = 1;
    delay(5);
    EN = 0;
}
/*********************************************************
函数名:写变量函数
调  用:writechar(z);
参  数:无符号十六进制数0x??
返回值:无
结  果:将数据发送至1602内部的显示内存中
备  注:不能将整数型变量count直接作为writedata的参数,需要将个、十、百位分别+48(0x30)转化成ascii码。
/*********************************************************/
void  writechar(unsigned char z)
{
    unsigned char A;
    unsigned char B;
    unsigned char C;
    RS = 1;
    EN = 0;
    RW = 0;
    A = z / 1 % 10;                                    //取个位
    B = z / 10 % 10;                                //取十位
    C = z / 100 % 10;                                //取百位
    writedata(C + 48);
    delay(5);
    writedata(B + 48);                            //分离出变量的每一位数,再将每一位加上0x30,
    delay(5);
    writedata(A + 48);                            //这样就变成了ASCII码了,再送给LCD才能显示出来变量的每一位的值。
    delay(5);
}
/*********************************************************
函数名:1602设置初始化函数
调  用:init1602();
参  数:无
返回值:无
结  果:初始化。具体指令在手册中查询。
/*********************************************************/
void  init1602()
{
    writeinstruction(0x38);                        //设置显示模式 指令码 00111000 => 0x38      
    delay(5);
    writeinstruction(0x0C);                        //[00001DCB]:D为显示开关,B为光标开关,C为光标闪烁开关。0x0C:开显示 不显示光标 不闪烁
    delay(5);
    writeinstruction(0x06);                        //写一个字符后地址指针加一
    delay(5);
    writeinstruction(0x01);                       //清屏       
    delay(5);
}
/**********************************************************
函数名:主函数main
/**********************************************************/
void  main()
{
    char i;
    init1602();
    writeinstruction(0x80);                      //设置数据地址指针从第一个开始
    delay(5);
    /* 初始化中断 */
    EA = 1;
    EX0 = 1;
    EX1 = 1;
    /* 写初始字符:COUNT:0 */
    for (i = 0; i < 6; ++i)                     //逐个写入第一行数据
    {
        writedata(table1[i]);
        delay(20);
    }
    writeinstruction(0x02);                     //第一行光标返回
    writeinstruction(0x80 + 0x40);                 //数据地址指针指向第二行第一个    
    count = EEPROMReadByte(0x2000);
    count |= (EEPROMReadByte(0x2001)<<8);
    writechar(count);
    /*砸瓦鲁多 */
    while (1);                                    //程序停在这里,等待中断到来
}
/*********************************************************
函数名:中断显示计数值函数
调  用:按下button1
参  数:无
返回值:无
结  果:显示当前计数值。具体指令在手册中查询。
/*********************************************************/
void Display()interrupt 0 using 1
{
    int i;
    if (k1 == 0)
    {
        delay(10);
        if (k1 == 0)
        {
            count++;                              //按键松开后,程序会运行到这里,计数值加1
            while (!k1);                     //如果按键没有松开,程序会停在这里
        }
        writeinstruction(0x01);                     //清屏   
        for (i = 0; i < 6; ++i)                  //逐个写入第一行数据
        {
            writedata(table1[i]);
            delay(20);
        }
        writeinstruction(0x02);                 //回车
        writeinstruction(0x80 + 0x40);          //换行                    
        writechar(count);
        EEPROMWriteByte(0x2000, count);
        EEPROMWriteByte(0x2001, count>>8);
    }
    /*********************************************************
    函数名:中断清零函数
    调  用:按下button2;
    参  数:无
    返回值:无
    结  果:计数值清零。具体指令在手册中查询。
    /*********************************************************/
}
void Clear() interrupt 2 using 1
{
    int i;
    writeinstruction(0x01);
    count = 0;                                     //count计数清零
    for (i = 0; i < 6; ++i)                     //逐个写入第一行数据
    {
        writedata(table1[i]);
        delay(20);
    }
    writeinstruction(0x02);                     //回车
    writeinstruction(0x80 + 0x40);                 //换行
    writechar(count);
}
    >
#include"i2c.h"
        /*******************************************************************************
        * 函数名         : Delay10us()
        * 函数功能           : 延时10us
        * 输入           : 无
        * 输出              : 无
        *******************************************************************************/
        void Delay10us()
    {
        unsigned char a, b;
        for (b = 1; b > 0; b--)
            for (a = 2; a > 0; a--);
    }
    /*******************************************************************************
    * 函数名         : I2cStart()
    * 函数功能         : 起始信号:在SCL时钟信号在高电平期间SDA信号产生一个下降沿
    * 输入           : 无
    * 输出              : 无
    * 备注           : 起始之后SDA和SCL都为0
    *******************************************************************************/
    void I2cStart()
    {
        SDA = 1;
        Delay10us();
        SCL = 1;
        Delay10us();//建立时间是SDA保持时间>4.7us
        SDA = 0;
        Delay10us();//保持时间是>4us
        SCL = 0;
        Delay10us();
    }
    /*******************************************************************************
    * 函数名         : I2cStop()
    * 函数功能         : 终止信号:在SCL时钟信号高电平期间SDA信号产生一个上升沿
    * 输入           : 无
    * 输出              : 无
    * 备注           : 结束之后保持SDA和SCL都为1;表示总线空闲
    *******************************************************************************/
    void I2cStop()
    {
        SDA = 0;
        Delay10us();
        SCL = 1;
        Delay10us();//建立时间大于4.7us
        SDA = 1;
        Delay10us();
    }
    /*******************************************************************************
    * 函数名         : I2cSendByte(unsigned char dat)
    * 函数功能         : 通过I2C发送一个字节。在SCL时钟信号高电平期间,保持发送信号SDA保持稳定
    * 输入           : num
    * 输出              : 0或1。发送成功返回1,发送失败返回0
    * 备注           : 发送完一个字节SCL=0,SDA=1
    *******************************************************************************/
    unsigned char I2cSendByte(unsigned char dat)
    {
        unsigned char a = 0, b = 0;//最大255,一个机器周期为1us,最大延时255us。        
        for (a = 0; a < 8; a++)//要发送8位,从最高位开始
        {
            SDA = dat >> 7;     //起始信号之后SCL=0,所以可以直接改变SDA信号
            dat = dat << 1;
            Delay10us();
            SCL = 1;
            Delay10us();//建立时间>4.7us
            SCL = 0;
            Delay10us();//时间大于4us        
        }
        SDA = 1;
        Delay10us();
        SCL = 1;
        while (SDA)//等待应答,也就是等待从设备把SDA拉低
        {
            b++;
            if (b > 200)     //如果超过2000us没有应答发送失败,或者为非应答,表示接收结束
            {
                SCL = 0;
                Delay10us();
                return 0;
            }
        }
        SCL = 0;
        Delay10us();
        return 1;
    }
    /*******************************************************************************
    * 函数名         : I2cReadByte()
    * 函数功能           : 使用I2c读取一个字节
    * 输入           : 无
    * 输出              : dat
    * 备注           : 接收完一个字节SCL=0,SDA=1.
    *******************************************************************************/
    unsigned char I2cReadByte()
    {
        unsigned char a = 0, dat = 0;
        SDA = 1;            //起始和发送一个字节之后SCL都是0
        Delay10us();
        for (a = 0; a < 8; a++)//接收8个字节
        {
            SCL = 1;
            Delay10us();
            dat <<= 1;
            dat |= SDA;
            Delay10us();
            SCL = 0;
            Delay10us();
        }
        return dat;
    }
    /*******************************************************************************
    * 函数名         : void At24c02Write(unsigned char addr,unsigned char dat)
    * 函数功能           : 往24c02的一个地址写入一个数据
    * 输入           : 无
    * 输出              : 无
    *******************************************************************************/
    void At24c02Write(unsigned char addr, unsigned char dat)
    {
        I2cStart();
        I2cSendByte(0xa0);//发送写器件地址
        I2cSendByte(addr);//发送要写入内存地址
        I2cSendByte(dat);    //发送数据
        I2cStop();
    }
    /*******************************************************************************
    * 函数名         : unsigned char At24c02Read(unsigned char addr)
    * 函数功能           : 读取24c02的一个地址的一个数据
    * 输入           : 无
    * 输出              : 无
    *******************************************************************************/
    unsigned char At24c02Read(unsigned char addr)
    {
        unsigned char num;
        I2cStart();
        I2cSendByte(0xa0); //发送写器件地址
        I2cSendByte(addr); //发送要读取的地址
        I2cStart();
        I2cSendByte(0xa1); //发送读器件地址
        num = I2cReadByte(); //读取数据
        I2cStop();
        return num;
    }



#ifndef __I2C_H_
#define __I2C_H_
#include <reg52.h>
    sbit SCL = P1 ^ 6;
    sbit SDA = P1 ^ 7;
    void I2cStart();
    void I2cStop();
    unsigned char I2cSendByte(unsigned char dat);
    unsigned char I2cReadByte();
    void At24c02Write(unsigned char addr, unsigned char dat);
    unsigned char At24c02Read(unsigned char addr);
#endif
        >