[活动专区] 【AT-START-F407测评】+I2C接口实验读取传感器

[复制链接]
 楼主| WoodData 发表于 2021-2-8 18:10 | 显示全部楼层 |阅读模式
#申请原创#      本次测试的是I2C外设,测试使用了ROHM的传感器,光照度传感器RPR-0521RS,气压传感器BM1383AGLV,地磁传感器BM1422AGMV。这几个传感器都是I2C接口,可以一起接I2C测试。
如图:使用arduino接口插在开发板。I2C使用的是I2C1,对应管脚为PB8:SCL,PB9:SDA。
4.jpg

PBB8,PB9对应的I2C功能引脚是重映射功能,需要重映射使能。
5.jpg

下面是重映射寄存器,为1就使能。未使能时I2C对应的PB6,PB7。
1.jpg

程序参考官方的BSP中的EEPROM例程的I2C程序,需要注意的是要使能IO复用重映射的时钟和I2C1的IO重映射使能。再就是I2C设备地址。
I2C初始化:
  1.   GPIO_InitType GPIO_InitStructure;
  2.   I2C_InitType  I2C_InitStructure;  
  3.    
  4.   /* GPIO Periph clock enable */
  5.   RCC_APB2PeriphClockCmd(RCC_APB2PERIPH_GPIOB|RCC_APB2PERIPH_AFIO , ENABLE);
  6.   /* I2C Periph clock enable */
  7.   RCC_APB1PeriphClockCmd(RCC_APB1PERIPH_I2C1, ENABLE);
  8.    
  9.   /* GPIO configuration */  
  10.   /* Configure I2C pins: SCL */
  11.   GPIO_InitStructure.GPIO_Pins = I2C_SCL_PIN;
  12.   GPIO_InitStructure.GPIO_MaxSpeed = GPIO_MaxSpeed_50MHz;
  13.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
  14.   GPIO_Init(I2C_SCL_Port, &GPIO_InitStructure);
  15.   /* Configure I2C pins: SDA */
  16.   GPIO_InitStructure.GPIO_Pins = I2C_SDA_PIN;
  17.   GPIO_Init(I2C_SDA_Port, &GPIO_InitStructure);
  18.   
  19.   //重映射到PB8,PB9
  20.   GPIO_PinsRemapConfig(GPIO_Remap_I2C1,ENABLE);


  21.   /* I2C configuration */
  22.   I2C_InitStructure.I2C_Mode = I2C_Mode_I2CDevice;
  23.   I2C_InitStructure.I2C_FmDutyCycle = I2C_FmDutyCycle_2_1;
  24.   I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
  25.   I2C_InitStructure.I2C_AddrMode = I2C_AddrMode_7bit;
  26.   I2C_InitStructure.I2C_BitRate = 100000;        //100KHz
  27.   
  28.   /* I2C Peripheral Enable */
  29.   I2C_Cmd(I2C1, ENABLE);
  30.   
  31.   /* Apply I2C configuration after enabling it */
  32.   I2C_Init(I2C1, &I2C_InitStructure);


下面是实现读写函数,通过修改例程实现的。

  1. I2C_StatusType I2C_Mem_Read(I2C_Type* I2Cx, uint16_t DevAddr, uint16_t DataAddr,  
  2.                                                                 uint8_t *pBuffer, uint16_t NumByte,  uint32_t Timeout)
  3. {
  4.   /* Wait until BUSY flag is reset */
  5.   if(I2C_WaitOnFlagUntilTimeout(I2Cx, I2C_FLAG_BUSYF, SET, I2C_EVT_CHECK_NONE, I2C_TIMEOUT_BUSY_FLAG) != I2C_OK)
  6.   {
  7.     return I2C_ERROR_STEP_1;
  8.   }

  9.   /* Disable Pos */
  10.   I2C_NACKPositionConfig(I2Cx, I2C_NACKPosition_Current);
  11.   /* Enable Acknowledge */
  12.   I2C_AcknowledgeConfig(I2Cx, ENABLE);   
  13.   /* Send START condition */
  14.   I2C_GenerateSTART(I2Cx, ENABLE);
  15.   /* Wait until SB flag is set */
  16.   if(I2C_WaitOnFlagUntilTimeout(I2Cx, I2C_FLAG_STARTF, RESET, I2C_EVT_CHECK_NONE, Timeout) != I2C_OK)
  17.   {
  18.     /* Send STOP Condition */
  19.     I2C_GenerateSTOP(I2Cx, ENABLE);   
  20.     return I2C_ERROR_STEP_2;
  21.   }
  22.   /* Send slave address for write */
  23.   I2C_Send7bitAddress(I2Cx, (uint8_t)(DevAddr&0xff), I2C_Direction_Transmit);  
  24.   /* Wait until ADDR flag is set */
  25.   if(I2C_WaitOnFlagUntilTimeout(I2Cx, I2C_FLAG_ADDRF, RESET, I2C_EVT_CHECK_NONE, Timeout) != I2C_OK)
  26.   {
  27.     /* Send STOP Condition */
  28.     I2C_GenerateSTOP(I2Cx, ENABLE);   
  29.     return I2C_ERROR_STEP_3;
  30.   }  
  31.   /* Clear ADDR flag */
  32.   I2C_ClearADDRFlag(I2Cx);
  33.   /* Wait until TDE flag is set */
  34.   if(I2C_WaitOnFlagUntilTimeout(I2Cx, I2C_FLAG_TDE, RESET, I2C_EVT_CHECK_NONE, Timeout) != I2C_OK)
  35.   {
  36.     /* Send STOP Condition */
  37.     I2C_GenerateSTOP(I2Cx, ENABLE);   
  38.     return I2C_ERROR_STEP_4;
  39.   }   
  40.   /* Send Memory Address */
  41.   I2C_SendData(I2Cx, (uint8_t)(DataAddr&0xff));  
  42.   /* Wait until TDE flag is set */
  43.   if(I2C_WaitOnFlagUntilTimeout(I2Cx, I2C_FLAG_TDE, RESET, I2C_EVT_CHECK_ACKFAIL, Timeout) != I2C_OK)
  44.   {
  45.     /* Send STOP Condition */
  46.     I2C_GenerateSTOP(I2Cx, ENABLE);   
  47.     return I2C_ERROR_STEP_5;
  48.   }   
  49.   
  50.   /* Send START condition */
  51.   I2C_GenerateSTART(I2Cx, ENABLE);
  52.   /* Wait until SB flag is set */
  53.   if(I2C_WaitOnFlagUntilTimeout(I2Cx, I2C_FLAG_STARTF, RESET, I2C_EVT_CHECK_NONE, Timeout) != I2C_OK)
  54.   {
  55.     /* Send STOP Condition */
  56.     I2C_GenerateSTOP(I2Cx, ENABLE);   
  57.     return I2C_ERROR_STEP_6;
  58.   }   
  59.   /* Send slave address for read */
  60.   I2C_Send7bitAddress(I2Cx, (uint8_t)(DevAddr&0xff), I2C_Direction_Receive);
  61.   
  62.   /* Wait until ADDR flag is set */
  63.   if(I2C_WaitOnFlagUntilTimeout(I2Cx, I2C_FLAG_ADDRF, RESET, I2C_EVT_CHECK_ACKFAIL, Timeout) != I2C_OK)
  64.   {
  65.     /* Send STOP Condition */
  66.     I2C_GenerateSTOP(I2Cx, ENABLE);   
  67.     return I2C_ERROR_STEP_7;
  68.   }
  69.   
  70.   if(NumByte == 1)
  71.   {
  72.     /* Disable Acknowledge */
  73.     I2C_AcknowledgeConfig(I2Cx, DISABLE);
  74.     /* Clear ADDR flag */
  75.     I2C_ClearADDRFlag(I2Cx);
  76.     /* Send STOP Condition */
  77.     I2C_GenerateSTOP(I2Cx, ENABLE);
  78.   }
  79.   else if(NumByte == 2)
  80.   {
  81.     /* Enable Pos */
  82.     I2C_NACKPositionConfig(I2Cx, I2C_NACKPosition_Next);   
  83.     /* Clear ADDR flag */
  84.     I2C_ClearADDRFlag(I2Cx);
  85.     /* Disable Acknowledge */
  86.     I2C_AcknowledgeConfig(I2Cx, DISABLE);
  87.   }
  88.   else
  89.   {
  90.     /* Enable Acknowledge */
  91.     I2C_AcknowledgeConfig(I2Cx, ENABLE);
  92.     /* Clear ADDR flag */
  93.     I2C_ClearADDRFlag(I2Cx);
  94.   }

  95.   while(NumByte > 0)
  96.   {
  97.     if(NumByte <= 3)
  98.     {
  99.       /* One byte */
  100.       if(NumByte == 1)
  101.       {
  102.         /* Wait until RXNE flag is set */
  103.         if(I2C_WaitOnFlagUntilTimeout(I2Cx, I2C_FLAG_RDNE, RESET, I2C_EVT_CHECK_NONE, Timeout) != I2C_OK)
  104.         {
  105.           /* Send STOP Condition */
  106.           I2C_GenerateSTOP(I2Cx, ENABLE);
  107.           return I2C_ERROR_STEP_8;
  108.         }
  109.         /* Read data from DR */
  110.         (*pBuffer++) = I2C_ReceiveData(I2Cx);
  111.         NumByte--;
  112.       }
  113.       /* Two bytes */
  114.       else if(NumByte == 2)
  115.       {
  116.         /* Wait until BTF flag is set */
  117.         if(I2C_WaitOnFlagUntilTimeout(I2Cx, I2C_FLAG_BTFF, RESET, I2C_EVT_CHECK_NONE, Timeout) != I2C_OK)
  118.         {
  119.           /* Send STOP Condition */
  120.           I2C_GenerateSTOP(I2Cx, ENABLE);         
  121.           return I2C_ERROR_STEP_9;
  122.         }
  123.         /* Send STOP Condition */
  124.         I2C_GenerateSTOP(I2Cx, ENABLE);
  125.         /* Read data from DR */
  126.         (*pBuffer++) = I2C_ReceiveData(I2Cx);
  127.         NumByte--;        
  128.         /* Read data from DR */
  129.         (*pBuffer++) = I2C_ReceiveData(I2Cx);
  130.         NumByte--;
  131.       }
  132.       /* 3 Last bytes */
  133.       else
  134.       {
  135.         /* Wait until BTF flag is set */
  136.         if(I2C_WaitOnFlagUntilTimeout(I2Cx, I2C_FLAG_BTFF, RESET, I2C_EVT_CHECK_NONE, Timeout) != I2C_OK)
  137.         {
  138.           /* Send STOP Condition */
  139.           I2C_GenerateSTOP(I2Cx, ENABLE);         
  140.           return I2C_ERROR_STEP_10;
  141.         }
  142.         /* Disable Acknowledge */
  143.         I2C_AcknowledgeConfig(I2Cx, DISABLE);

  144.         /* Read data from DR */
  145.         (*pBuffer++) = I2C_ReceiveData(I2Cx);
  146.         NumByte--;

  147.         /* Wait until BTF flag is set */
  148.         if(I2C_WaitOnFlagUntilTimeout(I2Cx, I2C_FLAG_BTFF, RESET, I2C_EVT_CHECK_NONE, Timeout) != I2C_OK)
  149.         {
  150.           /* Send STOP Condition */
  151.           I2C_GenerateSTOP(I2Cx, ENABLE);         
  152.           return I2C_ERROR_STEP_11;
  153.         }

  154.         /* Send STOP Condition */
  155.         I2C_GenerateSTOP(I2Cx, ENABLE);

  156.         /* Read data from DR */
  157.         (*pBuffer++) = I2C_ReceiveData(I2Cx);
  158.         NumByte--;
  159.         
  160.         /* Read data from DR */
  161.         (*pBuffer++) = I2C_ReceiveData(I2Cx);
  162.         NumByte--;
  163.       }
  164.     }
  165.     else
  166.     {
  167.       /* Wait until RXNE flag is set */
  168.       if(I2C_WaitOnFlagUntilTimeout(I2Cx, I2C_FLAG_RDNE, RESET, I2C_EVT_CHECK_NONE, Timeout) != I2C_OK)   
  169.       {
  170.         /* Send STOP Condition */
  171.         I2C_GenerateSTOP(I2Cx, ENABLE);        
  172.         return I2C_ERROR_STEP_12;
  173.       }
  174.       /* Read data from DR */
  175.       (*pBuffer++) = I2C_ReceiveData(I2Cx);
  176.       NumByte--;
  177.     }
  178.   }
  179.   return I2C_OK;
  180. }


  181. I2C_StatusType I2C_Mem_Write(I2C_Type* I2Cx, uint16_t DevAddr, uint16_t DataAddr,  
  182.                                                                 uint8_t *pBuffer, uint16_t NumByte,  uint32_t Timeout)
  183. {
  184.   /* Wait until BUSY flag is reset */
  185.   if(I2C_WaitOnFlagUntilTimeout(I2Cx, I2C_FLAG_BUSYF, SET, I2C_EVT_CHECK_NONE, I2C_TIMEOUT_BUSY_FLAG) != I2C_OK)
  186.   {
  187.     return I2C_ERROR_STEP_1;
  188.   }
  189.   
  190.   /* Disable Pos */
  191.   I2C_NACKPositionConfig(I2Cx, I2C_NACKPosition_Current);  
  192.   /* Send START condition */
  193.   I2C_GenerateSTART(I2Cx, ENABLE);
  194.   /* Wait until SB flag is set */
  195.   if(I2C_WaitOnFlagUntilTimeout(I2Cx, I2C_FLAG_STARTF, RESET, I2C_EVT_CHECK_NONE, Timeout) != I2C_OK)
  196.   {
  197.     /* Send STOP Condition */
  198.     I2C_GenerateSTOP(I2Cx, ENABLE);   
  199.     return I2C_ERROR_STEP_2;
  200.   }
  201.   /* Send slave address for write */
  202.   I2C_Send7bitAddress(I2Cx, (uint8_t)(DevAddr&0xff), I2C_Direction_Transmit);
  203.   /* Wait until ADDR flag is set */
  204.   if(I2C_WaitOnFlagUntilTimeout(I2Cx, I2C_FLAG_ADDRF, RESET, I2C_EVT_CHECK_ACKFAIL, Timeout) != I2C_OK)
  205.   {
  206.     /* Send STOP Condition */
  207.     I2C_GenerateSTOP(I2Cx, ENABLE);   
  208.     return I2C_ERROR_STEP_3;
  209.   }
  210.   
  211.   /* Clear ADDR flag */
  212.   I2C_ClearADDRFlag(I2Cx);
  213.   /* Wait until TDE flag is set */
  214.   if(I2C_WaitOnFlagUntilTimeout(I2Cx, I2C_FLAG_TDE, RESET, I2C_EVT_CHECK_NONE, Timeout) != I2C_OK)
  215.   {
  216.     /* Send STOP Condition */
  217.     I2C_GenerateSTOP(I2Cx, ENABLE);   
  218.     return I2C_ERROR_STEP_4;
  219.   }  
  220.   /* Send Memory Address */
  221.   I2C_SendData(I2Cx, (uint8_t)(DataAddr&0xff));  
  222.   /* Wait until TDE flag is set */
  223.   if(I2C_WaitOnFlagUntilTimeout(I2Cx, I2C_FLAG_TDE, RESET, I2C_EVT_CHECK_ACKFAIL, Timeout) != I2C_OK)
  224.   {
  225.     /* Send STOP Condition */
  226.     I2C_GenerateSTOP(I2Cx, ENABLE);   
  227.     return I2C_ERROR_STEP_5;
  228.   }
  229.   
  230.   while(NumByte > 0)
  231.   {
  232.     /* Wait until TDE flag is set */
  233.     if(I2C_WaitOnFlagUntilTimeout(I2Cx, I2C_FLAG_TDE, RESET, I2C_EVT_CHECK_ACKFAIL, Timeout) != I2C_OK)
  234.     {
  235.       /* Send STOP Condition */
  236.       I2C_GenerateSTOP(I2Cx, ENABLE);      
  237.       return I2C_ERROR_STEP_6;
  238.     }
  239.     /* Write data to DR */
  240.     I2C_SendData(I2Cx, (*pBuffer++));
  241.     NumByte--;
  242.   }

  243.   /* Wait until BTF flag is set */
  244.   if(I2C_WaitOnFlagUntilTimeout(I2Cx, I2C_FLAG_BTFF, RESET, I2C_EVT_CHECK_ACKFAIL, Timeout) != I2C_OK)
  245.   {
  246.     /* Send STOP Condition */
  247.     I2C_GenerateSTOP(I2Cx, ENABLE);   
  248.     return I2C_ERROR_STEP_7;
  249.   }  
  250.   
  251.   /* Send STOP Condition */
  252.   I2C_GenerateSTOP(I2Cx, ENABLE);
  253.   return I2C_OK;
  254. }



通过逻辑分析仪捕的I2C协议时序。
2.jpg


6.jpg

通过串口输出,读取的3个传感器的数值。
3.jpg



代码
游客,如果您要查看本帖隐藏内容请回复







saintloong 发表于 2021-2-9 08:25 | 显示全部楼层
有源码的都是好同志
zhengfish 发表于 2021-2-9 08:40 | 显示全部楼层
kankan
jinglixixi 发表于 2021-2-9 17:16 | 显示全部楼层
学习学习
caizhiwei 发表于 2021-2-10 17:49 | 显示全部楼层
牛X,好家伙我也试试看!
cangbai 发表于 2021-2-14 20:59 | 显示全部楼层
学习学习
wziyi 发表于 2021-2-15 12:36 | 显示全部楼层
学习了
billy_2005 发表于 2021-3-1 14:25 | 显示全部楼层
感谢楼主分享
psh12 发表于 2021-8-2 16:04 | 显示全部楼层
谢谢分享
单片小菜 发表于 2021-8-2 17:03 | 显示全部楼层
看见源码了,一般人都没有看见源码的。没有办法的。
sobadman 发表于 2021-11-30 16:06 | 显示全部楼层
有源码的都是好同志
schow 发表于 2021-11-30 16:51 | 显示全部楼层
有源码的都是好同志
xiang752 发表于 2021-12-21 21:26 | 显示全部楼层
dbayj 发表于 2021-12-28 22:56 | 显示全部楼层
谢谢分享
wifi99 发表于 2022-1-1 21:27 | 显示全部楼层
学习学习
betterchips 发表于 2022-1-10 14:59 | 显示全部楼层
有源码的都是好同志
usysm 发表于 2022-1-11 20:44 | 显示全部楼层
I2C接口的硬件接口吗   
typeof 发表于 2022-1-11 20:54 | 显示全部楼层
使用keil开发的吗   
yujielun 发表于 2022-1-11 20:54 | 显示全部楼层
AT-START-F407性能怎么样  
htmlme 发表于 2022-1-11 20:54 | 显示全部楼层
RPR-0521RS一般都是光敏电阻之类
您需要登录后才可以回帖 登录 | 注册

本版积分规则

127

主题

4778

帖子

28

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