📄 readwriteat24c08.c
字号:
#include <reg51.h>
// 对AT24C08的读、写
// extern void DelayMs(unsigned int);
// extern void ReadI2C(unsigned char *RamAddress,unsigned char RomAddress,unsigned char bytes);
// extern void WriteI2C(unsigned char *RamAddress,unsigned char RomAddress,unsigned char bytes);
/***************************************************************************/
#define WriteDeviceAddress 0xa0 //写器件地址
#define ReadDviceAddress 0xa1 //读器件地址
/***************************************************************************/
sbit SCL=P1^0; //I2C时钟线SCL
sbit SDA=P1^1; //I2C数据线SDA
sbit DOG=P3^7; //程序运行标志及数据读写正确标志
/***************************************************************************/
void DelayMs(unsigned int number) //延时子程序,延时大小取决于工作频率和number值
{
unsigned char temp;
for(;number!=0;number--,DOG=!DOG) //循环,DOG用于输出状态信号
{
for(temp=112;temp!=0;temp--) //空循环
{
}
}
}
/***************************************************************************/
void Start() //起始信号子程序
{
SDA=1;
DelayMs(1); //延时
SCL=1;
DelayMs(1);
SDA=0;
DelayMs(1);
SCL=0;
DelayMs(1);
}
/***************************************************************************/
void Stop() //终止信号子程序
{
SCL=0;
DelayMs(1);
SDA=0;
DelayMs(1);
SCL=1;
DelayMs(1);
SDA=1;
DelayMs(1);
}
/***************************************************************************/
void Ack() //发送应答位子程序
{
SDA=0;
DelayMs(1);
SCL=1;
DelayMs(1);
SCL=0;
DelayMs(1);
SDA=1;
DelayMs(1);
}
/***************************************************************************/
void NoAck() //发送非应答位子程序
{
SDA=1;
DelayMs(1);
SCL=1;
DelayMs(1);
SCL=0;
DelayMs(1);
}
/***************************************************************************/
bit TestAck() //应答位检查子程序
{
bit ErrorBit;
SDA=1;
DelayMs(1);
SCL=1;
DelayMs(1);
ErrorBit=SDA; //读入数据线上的应答状态
DelayMs(1);
SCL=0;
DelayMs(1);
return(ErrorBit); //返回应答状态,0为正常应答信号,1为非应答信号
}
/***************************************************************************/
bit Write8Bit(unsigned char input) //写一个字节数据子程序
{ //input为待发送的数据
unsigned char temp;
for(temp=8;temp!=0;temp--) //循环移位,逐位发送数据
{
SDA=(bit)(input&0x80); //取数据的最高位
DelayMs(1);
SCL=1;
DelayMs(1);
SCL=0;
DelayMs(1);
input=input<<1; //左移一位
}
return 1;
}
/***************************************************************************/
void WriteI2C(unsigned char *Wdata,unsigned char RomAddress,unsigned char number)
{ //写n个字节数据子程序
Start(); //启动
Write8Bit(WriteDeviceAddress); //写写器件的寻址地址
TestAck(); //应答检查
Write8Bit(RomAddress); //写入I2C器件内部的数据存储首地址
TestAck(); //应答检查
for(;number!=0;number--) //循环,逐个字节发送
{
Write8Bit(*Wdata); //写一个字节
TestAck(); //应答检查
Wdata++; //指针增加,指向下一个数据
}
Stop(); //停止
DelayMs(10);
}
/***************************************************************************/
unsigned char Read8Bit() //读一个字节数据子程序
{
unsigned char temp,rbyte=0;
for(temp=8;temp!=0;temp--) //循环,逐位读入字节数据
{
SCL=1;
DelayMs(1);
rbyte=rbyte<<1; //左移一位
DelayMs(1);
rbyte=rbyte|((unsigned char)(SDA)); //数据线SDA上的数据存入rbyte的最低位
SCL=0;
DelayMs(1);
}
return(rbyte); //返回读入的字节数据
}
/***************************************************************************/
void ReadI2C(unsigned char *RamAddress,unsigned char RomAddress,unsigned char bytes)
{ //读n个字节数据子程序
Start(); //启动
Write8Bit(WriteDeviceAddress); //写写器件的寻址地址
TestAck(); //应答检查
Write8Bit(RomAddress); //写I2C器件内部数据的读取首地址
TestAck(); //应答检查
Start(); //重新启动
Write8Bit(ReadDviceAddress); //写读器件的寻址地址
TestAck(); //应答检查
while(bytes!=1) //循环读入字节数据
{
*RamAddress=Read8Bit(); //读入一个字节
Ack(); //应答
RamAddress++; //地址指针递增
bytes--; //待读入数据个数递减
}
*RamAddress=Read8Bit(); //读入最后一个字节数据
NoAck(); //非应答
Stop(); //停止
}
void main()
{
unsigned char writeByte[8]={0xC0,0x34,0x12,0x11,0x22,0x01,0x00,0x00};
//需要写的8个字节USB数据ID
unsigned char readByte[8]; //用于存读入的8个字节数据
unsigned char *addw; //写数据指针操作
unsigned char *addr; //读数据指针操作
unsigned char i;
unsigned char ok=0;
bit write=1; //读写标志
DOG=0;
while(1)
{
if(write==1) //当write==1时,执行写和读操作
{
addw=writeByte; //写地址映射
addr=readByte; //读地址映射
WriteI2C(addw,0x00,8); //写数据
ReadI2C(addr,0x00,8); //读数据
for(i=0;i<8;i++) //判断每个字节读写是否一致
{
if(writeByte[i]==readByte[i])
{
ok++;
}
}
if(ok==8)
{
DOG=1; //当读写一致时,P3.7输出高电平
}
else
{
DOG=0; //当读写不一致时,P3.7输出低电平
}
write=0; //置write==0,读写完毕
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -