PM2.5测量 -- PMS5003粉尘传感器使用

  PMS5003是一款基于激光散射原理的通用颗粒物浓度传感器,可测量PM1.0、PM2.5、PM10的值,数据通过UART串口直接输出,使用时只需根据传输协议将数据解码即可。(官网)

传感器详情请查看产品数据手册,下载连接:PMS5XXX 系列数据手册

下附基于STM32的程序

/**
 * 根据协议格式接收数据,串口每接收一次数据调用一次
 */
void pms5003ReceiveDataDepare(uint8_t data)
{
    static uint8_t state;
    static uint8_t data_len = 30, data_cnt;  // 数据帧长度 = 2*13 + 2 
    static uint8_t rxBuffer[33];

    if (state == 0 && data == 0x42) {  // 起始符1判断
        state = 1;
        rxBuffer[0] = data; 
    } else if (state == 1 && data == 0x4d) {  // 起始符2判断
        state = 2;
        rxBuffer[1] = data;
    } else if (state == 2 && data_len > 0) {  // 开始接收数据
        data_len--;
        rxBuffer[2 + data_cnt++] = data;
        if(data_len == 0)   state = 3;  // 数据接收完成
    } else if (state == 3) {
        state = 0;
        data_cnt = 0; 
        data_len = 30; 
        pms5003ReceiveDataAnl(rxBuffer);  
    }
}

/**
 * 根据协议格式解析数据,接收一帧数据完成后调用
 */
void pms5003ReceiveDataAnl(uint8_t *data_buffer)
{
    uint8_t i;
    uint32_t sum = 0;

    /* 校验和计算 */
    for (i=0; i<30; i++) {
        sum += data_buffer[i];
    }
    /* 接收数据校验和判断 */
    if (sum != ((data_buffer[30]<<8) | data_buffer[31])) {
        return;
    }

    /* 大气环境下 */
    pm1_val  = (data_buffer[10] << 8) | data_buffer[11];
    pm25_val = (data_buffer[12] << 8) | data_buffer[13];
    pm10_val = (data_buffer[14] << 8) | data_buffer[15]; 
}

/**
 *  串口3设置,用于与PMS5003通信   B10  PB11
 */
void usart3Config(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    NVIC_InitTypeDef  NVIC_InitStructure;
    USART_InitTypeDef USART_InitStructure;

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);

    RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);  // RCC_APB1 !!!

    /* Configure USART3.Tx(PB10) as alternate function push-pull */
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_Init(GPIOB, &GPIO_InitStructure);

    /* Configure USART3.Rx(PB11) as input floating */
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_Init(GPIOB, &GPIO_InitStructure);

    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
    NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;            
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;   
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;         
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;             
    NVIC_Init(&NVIC_InitStructure);   

    /* USART3 Mode config */
    USART_InitStructure.USART_BaudRate = 9600;  
    USART_InitStructure.USART_WordLength = USART_WordLength_8b;
    USART_InitStructure.USART_StopBits = USART_StopBits_1;
    USART_InitStructure.USART_Parity = USART_Parity_No;                     
    USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;                                              
    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
    USART_Init(USART3, &USART_InitStructure);

    USART_ClearITPendingBit(USART3, USART_IT_RXNE); 

    /* Enable USART3 receive interrupt */
    USART_ITConfig(USART3, USART_IT_RXNE, ENABLE); 

    /* Enable USART3 */
    USART_Cmd(USART3, ENABLE);
}

void USART3_IRQHandler(void)
{
    uint8_t com_data;

    if (USART_GetITStatus(USART3, USART_IT_RXNE)) {
        USART_ClearITPendingBit(USART3, USART_IT_RXNE);  
        com_data = USART3->DR;
        pms5003ReceiveDataDepare(com_data);     
    }
}

你可能感兴趣的:(单片机应用)