当前位置:网站首页>基于STM32F103的消防系统之MQ-4气体传感器
基于STM32F103的消防系统之MQ-4气体传感器
2022-07-30 05:47:00 【计算机小袁】
硬件材料
开发板 野火STM32-F103指南者

气体传感器 MQ-4气体传感器

引脚连接
注意!!必须使用5v电压,否则会造成电压过低测不准。
代码简介
MQ-4气体传感器采用的是模拟输入的模式,即使用模拟信号进行信息传递。使用模拟信号输入可以探测到周围气体的浓度值。
软件代码
.C文件
#include "mq4.h"
__IO uint16_t ADC_ConvertedValue;
/**
* @brief ADC GPIO 初始化
* @param 无
* @retval 无
*/
static void ADCx_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
// 打开 ADC IO端口时钟
ADC_GPIO_APBxClock_FUN ( ADC_GPIO_CLK, ENABLE );
// 配置 ADC IO 引脚模式
// 必须为模拟输入
GPIO_InitStructure.GPIO_Pin = ADC_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
// 初始化 ADC IO
GPIO_Init(ADC_PORT, &GPIO_InitStructure);
}
/**
* @brief 配置ADC工作模式
* @param 无
* @retval 无
*/
static void ADCx_Mode_Config(void)
{
ADC_InitTypeDef ADC_InitStructure;
// 打开ADC时钟
ADC_APBxClock_FUN ( ADC_CLK, ENABLE );
// ADC 模式配置
// 只使用一个ADC,属于独立模式
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;
// 转换通道1个
ADC_InitStructure.ADC_NbrOfChannel = 1;
// 初始化ADC
ADC_Init(ADCx, &ADC_InitStructure);
// 配置ADC时钟为PCLK2的8分频,即9MHz
RCC_ADCCLKConfig(RCC_PCLK2_Div8);
// 配置 ADC 通道转换顺序和采样时间
ADC_RegularChannelConfig(ADCx, ADC_CHANNEL, 1,
ADC_SampleTime_55Cycles5);
// ADC 转换结束产生中断,在中断服务程序中读取转换值
ADC_ITConfig(ADCx, ADC_IT_EOC, ENABLE);
// 开启ADC ,并开始转换
ADC_Cmd(ADCx, ENABLE);
// 初始化ADC 校准寄存器
ADC_ResetCalibration(ADCx);
// 等待校准寄存器初始化完成
while(ADC_GetResetCalibrationStatus(ADCx));
// ADC开始校准
ADC_StartCalibration(ADCx);
// 等待校准完成
while(ADC_GetCalibrationStatus(ADCx));
// 由于没有采用外部触发,所以使用软件触发ADC转换
ADC_SoftwareStartConvCmd(ADCx, ENABLE);
}
static void ADC_NVIC_Config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
// 优先级分组
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
// 配置中断优先级
NVIC_InitStructure.NVIC_IRQChannel = ADC_IRQ;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
/**
* @brief ADC初始化
* @param 无
* @retval 无
*/
void ADCx_Init(void)
{
ADCx_GPIO_Config();
ADCx_Mode_Config();
ADC_NVIC_Config();
}
/*********************************************END OF FILE**********************/
.H文件
#ifndef __MQ_H
#define __MQ_H
#include "stm32f10x.h"
// ADC 编号选择
// 可以是 ADC1/2,如果使用ADC3,中断相关的要改成ADC3的
#define ADC_APBxClock_FUN RCC_APB2PeriphClockCmd
#define ADCx ADC2
#define ADC_CLK RCC_APB2Periph_ADC2
// ADC GPIO宏定义
// 注意:用作ADC采集的IO必须没有复用,否则采集电压会有影响
#define ADC_GPIO_APBxClock_FUN RCC_APB2PeriphClockCmd
#define ADC_GPIO_CLK RCC_APB2Periph_GPIOA //修改时钟
#define ADC_PORT GPIOA //修改寄存器
#define ADC_PIN GPIO_Pin_5 //修改引脚
// ADC 通道宏定义
#define ADC_CHANNEL ADC_Channel_5 //修改
// ADC 中断相关宏定义
#define ADC_IRQ ADC1_2_IRQn
#define ADC_IRQHandler ADC1_2_IRQHandler
//#define ADC_IRQ ADC3_IRQn
//#define ADC_IRQHandler ADC3_IRQHandler
void ADCx_Init(void);
#endif /* __ADC_H */
main.c
void MQ-4(void)
{
ADC_ConvertedValueLocal =(float) ADC_ConvertedValue/4096*3.3;//读取电压值
/*此处可加上一行代码,将电压值转换为浓度值*/
delay_ms(200);
printf("read_dht11_finish=%f\r\n", ADC_ConvertedValueLocal);
if(ADC_ConvertedValueLocal > x) //可改为浓度大于某值
{
/*用户自定义操作*/
}
else
{
/*用户自定义操作*/
}
}阅读至此,可以知道MQ-4气体传感器的模拟输入是直接借用ADC检测的代码,若是空白代码,从头编写,可能需要在stm32f10x_it.c文件添加一定的代码。
MQ-4气体传感器只是一个基础的情况探测模块,具体的应用会在以后的章节进行讲解。比如检测到燃气泄漏之后的操作,响应。
边栏推荐
- BLDC电机应用持续火爆,“网红神器”筋膜枪前景几何?
- js高级学习笔记(详细)
- [Quick MSP430f149] Notes on learning MSP430f149 during the game
- 服务器基础知识:包含基本概念,作用,服务器选择,服务器管理等(学习来自米拓建站)
- c语言编程练习
- Kunlun On-state Screen Production (serial 1)---Contact
- 关于 PCB 多层板制程能力不得不说的那些事儿
- Knowledge of the day: handwritten deep copy and shallow copy (solves the problem of circular references)
- CPU缓存一致性问题
- vscode 设置 sublime 的主题
猜你喜欢

Diwen serial screen production (serialization 1) ===== preparation work

无法完成包的安装npm ERR! Refusing to install package with name “moment“ under a package also called “moment“

2021 soft exam intermediate pass

VsCode打开终端的方法

QT Weekly Skills (1) ~~~~~~~~~ Running Icon

51数码管显示

QT serial 4: LORA test platform based on QT and STM32H750 (3)

led闪烁

VSCode hides the left activity bar

xxx is not in the sudoers file.This incident will be reported error
随机推荐
this的指向问题
【markdown常用用法】
Kunlun State Screen Production (serialization 4) --- Basics (graphical setting and display, button lights)
TCP为什么要三次握手,握手过程中丢包会怎么样?
Real-time waveform display of CAN communication data based on QT (serial eight) ==== "Sub function or new class calls ui control"
使用Dva项目作Antd的Demo
vs compile boost library script
Difference between logical shift right and arithmetic right shift
测试第一题
每日一知识:手写深拷贝和浅拷贝(解决了循环引用的问题)
工程师必看:常见的PCB检测方法有哪些?
ES6语法笔记(ES6~ES11)
(*(void (*)())0)()的解读
js高级学习笔记(详细)
ipconfig Command Guide
洛谷一P1097 [NOIP2007 提高组] 统计数字
查看 word版本号
FPGA parsing B code----serial 2
关于报错vscode
HSPF model application