[ ST NUCLEO-U575ZI-Q 测评] 优化OLED显示 - RF/无线 - 电子工程世界-论坛 (eeworld.com.cn)
在驱动OLED的基础上,增加sht20的温湿度显示:
sht20的驱动:
其中sht20.h和sht20.c 是基于麒麟座官方的例程移植的,原作者是张继瑞。源码下载地址:https://github.com/simonliu009/STM32F10x-I2C-SHT20-LCD1602
sht20.h:
#ifndef __SHT20_H
#define __SHT20_H
#include "main.h"
/*SHT20 设备操作相关宏定义,详见手册*/
#define SHT20_ADDRESS 0X40
#define SHT20_Write_Add 0x80 //STM32 hal库 slave地址为读地址
#define SHT20_Read_Add 0x81
#define SHT20_Measurement_RH_HM 0XE5
#define SHT20_Measurement_T_HM 0XE3
#define SHT20_Measurement_RH_NHM 0XF5 //非主机模式地址
#define SHT20_Measurement_T_NHM 0XF3 //非主机模式地址
#define SHT20_READ_REG 0XE7
#define SHT20_WRITE_REG 0XE6
#define SHT20_SOFT_RESET 0XFE
extern I2C_HandleTypeDef hi2c1;
void SHT20_Init(void);
unsigned char SHT20_SoftReset(void);
unsigned char SHT20_Read_Reg(void);
float SHT20_GetTemperature(void);
float SHT20_GetHumidity(void);
#endif
sht20.c
#include "sht20.h"
#include "main.h"
#include <stdio.h>
unsigned char SHT20_SoftReset(void)
{
uint8_t buffer =SHT20_SOFT_RESET;
if(HAL_OK==HAL_I2C_Master_Transmit(&hi2c1, SHT20_Write_Add, &buffer, 1, 1000))
{
printf("成功\r\n");
return 0;
}
else
{
printf("失败\r\n");
return 1;
}
}
unsigned char SHT20_Read_Reg(void)
{
uint8_t buffer =SHT20_READ_REG;
if(HAL_OK==HAL_I2C_Master_Transmit(&hi2c1, SHT20_Write_Add, &buffer, 1, 1000))
{
if(HAL_OK==HAL_I2C_Master_Receive(&hi2c1, SHT20_Write_Add, &buffer, 1, 1000))
{
return buffer;
}
else
{
return 1;
}
}
else
{
return 1;
}
}
unsigned char SHT20_Config(void)//采集温度为14位,采集湿度为12位
{
uint8_t val;
val = (SHT20_Read_Reg()&0x7E)|0x00;
if(HAL_OK==HAL_I2C_Mem_Write(&hi2c1, (uint16_t)SHT20_Write_Add, (uint16_t)SHT20_WRITE_REG, (uint16_t)I2C_MEMADD_SIZE_8BIT, &val, 1, 1000))
{
return 0;
}
else
{
return 1;
}
}
void SHT20_Init()
{
if(SHT20_SoftReset())
{
printf("SHT20 复位失败\r\n");
}
else
{
printf("SHT20 复位成功\r\n");
}
HAL_Delay(100);
if(SHT20_Config())
{
printf("初始化失败\r\n");
}
else
{
printf("初始化成功\r\n");
}
}
float SHT20_GetTemperature(void)
{
uint8_t val[3];
if(HAL_OK==HAL_I2C_Mem_Read(&hi2c1, (uint16_t)SHT20_Write_Add, (uint16_t)SHT20_Measurement_T_HM, (uint16_t)I2C_MEMADD_SIZE_8BIT, val, 3, 1000))
{
return (-46.85 + 175.72 * (((val[0]<<8)|(val[1]&0xFC)) / 65536.0));
}
else
{
printf("error\r\n");
}
}
float SHT20_GetHumidity(void)
{
uint8_t val[3];
if(HAL_OK==HAL_I2C_Mem_Read(&hi2c1, (uint16_t)SHT20_Write_Add, (uint16_t)SHT20_Measurement_RH_HM, (uint16_t)I2C_MEMADD_SIZE_8BIT, val, 3, 1000))
{
return -6+125*(((val[0]<<8)|(val[1]&0xF0))/65536.0);
}
else
{
printf("error\r\n");
}
}
在原来的工程基础上增加sht20的文件夹,并把sht20.c加入工程:
把头文件也加入工程:
在main.c主函数中引入sht20.h,添加代码如下:
SHT20_Init();
/* USER CODE BEGIN 2 */
printf("start\r\n");
char lv[20];
float spiffs;
OLED_Init();
OLED_Clear();
OLED_Refresh();
rxtx_it_usart.init();
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
tickstart = HAL_GetTick();
HAL_Delay(500);
sprintf(lv,"Temp:%0.2f",SHT20_GetTemperature()); //获取温度并转换为字符串
OLED_ShowString(0,0,lv,24,1); //写入OLED显示缓存
sprintf(lv,"Humi:%0.2f",SHT20_GetHumidity()); //获取湿度并转换为字符串
OLED_ShowString(0,30,lv,24,1); //写入OLED显示缓存
OLED_Refresh(); //写出到SSD1306
【硬件接线】
/**I2C1 GPIO Configuration
PB8 ------> I2C1_SCL
PB9 ------> I2C1_SDA
*/
连接好后,下载工程到开发板,效果如下图: