AT24C02断电保存,数据写入(rct6)

AT24C02的存储容量为2K bit,内容分成32页,每页8Byte,共256Byte,操作时有两种寻址方式:芯片寻址和片内子地址寻址。
(1)芯片寻址:AT24C02的芯片地址为1010,其地址控制字格式为1010A2A1A0R/W。其中A2,A1,A0可编程地址选择位。A2,A1,A0引脚接高、低电平后得到确定的三位编码,与1010形成7位编码,即为该器件的地址码。R/W为芯片读写控制位,该位为0,表示芯片进行写操作。
(2)当R/W 位为 0 时,表示写方向,所以加上 7 位地址,其值为“ 0xA0”,常称该值为 I2C 设备的“写地址”;当 R/W 位为 1 时,表示读方向,加上 7 位地址,其值为“ 0xA1”,常称该值为“读地址”。
所以AT24C02的地址是0~255。

//在AT24CXX里面的指定地址开始写入指定个数的数据
//WriteAddr:开始写入的地址,对24c02为0~255
//pBuffer:数据数组首地址
//NumToWrite:要写入数据的个数
void AT24CXX_Write(u16 WriteAddr,u8 *pBuffer,u16 NumToWrite)
{
	while(NumToWrite--)
	{
		AT24CXX_WriteOneByte(WriteAddr,*pBuffer);
		WriteAddr++;
		pBuffer++;
	}
}
 
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
AT24C08 E2PROM存储实验例程C51单片机KEIL工程源码文件4个合集: 24C08多花样流水等实验 24C08断电记忆功能的00-99计时器 将按键次数写入AT24C08,再读出并用1602LCD显示 将数据0x0f写入AT24C08再读出送P0口显示 /*****************************************************************/ void write_24c08(uchar address,uchar shuju) //给指定地址中写入数据 { start(); write_byte(0xa0); // 最低位为0写,1读 respons(); write_byte(address); respons(); write_byte(shuju); respons(); stop(); } /*****************************************************************/ uchar read_24c08(uchar address) //从24c08指定地址中读出数据 { uchar date; start(); write_byte(0xa0); respons(); write_byte(address); respons(); start(); write_byte(0xa1); respons(); date=read_byte(); stop(); return date; } /////////////24C02读写驱动程序完///////////////////// /***********************************************************/ void LEDshow() //LED显示函数 { P0=table[sec/10]; shiwei=0; delay1(40); shiwei=1; P0=table[sec%10]; gewei=0; delay1(40); gewei=1; } /***********************************************************/ void main(void) { TMOD=0x01; //定时器工作在方式1 ET0=1; EA=1; init_24c08(); //初始化24C08 sec=read_24c08(2);//读出保存数据赋于sec TH0=(65536-50000)/256; //对TH0 TL0赋值 TL0=(65536-50000)%256; //使定时器0.05秒中断一次 TR0=1; //开始计时 while(1) { LEDshow(); if(write==1) //判断计时器是否计时一秒 { write=0; //清零 write_24c08(2,sec); //在24c08的地址2中写入数据sec } if(K5==0){ delay1(10); if(K5==0){ sec=0; } } } } /**************************************************************/ void t0(void) interrupt 1 using 0 //定时中断服务函数 { TH0=(65536-50000)/256; //对TH0 TL0赋值 TL0=(65536-50000)%256; //重装计数初值 count++; //每过50ms tcnt加一 if(count==20) //计满20次(1秒)时 { count=0; //重新再计 sec++; write=1; //1秒写一次24C08 if(sec==100) //定时100秒,在从零开始计时 {sec=0;} } }
对于向AT24C02写入多个数据的串行口通信,可以按照以下步骤进行: 1. 先发送起始信号(Start Bit)和器件地址(Device Address)加写入标志位(Write Bit)。 2. 发送要写入数据地址(Memory Address),可以是单个字节或多个字节。 3. 向AT24C02发送要写入数据,可以是单个字节或多个字节。 4. 发送停止信号(Stop Bit)。 下面是一个示例代码,假设要向AT24C02的地址0x50写入数据0x12、0x34、0x56: ```c #include <reg51.h> #define I2C_SCL P1_7 #define I2C_SDA P1_6 void delay_us(unsigned int us) { while (us--); } void i2c_start(void) { I2C_SDA = 1; I2C_SCL = 1; delay_us(2); I2C_SDA = 0; delay_us(2); I2C_SCL = 0; } void i2c_stop(void) { I2C_SDA = 0; I2C_SCL = 1; delay_us(2); I2C_SDA = 1; delay_us(2); } void i2c_write_byte(unsigned char dat) { unsigned char i; for (i = 0; i < 8; i++) { I2C_SCL = 0; delay_us(2); I2C_SDA = (dat & 0x80) ? 1 : 0; dat <<= 1; delay_us(2); I2C_SCL = 1; delay_us(2); } I2C_SCL = 0; I2C_SDA = 1; delay_us(2); I2C_SCL = 1; delay_us(2); I2C_SCL = 0; } void i2c_write(unsigned char addr, unsigned char memaddr, unsigned char *buf, unsigned char len) { unsigned char i; i2c_start(); i2c_write_byte(addr); i2c_write_byte(memaddr); for (i = 0; i < len; i++) { i2c_write_byte(buf[i]); } i2c_stop(); } void main() { unsigned char buf[3] = {0x12, 0x34, 0x56}; i2c_write(0xA0, 0x00, buf, 3); while (1); } ``` 其中,i2c_start()和i2c_stop()函数用于发送起始信号和停止信号,i2c_write_byte()函数用于向AT24C02写入单个字节,i2c_write()函数用于向AT24C02连续写入多个字节。在主函数中,先定义了要写入数据buf数组,然后调用i2c_write()函数写入数据。注意,0xA0是AT24C02的器件地址,0x00是要写入数据地址。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值