jinglixixi 发表于 2021-1-31 23:31

【ST MEMS防水气压传感器LPS27HHW测评】+例程存在的问题

本帖最后由 jinglixixi 于 2021-2-1 00:02 编辑

<p>在前面的介绍中,对例程进行了相应的分析并介绍了在STM32F411开发板的基础上进行程序移入的处理方法。</p>

<p>在程序移入后,借助串口已能把模拟值按原程序的输出出来,且为其配置了OLED屏来辅助检测值的输出。</p>

<p style="text-align:justify">原指望在短时间内解决驱动的问题,从而能将例程顺利地放到STM32F411开发板上来进行功能检测。但是,这里有关键的问题没有解决,就是原程序缺少一个关键的定义,即语句dev_ctx.handle = &amp;hi2c1;</p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span style="font-size:11.0pt"><span style="font-family:宋体">会因为找不到hi2c1而无法完成编译。</span></span></span></span></p>

<p style="text-align:justify">后来添加了下面的语句就可以通过编译了:</p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span style="font-size:11.0pt"><span style="font-family:宋体">static I2C_HandleTypeDef &nbsp; &nbsp;hi2c1;</span></span></span></span></p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span style="font-size:11.0pt"><span style="font-family:宋体">修改后程序如下:</span></span></span></span></p>

<p>void tx_com( uint8_t *tx_buffer, uint16_t len )</p>

<p>{</p>

<p>&nbsp; &nbsp;&nbsp;&nbsp;HAL_UART_Transmit( &amp;UartHandle, tx_buffer, len, 1000 );</p>

<p>}</p>

<p style="text-align:justify">&nbsp;</p>

<p>int main(void)</p>

<p>{</p>

<p>&nbsp; HAL_Init();</p>

<p>&nbsp; SystemClock_Config();</p>

<p>&nbsp; //&nbsp; #define USARTx&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;USART2</p>

<p>&nbsp; UartHandle.Instance&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; = USARTx;</p>

<p>&nbsp; UartHandle.Init.BaudRate&nbsp;&nbsp;&nbsp;&nbsp; = 9600;</p>

<p>&nbsp; UartHandle.Init.WordLength&nbsp;&nbsp; = UART_WORDLENGTH_9B; &nbsp;//8B</p>

<p>&nbsp; UartHandle.Init.StopBits&nbsp;&nbsp;&nbsp;&nbsp; = UART_STOPBITS_1;</p>

<p>&nbsp; UartHandle.Init.Parity&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; = UART_PARITY_ODD;</p>

<p>&nbsp; UartHandle.Init.HwFlowCtl&nbsp;&nbsp;&nbsp; = UART_HWCONTROL_NONE;</p>

<p>&nbsp; UartHandle.Init.Mode&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; = UART_MODE_TX_RX;</p>

<p>&nbsp; UartHandle.Init.OverSampling = UART_OVERSAMPLING_16;</p>

<p>&nbsp;</p>

<p>&nbsp; if(HAL_UART_Init(&amp;UartHandle) != HAL_OK)</p>

<p>&nbsp; {</p>

<p>&nbsp;&nbsp;&nbsp; Error_Handler();</p>

<p>&nbsp; }</p>

<p>&nbsp; printf(&quot;LPS27HHW &amp; STM32F411 TEST \r\n&quot;);</p>

<p>&nbsp;</p>

<p>&nbsp; stmdev_ctx_t dev_ctx;</p>

<p>&nbsp; dev_ctx.write_reg = platform_write;</p>

<p>&nbsp; dev_ctx.read_reg = platform_read;</p>

<p>&nbsp;dev_ctx.handle = &amp;hi2c1;&nbsp; &nbsp; &nbsp; // ***</p>

<p>&nbsp; whoamI = 0;</p>

<p>&nbsp; lps27hhw_device_id_get(&amp;dev_ctx, &amp;whoamI);</p>

<p>&nbsp; if ( whoamI != LPS27HHW_ID )</p>

<p>&nbsp;&nbsp;&nbsp; while(1);</p>

<p>&nbsp; lps27hhw_reset_set(&amp;dev_ctx, PROPERTY_ENABLE);</p>

<p>&nbsp; do {</p>

<p>&nbsp;&nbsp;&nbsp; lps27hhw_reset_get(&amp;dev_ctx, &amp;rst);</p>

<p>&nbsp; } while (rst);</p>

<p>&nbsp;</p>

<p>&nbsp; lps27hhw_block_data_update_set(&amp;dev_ctx, PROPERTY_ENABLE);</p>

<p>&nbsp; lps27hhw_data_rate_set(&amp;dev_ctx, LPS27HHW_10_Hz_LOW_NOISE);</p>

<p>&nbsp;</p>

<p>&nbsp; while(1)</p>

<p>&nbsp; {</p>

<p>&nbsp;&nbsp;&nbsp; lps27hhw_reg_t reg;</p>

<p>&nbsp;&nbsp;&nbsp; lps27hhw_read_reg(&amp;dev_ctx, LPS27HHW_STATUS, (uint8_t *)&amp;reg, 1);</p>

<p>&nbsp;&nbsp;&nbsp;&nbsp;if (reg.status.p_da)</p>

<p>&nbsp;&nbsp;&nbsp; {</p>

<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; memset(data_raw_pressure.u8bit, 0x00, sizeof(int32_t));</p>

<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; lps27hhw_pressure_raw_get(&amp;dev_ctx, data_raw_pressure.u8bit);</p>

<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pressure_hPa = lps27hhw_from_lsb_to_hpa( data_raw_pressure.i32bit);</p>

<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sprintf((char*)tx_buffer, &quot;pressure :%6.2f\r\n&quot;, pressure_hPa);</p>

<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; tx_com( tx_buffer, strlen( (char const*)tx_buffer ) );</p>

<p>&nbsp;&nbsp;&nbsp; }</p>

<p>&nbsp;&nbsp;&nbsp;&nbsp;if (reg.status.t_da)</p>

<p>&nbsp;&nbsp;&nbsp; {</p>

<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; memset(data_raw_temperature.u8bit, 0x00, sizeof(int16_t));</p>

<p>&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;lps27hhw_temperature_raw_get(&amp;dev_ctx, data_raw_temperature.u8bit);</p>

<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; temperature_degC = lps27hhw_from_lsb_to_celsius( data_raw_temperature.i16bit );</p>

<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sprintf((char*)tx_buffer, &quot;temperature :%6.2f\r\n&quot;, temperature_degC );</p>

<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; tx_com( tx_buffer, strlen( (char const*)tx_buffer ) );</p>

<p>&nbsp;&nbsp;&nbsp; }</p>

<p>&nbsp; }</p>

<p>}</p>

<p style="text-align:justify">&nbsp;</p>

threebb 发表于 2021-2-1 09:37

66666666666666

littleshrimp 发表于 2021-2-1 10:16

<p>STM32和传感器通信需要配置I2C接口,例程里是默认配置,针对它原来的测试环境,在你的板子上运行需要根据你的配置来设置handle</p>

littleshrimp 发表于 2021-2-1 16:28

本帖最后由 littleshrimp 于 2021-2-1 16:29 编辑

<p>你帖子里的代码&ldquo;UartHandle&rdquo;实际上就和&ldquo;hi2c1&rdquo;类似,你可以参照UartHandle来实现STM32的I2C配置,然后把这个handle传递给dev_ctx.handle。</p>

<p>或者你如果你觉得这个handle不好理解,用着不习惯,你还可以看一下read_data_simple.c里的platform_write和platform_read它实际上还是调用了STM32的I2C或者SPI接口,如果你正确的配置了STM32对应的接口,比如I2C,那么你可以改成下图这样,让传感器调用platform_read函数时能够调用对应的接口把数据读出来。</p>

<p></p>

jinglixixi 发表于 2021-2-1 20:16

littleshrimp 发表于 2021-2-1 16:28
你帖子里的代码&ldquo;UartHandle&rdquo;实际上就和&ldquo;hi2c1&rdquo;类似,你可以参照UartHandle来实现S ...

<p>是这样,当时我也是将UartHandle的tx_com赋给dev_ctx.handle来通过编译的。但构建一个像它那样的函数就比较困难了,因为无法知道该函数的功能要求及结构。其实例程中应该给出相应的函数,至于用户想怎样用,仿照改造就是。这实在是种缺陷!</p>

littleshrimp 发表于 2021-2-2 10:22

jinglixixi 发表于 2021-2-1 20:16
是这样,当时我也是将UartHandle的tx_com赋给dev_ctx.handle来通过编译的。但构建一个像它那样的函数就比 ...

<p>如何构建一个i2c的handle这个属于单片机的部分,一个传感器驱动它要适合不同的平台,官方提供了STM32的主要代码,如果你对STM32了解的话,怎么配置I2C接口应该是一件非常容易的事,就像你之前配置UART一样,它和传感器无关。</p>

<p>对于STM32你可以参考官方的I2C例程,或者最简单的方法就是通过STM32CubeMX配置I2C接口,点几下鼠标再点生成代码一个带有I2C配置的工程就创建好了,你要做的就是将生成的工程里的i2c的handle传递给传感器驱动。</p>

jinglixixi 发表于 2021-2-2 10:34

<div class='shownolgin' data-isdigest='no'><p>好的服务比说啥都重要!!!</p>
</div><script>showreplylogin();</script><script type="text/javascript">(function(d,c){var a=d.createElement("script"),m=d.getElementsByTagName("script"),eewurl="//counter.eeworld.com.cn/pv/count/";a.src=eewurl+c;m.parentNode.insertBefore(a,m)})(document,523)</script>
页: [1]
查看完整版本: 【ST MEMS防水气压传感器LPS27HHW测评】+例程存在的问题