📄 write_read.c
字号:
#include "reg52.h"
// #include "def.h"
#include "absacc.h"
#include "eep16.h"
#define uchar unsigned char
sbit a16sda=P1^6;
sbit a16scl=P1^7;
#define CAT24C16Slave 0xA0
/****************************************\
|微秒延时 |
\****************************************/
void delaytime_10us(uchar times)
{
int i;
do{
for(i=0;i<5;i++);}
while(times--);
}
/********************************************************\
| 函数名 AT24C16Start |
| 函数功能 向I2C总线发送一个开始信号 |
\********************************************************/
void CAT24C16Start(void)
{
a16sda=1;
delaytime_10us(1);
a16scl=1;
delaytime_10us(1);
a16sda=0;
delaytime_10us(1);
a16scl=0 ;
delaytime_10us(1);
}
/********************************************************\
| 函数名 AT24C16Stop |
| 函数功能 向I2C总线发送一个停止信号 |
\********************************************************/
void CAT24C16Stop(void)
{
a16sda=0;
delaytime_10us(1);
a16scl=1;
delaytime_10us(1);
a16sda=1;
delaytime_10us(1);
a16scl=0;
delaytime_10us(1);
}
/********************************************************\
| 函数名 AT24C02Clock |
| 函数功能 从I2C总线接受一BIT数据 |
| 参数 无 |
| 返回值 unsigned char :从I2C总线接受的数据 |
\********************************************************/
unsigned char CAT24C16Clock(void)
{
unsigned char SDA_value;
delaytime_10us(1);
a16scl=1;
delaytime_10us(1);
SDA_value=a16sda ;
delaytime_10us(1);
a16scl=0;
delaytime_10us(1);
return(SDA_value);
}
/********************************************************\
| 函数名 AT24C02Ack |
| 函数功能 向I2C总线发送一个数据接受完成信号 |
\********************************************************/
void CAT24C16Ack()
{
a16scl=1;
delaytime_10us(1);
a16sda=0;
delaytime_10us(1);
a16scl=0;
}
/*******************************************************\
| 函数名 AT24C02Nack |
| 函数功能 从器件应答信号是否产生 |
\*******************************************************/
bit CAT24C16Nack()
{
a16sda=1;
if (CAT24C16Clock()==0)
{
return 1;
}
else
{
return 0;
}
}
/*******************************************************\
| 函数名 AT24C16OutByte |
| 函数功能 向I2C总线发送一个字节的数据 |
\*******************************************************/
void CAT24C16OutByte(uchar byte)
{
char count;
for (count=0; count<8; count++)
{
if ((byte & 0x80)==0)
{
a16sda=0;
}
else
{
a16sda=1;
}
byte <<=1;
a16scl=1;
delaytime_10us(1);
delaytime_10us(1);
a16scl=0;
delaytime_10us(1);
}
}
/*******************************************************\
| 函数名 AT24C16GetByte |
| 函数功能 从I2C总线接受一个字节的数据 |
\*******************************************************/
uchar CAT24C16GetByte()
{
uchar byte=0,temp,count;
for (count=0; count<8; count++)
{
byte <<=1;
a16sda=1;
temp = CAT24C16Clock();
if (temp == 1)
{
byte=byte|0x01;
}
}
return(byte);
}
/*******************************************************\
| 函数名 CAT24C16DummyWrite |
| 函数功能 向I2C总线发送开始信号和从地址 |
\*******************************************************/
bit CAT24C16DummyWrite(uchar addr)
{
CAT24C16Start();
CAT24C16OutByte(CAT24C16Slave);
if (CAT24C16Nack()==0)
{
return 0;
}
CAT24C16OutByte(addr);
if (CAT24C16Nack()==0)
{
return 0;
}
return 1;
}
/*******************************************************\
| 函数名 AT24C02Read |
| 函数功能 从AT24C02读取数据 |
| 参数 data : 读取数据的缓冲区 |
| addr : 准备读去的AT24C02扁移地址 |
| leng : 准备读去的数据长度(字节) |
| 返回值 0 : 读数据成功 |
| 1 : 读数据失败 |
\*******************************************************/
char CAT24C16Read(uchar * _data,uchar addr,uchar leng)
{
unsigned char n;
if (CAT24C16DummyWrite(addr)==0)
{
return 0;
}
CAT24C16Start();
CAT24C16OutByte(CAT24C16Slave|0x01);
if (CAT24C16Nack()==0)
{
return 0;
}
for (n=0;n<leng-1; n++)
{
_data[n] = CAT24C16GetByte();
CAT24C16Ack();
}
_data[leng - 1]=CAT24C16GetByte();
CAT24C16Stop();
delaytime_10us(20);
return 1;
}
/*******************************************************\
| 函数名 AT24C02Write |
| 函数功能 从AT24C02读取数据 |
| 参数 data : 待写数据的缓冲区 |
| addr : 待写的AT24C02扁移地址 |
| leng : 待写数据长度(字节
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -