📄 i0模拟i2c.c
字号:
#include "config.h"
#define SDA 1<<14
#define SCL 1<<11
uint8 ack;
uint8 data_buf[32];
uint8 data_buf3[256];
uint8 I2C_num;
uint8 I2C_buf[32];
uint8 I2C_buf1[32];
uint8 I2C_buf2[32];
uint16 I2C_addr;
#define uchar unsigned char
uchar ACK_flag;
//==================================================================================================
void I2c_start(void)
{
IO0SET |= SDA;
IO0SET |= SCL;
//Delay1(1); // 延时5us
IO0CLR |= SDA;
//Delay1(1);
IO0CLR |= SCL;
}
//==================================================================================================
void I2c_stop(void)
{
IO0CLR |= SDA;
IO0SET |= SCL ;
//Delay1(1);
IO0SET |= SDA;
//Delay1(1);
IO0CLR |= SCL;
//Delay1(2);
}
//==================================================================================================
void I2cSendByte(unsigned char dat)
{
unsigned char n=8;
while(n--)
{
if((dat&0x80) == 0x80)
{
IO0SET |= SDA;
IO0SET |= SCL;
//Delay1(2);
IO0CLR |= SCL;
}
else
{
IO0CLR |= SDA;
IO0SET |= SCL;
//Delay1(2);
IO0CLR |= SCL;
}
dat = dat<<1;
}
}
//==================================================================================================
unsigned char I2creceiveByte(void)
{
unsigned char n=8;
unsigned char tdata=0x01;
IO0DIR &= (~(SDA)); //置P0.14为输入
while(n--)
{
//Delay1(1);
IO0SET |= SCL;
//Delay1(1);
tdata = tdata<<1;
if((IO0PIN & SDA) == SDA)
tdata = tdata|0x01;
IO0CLR |= SCL;
}
IO0DIR |= SDA;
return(tdata);
}
//-----------------------------------------------------------------------------------
uint8 I2C_GetAck(void)
{
IO0DIR &= (~(SDA)); //置P0.14为输入
//Delay1(1);
IO0SET |=SCL;
//Delay1(1);
if(IO0PIN & SDA) //判断SDA
ack = 1;
else
ack = 0;
IO0CLR |=SCL;
IO0DIR |= SDA;
return ack;
}
//-----------------------------------------------------------------------------------
void I2c_PutAck(uint8 a)
{
IO0DIR |= SDA;
if(a==0) IO0CLR |=SDA;
else IO0SET |=SDA;
//Delay1(1);
IO0SET |=SCL;
//Delay1(1);
IO0CLR |=SCL;
//Delay1(1);
}
//==================================================================================================
uint8 Write_Nbyte(uint8 sla, uint8 suba_type, uint32 CFG_add, uint8 *data, uint32 num)
{
uint8 sla1;
sla1=sla;
I2c_start();
I2cSendByte(sla1);
ACK_flag =I2C_GetAck();
if (ACK_flag )
{
I2c_stop();
return 0;
}
if(suba_type==2)
{
I2cSendByte(CFG_add>>8);
ACK_flag = I2C_GetAck();
if (ACK_flag)
{
I2c_stop();
return 0;
}
}
I2cSendByte(CFG_add);
ACK_flag = I2C_GetAck();
if ( ACK_flag )
{
I2c_stop();
return 0;
}
while(num--)
{
I2cSendByte(*data);
data++;
ACK_flag = I2C_GetAck();
if ( ACK_flag )
{
I2c_stop();
return 0;
}
}
I2c_stop();
return(1);
}
//==================================================================================================
uint8 Read_Nbyte(uint8 sla, uint8 p0, uint8 suba_type, uint32 CFG_add, uint8 * buff, uint32 num)
{
uint8 sla1;
if(num >0)
{
sla1=sla;
if(p0==1)
sla1=sla1+2;
I2c_start();
I2cSendByte(sla1);
ACK_flag = I2C_GetAck();
if ( ACK_flag)
{
I2c_stop();
return 0;
}
if(suba_type==2)
{
I2cSendByte((CFG_add>>8));
ACK_flag = I2C_GetAck();
if ( ACK_flag)
{
I2c_stop();
return 0;
}
}
I2cSendByte(CFG_add);
ACK_flag = I2C_GetAck();
if ( ACK_flag )
{
I2c_stop();
return 0;
}
I2c_start();
I2cSendByte(sla1+1);
ACK_flag =I2C_GetAck();
if (ACK_flag )
{
I2c_stop();
return 0;
}
num= num-1;
while(num--)
{
*buff=I2creceiveByte();
buff++;
I2c_PutAck(0);
}
*buff=I2creceiveByte();
I2c_PutAck(1);
I2c_stop();
return(1);
}
else
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -