基于STM32F103C8T6单片机控制温湿度,光照强度,烟雾浓度,报警和风扇的开启代码
时间: 2024-05-07 10:19:24 浏览: 103
以下是基于STM32F103C8T6单片机控制温湿度、光照强度、烟雾浓度、报警和风扇的开启代码:
```c
#include "stm32f10x.h"
#include "dht11.h"
#include "delay.h"
#define DHT11_PORT GPIOB
#define DHT11_PIN GPIO_Pin_5
#define MQ2_PORT GPIOB
#define MQ2_PIN GPIO_Pin_6
#define FAN_PORT GPIOB
#define FAN_PIN GPIO_Pin_7
#define LED_PORT GPIOB
#define LED_PIN GPIO_Pin_8
#define ADC1_DR_Address ((uint32_t)0x4001244C)
int16_t temperature = 0;
int16_t humidity = 0;
uint16_t lightIntensity = 0;
uint16_t smokeDensity = 0;
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
// DHT11
GPIO_InitStructure.GPIO_Pin = DHT11_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
GPIO_Init(DHT11_PORT, &GPIO_InitStructure);
// MQ2
GPIO_InitStructure.GPIO_Pin = MQ2_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(MQ2_PORT, &GPIO_InitStructure);
// Fan
GPIO_InitStructure.GPIO_Pin = FAN_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(FAN_PORT, &GPIO_InitStructure);
// LED
GPIO_InitStructure.GPIO_Pin = LED_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(LED_PORT, &GPIO_InitStructure);
}
void ADC_Configuration(void)
{
ADC_InitTypeDef ADC_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
DMA_DeInit(DMA1_Channel1);
DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address;
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&lightIntensity;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
DMA_InitStructure.DMA_BufferSize = 1;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel1, &DMA_InitStructure);
DMA_Cmd(DMA1_Channel1, ENABLE);
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_InitStructure.ADC_ScanConvMode = DISABLE;
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfChannel = 1;
ADC_Init(ADC1, &ADC_InitStructure);
ADC_RegularChannelConfig(ADC1, ADC_Channel_8, 1, ADC_SampleTime_55Cycles5);
ADC_DMACmd(ADC1, ENABLE);
ADC_Cmd(ADC1, ENABLE);
ADC_ResetCalibration(ADC1);
while (ADC_GetResetCalibrationStatus(ADC1))
;
ADC_StartCalibration(ADC1);
while (ADC_GetCalibrationStatus(ADC1))
;
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
}
void Fan_On(void)
{
GPIO_SetBits(FAN_PORT, FAN_PIN);
}
void Fan_Off(void)
{
GPIO_ResetBits(FAN_PORT, FAN_PIN);
}
void LED_On(void)
{
GPIO_SetBits(LED_PORT, LED_PIN);
}
void LED_Off(void)
{
GPIO_ResetBits(LED_PORT, LED_PIN);
}
void Alarm_On(void)
{
LED_On();
Fan_On();
}
void Alarm_Off(void)
{
LED_Off();
Fan_Off();
}
void Read_DHT11(void)
{
uint8_t buffer[5] = {0};
DHT11_Start();
if (DHT11_Check_Response() == DHT11_OK)
{
buffer[0] = DHT11_Read_Byte();
buffer[1] = DHT11_Read_Byte();
buffer[2] = DHT11_Read_Byte();
buffer[3] = DHT11_Read_Byte();
buffer[4] = DHT11_Read_Byte();
if (DHT11_Check_Sum(buffer) == DHT11_OK)
{
humidity = buffer[0];
temperature = buffer[2];
}
}
}
void Read_LightIntensity(void)
{
lightIntensity = ADC_GetConversionValue(ADC1);
}
void Read_SmokeDensity(void)
{
smokeDensity = ADC_GetConversionValue(ADC1);
}
int main(void)
{
GPIO_Configuration();
ADC_Configuration();
while (1)
{
Read_DHT11();
Read_LightIntensity();
Read_SmokeDensity();
if (temperature > 30 || humidity > 70)
{
Alarm_On();
}
else if (smokeDensity > 500)
{
Alarm_On();
}
else
{
Alarm_Off();
}
if (lightIntensity > 1000)
{
LED_On();
}
else
{
LED_Off();
}
delay_ms(1000);
}
}
```
注:以上代码需要配合DHT11温湿度传感器、MQ-2烟雾传感器和光敏电阻模块使用。其中DHT11的驱动代码和延时函数的代码可以在网上找到。
阅读全文