用stm32f103c8t6实现mq2烟雾报警,并用串口输出的代码
时间: 2023-08-28 18:05:13 浏览: 104
基于STM32F103c8t6的空气质量传感器 MQ135传感器 有害气体检测模块(串口显示)
5星 · 资源好评率100%
好的,以下是用STM32F103C8T6实现MQ2烟雾报警并用串口输出的代码,供参考:
```
#include "stm32f10x.h"
#include "stdio.h"
#define MQ2_PIN GPIO_Pin_0
#define MQ2_PORT GPIOA
void GPIO_Configuration(void);
void USART_Configuration(void);
void USART_SendChar(char c);
int main(void)
{
uint16_t adc_value;
char buffer[10];
GPIO_Configuration();
USART_Configuration();
while(1)
{
GPIO_SetBits(MQ2_PORT, MQ2_PIN); // 开启MQ2传感器
ADC_SoftwareStartConvCmd(ADC1, ENABLE); // 启动ADC转换
while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC)); // 等待转换完成
adc_value = ADC_GetConversionValue(ADC1); // 读取ADC值
GPIO_ResetBits(MQ2_PORT, MQ2_PIN); // 关闭MQ2传感器
if(adc_value > 1000) // 判断MQ2传感器是否检测到烟雾
{
sprintf(buffer, "%d", adc_value); // 将ADC值转换为字符串
USART_SendChar('S'); // 发送烟雾报警信号
USART_SendChar(buffer[0]); // 发送ADC值的第一位数字
USART_SendChar(buffer[1]); // 发送ADC值的第二位数字
USART_SendChar(buffer[2]); // 发送ADC值的第三位数字
USART_SendChar('\n'); // 发送换行符
}
delay_ms(1000); // 延时1秒钟
}
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_ADC1 | RCC_APB2Periph_AFIO, ENABLE);
GPIO_InitStructure.GPIO_Pin = MQ2_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(MQ2_PORT, &GPIO_InitStructure);
ADC_InitTypeDef ADC_InitStructure;
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_InitStructure.ADC_ScanConvMode = DISABLE;
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
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_Cmd(ADC1, ENABLE);
ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_239Cycles5);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void USART_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Tx;
USART_Init(USART1, &USART_InitStructure);
USART_Cmd(USART1, ENABLE);
}
void USART_SendChar(char c)
{
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
USART_SendData(USART1, c);
}
void delay_ms(uint16_t ms)
{
volatile uint32_t nCount;
RCC_ClocksTypeDef RCC_Clocks;
RCC_GetClocksFreq(&RCC_Clocks);
nCount = (RCC_Clocks.HCLK_Frequency / 10000) * ms;
for(; nCount != 0; nCount--);
}
```
注意:以上代码仅供参考,需要根据具体的硬件环境和需求进行适当修改。
阅读全文