sht20中写用户寄存器_STM32F030读取温湿度传感器SHT20的C程序源码(经实测实用过)...
时间: 2023-12-10 09:41:40 浏览: 118
STM32F030读取温湿度传感器SHT20的C程序源码经实测实用过.docx
以下是一个参考实现,可以用于读取SHT20的温度和湿度数据。请注意,这只是一个简单的示例代码,可能需要根据具体情况进行调整。
```c
#include "stm32f0xx.h"
#include "delay.h"
#define SHT20_I2C_ADDR 0x80 // SHT20的I2C地址
// SHT20命令定义
#define SHT20_MEASURE_TEMPERATURE_HOLD_MASTER 0xE3 // 测量温度,并等待主机读取数据
#define SHT20_MEASURE_HUMIDITY_HOLD_MASTER 0xE5 // 测量湿度,并等待主机读取数据
#define SHT20_WRITE_USER_REG 0xE6 // 写用户寄存器
#define SHT20_READ_USER_REG 0xE7 // 读用户寄存器
#define SHT20_SOFT_RESET 0xFE // 软件复位
// I2C相关配置
#define I2C_SCL_PIN GPIO_Pin_9
#define I2C_SDA_PIN GPIO_Pin_10
#define I2C_GPIO_PORT GPIOB
#define I2C_GPIO_CLK RCC_AHBPeriph_GPIOB
#define I2C_RCC_CLK RCC_APB1Periph_I2C1
#define I2C_SPEED 100000 // I2C速率
I2C_InitTypeDef I2C_InitStructure;
// 初始化I2C接口
void I2C_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
I2C_DeInit(I2C1);
RCC_APB1PeriphClockCmd(I2C_RCC_CLK, ENABLE);
RCC_AHBPeriphClockCmd(I2C_GPIO_CLK, ENABLE);
GPIO_PinAFConfig(I2C_GPIO_PORT, GPIO_PinSource9, GPIO_AF_1);
GPIO_PinAFConfig(I2C_GPIO_PORT, GPIO_PinSource10, GPIO_AF_1);
GPIO_InitStructure.GPIO_Pin = I2C_SCL_PIN | I2C_SDA_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(I2C_GPIO_PORT, &GPIO_InitStructure);
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
I2C_InitStructure.I2C_OwnAddress1 = 0x00;
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_InitStructure.I2C_ClockSpeed = I2C_SPEED;
I2C_Init(I2C1, &I2C_InitStructure);
I2C_Cmd(I2C1, ENABLE);
}
// 向SHT20发送命令
void SHT20_SendCommand(uint8_t cmd)
{
while (I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY))
;
I2C_GenerateSTART(I2C1, ENABLE);
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT))
;
I2C_Send7bitAddress(I2C1, SHT20_I2C_ADDR, I2C_Direction_Transmitter);
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))
;
I2C_SendData(I2C1, cmd);
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED))
;
}
// 从SHT20读取数据
uint16_t SHT20_ReadData(void)
{
uint16_t data = 0;
uint8_t byte1, byte2;
while (I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY))
;
I2C_GenerateSTART(I2C1, ENABLE);
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT))
;
I2C_Send7bitAddress(I2C1, SHT20_I2C_ADDR, I2C_Direction_Receiver);
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED))
;
while (I2C_GetFlagStatus(I2C1, I2C_FLAG_RXNE) == RESET)
;
byte1 = I2C_ReceiveData(I2C1);
I2C_AcknowledgeConfig(I2C1, DISABLE);
while (I2C_GetFlagStatus(I2C1, I2C_FLAG_RXNE) == RESET)
;
byte2 = I2C_ReceiveData(I2C1);
I2C_GenerateSTOP(I2C1, ENABLE);
data = (byte1 << 8) | byte2;
return data;
}
// 读取温度
float SHT20_ReadTemperature(void)
{
float t = 0.0f;
SHT20_SendCommand(SHT20_MEASURE_TEMPERATURE_HOLD_MASTER);
delay_ms(80); // 等待测量完成
uint16_t raw_data = SHT20_ReadData();
t = -46.85f + 175.72f * ((float)raw_data / (float)0xFFFF);
return t;
}
// 读取湿度
float SHT20_ReadHumidity(void)
{
float h = 0.0f;
SHT20_SendCommand(SHT20_MEASURE_HUMIDITY_HOLD_MASTER);
delay_ms(80); // 等待测量完成
uint16_t raw_data = SHT20_ReadData();
h = -6.0f + 125.0f * ((float)raw_data / (float)0xFFFF);
return h;
}
// 写用户寄存器
void SHT20_WriteUserRegister(uint8_t value)
{
SHT20_SendCommand(SHT20_WRITE_USER_REG);
I2C_SendData(I2C1, value);
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED))
;
}
// 读用户寄存器
uint8_t SHT20_ReadUserRegister(void)
{
uint8_t data = 0;
SHT20_SendCommand(SHT20_READ_USER_REG);
while (I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY))
;
I2C_GenerateSTART(I2C1, ENABLE);
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT))
;
I2C_AcknowledgeConfig(I2C1, DISABLE);
I2C_Send7bitAddress(I2C1, SHT20_I2C_ADDR, I2C_Direction_Receiver);
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED))
;
data = I2C_ReceiveData(I2C1);
I2C_GenerateSTOP(I2C1, ENABLE);
return data;
}
int main(void)
{
float temperature, humidity;
I2C_Configuration();
SHT20_WriteUserRegister(0x00); // 设置SHT20的用户寄存器
temperature = SHT20_ReadTemperature();
humidity = SHT20_ReadHumidity();
while (1)
;
}
```
阅读全文