先用cubemx配置好一个串口
84.png
记得把中断勾上
85.png

开始写代码:

s7003.h

#include "main.h"
#include "stm32l0xx_hal.h"

uint32_t get_cf_pm_2_5();
uint32_t get_cf_pm_1();

uint32_t get_air_pm_2_5();
uint32_t get_air_pm_1();

s7003.c

#include "s7003.h"
#include "12864.h"

uint8_t RxData1[32];
extern UART_HandleTypeDef huart1;

struct
{
    uint32_t CF_PM1;
    uint32_t CF_PM2_5;
    uint32_t CF_PM10;

    uint32_t Air_PM1;
    uint32_t Air_PM2_5;
    uint32_t Air_PM10;

    uint32_t L_um3;
    uint32_t L_um5;
    uint32_t L_1um;
    uint32_t L_2um5;
    uint32_t L_5um;
    uint32_t L_10um;
}GetAirdata;

void get_pm()
{
        static long ti;
        char a;
            HAL_UART_Receive_IT(&huart1,RxData1,32);    
            if(RxData1[0] == 0x42 && RxData1[1] == 0x4d)
            {
                //printf_str(0,0,0,"yes",1);
                GetAirdata.CF_PM1 = RxData1[4] << 8 | RxData1[5];
                GetAirdata.CF_PM2_5 = (uint8_t)(RxData1[6] << 8 | RxData1[7]);
                GetAirdata.CF_PM10 = RxData1[8] << 8 | RxData1[9] + 48;

                GetAirdata.Air_PM1 = RxData1[10] << 8 | RxData1[11] + 48;
                GetAirdata.Air_PM2_5 = RxData1[12] << 8 | RxData1[13] + 48;
                GetAirdata.Air_PM10 =RxData1[14] << 8 | RxData1[15];

                GetAirdata.L_um3 = RxData1[16] << 8 | RxData1[17];
                GetAirdata.L_um5 = RxData1[18] << 8 | RxData1[19];
                GetAirdata.L_1um = RxData1[20] << 8 | RxData1[21];
                GetAirdata.L_2um5 = RxData1[22] << 8 | RxData1[23];
                GetAirdata.L_5um = RxData1[24] << 8 | RxData1[25];
                GetAirdata.L_10um = RxData1[26] << 8 | RxData1[27];
                for(char num=0;num<32;num++)
                {
                    RxData1[num] = 0;    
                }


            }


}

uint32_t get_cf_pm_2_5()
{
    get_pm();
    return GetAirdata.CF_PM2_5;
}
uint32_t get_cf_pm_1()
{
    get_pm();
    return GetAirdata.CF_PM1;
}

uint32_t get_air_pm_2_5()
{
    get_pm();
    return GetAirdata.Air_PM2_5;
}
uint32_t get_air_pm_1()
{
    get_pm();
    return GetAirdata.Air_PM1;
}

98.jpg
99.jpg

Last modification:July 3rd, 2020 at 09:22 pm
如果觉得我的文章对你有用,请随意赞赏