文章目录
- 传感器介绍
- 数据处理
- 解析pm2.5的代码
- 帮助、问询
传感器介绍
PMS5003是一款基于激光散射原理的数字式通用颗粒物浓度传感器,可连续采集
并计算单位体积内空气中不同粒径的悬浮颗粒物个数,即颗粒物浓度分布,进而
换算成为质量浓度,并以通用数字接口形式输出。本传感器可入各种与空气中
悬浮颗粒物浓度相关的仪器仪表或环境改善设备,为其提供及时准确的浓度数
据。
数据处理
读取的数据:
解析pm2.5的代码
逻辑是:
1、初始化串口和定时器10ms中断
2、循环读取一帧数据到rev_pm25_bufferdata
3、在主函数解析数据rev_pm25_bufferdata
4、最终的pm2.5数值就是pm25_value
使用pm25_value 的数据即可。
#include "sys.h"
#include "delay.h"
#include "usart.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "led.h"
#include "timer.h"
char rev_pm25_bufferdata[50];
char rev_pm25_data_cnt = 0;
char rev_pm25_flag = 0;
char rev_pm25_timercnt = 0;
char rev_pm25_sucess = 0;
uint32_t pm25_value = 0;
uint32_t pm25_verify = 0;
unsigned char pm25_verify_cnt = 0;
int main(void) {
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
delay_init();
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE);
delay_ms(500);
uart_init(9600);
TIM4_Int_Init(99, 7199); /* 10Khz的计数频率,计数到50为10ms */
TIM_Cmd(TIM4, ENABLE); /* 使能TIMx 开始计时 */
while (1) {
delay_ms(5);
if (rev_pm25_sucess == 1) {
rev_pm25_sucess = 0;
if ((rev_pm25_bufferdata[0] == 0x42) && (rev_pm25_bufferdata[1] == 0x4d)) {
pm25_verify = 0;
for (pm25_verify_cnt = 0; pm25_verify_cnt < 30; pm25_verify_cnt++) {
pm25_verify += rev_pm25_bufferdata[pm25_verify_cnt];
}
if (pm25_verify == (rev_pm25_bufferdata[30] * 256 + rev_pm25_bufferdata[31])) {
pm25_value = rev_pm25_bufferdata[6] * 256 + rev_pm25_bufferdata[7];
}
}
}
}
}
// 10ms
void TIM4_IRQHandler(void) /* TIM4中断 */
{
if (TIM_GetITStatus(TIM4, TIM_IT_Update) != RESET) /* 检查TIM4更新中断发生与否 */
{
TIM_ClearITPendingBit(TIM4, TIM_IT_Update); /* 清除TIM4更新中断标志 */
//10ms一帧
if (rev_pm25_flag == 1) {
rev_pm25_timercnt++;
}
if (rev_pm25_timercnt >= 4) {
rev_pm25_timercnt = 0;
rev_pm25_flag = 0;
//数据处理标志
rev_pm25_sucess = 1;
}
}
}
//A9模块
void USART1_IRQHandler(void) /* 串口1中断服务程序 */
{
u8 receiveData;
if (USART_GetITStatus(USART1, USART_IT_RXNE) !=
RESET) /* 接收中断(接收到的数据必须是0x0d 0x0a结尾) */
{
receiveData = USART_ReceiveData(USART1); /* 读取接收到的数据 */
if (rev_pm25_flag == 0) {
rev_pm25_data_cnt = 0;
rev_pm25_bufferdata[rev_pm25_data_cnt++] = receiveData;
rev_pm25_flag = 1;
} else if (rev_pm25_flag == 1) {
rev_pm25_bufferdata[rev_pm25_data_cnt++] = receiveData;
if (rev_pm25_data_cnt >= 50) {
rev_pm25_data_cnt = 0;
}
}
}
}
帮助、问询
https://docs.qq.com/sheet/DUEdqZ2lmbmR6UVdU?tab=BB08J2