1.自己设置温度湿度阈值,当温度过高时,打开风扇,蜂鸣器报警
2.当湿度比较高时,打开LED1灯,蜂鸣器报警
//蜂鸣器
bz.h
cs
#ifndef __BZ_H__
#define __BZ_H__
#include "stm32mp1xx_rcc.h"
#include "stm32mp1xx_gpio.h"
void all_bz_init();
void bz_on();
void bz_off();
#endif
bz.c
cs
#include "bz.h"
//PB6
void all_bz_init()
{
//GPIOB时钟使能
RCC->MP_AHB4ENSETR |= (0x1<<1);
//PB6设置为输出
GPIOB->MODER &= (~(0X3<<12));
GPIOB->MODER |= (0X1<<12);
//PB6设置为推挽输出
GPIOB->OTYPER &= (~(0X1<<6));
//PB6设置为低速输出
GPIOB->OSPEEDR &= (~(0X3<<12));
//PB6无上拉下拉电阻
GPIOB->PUPDR &= (~(0X3<<12));
//默认输出低电平
GPIOB->ODR &= (~(0X1<<6));
}
void bz_on()
{
GPIOB->ODR |= (0X1<<6);
}
void bz_off()
{
GPIOB->ODR &= (~(0X1<<6));
}
//风扇
fan.h
cs
#ifndef __FAN_H__
#define __FAN_H__
#include "stm32mp1xx_rcc.h"
#include "stm32mp1xx_gpio.h"
void all_fan_init();
void fan_on();
void fan_off();
#endif
fan.c
cs
#include "fan.h"
//风扇 PE9
void all_fan_init()
{
//GPIOE时钟使能
RCC->MP_AHB4ENSETR |= (0x1<<4);
//PE9设置为输出
GPIOE->MODER &= (~(0X3<<18));
GPIOE->MODER |= (0X1<<18);
//PE9设置为推挽输出
GPIOE->OTYPER &= (~(0X1<<9));
//PE9设置为低速输出
GPIOE->OSPEEDR &= (~(0X3<<9));
//PE9无上拉下拉电阻
GPIOE->PUPDR &= (~(0X3<<18));
//默认输出低电平
GPIOE->ODR &= (~(0X1<<9));
}
void fan_on()
{
GPIOE->ODR |= (0X1<<9);
}
void fan_off()
{
GPIOE->ODR &= (~(0X1<<9));
}
main.c
cs
#include"si7006.h"
#include"led.h"
#include"fan.h"
#include"bz.h"
//PE9 fan PB6 bz
int main()
{
//i2c初始化
i2c_init();
//si7006初始化
si7006_init();
//led,fan,bz初始化
all_bz_init();
all_led_init();
all_fan_init();
short tem;
unsigned int short hum;
while(1)
{
//读取温度数据
tem=si7006_read_tem();
//读取湿度数据
hum=si7006_read_hum();
//计算温湿度数据
hum=125*hum/65536-6;
tem=175.72*tem/65536-46.85;
printf("hum:%d\n",hum);
printf("tem:%d\n",tem);
//设置阈值
if(hum<=55)
{
LED1_OFF();
}
else
{
LED1_ON();
}
if(tem>=25)
{
fan_on();
bz_on();
}
else{
fan_off();
bz_off();
}
//串口打印
delay(1000);
printf("hum:%d\n",hum);
printf("tem:%d\n",tem);
}
return 0;
}
现象视频
VID_20240325_195625