Skip to content

天空星用 SHT30 采集温湿度并刷新 LCD ​

每轮读取 SHT30 的温度和湿度,将结果画在 ILI9341 上。上行是温度,下行是湿度,底部色块表示本轮数据是否有效。

准备模块和接线 ​

  • 天空星 STM32F407VET6 和 3.3 V 兼容的 SHT30 模块。PB6 接 SCL,PB7 接 SDA,ADDR 接低选择 0x44,总线加到 3.3 V 的上拉,模块与板共地。
  • 16 位 8080 接口 ILI9341 按并口屏示例接线。并口使用 PD、PE 和 PC6,可与这里的 I²C1 同时使用。

沿执行路径读代码 ​

  1. main() 初始化 LCD 和 I²C1,保存两份状态。初始化失败时停在主循环前。
  2. measure() 发送 24 00,等待 20 ms,再收六字节。温度的两个字节和 CRC 在前,湿度和 CRC 在后。
  3. sht30_decode() 先检查两组 CRC,再换算为 0.1 °C 和 0.1 %RH。主循环用一组新值更新两条图,并把状态块画成绿色。
  4. 发送、接收或 CRC 失败时,条形图保留上次成功值,状态块变红。等待 500 ms 后继续采样。

完整源码 ​

下载 main.c · 下载 CRC 与换算 · 下载主机检查 · 下载构建文件

c
#include "../../stm32/fsmc/common/ili9341.h"
#include "sample.h"

enum { ADDRESS = 0x44U << 1, BAR_X = 20U, BAR_WIDTH = 200U };
enum { SAMPLE_OK, SAMPLE_TX_FAILED, SAMPLE_RX_FAILED, SAMPLE_BAD_CRC };

static I2C_HandleTypeDef bus;
static volatile HAL_StatusTypeDef lcd_init_status;
static volatile HAL_StatusTypeDef i2c_init_status;
static volatile unsigned sample_status;
static volatile uint32_t samples_ok;
static volatile uint32_t samples_failed;
static volatile Sht30Sample last_sample;

void SysTick_Handler(void) { HAL_IncTick(); }

static HAL_StatusTypeDef bus_init(void)
{
  __HAL_RCC_GPIOB_CLK_ENABLE();
  __HAL_RCC_I2C1_CLK_ENABLE();
  GPIO_InitTypeDef pins = {
    .Pin = GPIO_PIN_6 | GPIO_PIN_7, .Mode = GPIO_MODE_AF_OD,
    .Speed = GPIO_SPEED_FREQ_LOW, .Alternate = GPIO_AF4_I2C1,
  };
  HAL_GPIO_Init(GPIOB, &pins);
  bus.Instance = I2C1;
  bus.Init = (I2C_InitTypeDef){
    .ClockSpeed = 100000U, .DutyCycle = I2C_DUTYCYCLE_2,
    .AddressingMode = I2C_ADDRESSINGMODE_7BIT,
  };
  return HAL_I2C_Init(&bus);
}

static unsigned measure(Sht30Sample *sample)
{
  // 1. 启动一次高重复性、无时钟拉伸的测量。发送失败时这轮到此结束。
  uint8_t command[] = {0x24U, 0x00U};
  if (HAL_I2C_Master_Transmit(&bus, ADDRESS, command, sizeof(command), 30U) != HAL_OK)
  {
    return SAMPLE_TX_FAILED;
  }

  // 2. 转换完成后一次读六个字节。两组 CRC 均通过才把本轮温湿度交给主循环。
  HAL_Delay(20U);
  uint8_t frame[6];
  if (HAL_I2C_Master_Receive(&bus, ADDRESS, frame, sizeof(frame), 30U) != HAL_OK)
  {
    return SAMPLE_RX_FAILED;
  }
  return sht30_decode(frame, sample) ? SAMPLE_OK : SAMPLE_BAD_CRC;
}

static void draw_bar(uint16_t y, uint16_t filled, uint16_t color)
{
  if (filled > 0)
  {
    ili9341_fill(BAR_X, y, BAR_X + filled - 1U, y + 19U, color);
  }
  if (filled < BAR_WIDTH)
  {
    ili9341_fill(BAR_X + filled, y, BAR_X + BAR_WIDTH - 1U, y + 19U, 0x2104U);
  }
}

int main(void)
{
  // 1. 初始化时基和两条总线。任一初始化失败时保留状态,由调试器读取。
  HAL_Init();
  lcd_init_status = ili9341_init();
  i2c_init_status = bus_init();
  if (lcd_init_status != HAL_OK || i2c_init_status != HAL_OK)
  {
    for (;;) {}
  }
  ili9341_fill(0U, 0U, LCD_WIDTH - 1U, LCD_HEIGHT - 1U, 0x0000U);

  for (;;)
  {
    // 2. measure 交付同一轮的两项有效读数。主循环据此更新两行条形图和调试快照。
    Sht30Sample next;
    sample_status = measure(&next);
    if (sample_status == SAMPLE_OK)
    {
      last_sample = next;
      ++samples_ok;
      draw_bar(50U, bar_width(next.temperature_tenths, 500U, BAR_WIDTH), 0xFD20U);
      draw_bar(100U, bar_width(next.humidity_tenths, 1000U, BAR_WIDTH), 0x07FFU);
      ili9341_fill(20U, 145U, 39U, 164U, 0x07E0U);
    }
    else
    {
      // 3. 失败时保留上次的条形图,红色状态块提示它们已过期。下一轮继续采样。
      ++samples_failed;
      ili9341_fill(20U, 145U, 39U, 164U, 0xF800U);
    }
    HAL_Delay(500U);
  }
}
c
#ifndef KB_SHT30_SAMPLE_H
#define KB_SHT30_SAMPLE_H

#include <stdbool.h>
#include <stdint.h>

typedef struct {
  int16_t temperature_tenths;
  uint16_t humidity_tenths;
} Sht30Sample;

static inline uint8_t sht30_crc(const uint8_t word[2])
{
  uint8_t crc = 0xFFU;
  for (unsigned i = 0; i < 2U; ++i)
  {
    crc ^= word[i];
    for (unsigned bit = 0; bit < 8U; ++bit)
    {
      const uint8_t polynomial = (crc & 0x80U) != 0U ? 0x31U : 0U;
      crc = (uint8_t)((crc << 1U) ^ polynomial);
    }
  }
  return crc;
}

// 两组 CRC 都通过才更新输出。单位分别为 0.1 °C 和 0.1 %RH。
static inline bool sht30_decode(const uint8_t frame[6], Sht30Sample *out)
{
  if (sht30_crc(frame) != frame[2] || sht30_crc(frame + 3) != frame[5])
  {
    return false;
  }
  const uint32_t temperature = ((uint32_t)frame[0] << 8U) | frame[1];
  const uint32_t humidity = ((uint32_t)frame[3] << 8U) | frame[4];
  out->temperature_tenths = (int16_t)((int)((temperature * 1750U + 32767U) / 65535U) - 450);
  out->humidity_tenths = (uint16_t)((humidity * 1000U + 32767U) / 65535U);
  return true;
}

// [0, maximum] 映射到 [0, width] 个像素。负值和超范围值在两端截断。
static inline uint16_t bar_width(int value, uint16_t maximum, uint16_t width)
{
  if (value <= 0) { return 0; }
  if ((unsigned)value >= maximum) { return width; }
  return (uint16_t)(((uint32_t)value * width) / maximum);
}

#endif

编译与观察 ​

sh
cmake -S examples/projects/sht30-display -B .work/sky-sht30-display -G Ninja -DCUBE_F4_ROOT="D:/SDK/STM32Cube_FW_F4_V1.28.3"
cmake --build .work/sky-sht30-display

通过 SWD 加载 .work/sky-sht30-display/sky-sht30-display.elf。橙色条将 0~50 °C 映射到 0~200 像素,青色条将 0~100 %RH 映射到同样宽度。超出范围会截断,完整测量值在 last_sample。看 sample_status 可分辨发送、接收和 CRC 错误。

练习 ​

预测题 湿度 CRC 错误,温度 CRC 正确。这一轮屏幕会怎样?

核对预测

两条图都保留上次成功值,状态块变红,samples_failed 加一。等两组 CRC 都通过再一起更新。

改动题 将温度的满量程改为 40 °C,预测 25 °C 时的宽度。

核对改法

将主循环传给 bar_width() 的最大值改为 400U。输入值 250 经 250 × 200 / 400 得 125 像素。可把该输入补入 check.c。

验证记录 ​

已用 STM32CubeF4 V1.28.3 交叉编译链接。主机核对手册的 BE EF → 92 CRC、坏帧、负温度、湿度满量程及条形图边界。屏幕、模块和断线恢复待实板。

sh
mkdir -p .work
cc -std=c11 -Wall -Wextra -Werror examples/projects/sht30-display/check.c -o .work/sht30-check
.work/sht30-check

命令、时序和 CRC 见 Sensirion SHT3x-DIS 数据手册。