[STM8] 还是坑爹的I2C问题

[复制链接]
11029|30
luofeng2g 发表于 2015-8-29 09:01 | 显示全部楼层
gaoyang9992006 发表于 2015-8-14 16:26
坑爹的代码。我都难以理解了。

:lol
tangmix 发表于 2015-8-31 21:39 | 显示全部楼层
对啊,我的板子上有SPI存储器位置的,发现24CXX调试麻烦的,明天该SPI的
351357741 发表于 2015-9-1 17:55 | 显示全部楼层
我是来学习的。。。
fanxsd 发表于 2015-9-2 08:55 | 显示全部楼层
这坑爹的代码!!
 楼主| ww_升 发表于 2015-9-29 08:41 | 显示全部楼层
解决了,及时清除标志位,然后加入超时保护就可以很稳定了
HORSE7812 发表于 2015-9-29 10:12 | 显示全部楼层
ccw1986 发表于 2015-9-29 11:10 | 显示全部楼层
确实够坑爹啊,不忍直视
734774645 发表于 2015-9-29 14:16 来自手机 | 显示全部楼层
我很少用iic的,都是用spi
qgmfly 发表于 2015-9-29 16:01 | 显示全部楼层
:P,围观学习
dronefly 发表于 2016-4-4 23:10 | 显示全部楼层
你的问题出现在函数库里面的原函数返回的值不对。需要你作适当的修改
函数库里面的原函数如下:
ErrorStatus I2C_CheckEvent(I2C_Event_TypeDef I2C_Event)
{
  __IO uint16_t lastevent = 0x00;
  uint8_t flag1 = 0x00 ;
  uint8_t flag2 = 0x00;
  ErrorStatus status = ERROR;

  /* Check the parameters */
  assert_param(IS_I2C_EVENT_OK(I2C_Event));

  if (I2C_Event == I2C_EVENT_SLAVE_ACK_FAILURE)
  {
    lastevent = I2C->SR2 & I2C_SR2_AF;
  }
  else
  {
    flag1 = I2C->SR1;
    flag2 = I2C->SR3;
    lastevent = ((uint16_t)((uint16_t)flag2 << (uint16_t)8) | (uint16_t)flag1);
  }
  /* Check whether the last event is equal to I2C_EVENT */
  if (((uint16_t)lastevent & (uint16_t)I2C_Event) == (uint16_t)I2C_Event)
  {
    /* SUCCESS: last event is equal to I2C_EVENT */
    status = SUCCESS;
  }
  else
  {
    /* ERROR: last event is different from I2C_EVENT */
    status = ERROR;
  }

  /* Return status */
  return status;
}

仔细看红色的返回值。ST的程序员在这上面写法是有点问题的。其它ERROR与SUCCESS某种意义上是0与1。但这样的写**有问题的。
dronefly 发表于 2016-4-4 23:17 | 显示全部楼层
楼上的原程序是在文件:stm8_i2c.c里面。

在文件STM8S.H里面找到的:
typedef enum {FALSE = 0, TRUE = !FALSE} bool;

typedef enum {RESET = 0, SET = !RESET} FlagStatus, ITStatus, BitStatus, BitAction;

typedef enum {DISABLE = 0, ENABLE = !DISABLE} FunctionalState;
#define IS_FUNCTIONALSTATE_OK(STATE) (((STATE) == DISABLE) || ((STATE) == ENABLE))

typedef enum {ERROR = 0, SUCCESS = !ERROR} ErrorStatus;

这是很讨厌的定义方法。很多时候就是在这里出了错。它如果直接给返回0,或者1就不会出错。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 在线客服 返回列表 返回顶部