1.自己设置温度湿度阈值,当温度过高时,打开风扇,蜂鸣器报警
2.当湿度比较高时,打开LED1灯,蜂鸣器报警
main.c
#include "si7006.h"
#include "CH1.h"
#include "led.h"
// 延时函数int main()
{// iic初始化iic_init();// si7006初始化si7006_init();// 风扇,蜂鸣器,LED灯初始化TIM1_init();TIM4_init();all_led_init();// 温度short tem;// 湿度unsigned int short hum;while (1){// 读取温度数据tem=si7006_read_tme();// 读取湿度数据hum=si7006_read_hum();// 计算温湿度数据tem=175.72*tem/65536-46.85;hum=125*hum/65536-6;// 串口打印printf("tem:%d\n",tem);printf("hum:%d",hum);if(tem>25){TIM1_ON();TIM4_ON();}if(hum>60){LED1_ON();TIM4_ON();}delay(1000);TIM1_OFF();LED1_OFF();TIM4_OFF();}return 0;
}
si7006.c
#include"si7006.h"
//封装延时函数
void delay(int ms)
{int i,j;for(i=0;i<ms;i++){for(j=0;j<2000;j++){}}
}
void si7006_init()
{//发起起始信号i2c_start();//发送从机地址+写标志i2c_write_byte(0X40<<1|0);//等待从机应答i2c_wait_ack();//发送寄存器地址 0XE6i2c_write_byte(0XE6);//等待从机应答i2c_wait_ack();//传输要写入的数据0X3A、i2c_write_byte(0X3A);//等待从机应答i2c_wait_ack();//发送终止信号i2c_stop();
}short si7006_read_tem()
{short tem;char tem_h,tem_l;//发起起始信号i2c_start();//发送从机地址+写标志i2c_write_byte(0x40<<1|0);//等待从机应答i2c_wait_ack();//发送寄存器地址 0XE3i2c_write_byte(0xe3);//等待从机应答i2c_wait_ack();//重复起始信号i2c_start();//发送从机地址+读标志i2c_write_byte(0x40<<1|1);//等待从机应答i2c_wait_ack();delay(100);//等待从机测量数据//接收数据高八位//发送应答信号tem_h=i2c_read_byte(0);//接收数据低八位//发送非应答信号tem_l=i2c_read_byte(1);//将高八位和低八位合成一个数据 高八位<<8|低8位tem=tem_h<<8|tem_l;return tem;
}unsigned short si7006_read_hum()
{unsigned short hum;unsigned char hum_h,hum_l;//发起起始信号i2c_start();//发送从机地址+写标志i2c_write_byte(0x40<<1|0);//等待从机应答i2c_wait_ack();//发送寄存器地址 0XE3i2c_write_byte(0xe5);//等待从机应答i2c_wait_ack();//重复起始信号i2c_start();//发送从机地址+读标志i2c_write_byte(0x40<<1|1);//等待从机应答i2c_wait_ack();delay(100);//等待从机测量数据//接收数据高八位//发送应答信号hum_h=i2c_read_byte(0);//接收数据低八位//发送非应答信号hum_l=i2c_read_byte(1);//将高八位和低八位合成一个数据 高八位<<8|低8位hum=hum_h<<8|hum_l;return hum;
}
si7006.h
#ifndef __SI7006_H_
#define __SI7006_H_
#include "iic.h"void si7006_init();
short si7006_read_tem();
unsigned short si7006_read_hum();
#endif
CH1.c
#include "CH1.h"
//风扇的初始化
void TIM1_init()
{//GPIOE时钟使能RCC |= (0x1 << 4);//PE9设置为输出GPIOE->MODER &= (~(0X3 << 18));GPIOE->MODER |= (0X1 << 18);//PE9设置为推挽输出GPIOE->OTYPER &= (~(0X1 << 9));//PE9设置为低速输出GPIOE->OSPEEDR &= (~(0X3 << 18));//PE9无上拉下拉电阻GPIOE->OPUPDR &= (~(0X3 << 18));//默认输出低电平GPIOE->ODR &= (~(0X1 << 9));
}
//蜂鸣器初始化
void TIM4_init()
{//GPIOB时钟使能RCC |= (0x1 << 1);//PB6设置为输出GPIOB->MODER &= (~(0X3 << 12));GPIOB->MODER |= (0X1 << 12);//PB6设置为推挽输出GPIOB->OTYPER &= (~(0X1 << 6));//PB6设置为低速输出GPIOB->OSPEEDR &= (~(0X3 << 12));//PB6无上拉下拉电阻GPIOB->OPUPDR &= (~(0X3 << 12));//默认输出低电平GPIOB->ODR &= (~(0X1 << 6));
}
// 风扇转动
void TIM1_ON()
{GPIOE->ODR |= (0X1 << 9);
}
// 风扇关闭
void TIM1_OFF()
{GPIOE->ODR |= (~(0X1 << 9));
}
// 蜂鸣器启动
void TIM4_ON()
{GPIOB->ODR |= (0X1 << 6);
}
// 蜂鸣器关闭
void TIM4_OFF()
{GPIOB->ODR |= (~(0X1 << 6));
}