1 Star 0 Fork 8

Anderson/vtor_elec_module

forked from vtor3478/vtor_elec_module 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
vtor_i2c.c 5.34 KB
一键复制 编辑 原始数据 按行查看 历史
#include "vtor_i2c.h"
#ifdef __VTOR_I2C__
// https://blog.csdn.net/xiaoyuanwuhui/article/details/107430279
// 发送IIC起始信号
void VtorI2c_Start(VtorI2c* i2c)
{
VtorI2c_SetSDA(i2c, GPIO_LEVEL_HIGH); // 拉高信号线
VtorI2c_SetSCL(i2c, GPIO_LEVEL_HIGH); // 拉高时钟线
VtorI2c_Delay(4);
VtorI2c_SetSDA(i2c, GPIO_LEVEL_LOW); // 拉高信号线
VtorI2c_Delay(4);
}
// 发送IIC停止信号
void VtorI2c_Stop(VtorI2c* i2c)
{
VtorI2c_SetSCL(i2c, GPIO_LEVEL_LOW); // 拉高时钟线
VtorI2c_SetSDA(i2c, GPIO_LEVEL_LOW);
VtorI2c_Delay(4);
VtorI2c_SetSCL(i2c, GPIO_LEVEL_HIGH);
VtorI2c_Delay(4);
VtorI2c_SetSDA(i2c, GPIO_LEVEL_HIGH);
VtorI2c_Delay(4);
}
// IIC发送ACK信号
void VtorI2c_Ack(VtorI2c* i2c)
{
VtorI2c_SetSCL(i2c, GPIO_LEVEL_LOW);
VtorI2c_SetSDA(i2c, GPIO_LEVEL_LOW);
VtorI2c_Delay(4);
VtorI2c_SetSCL(i2c, GPIO_LEVEL_HIGH);
VtorI2c_Delay(4);
VtorI2c_SetSCL(i2c, GPIO_LEVEL_LOW);
VtorI2c_Delay(4);
}
// IIC不发送ACK信号
void VtorI2c_NAck(VtorI2c* i2c)
{
VtorI2c_SetSCL(i2c, GPIO_LEVEL_LOW);
VtorI2c_SetSDA(i2c, GPIO_LEVEL_HIGH);
VtorI2c_Delay(4);
VtorI2c_SetSCL(i2c, GPIO_LEVEL_HIGH);
VtorI2c_Delay(4);
VtorI2c_SetSCL(i2c, GPIO_LEVEL_LOW);
VtorI2c_Delay(4);
}
// IIC等待ACK信号
unsigned char VtorI2c_WaitAck(VtorI2c* i2c)
{
uint8_t ack = 0;
VtorI2c_SetSCL(i2c, GPIO_LEVEL_LOW);
VtorI2c_SetSDA(i2c, GPIO_LEVEL_HIGH);
VtorI2c_SetSDA_Dir(i2c, GPIO_DIR_INPUT);
VtorI2c_Delay(4);
VtorI2c_SetSCL(i2c, GPIO_LEVEL_HIGH);
VtorI2c_Delay(4);
ack = VtorI2c_ReadSDA(i2c);
VtorI2c_SetSCL(i2c, GPIO_LEVEL_LOW);
VtorI2c_SetSDA_Dir(i2c, GPIO_DIR_OUTPUT);
return ack;
}
// IIC发送一个字节
void VtorI2c_SendByte(VtorI2c* i2c, uint8_t txd)
{
for(uint8_t i=0; i<8; i++)
{
VtorI2c_SetSCL(i2c, GPIO_LEVEL_LOW);
if(txd & 0x80)
{
VtorI2c_SetSDA(i2c, GPIO_LEVEL_HIGH);
}
else
{
VtorI2c_SetSDA(i2c, GPIO_LEVEL_LOW);
}
txd <<= 1;
VtorI2c_Delay(4);
VtorI2c_SetSCL(i2c, GPIO_LEVEL_HIGH);
VtorI2c_Delay(4);
}
VtorI2c_SetSCL(i2c, GPIO_LEVEL_LOW);
}
// IIC读取一个字节
uint8_t VtorI2c_ReadByte(VtorI2c* i2c)
{
uint8_t rxd = 0;
VtorI2c_SetSDA_Dir(i2c, GPIO_DIR_INPUT);
for(uint8_t i=0; i<8; i++)
{
rxd <<= 1;
VtorI2c_SetSCL(i2c, GPIO_LEVEL_LOW);
VtorI2c_Delay(4);
VtorI2c_SetSCL(i2c, GPIO_LEVEL_HIGH);
VtorI2c_Delay(4);
if(VtorI2c_ReadSDA(i2c))
{
rxd |= 0x01;
}
}
VtorI2c_SetSCL(i2c, GPIO_LEVEL_LOW);
VtorI2c_SetSDA_Dir(i2c, GPIO_DIR_OUTPUT);
return rxd;
}
// 连续写N个字节
uint8_t VtorI2c_WriteMem(VtorI2c* i2c, uint8_t slaveAddress, uint16_t regAddress, uint8_t regAddrLen , uint8_t* buf, uint16_t len)
{
VtorI2c_Start(i2c);
slaveAddress &= 0xfe;
VtorI2c_SendByte(i2c, slaveAddress); //发送设备地址+写信号
if(VTOR_I2C_STATE_OK != VtorI2c_WaitAck(i2c))
{
VtorI2c_Stop(i2c);
return VTOR_I2C_STATE_DEV_ADDR_NAK;
}
VtorI2c_SendByte(i2c, regAddress);
if(VTOR_I2C_STATE_OK != VtorI2c_WaitAck(i2c))
{
VtorI2c_Stop(i2c);
return VTOR_I2C_STATE_DEV_REG_NAK;
}
for(uint16_t i=0; i<len; i++)
{
VtorI2c_SendByte(i2c, buf[i]);
if(i<len-1)
{
if(VTOR_I2C_STATE_OK != VtorI2c_WaitAck(i2c))
{
VtorI2c_Stop(i2c);
return VTOR_I2C_STATE_DEV_DATA_NAKE;
}
}
}
VtorI2c_NAck(i2c);
VtorI2c_Stop(i2c);
return VTOR_I2C_STATE_OK;
}
// 连续读N个字节
uint8_t VtorI2c_ReadMem(VtorI2c* i2c, uint8_t slaveAddress, uint16_t regAddress, uint8_t regAddrLen , uint8_t* buf, uint16_t len)
{
VtorI2c_Start(i2c);
slaveAddress &= 0xfe;
VtorI2c_SendByte(i2c, slaveAddress); //发送设备地址+写信号
if(VTOR_I2C_STATE_OK != VtorI2c_WaitAck(i2c))
{
VtorI2c_Stop(i2c);
return VTOR_I2C_STATE_DEV_ADDR_NAK;
}
VtorI2c_SendByte(i2c, regAddress);
if(VTOR_I2C_STATE_OK != VtorI2c_WaitAck(i2c))
{
VtorI2c_Stop(i2c);
return VTOR_I2C_STATE_DEV_REG_NAK;
}
VtorI2c_Start(i2c);
slaveAddress |= 0x01;
VtorI2c_SendByte(i2c, slaveAddress); // 读操作
if(VTOR_I2C_STATE_OK != VtorI2c_WaitAck(i2c))
{
VtorI2c_Stop(i2c);
return VTOR_I2C_STATE_DEV_REG_NAK;
}
for(uint16_t i=0; i<len; i++)
{
buf[i] = VtorI2c_ReadByte(i2c);
if(i<len-1)
{
VtorI2c_Ack(i2c);
}
}
VtorI2c_NAck(i2c);
VtorI2c_Stop(i2c);
return VTOR_I2C_STATE_OK;
}
uint8_t VtorI2c_CheckIo(VtorI2c* i2c)
{
uint8_t ioRet = 0;
VtorI2c_SetSCL(i2c, GPIO_LEVEL_HIGH);
VtorI2c_Delay(4);
if(GPIO_LEVEL_HIGH != VtorI2c_ReadSCL(i2c))
{
ioRet |= 0x80;
}
VtorI2c_SetSCL(i2c, GPIO_LEVEL_LOW);
VtorI2c_Delay(4);
if(GPIO_LEVEL_LOW != VtorI2c_ReadSCL(i2c))
{
ioRet |= 0x40;
}
VtorI2c_SetSDA_Dir(i2c, GPIO_DIR_OUTPUT);
VtorI2c_SetSDA(i2c, GPIO_LEVEL_HIGH);
VtorI2c_Delay(4);
if(GPIO_LEVEL_HIGH != VtorI2c_ReadSDA(i2c))
{
ioRet |= 0x20;
}
VtorI2c_SetSDA(i2c, GPIO_LEVEL_LOW);
VtorI2c_Delay(4);
if(GPIO_LEVEL_LOW != VtorI2c_ReadSDA(i2c))
{
ioRet |= 0x10;
}
VtorI2c_SetSDA(i2c, GPIO_LEVEL_HIGH);
VtorI2c_SetSCL(i2c, GPIO_LEVEL_HIGH);
return ioRet;
}
uint8_t VtorI2c_CheckDevice(VtorI2c* i2c, uint8_t addr)
{
uint8_t ret = VTOR_I2C_STATE_ERROR;
VtorI2c_Start(i2c);
VtorI2c_SendByte(i2c, addr);
if(VTOR_I2C_STATE_OK == VtorI2c_WaitAck(i2c))
{
ret = VTOR_I2C_STATE_OK;
}
VtorI2c_Stop(i2c);
VtorI2c_Delay(20);
VtorI2c_Stop(i2c);
return ret;
}
#endif // __VTOR_I2C__
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/Thomas_Ti/vtor_elec_module.git
git@gitee.com:Thomas_Ti/vtor_elec_module.git
Thomas_Ti
vtor_elec_module
vtor_elec_module
main

搜索帮助

0d507c66 1850385 C8b1a773 1850385