[STM32L4+] 【STEVAL-STWINKT1B测评】2、使用温度传感器STTS751读取温度

[复制链接]
 楼主| xinmeng_wit 发表于 2024-8-16 21:14 | 显示全部楼层 |阅读模式
<

一、原理图确认

STWINKT1B开发板板载了两个温度传感器,一个是STTS751,另一个是U2HTS221。其中U2HTS221还可以测量湿度。
本次使用STTS751传感器来测量温度。

该STTS751是一个数字温度传感器,通过 2 线 SMBus 2.0 兼容总线进行通信。温度测量时,用户可配置分辨率在 9 到 12 位之间。在 9 位时,最小步长为 0.5 °C,在 12 位时,最小步长为 0.0625 °C。 在默认分辨率(10 位,0.25 °C/LSB)下,标称转换时间为 21 毫秒。多达 8 个器件可以无歧义地共享同一个 2 线 SMBus,从而允许单个应用监控多个温度区。

先来确认一下该芯片(STTS751)与MCU的引脚连接信息。

1.jpg

2.png
由以上原理图可以确认,温度传感器使用的是I2C接口,接在MCU的PF0和PF1。

另外,为了能够显示测量的温度值,需要使用STlink上的一路串口(USART2)。
引脚位置如下图:
3.png

由原理图可以看出来USART2使用的是MCU引脚的PD5和PD6引脚,并且直接通过STLINK输出,无需外部接线,很方便。

二、cubeMX配置
1、I2C2配置
速度配置为100kHz
上升时间和下降时间分别为250ns和250ns。,其它的维持默认即可
4.png

2、USART2配置
主要是波特率相关参数的配置,其它的保持默认就可以了。
5.png

三、代码编写
1、驱动
关于传感器的驱动程序,ST官方提供了各种传感器的驱动程序,可以直接使用,完全无需重复造轮子。
ST官方驱动github地址:https://github.com/STMicroelectronics/STMems_Standard_C_drivers/tree/master/stts751_STdC
6.png
2、应用程序
ST的官方也给出了一个应用程序的example,可以拿来稍做修改就能在自己项目中使用。
7.png

修改后的应用程序如下:
  1. /*
  2. * This example was developed using the following STMicroelectronics
  3. * evaluation boards:
  4. *
  5. * - NUCLEO_F401RE + X-NUCLEO-IKS01A3
  6. * - DISCOVERY_SPC584B + STEVAL-MKI198V1K
  7. *
  8. * Used interfaces:
  9. *
  10. * NUCLEO_STM32F401RE - Host side: UART(COM) to USB bridge
  11. *                    - I2C(Default) / SPI(supported)
  12. *
  13. * DISCOVERY_SPC584B  - Host side: UART(COM) to USB bridge
  14. *                    - Sensor side: I2C(Default) / SPI(supported)
  15. *
  16. * If you need to run this example on a different hardware platform a
  17. * modification of the functions: `platform_write`, `platform_read`,
  18. * `tx_com` and 'platform_init' is required.
  19. *
  20. */

  21. /* STMicroelectronics evaluation boards definition
  22. *
  23. * Please uncomment ONLY the evaluation boards in use.
  24. * If a different hardware is used please comment all
  25. * following target board and redefine yours.
  26. */

  27. //#define NUCLEO_F401RE    /* little endian */
  28. //#define SPC584B_DIS      /* big endian */

  29. /* ATTENTION: By default the driver is little endian. If you need switch
  30. *            to big endian please see "Endianness definitions" in the
  31. *            header file of the driver (_reg.h).
  32. */

  33. #define SENSOR_BUS hi2c2

  34. /* Includes ------------------------------------------------------------------*/
  35. #include <string.h>
  36. #include <stdio.h>
  37. #include "stts751_reg.h"
  38. #include "i2c.h"
  39. #include "usart.h"

  40. /* Private macro -------------------------------------------------------------*/

  41. /* Private variables ---------------------------------------------------------*/
  42. static int16_t data_raw_temperature;
  43. static float temperature_degC;
  44. static stts751_id_t whoamI;
  45. static uint8_t tx_buffer[1000];
  46. stmdev_ctx_t dev_ctx;

  47. /* Extern variables ----------------------------------------------------------*/

  48. /* Private functions ---------------------------------------------------------*/
  49. /*
  50. *   WARNING:
  51. *   Functions declare in this section are defined at the end of this file
  52. *   and are strictly related to the hardware platform used.
  53. *
  54. */
  55. static int32_t platform_write(void *handle, uint8_t reg, const uint8_t *bufp,
  56.                               uint16_t len);
  57. static int32_t platform_read(void *handle, uint8_t reg, uint8_t *bufp,
  58.                              uint16_t len);
  59. static void tx_com( uint8_t *tx_buffer, uint16_t len );
  60. static void platform_delay(uint32_t ms);
  61. static void platform_init(void);

  62. /* Main Example --------------------------------------------------------------*/
  63. void stts751_read_data_polling(void)
  64. {
  65.     /* Read output only if not busy */
  66.     uint8_t flag;
  67.     stts751_flag_busy_get(&dev_ctx, &flag);

  68.     if (flag) {
  69.       /* Read temperature data */
  70.       memset(&data_raw_temperature, 0, sizeof(int16_t));
  71.       stts751_temperature_raw_get(&dev_ctx, &data_raw_temperature);
  72.       temperature_degC = stts751_from_lsb_to_celsius(
  73.                            data_raw_temperature);
  74.       sprintf((char *)tx_buffer, "Temperature [degC]:%3.2f\r\n",
  75.               temperature_degC);
  76.       tx_com(tx_buffer, strlen((char const *)tx_buffer));
  77.     }
  78. }

  79. void stts751_init( void )
  80. {
  81.   /* Initialize mems driver interface */
  82.   dev_ctx.write_reg = platform_write;
  83.   dev_ctx.read_reg = platform_read;
  84.   dev_ctx.mdelay = platform_delay;
  85.   dev_ctx.handle = &SENSOR_BUS;
  86.   /* Initialize platform specific hardware */
  87.   platform_init();
  88.   /* Check device ID */
  89.   stts751_device_id_get(&dev_ctx, &whoamI);

  90.   if ( (whoamI.product_id != STTS751_ID_0xxxx) ||
  91.        //(whoamI.product_id != STTS751_ID_1xxxx) ||
  92.        (whoamI.manufacturer_id != STTS751_ID_MAN) ||
  93.        (whoamI.revision_id != STTS751_REV) )
  94.     while (1); /* manage here device not found */

  95.   /* Enable interrupt on high(=49.5 degC)/low(=-4.5 degC) temperature. */
  96.   float temperature_high_limit = 49.5f;
  97.   stts751_high_temperature_threshold_set(&dev_ctx,
  98.                                          stts751_from_celsius_to_lsb(temperature_high_limit));
  99.   float temperature_low_limit = -4.5f;
  100.   stts751_low_temperature_threshold_set(&dev_ctx,
  101.                                         stts751_from_celsius_to_lsb(temperature_low_limit));
  102.   stts751_pin_event_route_set(&dev_ctx,  PROPERTY_ENABLE);
  103.   /* Set Output Data Rate */
  104.   stts751_temp_data_rate_set(&dev_ctx, STTS751_TEMP_ODR_1Hz);
  105.   /* Set Resolution */
  106.   stts751_resolution_set(&dev_ctx, STTS751_11bit);
  107. }

  108. /*
  109. * [url=home.php?mod=space&uid=247401]@brief[/url]  Write generic device register (platform dependent)
  110. *
  111. * @param  handle    customizable argument. In this examples is used in
  112. *                   order to select the correct sensor bus handler.
  113. * @param  reg       register to write
  114. * @param  bufp      pointer to data to write in register reg
  115. * @param  len       number of consecutive register to write
  116. *
  117. */
  118. static int32_t platform_write(void *handle, uint8_t reg, const uint8_t *bufp,
  119.                               uint16_t len)
  120. {
  121.   HAL_I2C_Mem_Write(handle, STTS751_0xxxx_ADD_7K5, reg,
  122.                     I2C_MEMADD_SIZE_8BIT, (uint8_t*) bufp, len, 1000);
  123.   return 0;
  124. }

  125. /*
  126. * @brief  Read generic device register (platform dependent)
  127. *
  128. * @param  handle    customizable argument. In this examples is used in
  129. *                   order to select the correct sensor bus handler.
  130. * @param  reg       register to read
  131. * @param  bufp      pointer to buffer that store the data read
  132. * @param  len       number of consecutive register to read
  133. *
  134. */
  135. static int32_t platform_read(void *handle, uint8_t reg, uint8_t *bufp,
  136.                              uint16_t len)
  137. {
  138.   HAL_I2C_Mem_Read(handle, STTS751_0xxxx_ADD_7K5, reg,
  139.                    I2C_MEMADD_SIZE_8BIT, bufp, len, 1000);
  140.   return 0;
  141. }

  142. /*
  143. * @brief  Send buffer to console (platform dependent)
  144. *
  145. * @param  tx_buffer     buffer to transmit
  146. * @param  len           number of byte to send
  147. *
  148. */
  149. static void tx_com(uint8_t *tx_buffer, uint16_t len)
  150. {
  151.   HAL_UART_Transmit(&huart2, tx_buffer, len, 1000);
  152. }

  153. /*
  154. * @brief  platform specific delay (platform dependent)
  155. *
  156. * @param  ms        delay in ms
  157. *
  158. */
  159. static void platform_delay(uint32_t ms)
  160. {
  161.   HAL_Delay(ms);
  162. }

  163. /*
  164. * @brief  platform specific initialization (platform dependent)
  165. */
  166. static void platform_init(void)
  167. {
  168. }
然后在main函数中,调用温度读取函数:
8.png

四、运行效果
9.png

以上,温度传感器STTS751的驱动和数据读取与显示。
2.png
狄克爱老虎油 发表于 2024-8-17 22:29 来自手机 | 显示全部楼层
这个传感器精度高吗
 楼主| xinmeng_wit 发表于 2024-8-19 08:57 | 显示全部楼层

±0.5℃
呐咯密密 发表于 2024-9-20 10:08 | 显示全部楼层
开放的这个库太方便了,只要几行代码就搞定了
csv7k1 发表于 2024-9-23 17:46 来自手机 | 显示全部楼层
使用STTS751传感器来测量温度
szt1993 发表于 2024-9-24 08:56 | 显示全部楼层
很简洁的历程,适合借鉴开发
您需要登录后才可以回帖 登录 | 注册

本版积分规则

70

主题

276

帖子

2

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