stm32f103rct6开发板烟雾报警器LCD显示烟雾值的main.c MQ2.c代码
时间: 2023-12-19 09:07:57 浏览: 103
stm32+esp8266+阿里云+温湿度+光照-烟雾-远程显示+控制-之STM32代码
5星 · 资源好评率100%
以下是基于stm32f103rct6开发板的烟雾报警器LCD显示烟雾值的main.c和MQ2.c代码,仅供参考:
main.c
```
#include "stm32f10x.h"
#include "lcd1602.h"
#include "MQ2.h"
void Delay(__IO uint32_t nCount);
int main(void)
{
uint16_t smoke_value;
char str[16];
/* 初始化LCD */
LCD_Init();
/* 显示欢迎信息 */
LCD_ShowString(0, 0, "Smoke Alarm");
LCD_ShowString(0, 1, "Initializing...");
/* 初始化MQ2传感器 */
MQ2_Init();
/* 清空LCD显示 */
LCD_Clear();
/* 显示烟雾值 */
while (1)
{
smoke_value = MQ2_Read();
sprintf(str, "Smoke Value:%d", smoke_value);
LCD_ShowString(0, 0, str);
Delay(1000);
}
}
void Delay(__IO uint32_t nCount)
{
for (; nCount != 0; nCount--);
}
```
MQ2.c
```
#include "MQ2.h"
void MQ2_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
ADC_InitTypeDef ADC_InitStructure;
/* 使能GPIOA和ADC1时钟 */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_ADC1, ENABLE);
/* 配置PA1为模拟输入 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* ADC1配置 */
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);
/* 使能ADC1 */
ADC_Cmd(ADC1, ENABLE);
/* ADC1校准 */
ADC_ResetCalibration(ADC1);
while (ADC_GetResetCalibrationStatus(ADC1));
ADC_StartCalibration(ADC1);
while (ADC_GetCalibrationStatus(ADC1));
}
uint16_t MQ2_Read(void)
{
uint16_t adc_value;
/* 启动ADC转换 */
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
/* 等待转换完成 */
while (!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC));
/* 读取ADC转换值 */
adc_value = ADC_GetConversionValue(ADC1);
return adc_value;
}
```
阅读全文