[ ST NUCLEO-U575ZI-Q 测评] SHT20温湿度计
<div class='showpostmsg'><p><a href="https://bbs.eeworld.com.cn/thread-1230832-1-1.html">[ ST NUCLEO-U575ZI-Q 测评] 优化OLED显示 - RF/无线 - 电子工程世界-论坛 (eeworld.com.cn)</a></p><p>在驱动OLED的基础上,增加sht20的温湿度显示:</p>
<p><strong>sht20的驱动:</strong></p>
<p>其中sht20.h和sht20.c 是基于麒麟座官方的例程移植的,原作者是张继瑞。源码下载地址:https://github.com/simonliu009/STM32F10x-I2C-SHT20-LCD1602</p>
<p>sht20.h:</p>
<pre>
<code>#ifndef __SHT20_H
#define __SHT20_H
#include "main.h"
/*SHT20 设备操作相关宏定义,详见手册*/
#define SHT20_ADDRESS0X40
#define SHT20_Write_Add 0x80 //STM32 hal库 slave地址为读地址
#define SHT20_Read_Add 0x81
#define SHT20_Measurement_RH_HM0XE5
#define SHT20_Measurement_T_HM0XE3
#define SHT20_Measurement_RH_NHM0XF5//非主机模式地址
#define SHT20_Measurement_T_NHM0XF3 //非主机模式地址
#define SHT20_READ_REG0XE7
#define SHT20_WRITE_REG0XE6
#define SHT20_SOFT_RESET0XFE
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</code></pre>
<p>sht20.c</p>
<pre>
<code>#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;
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<<8)|(val&0xFC)) / 65536.0));
}
else
{
printf("error\r\n");
}
}
float SHT20_GetHumidity(void)
{
uint8_t val;
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<<8)|(val&0xF0))/65536.0);
}
else
{
printf("error\r\n");
}
}</code></pre>
<p>在原来的工程基础上增加sht20的文件夹,并把sht20.c加入工程:</p>
<p> 把头文件也加入工程:</p>
<p> 在main.c主函数中引入sht20.h,添加代码如下:</p>
<pre>
<code>SHT20_Init();
/* USER CODE BEGIN 2 */
printf("start\r\n");
char lv;
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</code></pre>
<p>【硬件接线】</p>
<pre>
<code> /**I2C1 GPIO Configuration
PB8 ------> I2C1_SCL
PB9 ------> I2C1_SDA
*/</code></pre>
<p>连接好后,下载工程到开发板,效果如下图:</p>
<p> </p>
</div><script> var loginstr = '<div class="locked">查看本帖全部内容,请<a href="javascript:;" style="color:#e60000" class="loginf">登录</a>或者<a href="https://bbs.eeworld.com.cn/member.php?mod=register_eeworld.php&action=wechat" style="color:#e60000" target="_blank">注册</a></div>';
if(parseInt(discuz_uid)==0){
(function($){
var postHeight = getTextHeight(400);
$(".showpostmsg").html($(".showpostmsg").html());
$(".showpostmsg").after(loginstr);
$(".showpostmsg").css({height:postHeight,overflow:"hidden"});
})(jQuery);
} </script><script type="text/javascript">(function(d,c){var a=d.createElement("script"),m=d.getElementsByTagName("script"),eewurl="//counter.eeworld.com.cn/pv/count/";a.src=eewurl+c;m.parentNode.insertBefore(a,m)})(document,523)</script> <p>测评汇总:免费申请|ST NUCLEO-U575ZI-Q https://bbs.eeworld.com.cn/thread-1228653-1-1.html</p>
页:
[1]