代码拉取完成,页面将自动刷新
同步操作将从 vtor3478/vtor_elec_module 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
#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__
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。