攀藤PMS5003主動式傳輸協議:
2019-04-17_112819.png (44.53 KB, 下載次數: 82)
下載附件
2019-4-17 11:30 上傳
官方推薦的電路:
2019-04-17_113135.png (45.36 KB, 下載次數: 90)
下載附件
2019-4-17 11:40 上傳
我的連接方式:
2019-04-17_113924.png (171.66 KB, 下載次數: 88)
下載附件
2019-4-17 11:40 上傳
微信圖片_20190417113430.jpg (203.64 KB, 下載次數: 76)
下載附件
2019-4-17 11:40 上傳
主程序:
- /*
- 功能說明:
- 讀取攀藤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;
- }
- }
復制代碼
|