STM32F103RCT6 SHT10驱动程序 STM32 HAL库
时间: 2023-06-13 12:07:43 浏览: 159
STM32 HAL库 SHT32工程文件
以下是一个简单的 SHT10 温湿度传感器的驱动程序,基于 STM32 HAL 库:
```c
#include "stm32f1xx_hal.h"
#define SHT10_DATA_PIN GPIO_PIN_0
#define SHT10_DATA_PORT GPIOA
#define SHT10_CLK_PIN GPIO_PIN_1
#define SHT10_CLK_PORT GPIOA
#define SHT10_CMD_MEASURE_TEMP 0b00000011
#define SHT10_CMD_MEASURE_HUMI 0b00000101
#define SHT10_CMD_READ_STATUS 0b00000111
#define SHT10_CMD_SOFT_RESET 0b00011110
void SHT10_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
__HAL_RCC_GPIOA_CLK_ENABLE();
GPIO_InitStruct.Pin = SHT10_DATA_PIN | SHT10_CLK_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(SHT10_DATA_PORT, &GPIO_InitStruct);
HAL_GPIO_WritePin(SHT10_DATA_PORT, SHT10_DATA_PIN, GPIO_PIN_SET);
HAL_GPIO_WritePin(SHT10_CLK_PORT, SHT10_CLK_PIN, GPIO_PIN_SET);
HAL_Delay(11);
SHT10_SendCommand(SHT10_CMD_SOFT_RESET);
}
void SHT10_SendByte(uint8_t data)
{
uint8_t i;
for (i = 0; i < 8; i++)
{
if (data & 0x80)
{
HAL_GPIO_WritePin(SHT10_DATA_PORT, SHT10_DATA_PIN, GPIO_PIN_SET);
}
else
{
HAL_GPIO_WritePin(SHT10_DATA_PORT, SHT10_DATA_PIN, GPIO_PIN_RESET);
}
data <<= 1;
HAL_GPIO_WritePin(SHT10_CLK_PORT, SHT10_CLK_PIN, GPIO_PIN_SET);
HAL_GPIO_WritePin(SHT10_CLK_PORT, SHT10_CLK_PIN, GPIO_PIN_RESET);
}
HAL_GPIO_WritePin(SHT10_DATA_PORT, SHT10_DATA_PIN, GPIO_PIN_SET);
HAL_GPIO_WritePin(SHT10_CLK_PORT, SHT10_CLK_PIN, GPIO_PIN_SET);
HAL_GPIO_WritePin(SHT10_CLK_PORT, SHT10_CLK_PIN, GPIO_PIN_RESET);
}
uint8_t SHT10_ReadByte(uint8_t ack)
{
uint8_t i, data = 0;
HAL_GPIO_WritePin(SHT10_DATA_PORT, SHT10_DATA_PIN, GPIO_PIN_SET);
for (i = 0; i < 8; i++)
{
HAL_GPIO_WritePin(SHT10_CLK_PORT, SHT10_CLK_PIN, GPIO_PIN_SET);
data <<= 1;
if (HAL_GPIO_ReadPin(SHT10_DATA_PORT, SHT10_DATA_PIN))
{
data |= 0x01;
}
HAL_GPIO_WritePin(SHT10_CLK_PORT, SHT10_CLK_PIN, GPIO_PIN_RESET);
}
if (ack)
{
HAL_GPIO_WritePin(SHT10_DATA_PORT, SHT10_DATA_PIN, GPIO_PIN_RESET);
}
HAL_GPIO_WritePin(SHT10_CLK_PORT, SHT10_CLK_PIN, GPIO_PIN_SET);
HAL_GPIO_WritePin(SHT10_CLK_PORT, SHT10_CLK_PIN, GPIO_PIN_RESET);
HAL_GPIO_WritePin(SHT10_DATA_PORT, SHT10_DATA_PIN, GPIO_PIN_SET);
return data;
}
void SHT10_SendCommand(uint8_t cmd)
{
uint8_t i;
HAL_GPIO_WritePin(SHT10_DATA_PORT, SHT10_DATA_PIN, GPIO_PIN_SET);
HAL_GPIO_WritePin(SHT10_CLK_PORT, SHT10_CLK_PIN, GPIO_PIN_RESET);
SHT10_SendByte(cmd);
for (i = 0; i < 100; i++)
{
HAL_Delay(1);
if (HAL_GPIO_ReadPin(SHT10_DATA_PORT, SHT10_DATA_PIN) == GPIO_PIN_RESET)
{
break;
}
}
HAL_GPIO_WritePin(SHT10_CLK_PORT, SHT10_CLK_PIN, GPIO_PIN_SET);
HAL_GPIO_WritePin(SHT10_CLK_PORT, SHT10_CLK_PIN, GPIO_PIN_RESET);
}
uint16_t SHT10_ReadData(uint8_t cmd)
{
uint16_t data;
SHT10_SendCommand(cmd);
data = SHT10_ReadByte(1);
data <<= 8;
data |= SHT10_ReadByte(0);
return data;
}
float SHT10_ReadTemperature(void)
{
uint16_t raw = SHT10_ReadData(SHT10_CMD_MEASURE_TEMP);
return -39.7 + 0.01 * (float)raw;
}
float SHT10_ReadHumidity(void)
{
uint16_t raw = SHT10_ReadData(SHT10_CMD_MEASURE_HUMI);
return -4.0 + 0.0405 * (float)raw + (-0.0000028) * (float)raw * (float)raw;
}
```
使用方法:
1. 在 `main()` 函数中调用 `SHT10_Init()` 进行初始化。
2. 调用 `SHT10_ReadTemperature()` 和 `SHT10_ReadHumidity()` 分别读取温度和湿度。
注意事项:
1. 该程序使用的是 SHT10 的标准通信协议,如果你使用的是其它版本的 SHT10,可能需要修改部分代码。
2. 通信速率较慢,最好在单次读取后加入适当的延时,以确保数据的准确性。
阅读全文