Yunhorn_OLD_R1_R2_R5/Src/i2c_user.c

213 lines
5.5 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "i2c_user.h"
#include "tof.h"
/************************************************
函数名称 uDelay
功 能 延时微秒(非精确延时)
参 数 uint32_t cnt---延时时间
返 回 值
*************************************************/
void uDelay(uint32_t cnt)
{
while(cnt)
{
__NOP();__NOP();
__NOP();__NOP();
__NOP();__NOP();
__NOP();__NOP();
__NOP();__NOP();
__NOP();__NOP();
cnt--;
}
}
/************************************************
函数名称 I2C_Start
功 能 I2C开始
参 数
返 回 值
*************************************************/
void I2C_Start(void)
{
I2C_SCL_HIGH(); //SCL高
uDelay(5);
I2C_SDA_HIGH(); //SDA高 -> 低
uDelay(5);
I2C_SDA_LOW(); //SDA低
uDelay(5);
I2C_SCL_LOW(); //SCL低(待写地址/数据)
uDelay(5);
}
/************************************************
函数名称 I2C_Stop
功 能 I2C停止
参 数
返 回 值
*************************************************/
void I2C_Stop(void)
{
I2C_SDA_LOW(); //SDA低 -> 高
uDelay(5);
I2C_SCL_HIGH(); //SCL高
uDelay(5);
I2C_SDA_HIGH(); //SDA高
uDelay(5);
}
/************************************************
函数名称 I2C_PutAck
功 能 I2C主机产生应答(或非应答)位
参 数 I2C_ACK ----- 应答
I2C_NOACK --- 非应答
返 回 值
*************************************************/
void I2C_PutAck(uint8_t Ack)
{
I2C_SCL_LOW(); //SCL低
uDelay(30);
if(I2C_ACK == Ack)
I2C_SDA_LOW(); //应答
else
I2C_SDA_HIGH(); //非应答
uDelay(30);
I2C_SCL_HIGH(); //SCL高 -> 低
uDelay(30);
I2C_SCL_LOW(); //SCL低
uDelay(30);
}
/************************************************
函数名称 I2C_GetAck
功 能 I2C主机读取应答(或非应答)位
参 数
返 回 值 I2C_ACK ----- 应答
I2C_NOACK --- 非应答
*************************************************/
uint8_t I2C_GetAck(void)
{
uint8_t ack;
I2C_SCL_LOW(); //SCL低 -> 高
uDelay(30);
I2C_SDA_HIGH(); //释放SDA(开漏模式有效)
uDelay(30);
I2C_SCL_HIGH(); //SCL高(读取应答位)
uDelay(30);
if(Read_I2C_SDA())
ack = I2C_NOACK; //非应答
else
ack = I2C_ACK; //应答
I2C_SCL_LOW(); //SCL低
uDelay(30);
return ack;
}
/************************************************
函数名称 I2C_WriteByte
功 能 I2C写一字节
参 数 Data --- 数据
返 回 值
*************************************************/
void I2C_WriteByte(uint8_t Data)
{
uint8_t cnt;
for(cnt=0; cnt<8; cnt++)
{
I2C_SCL_LOW(); //SCL低(SCL为低电平时变化SDA有效)
uDelay(30);
if(Data & 0x80)
I2C_SDA_HIGH(); //SDA高
else
I2C_SDA_LOW(); //SDA低
Data <<= 1;
I2C_SCL_HIGH(); //SCL高(发送数据)
uDelay(30);
}
I2C_SCL_LOW(); //SCL低(等待应答信号)
uDelay(30);
I2C_GetAck(); //读取应答位
}
/************************************************
函数名称 I2C_WriteByte7
功 能 I2C写一字节
参 数 Data --- 数据
返 回 值
*************************************************/
void I2C_WriteByte7(uint8_t Data)
{
uint8_t cnt;
for(cnt=0; cnt<7; cnt++)
{
I2C_SCL_LOW(); //SCL低(SCL为低电平时变化SDA有效)
uDelay(30);
if(Data & 0x80)
I2C_SDA_HIGH(); //SDA高
else
I2C_SDA_LOW(); //SDA低
Data <<= 1;
uDelay(30);
I2C_SCL_HIGH(); //SCL高(发送数据)
uDelay(30);
}
I2C_SCL_LOW(); //SCL低(等待应答信号)
uDelay(30);
I2C_GetAck(); //读取应答位
}
/************************************************
函数名称 I2C_ReadByte
功 能 I2C读一字节
参 数 ack --------- 产生应答(或者非应答)位
返 回 值 data -------- 读取的一字节数据
*************************************************/
uint8_t I2C_ReadByte(uint8_t ack)
{
uint8_t cnt;
uint8_t data;
I2C_SCL_LOW(); //SCL低
uDelay(30);
I2C_SDA_HIGH(); //释放SDA(开漏模式有效)
for(cnt=0; cnt<8; cnt++)
{
I2C_SCL_HIGH(); //SCL高(读取数据)
uDelay(30);
data <<= 1;
if(Read_I2C_SDA())
data |= 0x01; //SDA为高(数据有效)
I2C_SCL_LOW(); //SCL低
uDelay(30);
}
I2C_PutAck(ack); //产生应答(或者非应答)位
return data; //返回数据
}