一、模块来源
二、规格参数
工作电压:5-7V
消耗电流:最大20mA
最小粒子检出值:0.8微米
灵敏度:0.5V(0.1mg/m3)
清洁空气中电压:0.9V (典型)
重量:15g
尺寸大小:46x30x17.6mm
以上信息见厂家资料文件
三、移植过程
我们的目标是将例程移植至开发板上【能够判断粉尘浓度的功能】。首先要获取资料,查看数据手册应如何实现读取数据,再移植至我们的工程。
1、查看资料
GP2Y1014AU粉尘传感器在其中间有一个洞,空气可以自由流通,传感器内部邻角位置安装有红外发光二极管和光电晶体管,红外发光二极管定向发送红外光,当空气中有颗粒物阻碍红外线时,红外线发生漫反射,光电晶体管接收到红外光线,信号输出引脚电压会随之发生变化。该电压值在一定范围内与灰尘浓度成线性关系,因此在使用过程中,需要使用 ADC 采集该电压信号,并通过该电压值计算出空气中的灰尘浓度。
2、引脚选择
当前只有VO引脚需要使用到ADC接口。
蓝线 | 3V3 |
绿线 | GND |
LED-白线 | P408 |
黄线 | GND |
VO-黑线 | P000 |
红线 | 5V0 |
3、图形化配置
- 打开图形化界面:
- 设置
Pin
:
- 设置
ADC
:
Ctrl + S
保存!- 点击右上角
Generate Project Content
生成代码。
4、代码编写
新建两个文件 bsp_dust.c
和 bsp_dust.h
,并且将头文件路径添加到编译器中。
在文件 bsp_dust.c
和 bsp_dust.h
中,编写如下代码。
c
/*
* 立创开发板软硬件资料与相关扩展板软硬件资料官网全部开源
* 开发板官网:www.lckfb.com
* 文档网站:wiki.lckfb.com
* 技术支持常驻论坛,任何技术问题欢迎随时交流学习
* 嘉立创社区问答:https://www.jlc-bbs.com/lckfb
* 关注bilibili账号:【立创开发板】,掌握我们的最新动态!
* 不靠卖板赚钱,以培养中国工程师为己任
*/
#ifndef BSP_CODE_BSP_DUST_H_
#define BSP_CODE_BSP_DUST_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
#define BSP_LED_GPIO_PIN BSP_IO_PORT_04_PIN_08
#define GP2Y1014AU_VO(x) R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_LED_GPIO_PIN, x)
// 采样次数
#define SAMPLES 5
void GP2Y1014AU_Init(void);
char Read_dust_concentration(float *d_dat);
#endif /* BSP_CODE_BSP_DUST_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
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
c
/*
* 立创开发板软硬件资料与相关扩展板软硬件资料官网全部开源
* 开发板官网:www.lckfb.com
* 文档网站:wiki.lckfb.com
* 技术支持常驻论坛,任何技术问题欢迎随时交流学习
* 嘉立创社区问答:https://www.jlc-bbs.com/lckfb
* 关注bilibili账号:【立创开发板】,掌握我们的最新动态!
* 不靠卖板赚钱,以培养中国工程师为己任
*/
#include "bsp_dust.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;
}
/**********************************************************
* 函 数 名 称:GP2Y1014AU_Init
* 函 数 功 能:初始化GP2Y1014AU传感器
* 传 入 参 数:无
* 函 数 返 回:无
* 作 者:LCKFB
* 备 注:LP
**********************************************************/
void GP2Y1014AU_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;
}
delay_1ms(100); // 等待100ms,确保传感器稳定
}
/**********************************************************
* 函 数 名 称: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_Value
* 函 数 说 明:对ADC值进行平均值计算后输出
* 函 数 形 参:num采集次数
* 函 数 返 回:对应扫描的ADC值
* 作 者:LCKFB
* 备 注:无
******************************************************************/
int Get_ADC_Value(void)
{
int Data = 0;
int i;
for(i = 0; i < SAMPLES; i++)
{
Data += ADC_GET();
delay_ms(2);
}
Data = Data / SAMPLES;
return Data;
}
int Filter(int m)
{
static int flag_first = 0, _buff[10], sum;
const int _buff_max = 10;
int i;
if (flag_first == 0)
{
flag_first = 1;
for (i = 0, sum = 0; i < _buff_max; i++)
{
_buff[i] = m;
sum += _buff[i];
}
return m;
}
else
{
sum -= _buff[0];
for (i = 0; i < (_buff_max - 1); i++)
{
_buff[i] = _buff[i + 1];
}
_buff[9] = m;
sum += _buff[9];
i = sum / 10.0;
return i;
}
}
/******************************************************************
* 函 数 名 称:Read_dust_concentration
* 函 数 说 明:读取粉尘浓度
* 函 数 形 参:粉尘浓度
* 函 数 返 回:0成功 其他失败
* 作 者:LCKFB
* 备 注:无
******************************************************************/
char Read_dust_concentration(float *d_dat)
{
int value = 0;
GP2Y1014AU_VO(0);
delay_us(280);
value = Get_ADC_Value();
delay_us(40);
GP2Y1014AU_VO(1);
delay_us(9680);
value = Filter(value);
*d_dat = 0.17f * value - 0.1f; //转换公式
return 0;
}
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
四、移植验证
在 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_dust.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");
GP2Y1014AU_Init();
printf("\r\nGP2Y1014AU dust sensor initialized.\r\n");
while(1)
{
float value = 0;
Read_dust_concentration(&value);
printf("\r\nDust concentration: %.2f\r\n", value);
delay_1ms(500); // Delay for 500ms
}
}
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
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
编译烧录。
【代码下载】
- 跳转去下载模块移植代码:【点击跳转🚀】