[STM8] 硬件I2C的问题(新人求教)

[复制链接]
1246|1
 楼主| zaianda 发表于 2015-2-13 15:46 | 显示全部楼层 |阅读模式
本人使用STM8S207MB与触摸芯片TS20通信,使用硬件I2C。现在向TS20学数据已经没问题了,但是卡在读数据。
每当读了第一次数据之后,I2C通信就卡主了,就算复位也无效,一定要断电才行。
我初步分析是读完数据后从设备TS20没有释放SDA,导致无法再进行I2C通信。下面贴上读取数据的代码,希望大家能帮我分析一下,谢谢!
  1. void I2C_Touch_ReadData(u8* pBuffer,bool HLRAddr,u8 ReadAddr,u8 NumByteToRead)
  2. {  
  3.     /* While the bus is busy */
  4.   while(I2C_GetFlagStatus(I2C_FLAG_BUSBUSY));
  5.   
  6.   /* Generate start & wait event detection */
  7.     I2C_GenerateSTART(ENABLE);
  8.   /* Test on EV5 and clear it */
  9.   while (!I2C_CheckEvent(I2C_EVENT_MASTER_START_SENT));
  10.   
  11.   /* Send Hight Or Low address*/
  12.   if(HLRAddr)
  13.   {  
  14.     I2C_Send7bitAddress(VDD_ADDRESS, I2C_DIRECTION_TX);
  15.   }
  16.   else
  17.   {
  18.     I2C_Send7bitAddress(GND_ADDRESS, I2C_DIRECTION_TX);
  19.   }
  20.    /* Test on EV6 and clear it */
  21.     while (!I2C_CheckEvent(I2C_EVENT_MASTER_ADDRESS_ACKED));
  22.     I2C_ClearFlag(I2C_FLAG_ADDRESSSENTMATCHED);
  23.    
  24.    /* Send Address of first byte to be read & wait event detection */
  25.     //I2C_SendData((u8)(ReadAddr >> 8)); /* MSB */
  26.     /* Test on EV8 and clear it */
  27.     //while (!I2C_CheckEvent(I2C_EVENT_MASTER_BYTE_TRANSMITTED));
  28.     I2C_SendData((u8)(ReadAddr)); /* LSB */
  29.    /* Test on EV8 and clear it */
  30.     while (!I2C_CheckEvent(I2C_EVENT_MASTER_BYTE_TRANSMITTED));
  31.   
  32.    /* Send STRAT condition a second time */  
  33.    I2C_GenerateSTART(ENABLE);
  34.     /* Test on EV5 and clear it */
  35.    while (!I2C_CheckEvent(I2C_EVENT_MASTER_START_SENT));
  36.   
  37.   /* Send Hight Or Low address*/

  38.   if(HLRAddr)
  39.   {  
  40.     I2C_Send7bitAddress(VDD_R_ADDRESS, I2C_DIRECTION_RX);
  41.   }
  42.   else
  43.   {
  44.     I2C_Send7bitAddress(GND_R_ADDRESS, I2C_DIRECTION_RX);
  45.   }

  46.    /* Test on EV6 and clear it */
  47.     while (!I2C_CheckEvent(I2C_EVENT_MASTER_ADDRESS_ACKED));
  48.     I2C_ClearFlag(I2C_FLAG_ADDRESSSENTMATCHED);
  49.   
  50.   /* While there is data to be read */
  51.   while(NumByteToRead)  
  52.   {
  53.     if(NumByteToRead == 1)
  54.     {
  55.       /* Disable Acknowledgement */
  56.       I2C_AcknowledgeConfig(I2C_ACK_NONE);
  57.       
  58.       /* Send STOP Condition */
  59.       I2C_GenerateSTOP(ENABLE);
  60.     }

  61.     /* Test on EV7 and clear it */
  62.     if(I2C_CheckEvent(I2C_EVENT_MASTER_BYTE_RECEIVED))  
  63.     {      
  64.       /* Read a byte from the EEPROM */
  65.       *pBuffer = I2C_ReceiveData();

  66.       /* Point to the next location where the byte read will be saved */
  67.       pBuffer++;
  68.       
  69.       /* Decrement the read bytes counter */
  70.       NumByteToRead--;        
  71.     }   
  72.   }

  73.   /* Enable Acknowledgement to be ready for another reception */
  74.   //I2C_AcknowledgeConfig(I2C_ACK_CURR);
  75.   I2C_CR2 |= MASK_I2C_CR2_ACK;
  76. }
 楼主| zaianda 发表于 2015-2-13 15:47 | 显示全部楼层
在网上看了一下,完成读取数据后腰使用 I2C_CR2 |=0x04;重新启用I2C,但是我加进去后还是出问题呢。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

27

主题

124

帖子

2

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