|
攀藤PMS5003主動式傳輸協(xié)議:
官方推薦的電路:
我的連接方式:
主程序:
- /*
- 功能說明:
- 讀取攀藤PMS5003空氣質(zhì)量傳感器數(shù)據(jù)并顯示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; //串口讀數(shù)使能
- ET1 = 0; //禁止T1中斷
- ES = 0; //禁止串口中斷
- }
- void main()
- {
- uint8 i, strf;
- uint8 strDEC[4]; //用于LCD顯示的字符串
- uint8 tmp[32]; //用于保存PMS5003發(fā)送的32位數(shù)據(jù)
- uint16 local_code, check_code; //校驗碼相關(guān)
- uint16 pm5, pm6; //5和6對應(yīng)的是PMS5003數(shù)據(jù)5、6(大氣環(huán)境下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個字節(jié)開始讀取32字節(jié)數(shù)據(jù)
- do{
- while(RI==0); RI = 0; //等待1幀數(shù)據(jù)接收完畢,完了會自動置1,軟件重新置0接收數(shù)據(jù)
- strf = SBUF;
- if (strf == 0x42) i = 0; //PMS5003數(shù)據(jù)串第1個字節(jié)為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發(fā)送的校驗碼
- 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;
- }
- }
復(fù)制代碼
|
評分
-
查看全部評分
|