找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3389|回复: 5
打印 上一主题 下一主题
收起左侧

关于24c02如何存储并读取年月日时分秒?

[复制链接]
跳转到指定楼层
楼主
ID:479896 发表于 2019-2-24 10:53 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
如何把  年的前两位   和年的后两位     月  日   时  分  秒     存入24c02中      并 把他们读出来

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:123289 发表于 2019-2-25 08:06 | 只看该作者
24c02只是存储器,它并不会区分年月日时分秒,只由你自己才能解决这个问题:
方法:将你认为的年月日时分秒,存储于24c02任意位置都可以,但你要记住并认定这些位置,这样当你再读回这些位置的数据时,现将它们看成是年月日时分秒就行了。
回复

使用道具 举报

板凳
ID:164602 发表于 2019-2-25 09:11 | 只看该作者
先学会怎么控制24c02,就很容易了。给一个24c02的例程
主程序:
/*******************************************************************************
* 实验名                          : EEPROM实验
* 使用的IO            :
* 实验效果      : 按K1保存显示的数据,按K2读取上次保存的数据,按K3显示数据加一,
*按K4显示数据清零。
*        注意                                        :由于P3.2口跟红外线共用,所以做按键实验时为了不让红外线影响实验效果,最好把红外线先
*取下来。
*
*********************************************************************************/
#include<reg51.h>
#include"i2c.h"
//数码管IO
#define DIG        P0
sbit LSA=P2^2;
sbit LSB=P2^3;
sbit LSC=P2^4;
//按键IO
sbit K1=P3^1;
sbit K2=P3^0;
sbit K3=P3^2;
sbit K4=P3^3;
void At24c02Write(unsigned char ,unsigned char );
unsigned char At24c02Read(unsigned char );
void Delay1ms();
void Timer0Configuration();
unsigned char code DIG_CODE[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
unsigned char Num=0;
unsigned char disp[8]={0x3f,0x3f,0x3f,0x3f,0x3f,0x3f,0x3f,0x3f};
/*******************************************************************************
* 函数名         : main
* 函数功能                   : 主函数
* 输入           : 无
* 输出                  : 无
*******************************************************************************/

void main()
{
        unsigned int num0=0,num1=0,n;
        Timer0Configuration();
        while(1)
        {
                if(K1==0)
                {
                        Delay1ms();
                        if(K1==0)
                                At24c02Write(500,num0);
                        while((n<200)&&(K3==0))
                        {
                                n++;
                                Delay1ms();       
                        }                       
                        n=0;
                        n=0;
                }
               
                if(K2==0)
                {
                        Delay1ms();
                        if(K2==0)
                                num0=At24c02Read(500);
                        while((n<200)&&(K3==0))
                        {
                                n++;
                                Delay1ms();       
                        }                       
                        n=0;
                }                       
                if(K3==0)
                {
                        Delay1ms();
                        if(K3==0)
                                num0++;
                        while((n<200)&&(K3==0))
                        {
                                n++;
                                Delay1ms();       
                        }                       
                        n=0;
                        if(num0==256)
                                num0=0;
                }
                       
                if(K4==0)
                {
                        Delay1ms();
                        if(K4==0)
                                num0=0;
                        while((n<200)&&(K3==0))
                        {
                                n++;
                                Delay1ms();       
                        }                       
                        n=0;
                }                       
                disp[0]=DIG_CODE[num1/1000];//千位
                disp[1]=DIG_CODE[num1%1000/100];//百位
                disp[2]=DIG_CODE[num1%1000%100/10];//十位
                disp[3]=DIG_CODE[num1%1000%100%10];//个位
                disp[4]=DIG_CODE[num0/1000];//千位
                disp[5]=DIG_CODE[num0%1000/100];//百位
                disp[6]=DIG_CODE[num0%1000%100/10];//个位
                disp[7]=DIG_CODE[num0%1000%100%10];       
        }
}                                                                         
/*******************************************************************************
* 函数名         : Timer0Configuration()
* 函数功能                   : 设置计时器
* 输入           : 无
* 输出                  : 无
*******************************************************************************/

void Timer0Configuration()
{
        TMOD=0X02;//选择为定时器模式,工作方式2,仅用TRX打开启动。

        TH0=0X9C;        //给定时器赋初值,定时100us
        TL0=0X9C;       
        ET0=1;//打开定时器0中断允许
        EA=1;//打开总中断
        TR0=1;//打开定时器               
}
/*******************************************************************************
* 函数名         : Delay1ms()
* 函数功能                   : 延时
* 输入           : 无
* 输出                  : 无
*******************************************************************************/

void Delay1ms()   //误差 0us
{
    unsigned char a,b,c;
    for(c=1;c>0;c--)
        for(b=142;b>0;b--)
            for(a=2;a>0;a--);
}
/*******************************************************************************
* 函数名         : 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;       
}
/*******************************************************************************
* 函数名         : DigDisplay() interrupt 1
* 函数功能                   : 中断数码管显示
* 输入           : 无
* 输出                  : 无
*******************************************************************************/

void DigDisplay() interrupt 1
{
//定时器在工作方式二会自动重装初,所以不用在赋值。
//        TH0=0X9c;//给定时器赋初值,定时1ms
//        TL0=0X00;               
        DIG=0; //消隐
        switch(Num)         //位选,选择点亮的数码管,
        {
                case(7):
                        LSA=0;LSB=0;LSC=0; break;
                case(6):
                        LSA=1;LSB=0;LSC=0; break;
                case(5):
                        LSA=0;LSB=1;LSC=0; break;
                case(4):
                        LSA=1;LSB=1;LSC=0; break;
                case(3):
                        LSA=0;LSB=0;LSC=1; break;
                case(2):
                        LSA=1;LSB=0;LSC=1; break;
                case(1):
                        LSA=0;LSB=1;LSC=1; break;
                case(0):
                        LSA=1;LSB=1;LSC=1; break;       
        }
        DIG=disp[Num]; //段选,选择显示的数字。
        Num++;
        if(Num>7)
                Num=0;
}

I2c.h:
#ifndef __I2C_H_
#define __I2C_H_

#include<reg51.h>
sbit SCL=P2^1;
sbit SDA=P2^0;

void I2cStart();
void I2cStop();
unsigned char I2cSendByte(unsigned char dat);
unsigned char I2cReadByte();
void I2cReadRespon();
#endif

I2C.C:
#include"i2c.h"
/*******************************************************************************
* 函数名         : Delay1us()
* 函数功能                   : 延时
* 输入           : 无
* 输出                  : 无
*******************************************************************************/

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 num)
* 函数功能                   : 通过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;               
}
/*******************************************************************************
* 函数名         : I2cReadRespon()
* 函数功能                   : 接收完一个字节之后产生应答,以便接着接收下一个字节
* 输入           : 无
* 输出                  : 无
* 备注           : 接收完一个字节SCL=0
*******************************************************************************/
//void I2cReadRespon()
//{
//        SDA=0;
//        Delay10us();
//        SDA=1;
//        Delay10us();
//}
回复

使用道具 举报

地板
ID:158375 发表于 2019-2-25 09:43 | 只看该作者
所有需要的数据,存入相对应的地址,需要时在相应地址读出就可以了;这个地址需要你的程序记住哦。
回复

使用道具 举报

5#
ID:478073 发表于 2019-2-25 10:48 | 只看该作者
期实很简单,玩转24C02多字节读写就能搞定,将你年月日时分秒分别存到不同的存储单元(0-255).不过你这个实用意义不大,DS1302自带存储。多字节存储可看看这些
单片机LCD12864仿真与2402关机多种状态保存源码http://www.51hei.com/bbs/dpj-149174-1.html
单片机24C02存储多种花样流水灯仿真电路http://www.51hei.com/bbs/dpj-150080-1.html

评分

参与人数 2黑币 +24 收起 理由
Sawardeakar + 10 回帖助人的奖励!
jinsheng7533967 + 14

查看全部评分

回复

使用道具 举报

6#
ID:479896 发表于 2019-2-25 10:58 | 只看该作者
谢谢   帮助
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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