📄 readwriteat24c08.c
字号:
#include <reg51.h>
/***************************************************************************/
#define WriteDeviceAddress 0xAE //写器件地址
#define ReadDviceAddress 0xAF //读器件地址
/***************************************************************************/
sbit SCL=P1^0; //I2C时钟线SCL
sbit SDA=P1^1; //I2C数据线SDA
sbit STATUS=P3^1; //程序运行状态
/***************************************************************************/
void Delays(unsigned int number); //延时子函数
void StartI2C(); //起始信号子函数
void StopI2C(); //终止信号子函数
void AckI2C(); //发送应答位子函数
void NoAckI2C(); //发送非应答位子函数
bit TestAckI2C(); //应答位检查子函数
bit Write8BitI2C(unsigned char input); //单字节写子函数
unsigned char Read8BitI2C(); //单字节读子函数
//多字节写子函数
void WriteI2C(unsigned char *Wdata,unsigned char RomAddress,unsigned char number);
//多字节读子函数
void ReadI2C(unsigned char *RamAddress,unsigned char RomAddress,unsigned char bytes);
/***************************************************************************/
void main()
{
unsigned char writeData[10]={0xC0,0x10,0x34,0x12,0x11,0x15,0x22,0x01,0xAE,0xAF};
//需要写的10个字节USB数据ID
unsigned char readData[10]; //用于存读入的10个字节数据
unsigned char *writeadd; //写数据指针操作
unsigned char *readadd; //读数据指针操作
unsigned char i;
unsigned char ok=0;
STATUS=0;
writeadd=writeData; //写地址映射
readadd=readData; //读地址映射
WriteI2C(writeadd,0x00,8); //写数据
ReadI2C(readadd,0x00,8); //读数据
for(i=0;i<8;i++) //判断每个字节读写是否一致
{
if(writeData[i]==readData[i])
{
ok++;
}
}
if(ok==8)
STATUS=1; //当读写一致时,P3.7输出高电平
else
STATUS=0; //当读写不一致时,P3.7输出低电平
while(1)
{
}
}
void Delays(unsigned int number) //延时子程序
{
unsigned char temp;
for(;number!=0;number--) //循环
{
for(temp=0;temp<100;temp++) //空循环
{
}
}
}
void StartI2C() //起始信号子程序
{
SDA=1;
Delays(1); //延时,用于满足传输速率要求
SCL=1;
Delays(1);
SDA=0;
Delays(1);
SCL=0;
Delays(1);
}
void StopI2C() //终止信号子程序
{
SCL=0;
Delays(1); //延时,用于满足传输速率要求
SDA=0;
Delays(1);
SCL=1;
Delays(1);
SDA=1;
Delays(1);
}
void AckI2C() //发送应答位子程序
{
SDA=0;
Delays(1);
SCL=1;
Delays(1);
SCL=0;
Delays(1);
SDA=1;
Delays(1);
}
void NoAckI2C() //发送非应答位子程序
{
SDA=1;
Delays(1);
SCL=1;
Delays(1);
SCL=0;
Delays(1);
}
bit TestAckI2C() //应答位检查子程序
{
bit ErrorBit;
SDA=1;
Delays(1);
SCL=1;
Delays(1);
ErrorBit=SDA; //读入SDA上的应答状态
Delays(1);
SCL=0;
Delays(1);
return(ErrorBit); //返回应答状态,0为应答信号,1为非应答信号
}
bit Write8BitI2C(unsigned char input) // input为待发送的数据
{
unsigned char temp;
for(temp=8;temp!=0;temp--) //循环移位,逐位发送数据
{
SDA=(bit)(input&0x80); //取数据的最高位
Delays(1);
SCL=1;
Delays(1);
SCL=0;
Delays(1);
input=input<<1; //左移一位
}
return 1;
}
unsigned char Read8BitI2C() //读一个字节数据子程序
{
unsigned char temp,rbyte;
rbyte=0; //初始化
for(temp=0;temp<8;temp++) //循环,逐位读入字节数据
{
SCL=1;
Delays(1);
rbyte=rbyte<<1; //左移一位
Delays(1);
rbyte=rbyte|((unsigned char)(SDA)); //数据线上的数据存入rbyte的最低位
SCL=0;
Delays(1);
}
return(rbyte); //返回读入的数据
}
void WriteI2C(unsigned char *Wdata,unsigned char RomAddress,unsigned char number)
{ //写n个字节数据子程序
int nu;
StartI2C(); //启动
Write8BitI2C(WriteDeviceAddress); //写寻址地址
TestAckI2C(); //应答检查
Write8BitI2C(RomAddress); //写入I2C器件内部的数据存储首地址
TestAckI2C(); //应答检查
for(nu=number;nu!=0;nu--) //循环,逐个字节发送
{
Write8BitI2C(*Wdata); //单字节写
TestAckI2C(); //应答检查
Wdata++; //指针增加,指向下一个数据
}
StopI2C(); //停止
}
void ReadI2C(unsigned char *RamAddress,unsigned char RomAddress,unsigned char bytes)
{ //读n个字节数据子程序
StartI2C(); //启动
Write8BitI2C(WriteDeviceAddress); //写写器件的寻址地址
TestAckI2C(); //应答检查
Write8BitI2C(RomAddress); //写I2C器件内部数据的读取首地址
TestAckI2C(); //应答检查
StartI2C(); //重新启动
Write8BitI2C(ReadDviceAddress); //写读器件的寻址地址
TestAckI2C(); //应答检查
while(bytes!=1) //循环读入字节数据
{
*RamAddress=Read8BitI2C(); //读入一个字节
AckI2C(); //应答
RamAddress++; //地址指针递增
bytes--; //待读入数据个数递减
}
*RamAddress=Read8BitI2C(); //读入最后一个字节数据
NoAckI2C(); //非应答
StopI2C(); //停止
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -