您当前的位置: 首页 >  stm32

DS小龙哥

暂无认证

  • 2浏览

    0关注

    679博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

STM32F103ZE+SHT30检测环境温度与湿度(IIC模拟时序)

DS小龙哥 发布时间:2021-05-22 20:02:12 ,浏览量:2

一、环境介绍

工程编译软件:  keil5

温湿度传感器: SHT30

MCU : STM32F103ZET6

程序采用模块化编程,iic时序为一个模块(iic.c 和 iic.h),SHT30为一个模块(sht30.c 和 sht30.h);IIC时序采用模拟时序方式实现,IO口都采用宏定义方式,方便快速移植到其他平台使用。

工程源码完整下载地址: https://download.csdn.net/download/xiaolong1126626497/18973724

二、SHT30介绍

特点:

1. 湿度和温度传感器 2. 完全校准、线性化和温度 3. 补偿数字输出,宽电源电压范围,从2.4 V到5.5 V 4. I2C接口,通信速度高达1MHz和两个用户可选地址 5. 典型精度 +- 2%相对湿度和+- 0.3°C 6. 启动和测量时间非常快 7. 微型8针DFN封装

  

这是SHT30的7位IIC设备地址:   

 

三、设备运行效果

这是串口打印的数据:

 

 四、设备代码 4.1  main.c
#include "stm32f10x.h"
#include "led.h"
#include "delay.h"
#include "key.h"
#include "usart.h"
#include "iic.h"
#include "sht3x.h"

int main()
{
   float Humidity;
   float Temperature;
   USART1_Init(115200);
   USART1_Printf("设备运行正常....\r\n");
   IIC_Init(); 
   Init_SHT30();
   while(1)
   {
       //读取温湿度
       SHT3x_ReadData(&Humidity,&Temperature);
       USART1_Printf("温度:%0.2f,湿度:%0.2f\r\n",Temperature,Humidity);
       delay_ms(1000);
   }
}
4.2 sht30.c
#include "sht3x.h"

const int16_t POLYNOMIAL = 0x131;

/***************************************************************
* 函数名称: SHT30_reset
* 说    明: SHT30复位
* 参    数: 无
* 返 回 值: 无
***************************************************************/
void SHT30_reset(void)
{
    u8 r_s;
    
    IIC_Start(); //发送起始信号
    
    IIC_WriteOneByteData(SHT30_AddrW); //写设备地址
    r_s=IIC_GetACK();//获取应答
    if(r_s)printf("Init_SHT30_error:1\r\n");

    IIC_WriteOneByteData(0x30); //写数据
    r_s=IIC_GetACK();//获取应答
    if(r_s)printf("Init_SHT30_error:2\r\n");
    
    IIC_WriteOneByteData(0xA2); //写数据
    r_s=IIC_GetACK();//获取应答
    if(r_s)printf("Init_SHT30_error:3\r\n");
    
    IIC_Stop(); //停止信号 

    delay_ms(50);    
}


/***************************************************************
* 函数名称: Init_SHT30
* 说    明: 初始化SHT30,设置测量周期
* 参    数: 无
* 返 回 值: 无
***************************************************************/
void Init_SHT30(void)
{
     u8 r_s;
    
    IIC_Start(); //发送起始信号
    
    IIC_WriteOneByteData(SHT30_AddrW); //写设备地址
    r_s=IIC_GetACK();//获取应答
    if(r_s)printf("Init_SHT30_error:1\r\n");

    IIC_WriteOneByteData(0x22); //写数据
    r_s=IIC_GetACK();//获取应答
    if(r_s)printf("Init_SHT30_error:2\r\n");
    
    IIC_WriteOneByteData(0x36); //写数据
    r_s=IIC_GetACK();//获取应答
    if(r_s)printf("Init_SHT30_error:3\r\n");
    
    IIC_Stop(); //停止信号  
    
    delay_ms(200);
}


/***************************************************************
* 函数名称: SHT3x_CheckCrc
* 说    明: 检查数据正确性
* 参    数: data:读取到的数据
						nbrOfBytes:需要校验的数量
						checksum:读取到的校对比验值
* 返 回 值: 校验结果,0-成功		1-失败
***************************************************************/
u8 SHT3x_CheckCrc(char data[], char nbrOfBytes, char checksum)
{
	
    char crc = 0xFF;
    char bit = 0;
    char byteCtr ;
	
    //calculates 8-Bit checksum with given polynomial
    for(byteCtr = 0; byteCtr < nbrOfBytes; ++byteCtr)
    {
        crc ^= (data[byteCtr]);
        for ( bit = 8; bit > 0; --bit)
        {
            if (crc & 0x80) crc = (crc             
关注
打赏
1664009229
查看更多评论
0.0428s