攀藤PMS5003主动式传输协议:
官方推荐的电路:
我的连接方式:
主程序:
- /*
- 功能说明:
- 读取攀藤PMS5003空气质量传感器数据并显示PM2.5和PM10的值。
- */
- #include "config.h"
- #include "lcd1602.h"
- void UART_init()
- {
- TMOD = 0x20; //T1工作模式2 8位自动重装
- TH1 = 0xfd;
- TL1 = 0xfd; //比特率9600
- TR1 = 1; //启动T1定时器
- SM0 = 0;
- SM1 = 1; //串口工作方式1 10位异步
- REN = 1; //串口读数使能
- ET1 = 0; //禁止T1中断
- ES = 0; //禁止串口中断
- }
- void main()
- {
- uint8 i, strf;
- uint8 strDEC[4]; //用于LCD显示的字符串
- uint8 tmp[32]; //用于保存PMS5003发送的32位数据
- uint16 local_code, check_code; //校验码相关
- uint16 pm5, pm6; //5和6对应的是PMS5003数据5、6(大气环境下PM2.5和PM10)
- UART_init(); //串口初始化
- LCD1602_init();
- LCD1602_dis_str(0, 0, "PM2.5:");
- LCD1602_dis_str(0, 1, "PM10 :");
- local_code = 0;
- while(1)
- {
- //从第1个字节开始读取32字节数据
- do{
- while(RI==0); RI = 0; //等待1帧数据接收完毕,完了会自动置1,软件重新置0接收数据
- strf = SBUF;
- if (strf == 0x42) i = 0; //PMS5003数据串第1个字节为0x42
- tmp[i] = strf;
- i++;
- }while(i<32);
- for(i=0; i<30; i++)
- {
- local_code = local_code + tmp[i]; //本地校验码
- }
- check_code = ((uint16)tmp[30] << 8) + tmp[31]; //PMS5003发送的校验码
- if (local_code == check_code)
- {
- pm5 = ((uint16)tmp[12] << 8) + tmp[13];
- pm6 = ((uint16)tmp[14] << 8) + tmp[15];
- int2str(pm5, strDEC);
- LCD1602_dis_str(7, 0, " ");
- LCD1602_dis_str(7, 0, strDEC);
- int2str(pm6, strDEC);
- LCD1602_dis_str(7, 1, " ");
- LCD1602_dis_str(7, 1, strDEC);
- }
- check_code = 0;
- local_code = 0;
- }
- }
复制代码
|