SHT30

2023-12-26 21:58:37

sht.h

#ifndef  __SHT_H
#define  __SHT_H
/*
PB6  SCL
PB7  SDA
*/

#include "stm32f10x_conf.h"
#define slave_addr 0x88//从机地址
#define periodic_high 0x2737//采样频率
#define fetch_data_com 0xE00

extern void sht_init(void);
extern void sht_periodic_high(void);
extern void sht_fetch_data_com(void);
extern void sht_fetch_data(double sht[2]);

#endif

sht.c

#include"sht.h"
#include"iic.h"


void sht_init(void)
{
	IIC_Init ();
}

void sht_periodic_high(void)
{
	IIC_Start();
	IIC_Send_Byte (slave_addr);
	IIC_Wait_ACK ();
	IIC_Send_Byte (periodic_high>>8);//右移八位
	IIC_Wait_ACK ();
	IIC_Send_Byte (periodic_high&0xFF);//保留了低八位
	IIC_Wait_ACK ();
	IIC_Stop ();
	
	
}

void sht_fetch_data_com(void)
{
	IIC_Start ();
	IIC_Send_Byte (slave_addr);
	IIC_Wait_ACK ();
	IIC_Send_Byte (fetch_data_com>>8);//右移八位
	IIC_Wait_ACK ();
	IIC_Send_Byte (fetch_data_com&0xFF);//保留了低八位
	IIC_Wait_ACK ();
	IIC_Stop ();
}

void sht_fetch_data(double sht[2])
{
	u8 data[6]={0};
	u8 i=0;
	u16 tmp=0,hum=0;
	IIC_Start ();
	IIC_Send_Byte (slave_addr|0x01);//0x89
	IIC_Wait_ACK ();
	for(i=0;i<6;i++)
	{
		if(i==5)
			data[i]=IIC_Recv_Byte(0);
		else 
			data[i]=IIC_Recv_Byte(1);
	}
	IIC_Stop ();
	tmp=data[0];
	tmp<<=8;
	tmp|=data[1];
	
	hum=data[3];
	hum<<=8;
	hum|=data[4];
	
	sht[0]=-45+175*tmp/65535;
	sht[1]=100*hum/65535;
	
}

main.c

#include"iwdg.h"
#include"led.h"
#include"fmq.h"
#include"key.h"
#include"delay.h"
#include"inte.h"
#include"hc138.h"
#include"dht11.h"
#include"iic.h"
#include"eeprom.h"
#include"sht.h"

void h0(void)
{
	Led_On(0);
	Led_On(1);
	Led_On(2);
	
}
void h1(void)
{
	Fmq_On();
}
void h2(void)
{
	Led_Off(0);
	Fmq_Off();
	
}

int main(void)
{
	char buff[5]={0};
	double sht_value[2]={0};
	u8 arr[8]={1,2,3,4,5,6,7,8};
	u8 arr1[8]={0};
	int dht_data=0;
	int i=0,j=0;
	int ep=0;
	inte_init();
	Led_Init();	
	Key_Init();
	Fmq_Init();
	delay_init();
	hc138_init();
	dht11_init();
	eeprom_init();
	sht_init();
	eeprom_byte_write(0xA0,0x00,4);//写入
	set_inte_handler(h0,h1,h2);
	while(1)
	{
		sht_fetch_data(sht_value);
		ep=(int)sht_value[0];
		show_digital_Out(ep);
		
	}
	return 0;
}

文章来源:https://blog.csdn.net/qq_63507404/article/details/135224494
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。