📄 24cxx.c
字号:
/*连续读不好使*/
#include <reg51.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
const uchar WRITE = 0xa0;
const uchar READ = 0xa1;
sbit SCL = P1^6;
sbit SDA = P1^7;
sbit key = P1^2;
/************函数原型*******************/
void Delays(uchar n);
void Start_I2C(void);
void Stop_I2C(void);
bit Ask_I2C(void);
void Write_byte(uchar c_data);
bit Write_24CXX(uchar addr,uchar c_data);
bit Write_24CXXs(uchar addr,uchar *pr, uchar n);
uchar Read_byte(void);
uchar Read_24CXX(uchar addr);
void ASK(void);
void No_Ask(void);
void Read_24CXXs(uchar addr,uchar *pr, uchar n);
///////////////////////////////////
void main(void)
{
uchar arr[12] = {1,2,3,4,5,6,7,8,9,10,11,12};
uchar arr_1[8] = {0};
uchar i = 0;
/*写一个字节*/
//while(!Write_24CXX(0x12,0x10)); //好使 //有监测
/*写多个字节,一次最多可以时8个*/
/*for(i = 0; i < 10; i++) //好使
{
while( !Write_24CXXs(0x40+i*8,arr,8) ); //需要监测
Delays(10);
} */
/*读取数据,一个一个的读*/
for(i = 0; i<20; i++) //好使
{
P2 = Read_24CXX(0x40 + i); //需要监测
Delays(10);
}
//Read_24CXXs(0x10,arr_1,8);
while(1);
}
///////////////////////////////////////
/***************延时函数****************/
void Delays(uchar n)
{
uchar i, j;
for(i = n; i > 0; i--)
for(j = 112; j > 1; j--)
continue;
}
/*************启动信号*******************/
void Start_I2C(void)
{
SDA = 1;
SCL = 1;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
SDA = 0;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
SCL = 0;
}
/**************结束信号**************************/
void Stop_I2C(void)
{
SDA = 0;
SCL = 1;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
SDA = 1;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
}
/*********检验应答信号***************************/
bit Ask_I2C(void)
{
bit ask;
SDA = 1;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
SCL = 1;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
SCL = 0;
return ( ask = !SDA);
}
/************写一个字节**********************/
void Write_byte(uchar c_data)
{
uchar i;
for(i = 0; i < 8; i++)
{
if(c_data&0x80)
SDA = 1;
else SDA = 0;
SCL = 1;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
SCL = 0;
c_data <<= 1;
}
Delays(10);
}
/***********写应答信号***********************/
void ASK(void)
{
SDA = 0;
SCL = 1;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
SDA = 1;
SCL = 0;
}
/***********写非应答信号********************/
void No_Ask(void)
{
SDA = 1;
SCL = 1;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
SCL = 0;
SDA = 0;
}
/**********读取一个字节***************/
uchar Read_byte(void)
{
uchar i = 0;
uchar temp;
SDA =1;
for(i = 0; i < 8; i++)
{
temp <<= 1;
SCL = 1;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
if(SDA)temp++;
_nop_();
_nop_();
_nop_();
_nop_();
SCL = 0;
}
SCL = 0;
return temp;
}
/***********从片子中读取一个数据************************/
uchar Read_24CXX(uchar addr)
{
uchar temp ;
Start: Start_I2C(); ///写入读控制 //程序内部监测
Write_byte(WRITE);
if(!Ask_I2C()) goto Start; //检验应答位
Write_byte(addr); //写入要读的地址
if(!Ask_I2C()) goto Start;
//从新开始
Start_I2C(); ///写入读控制字
Write_byte(READ);
if(!Ask_I2C()) goto Start; //检验应答位
temp = Read_byte(); //读取数据
No_Ask(); //非应答
Stop_I2C();
return temp;
}
/************从片子中读取连续的多个数据********************/
void Read_24CXXs(uchar addr,uchar *pr, uchar n)
{
uchar temp ;
Start: Start_I2C();
///写入读控制字
Write_byte(WRITE);
if(!Ask_I2C()) goto Start; //检验应答位
Write_byte(addr); //写入要读的地址
if(!Ask_I2C()) goto Start;
//从新开始
Start_I2C();
///写入读控制字
Write_byte(READ);
if(!Ask_I2C()) goto Start; //检验应答位
while(n--) //连续读取
{
*pr = Read_byte(); //读取数据
No_Ask(); //非应答
pr++;
Delays(10);
}
Stop_I2C();
}
/**********向片子的一个地址写入一个数据*****/
bit Write_24CXX(uchar addr,uchar c_data)
{
Start_I2C();
Write_byte(WRITE);
if(!Ask_I2C()) return 0; //检验应答位
Write_byte(addr); //写入地址
if(!Ask_I2C()) return 0;
Write_byte(c_data); //写入数据
if(!Ask_I2C()) return 0;
Stop_I2C();
return 1;
}
/**********向片子中连续写入8个数据********最多8个****************/
bit Write_24CXXs(uchar addr,uchar *pr, uchar n)
{
Start_I2C();
///写入控制字
Write_byte(WRITE);
if(!Ask_I2C()) return 0; //检验应答位
Write_byte(addr); //写入地址
if(!Ask_I2C()) return 0;
while(n--)
{
Write_byte(*pr); //写入数据
if(!Ask_I2C()) return 0;
pr++;
}
Stop_I2C();
return 1;
}
/******************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -