近期使用stm8的库函数写功能,使用I2C时候遇到在调用 void I2C_Send7bitAddress(uint8_t Address, I2C_Direction_TypeDef Direction)的时候,会卡死的问题
通过调试发现在调用写地址函数后会进入到main.c的这个函数中
void assert_failed(u8* file, u32 line)
{
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
// printf("Wrong parameters value: file %s on line %d\r\n", file, line);
/* Infinite loop */
while (1)
{// return;
}
}
后来发现写地址函数会先检测输入参数是否正确如果不正确就会进入错误循环
贴上这个官方I2C库函数的代码
void I2C_Send7bitAddress(uint8_t Address, I2C_Direction_TypeDef Direction)
{
/* Check function parameters */
assert_param(IS_I2C_ADDRESS_OK(Address));
assert_param(IS_I2C_DIRECTION_OK(Direction));
/* Clear bit0 (direction) just in case */
Address &= (uint8_t)0xFE;
/* Send the Address + Direction */
I2C->DR = (uint8_t)(Address | (uint8_t)Direction);
}
这里
/* Check function parameters */
assert_param(IS_I2C_ADDRESS_OK(Address));
assert_param(IS_I2C_DIRECTION_OK(Direction));
注释的两句话就是检测参数是否正确,有一个比较坑的地方就在于,
如果你的地址是八位的,然后最后一位读写位不为0,那么就会出错
STM8这个比较特殊的一点在于,正常7位地址+1位RW位,这个RW位你必须自己留出来,比如某个设备地址是111 1100,我在其他环境下会声明为0x7C
但是在这边,你要用它的库就需要声明为1111 1000,0xF8才可以。
最后一位如果值为1,必出错跳转faild死循环