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

一、原理图确认

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的引脚连接信息。

 



 


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

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



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

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



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



三、代码编写
1、驱动
关于传感器的驱动程序,ST官方提供了各种传感器的驱动程序,可以直接使用,完全无需重复造轮子。
ST官方驱动github地址:https://github.com/STMicroelectronics/STMems_Standard_C_drivers/tree/master/stts751_STdC
 


2、应用程序
ST的官方也给出了一个应用程序的example,可以拿来稍做修改就能在自己项目中使用。
 



修改后的应用程序如下:

复制

/*

 * This example was developed using the following STMicroelectronics

 * evaluation boards:

 *

 * - NUCLEO_F401RE + X-NUCLEO-IKS01A3

 * - DISCOVERY_SPC584B + STEVAL-MKI198V1K

 *

 * Used interfaces:

 *

 * NUCLEO_STM32F401RE - Host side: UART(COM) to USB bridge

 *                    - I2C(Default) / SPI(supported)

 *

 * DISCOVERY_SPC584B  - Host side: UART(COM) to USB bridge

 *                    - Sensor side: I2C(Default) / SPI(supported)

 *

 * If you need to run this example on a different hardware platform a

 * modification of the functions: `platform_write`, `platform_read`,

 * `tx_com` and 'platform_init' is required.

 *

 */



/* STMicroelectronics evaluation boards definition

 *

 * Please uncomment ONLY the evaluation boards in use.

 * If a different hardware is used please comment all

 * following target board and redefine yours.

 */



//#define NUCLEO_F401RE    /* little endian */

//#define SPC584B_DIS      /* big endian */



/* ATTENTION: By default the driver is little endian. If you need switch

 *            to big endian please see "Endianness definitions" in the

 *            header file of the driver (_reg.h).

 */



#define SENSOR_BUS hi2c2



/* Includes ------------------------------------------------------------------*/

#include <string.h>

#include <stdio.h>

#include "stts751_reg.h"

#include "i2c.h"

#include "usart.h"



/* Private macro -------------------------------------------------------------*/



/* Private variables ---------------------------------------------------------*/

static int16_t data_raw_temperature;

static float temperature_degC;

static stts751_id_t whoamI;

static uint8_t tx_buffer[1000];

stmdev_ctx_t dev_ctx;



/* Extern variables ----------------------------------------------------------*/



/* Private functions ---------------------------------------------------------*/

/*

 *   WARNING:

 *   Functions declare in this section are defined at the end of this file

 *   and are strictly related to the hardware platform used.

 *

 */

static int32_t platform_write(void *handle, uint8_t reg, const uint8_t *bufp,

                              uint16_t len);

static int32_t platform_read(void *handle, uint8_t reg, uint8_t *bufp,

                             uint16_t len);

static void tx_com( uint8_t *tx_buffer, uint16_t len );

static void platform_delay(uint32_t ms);

static void platform_init(void);



/* Main Example --------------------------------------------------------------*/

void stts751_read_data_polling(void)

{

    /* Read output only if not busy */

    uint8_t flag;

    stts751_flag_busy_get(&dev_ctx, &flag);



    if (flag) {

      /* Read temperature data */

      memset(&data_raw_temperature, 0, sizeof(int16_t));

      stts751_temperature_raw_get(&dev_ctx, &data_raw_temperature);

      temperature_degC = stts751_from_lsb_to_celsius(

                           data_raw_temperature);

      sprintf((char *)tx_buffer, "Temperature [degC]:%3.2f\r\n",

              temperature_degC);

      tx_com(tx_buffer, strlen((char const *)tx_buffer));

    }

}



void stts751_init( void )

{

  /* Initialize mems driver interface */

  dev_ctx.write_reg = platform_write;

  dev_ctx.read_reg = platform_read;

  dev_ctx.mdelay = platform_delay;

  dev_ctx.handle = &SENSOR_BUS;

  /* Initialize platform specific hardware */

  platform_init();

  /* Check device ID */

  stts751_device_id_get(&dev_ctx, &whoamI);



  if ( (whoamI.product_id != STTS751_ID_0xxxx) ||

       //(whoamI.product_id != STTS751_ID_1xxxx) ||

       (whoamI.manufacturer_id != STTS751_ID_MAN) ||

       (whoamI.revision_id != STTS751_REV) )

    while (1); /* manage here device not found */



  /* Enable interrupt on high(=49.5 degC)/low(=-4.5 degC) temperature. */

  float temperature_high_limit = 49.5f;

  stts751_high_temperature_threshold_set(&dev_ctx,

                                         stts751_from_celsius_to_lsb(temperature_high_limit));

  float temperature_low_limit = -4.5f;

  stts751_low_temperature_threshold_set(&dev_ctx,

                                        stts751_from_celsius_to_lsb(temperature_low_limit));

  stts751_pin_event_route_set(&dev_ctx,  PROPERTY_ENABLE);

  /* Set Output Data Rate */

  stts751_temp_data_rate_set(&dev_ctx, STTS751_TEMP_ODR_1Hz);

  /* Set Resolution */

  stts751_resolution_set(&dev_ctx, STTS751_11bit);

}



/*

 * [url=home.php?mod=space&uid=247401]@brief[/url]  Write generic device register (platform dependent)

 *

 * @param  handle    customizable argument. In this examples is used in

 *                   order to select the correct sensor bus handler.

 * @param  reg       register to write

 * @param  bufp      pointer to data to write in register reg

 * @param  len       number of consecutive register to write

 *

 */

static int32_t platform_write(void *handle, uint8_t reg, const uint8_t *bufp,

                              uint16_t len)

{

  HAL_I2C_Mem_Write(handle, STTS751_0xxxx_ADD_7K5, reg,

                    I2C_MEMADD_SIZE_8BIT, (uint8_t*) bufp, len, 1000);

  return 0;

}



/*

 * @brief  Read generic device register (platform dependent)

 *

 * @param  handle    customizable argument. In this examples is used in

 *                   order to select the correct sensor bus handler.

 * @param  reg       register to read

 * @param  bufp      pointer to buffer that store the data read

 * @param  len       number of consecutive register to read

 *

 */

static int32_t platform_read(void *handle, uint8_t reg, uint8_t *bufp,

                             uint16_t len)

{

  HAL_I2C_Mem_Read(handle, STTS751_0xxxx_ADD_7K5, reg,

                   I2C_MEMADD_SIZE_8BIT, bufp, len, 1000);

  return 0;

}



/*

 * @brief  Send buffer to console (platform dependent)

 *

 * @param  tx_buffer     buffer to transmit

 * @param  len           number of byte to send

 *

 */

static void tx_com(uint8_t *tx_buffer, uint16_t len)

{

  HAL_UART_Transmit(&huart2, tx_buffer, len, 1000);

}



/*

 * @brief  platform specific delay (platform dependent)

 *

 * @param  ms        delay in ms

 *

 */

static void platform_delay(uint32_t ms)

{

  HAL_Delay(ms);

}



/*

 * @brief  platform specific initialization (platform dependent)

 */

static void platform_init(void)

{

}

然后在main函数中,调用温度读取函数:
 



四、运行效果
 



以上,温度传感器STTS751的驱动和数据读取与显示。

2.png (485.29 KB )

2.png


---------------------
作者:xinmeng_wit
链接:https://bbs.21ic.com/icview-3396498-1-1.html
来源:21ic.com
此文章已获得原创/原创奖标签,著作权归21ic所有,任何人未经允许禁止转载。 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值