MQ- 9气体传感器所使用的气敏材料是在清洁空气中电导率较低的二氧化锡 (SnO2)。 采用高低温循环检测方式低温(1.5V加热)检测一氧化碳,传感器的电导率随空气中一氧化碳气体浓度增加而增大,高温(5.0V加热)检测可燃气体甲烷、丙烷并清洗低温时吸附的杂散气体。
一、模块来源
二、规格参数
工作电压:3.3V ~ 5V
工作电流:150MA
输出方式: DO接口为数字量输出 AO接口为模拟量输出
读取方式:ADC
管脚数量:4 Pin(2.54mm间距排针)
以上信息见厂家资料文件
三、移植过程
我们的目标是将例程移植至开发板上【能够判断当前环境状况的功能】。首先要获取资料,查看数据手册应如何实现读取数据,再移植至我们的工程。
1、查看资料
因此DO引脚可以配置为GPIO的输入模式,AO引脚需要配置为ADC模拟输入模式。
2、引脚选择
VCC | 5V0 |
GND | GND |
DO | P407 |
AO | P000 |
3、图形化配置
- 打开图形化界面:
- 设置
Pin
:
- 设置
ADC
:
Ctrl + S
保存!- 点击右上角
Generate Project Content
生成代码。
4、代码编写
新建两个文件 bsp_mq9.c
和 bsp_mq9.h
,并且将头文件路径添加到编译器中。
在文件 bsp_mq9.c
和 bsp_mq9.h
中,编写如下代码。
c
/*
* 立创开发板软硬件资料与相关扩展板软硬件资料官网全部开源
* 开发板官网:www.lckfb.com
* 文档网站:wiki.lckfb.com
* 技术支持常驻论坛,任何技术问题欢迎随时交流学习
* 嘉立创社区问答:https://www.jlc-bbs.com/lckfb
* 关注bilibili账号:【立创开发板】,掌握我们的最新动态!
* 不靠卖板赚钱,以培养中国工程师为己任
*/
#ifndef BSP_CODE_BSP_MQ9_H_
#define BSP_CODE_BSP_MQ9_H_
#include "hal_data.h"
#include <stdio.h>
#ifndef delay_ms
#define delay_ms(x) R_BSP_SoftwareDelay(x, BSP_DELAY_UNITS_MILLISECONDS)
#endif
#ifndef delay_1ms
#define delay_1ms(x) R_BSP_SoftwareDelay(x, BSP_DELAY_UNITS_MILLISECONDS)
#endif
#ifndef delay_us
#define delay_us(x) R_BSP_SoftwareDelay(x, BSP_DELAY_UNITS_MICROSECONDS)
#endif
#ifndef delay_1us
#define delay_1us(x) R_BSP_SoftwareDelay(x, BSP_DELAY_UNITS_MICROSECONDS)
#endif
#ifndef u8
#define u8 uint8_t
#endif
#ifndef u16
#define u16 uint16_t
#endif
#ifndef u32
#define u32 uint32_t
#endif
#define BSP_ADC_CHANNEL ADC_CHANNEL_0
/* DO 引脚 */
#define BSP_DO_GPIO_PIN BSP_IO_PORT_04_PIN_07
static inline bsp_io_level_t MQ_DO(void) {
bsp_io_level_t p_pin_value;
fsp_err_t err = R_IOPORT_PinRead(&g_ioport_ctrl, BSP_DO_GPIO_PIN, &p_pin_value);
if(err != FSP_SUCCESS)
{
printf("GPIO Input Read Failed!!\r\n");
}
return p_pin_value;
}
// 采样次数
#define SAMPLES 5
void Module_BSP_Init(void);
int Get_Adc_MQ9_Value(void);
int Get_MQ9_Percentage_value(void);
char Get_MQ9_DO(void);
#endif /* BSP_CODE_BSP_MQ9_H_ */
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
c
/*
* 立创开发板软硬件资料与相关扩展板软硬件资料官网全部开源
* 开发板官网:www.lckfb.com
* 文档网站:wiki.lckfb.com
* 技术支持常驻论坛,任何技术问题欢迎随时交流学习
* 嘉立创社区问答:https://www.jlc-bbs.com/lckfb
* 关注bilibili账号:【立创开发板】,掌握我们的最新动态!
* 不靠卖板赚钱,以培养中国工程师为己任
*/
#include "bsp_mq9.h"
#include "stdio.h"
// ADC Scan complete flag
volatile bool adc_flag = false;
/* ADC callback */
void adc_callback(adc_callback_args_t *p_args)
{
FSP_PARAMETER_NOT_USED(p_args);
adc_flag = true;
}
/**********************************************************
* 函 数 名 称:Module_BSP_Init
* 函 数 说 明:该函数用于初始化模块
* 函 数 形 参:无
* 函 数 返 回:无
* 作 者:LCKFB
* 备 注:无
**********************************************************/
void Module_BSP_Init(void)
{
fsp_err_t err = FSP_SUCCESS;
// 打开ADC设备
err = R_ADC_Open(&g_adc0_ctrl, &g_adc0_cfg);
if (FSP_SUCCESS != err) {
printf("ADC Open Failed! \n");
return;
}
// 配置ADC扫描
err = R_ADC_ScanCfg(&g_adc0_ctrl, &g_adc0_channel_cfg);
if (FSP_SUCCESS != err) {
printf("ADC Scan Config Failed! \n");
R_ADC_Close(&g_adc0_ctrl); // 关闭已打开的ADC
return;
}
}
/**********************************************************
* 函 数 名 称:ADC_GET
* 函 数 功 能:读取一次ADC数据
* 传 入 参 数:无
* 函 数 返 回:无
* 作 者:LCKFB
* 备 注:LP
**********************************************************/
static int ADC_GET(void)
{
int gAdcResult = 0;
int timeOut = 1000;
fsp_err_t err = FSP_SUCCESS;
// 启动ADC扫描
err = R_ADC_ScanStart(&g_adc0_ctrl);
if (FSP_SUCCESS != err) {
printf("ADC扫描启动失败! \n");
return 9999; // 返回错误值
}
// 等待ADC总线处理完成
while(!adc_flag && timeOut--)
{
delay_us(1);
}
// 清除ADC采样完成标志位
adc_flag = false;
if(!timeOut)
{
printf("ADC_GET Failed!!!\r\n");
return 0;
}
// 读取ADC数据
err = R_ADC_Read(&g_adc0_ctrl, BSP_ADC_CHANNEL, (uint16_t *)&gAdcResult);
if (FSP_SUCCESS != err) {
printf("ADC Read Failed!\n");
return 9999; // 返回错误值
}
return gAdcResult;
}
/******************************************************************
* 函 数 名 称:Get_Adc_MQ9_Value
* 函 数 说 明:
* 函 数 形 参:
* 函 数 返 回:对应扫描的ADC值
* 作 者:LCKFB
* 备 注:无
******************************************************************/
int Get_Adc_MQ9_Value(void)
{
uint32_t Data = 0;
int i;
for(i = 0; i < SAMPLES; i++)
{
Data += ADC_GET();
delay_ms(5);
}
Data = Data / SAMPLES;
return Data;
}
/******************************************************************
* 函 数 名 称:Get_MQ9_Percentage_value
* 函 数 说 明:读取MQ9值,并且返回百分比
* 函 数 形 参:无
* 函 数 返 回:返回百分比
* 作 者:LCKFB
* 备 注:无
******************************************************************/
int Get_MQ9_Percentage_value(void)
{
int adc_max = 4095;
int adc_new = 0;
int Percentage_value = 0;
adc_new = Get_Adc_MQ9_Value();
Percentage_value = ((float)adc_new / (float)adc_max) * 100.f;
return Percentage_value;
}
/******************************************************************
* 函 数 名 称:Get_MQ9_DO
* 函 数 说 明:获取 MQ9 DO引脚的电平状态
* 函 数 形 参:无
* 函 数 返 回:0=未检测到高于灵敏度的酒精值 1=检测到高于灵敏度的酒精值
* 作 者:LCKFB
* 备 注:调整模块上的滑动电阻即可调整灵敏度
******************************************************************/
char Get_MQ9_DO(void)
{
if( MQ_DO() == 0 )
{
return 0;
}
else
{
return 1;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
四、移植验证
在 src\Applay\app.c
中输入代码如下:
c
/*
* 立创开发板软硬件资料与相关扩展板软硬件资料官网全部开源
* 开发板官网:www.lckfb.com
* 文档网站:wiki.lckfb.com
* 技术支持常驻论坛,任何技术问题欢迎随时交流学习
* 嘉立创社区问答:https://www.jlc-bbs.com/lckfb
* 关注bilibili账号:【立创开发板】,掌握我们的最新动态!
* 不靠卖板赚钱,以培养中国工程师为己任
*/
#include "app.h"
#include "stdio.h"
#include "bsp_uart.h"
#include "bsp_mq9.h"
/******************************************************************
* 函 数 名 称:led_blink
* 函 数 说 明:该函数用于控制LED灯的闪烁效果
* 运行时,LED灯每隔500毫秒闪烁一次
* 完整运行此函数需要1s时间
* 函 数 形 参:无
* 函 数 返 回:无
* 作 者:LC
* 备 注:无
******************************************************************/
static void led_blink(void)
{
/* Set the pin to low */
R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_04_PIN_02, BSP_IO_LEVEL_LOW);
/* Delay for 500 milliseconds */
R_BSP_SoftwareDelay(500, BSP_DELAY_UNITS_MILLISECONDS);
/* Set the pin to high */
R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_04_PIN_02, BSP_IO_LEVEL_HIGH);
/* Delay for another 500 milliseconds */
R_BSP_SoftwareDelay(500, BSP_DELAY_UNITS_MILLISECONDS);
}
/******************************************************************
* 函 数 名 称:Run
* 函 数 说 明:该函数是用户自定义的入口函数,等效于 main_app() 函数。
* 在此函数中可以编写用户的应用逻辑代码。
* 函 数 形 参:无
* 函 数 返 回:无
* 作 者:LC
* 备 注:无
******************************************************************/
void Run(void)
{
/* 初始化调试串口 */
/* | RX:P100 | TX:P101 | */
UART0_Debug_Init();
printf("\r\n= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\r\n");
printf("\r\n=== Welcome to use the DQX-R7FA6E2BB3CNE development board ====\r\n");
printf("\r\n======================= www.lckfb.com =========================\r\n");
printf("\r\n======================= wiki.lckfb.com ========================\r\n");
printf("\r\n======================= [Debug Uart0] =========================\r\n");
printf("\r\n=================== | RX:P100 | TX:P101 | =====================\r\n");
printf("\r\n= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\r\n");
Module_BSP_Init();
printf("MQ9 Sensor Initialization Complete\r\n");
while(1)
{
printf("\n");
printf("MQ9 Value = %d\r\n", Get_Adc_MQ9_Value());
printf("MQ9 Percentage = [%d%%]\r\n", Get_MQ9_Percentage_value());
printf("MQ9 DO = %d\r\n", Get_MQ9_DO());
delay_ms(500);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
编译烧录。
【代码下载】
- 跳转去下载模块移植代码:【点击跳转🚀】