新闻中心

EEPW首页 > 嵌入式系统 > 设计应用 > AT24C02EEPROM芯片《实现对其读和写》

AT24C02EEPROM芯片《实现对其读和写》

作者: 时间:2016-12-01 来源:网络 收藏
程序功能: 驱动 AT24C02EEPROM芯片,实现对其的读和写。让其保存计时器的输出sec值。

并可以保证在系统掉电后,重新加电时,可以读出上次掉电瞬间所保存的值。

本文引用地址:https://www.eepw.com.cn/article/201612/324253.htm

演示方法: 下载程序后,数码管开始显示自加数值。片刻后,关闭学习板

电源,然后打开,会发现数码管上显示的数值是断电瞬间所显

示的数值。(也即由AT24C02EEPROM芯片所记忆的数值。)

//试验AT24C02EEPROM芯片程序

#include

#include

#define uint unsigned int

#define uchar unsigned char

unsigned char sec,num; //定义计数值,每过1秒,sec加1

unsigned int times; //定时中断次数

bit write=0; //写24C02的标志;

sbit sda=P2^0; //IO口定义

sbit scl=P2^1;

sbit dula=P2^6;

sbit wela=P2^7;

unsigned char j,k;

void delay(unsigned char i) //延时程序

{

for(j=i;j>0;j--)

for(k=125;k>0;k--);

}

uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d, //数码管编码

0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};

void nop()

{

_nop_();

_nop_();

}

void display(uchar a,uchar b) //显示程序

{

dula=0;

P0=table[a]; //显示百位

dula=1;

dula=0;

wela=0;

P0=0x7e;

wela=1;

wela=0;

delay(5);

dula=0;

P0=table[b]; //显示十位

dula=1;

dula=0;

wela=0;

P0=0x7d;

wela=1;

wela=0;

delay(5);

}

/////////24C02读写驱动程序////////////////////

void delay1(unsigned char m)

{ unsigned int n;

for(n=0;n

}

void init() //24c02初始化子程序

{

scl=1;

nop();

sda=1;

nop();

}

void start() //启动I2C总线

{

sda=1;

nop();

scl=1;

nop();

sda=0;

nop();

scl=0;

nop();

}

void stop() //停止I2C总线

{

sda=0;

nop();

scl=1;

nop();

sda=1;

nop();

}

void writebyte(unsigned char j) //写一个字节

{

unsigned char i,temp;

temp=j;

for (i=0;i<8;i++)

{

temp=temp<<1;

scl=0;

nop();

sda=CY; //temp左移时,移出的值放入了CY中

nop();

scl=1; //待sda线上的数据稳定后,将scl拉高

nop();

}

scl=0;

nop();

sda=1;

nop();

}


上一页 1 2 下一页

关键词: AT24C02EEPROM读和

评论


技术专区

关闭