一、原理图
二、器件地址和写字节
器件地址
AT24C02为2k的,对应下面的地址(A0~2均接地为0),读这个存储器最低位为1,写为0(读地址0xa1,写地址0xa0)
写字节
流程:开始函数-》发送器件地址-》等待应答-》发送将数据地址(即要将数据写入在哪)-》等待应答-》发送要写入的数据-》等待应答-》停止函数
随机读
流程:开始-》发送器件地址(写0xa0)-》等待应答-》字节地址(指定要读哪里的数据)-》等待应答-》开始-》发送器件地址(0xa1)-》等待应答-》读数据-》停止
三、注意事项
补充蓝桥杯官网提供的驱动代码
写入时间需要时间,要延时
四、具体代码
练习内容:往AT24C02写入数据,读取并用数码管显示数据
#include "reg52.h"
#include "iic.h"
#include "smg.h"
unsigned char dat;
code unsigned char Seg_Table[] = {
0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,
0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};
void Delayxms(unsigned int t) //@12.000MHz
{
while(t--)
{
unsigned char data i, j;
i = 12;
j = 169;
do
{
while (--j);
} while (--i);
}
}
void initsystem()
{
hc573(4);
P0=0xff;
hc573(5);
P0=0x00;
AT24C02_write(0x01,222);
Delayxms(10);
}
void smgshow()
{
smgshow_bit(0,0xff);
Delayxms(2);
smgshow_bit(1,0xff);
Delayxms(2);
smgshow_bit(2,0xff);
Delayxms(2);
smgshow_bit(3,0xff);
Delayxms(2);
smgshow_bit(4,0xff);
Delayxms(2);
smgshow_bit(5,Seg_Table[dat/100]);
Delayxms(2);
smgshow_bit(6,Seg_Table[dat/10%10]);
Delayxms(2);
smgshow_bit(7,Seg_Table[dat%10]);
Delayxms(2);
smgshow_all(0xff);
}
void main()
{
initsystem();
while(1)
{
dat=AT24C02_read(0x01);
smgshow();
}
}