你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

关于STM32硬件I2C问题

[复制链接]
阿松松 提问时间:2015-1-5 14:38 /
前面和大家分享了模拟I2C代码,也来谈谈我的硬件I2C。
传感器地址为0x1a,这是实际的SCL与SDA波形图

实际从机地址为0x1a

实际从机地址为0x1a

下面这个图纯属测试,测试是否发出的数据正确,发送的数据为0xaa

测试

测试

代码如下,配置文件也检查了好几遍,并没有发现什么问题,可代码在发送了device地址后,就停了。

请牛牛们帮忙分析


  1.   
  2. /* Includes ------------------------------------------------------------------*/
  3. #include "stm32f4xx_i2c.h"
  4. #include "user_i2c.h"

  5. /**
  6.   * @brief  Initializes peripherals used by the I2C Periph driver.
  7.   * @param  None
  8.   * @retval None
  9.   */
  10. void I2C_HighLevel_Init(void)
  11. {
  12.   
  13.   GPIO_InitTypeDef  GPIO_InitStructure;
  14.   
  15.   /*!< I2C Periph clock enable */
  16.   RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
  17.   
  18.   /*!< I2C_SCL_GPIO_CLK and I2C_SDA_GPIO_CLK Periph clock enable */
  19.   RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);

  20.   /*!< GPIO configuration */  
  21.   /*!< Configure I2C pins: SCL */
  22.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
  23.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  24.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  25.   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  26.   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  27.   GPIO_Init(GPIOB, &GPIO_InitStructure);//高电平到低电平跳变

  28.   /*!< Configure I2C pins: SDA */
  29.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  30.   GPIO_Init(GPIOB, &GPIO_InitStructure);  //高电平到低电平跳变?
  31.   /*!< GPIO configuration */
  32.   /* Connect PXx to I2C_SCL*/
  33.   GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_I2C1);//低电平到高电平跳变?
  34.   /* Connect PXx to I2C_SDA*/
  35.   GPIO_PinAFConfig(GPIOB, GPIO_PinSource9, GPIO_AF_I2C1);//低电平到高电平跳变?
  36.   
  37.   /*!< I2C configuration */
  38.   I2C_InitTypeDef  I2C_InitStructure;
  39.   I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
  40.   I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
  41.   I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
  42.   I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
  43.   I2C_InitStructure.I2C_ClockSpeed = 200000;
  44.   
  45.   /* I2C Peripheral Enable */
  46.   I2C_Cmd(I2C1, ENABLE);
  47.   
  48.   /* Apply I2C configuration after enabling it */
  49.   I2C_Init(I2C1, &I2C_InitStructure);  
  50. }



  51. /**
  52.   * @brief Transmit start/adress/stop, receive sensors DATA from I2C Periph.
  53.   * @param  None
  54.   * @retval None
  55.   */
  56. UV_Ambient_Sensor I2C_Read(uint8_t Device_Addr, uint8_t Reg_Addr,uint8_t NumByteToRead)
  57. {
  58.   UV_Ambient_Sensor UV_value;
  59.   
  60. //  UV_Ambient_Sensor uv_amb_sensor;//store sensor value
  61. //  while(I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY));//check if i2c is busy
  62.   
  63.   I2C_GenerateSTART(I2C1, ENABLE);
  64.   while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));//wait ack
  65. //  I2C_GenerateSTART(I2C1, DISABLE);
  66.   
  67.   I2C_Send7bitAddress(I2C1, Device_Addr, I2C_Direction_Transmitter);// write slave address
  68.   while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));//
  69.   //slave ack

  70. /* Clear EV6 by setting again the PE bit */
  71.   I2C_Cmd(I2C1, ENABLE);

  72.   I2C_SendData(I2C1, Reg_Addr);// write slave register address
  73.   while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));//
  74.   //slave ack
  75.   
  76. /*!< Test on EV8 and clear it */
  77. //  while(!I2C_GetFlagStatus(I2C1, I2C_FLAG_BTF));//-------debug1
  78.   
  79. //  I2C_GenerateSTOP(I2C1, ENABLE);//uv datasheet need,stand i2c?
  80. //  I2C_GenerateSTOP(I2C1, DISABLE);//stop transmit stop signal
  81.   
  82.   I2C_GenerateSTART(I2C1, ENABLE);
  83. //  I2C_GenerateSTART(I2C1, DISABLE);
  84.   while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));//
  85.   
  86.   I2C_Send7bitAddress(I2C1, Device_Addr, I2C_Direction_Receiver);// Read
  87.   while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));//
  88.   //slave ack
  89.   
  90. /* One Byte Master Reception procedure (POLLING) -----------------------------*/
  91. /* While there is data to be read */
  92.   /*
  93.   while(NumByteToRead)  
  94.   {
  95.     if(NumByteToRead == 1)//Generate Stop before receive the last byte
  96.     {*/
  97. //      while(!I2C_GetFlagStatus(I2C1, I2C_FLAG_ADDR));//-------debug2
  98.       /* Disable Acknowledgement */
  99.       I2C_AcknowledgeConfig(I2C1, DISABLE);
  100.       
  101.       /* Send STOP Condition */
  102.       I2C_GenerateSTOP(I2C1, ENABLE);
  103. //    }

  104.     /* Test on EV7 and clear it */
  105.     while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED))//
  106.     {      
  107.       /* Read a byte from the sensor */
  108.       UV_value.Ambient_value = I2C_ReceiveData(I2C1);

  109.       /* Point to the next location where the byte read will be saved */
  110. //      pBuffer++;
  111.       
  112.       /* Decrement the read bytes counter */
  113. //      NumByteToRead--;        
  114.     }
  115. //  }

  116.   /* Enable Acknowledgement to be ready for another reception */
  117.   I2C_AcknowledgeConfig(I2C1, ENABLE);
  118.   return UV_value;
  119. }

  120. void I2C_Write(uint8_t Device_Addr, uint8_t Reg_Addr, uint8_t Ctrl_Cmd)
  121. {
  122. //  while(I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY));//check if i2c is busy
  123.   I2C_GenerateSTART(I2C1, ENABLE);// SCL与SDA均产生下降沿信号
  124.   while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));//wait ack

  125.   
  126.   I2C_Send7bitAddress(I2C1, Device_Addr, I2C_Direction_Transmitter);// write slave address,
  127.   while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));//
  128.   //slave ack
  129.   
  130.   I2C_SendData(I2C1, Reg_Addr);// write slave register address
  131.   while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));//
  132.   //slave ack

  133.   I2C_SendData(I2C1, Ctrl_Cmd);// data to be written
  134.   while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));//
  135.   //slave ack
  136.   
  137.   /* Send STOP Condition */
  138.   I2C_GenerateSTOP(I2C1, ENABLE);
  139. }

  140. /**
  141.   * @brief  Wait for EEPROM Standby state.
  142.   *
  143.   * @note  This function allows to wait and check that EEPROM has finished the
  144.   *        last operation. It is mostly used after Write operation: after receiving
  145.   *        the buffer to be written, the EEPROM may need additional time to actually
  146.   *        perform the write operation. During this time, it doesn't answer to
  147.   *        I2C packets addressed to it. Once the write operation is complete
  148.   *        the EEPROM responds to its address.
  149.   *
  150.   * @param  None
  151.   * @retval sEE_OK (0) if operation is correctly performed, else return value
  152.   *         different from sEE_OK (0) or the timeout user callback.
  153.   */
  154. void I2C_WaitEepromStandbyState(void)      
  155. {
  156.   __IO uint16_t SR1_Tmp = 0;
  157.   
  158. /* Keep looping till the slave acknowledge his address or maximum number
  159.    of trials is reached (this number is defined by sEE_MAX_TRIALS_NUMBER define
  160.    in STM324x7I_eval_i2c_ee.h file) */
  161.   do
  162.   {
  163.     /* Send START condition */
  164.     I2C_GenerateSTART(I2C1, ENABLE);
  165.     /* Read I2C1 SR1 register */
  166.     SR1_Tmp = I2C_ReadRegister(I2C1, I2C_Register_SR1);
  167.     /* Send sensor address for write */
  168.     I2C_Send7bitAddress(I2C1, 0x1A, I2C_Direction_Transmitter);
  169.   }while(!(I2C_ReadRegister(I2C1, I2C_Register_SR1) & 0x0002));

  170.   /* Clear AF flag */
  171.   I2C_ClearFlag(I2C1, I2C_FLAG_AF);
  172.   /* STOP condition */   
  173.   I2C_GenerateSTOP(I2C1, ENABLE);
  174. }
复制代码





收藏 评论24 发布时间:2015-1-5 14:38

举报

24个回答
阿松松 回答时间:2015-1-5 15:46:15
晓枫VS枯叶 发表于 2015-1-5 15:42
4GSa/s这采样率,这示波器多少钱啊,测量信号的带宽能达到多少!

额,你们都不关注重点。。。
公司买的,嗯,我之前还查过,全新的好像几万吧,可以搞个二手的来玩玩,几千块,不知道是不是原装的
350M的,我们搞这些小玩意儿,都是大马拉小车,浪费
阿松松 回答时间:2015-1-5 15:45:58
晓枫VS枯叶 发表于 2015-1-5 15:42
4GSa/s这采样率,这示波器多少钱啊,测量信号的带宽能达到多少!

额,你们都不关注重点。。。
公司买的,嗯,我之前还查过,全新的好像几万吧,可以搞个二手的来玩玩,几千块,不知道是不是原装的
350M的,我们搞这些小玩意儿,都是大马拉小车,浪费
阿松松 回答时间:2015-1-5 16:13:47
晓枫VS枯叶 发表于 2015-1-5 16:09
我还是实习生,哪有什么奖啊,公司的大部分产品是机械方面的,最近才搞起研发(其实是抄别人的东西)

...

确实啊,现在的研发,除了方案公司,其他的要不拿方案,要不抄,反正拿来方案,也是抄,哈哈
zhang7309 回答时间:2015-1-5 15:00:01
楼主IIC用的那种模式呢?从器件是否应该有响应?
hlt512 回答时间:2015-1-5 15:02:15
速度慢点,时序
阿松松 回答时间:2015-1-5 15:10:10
zhang7309 发表于 2015-1-5 15:00
楼主IIC用的那种模式呢?从器件是否应该有响应?

从器件不响应似乎,很奇怪。
标准I2C,STM32主
阿松松 回答时间:2015-1-5 15:11:15
hlt512 发表于 2015-1-5 15:02
速度慢点,时序

嗯,要标准速率吗,记得我试过100K的,还是不行。
我这个是200K的,一般来说,官方的要多快比较合适呢
麟狮蕟 回答时间:2015-1-5 15:37:47
这是什么软件啊~
阿松松 回答时间:2015-1-5 15:39:14
linshifaa 发表于 2015-1-5 15:37
这是什么软件啊~

软件????
麟狮蕟 回答时间:2015-1-5 15:42:08
143526hhprdjhad9dhp9hh.jpg 这个软件啊
晓枫VS枯叶 回答时间:2015-1-5 15:42:19
4GSa/s这采样率,这示波器多少钱啊,测量信号的带宽能达到多少!
阿松松 回答时间:2015-1-5 15:44:01

额,这个是示波器,图像可以直接抓取保存到U盘上
阿松松 回答时间:2015-1-5 15:44:33

额,这个是示波器,图像可以直接抓取保存到U盘上
麟狮蕟 回答时间:2015-1-5 15:45:43
这么高级!!没用过!!1
阿松松 回答时间:2015-1-5 15:47:16
linshifaa 发表于 2015-1-5 15:45
这么高级!!没用过!!1

现在的数字示波器,基本上都有这个功能了吧,不像以前,还要拿手机拍个图
这个功能,挺赞的
12下一页

所属标签

相似问题

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
st-img 微信公众号
st-img 手机版