- /*
- * 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的驱动和数据读取与显示。