Demo

Dependencies:   mbed

Committer:
nightseas
Date:
Thu May 19 15:52:24 2016 +0000
Revision:
2:0ee90da44162
Parent:
1:0c1053275589
AirBoxProtoDemo

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nightseas 0:3dac4f477e98 1 #include "SysConfig.h"
nightseas 0:3dac4f477e98 2
nightseas 0:3dac4f477e98 3 #define ZPH01_CMD_LEN 0
nightseas 0:3dac4f477e98 4 #define ZPH01_DAT_LEN 9
nightseas 0:3dac4f477e98 5 #define ZPH01_GAS_ID 0x18
nightseas 0:3dac4f477e98 6
nightseas 0:3dac4f477e98 7 #define uart_zph01 uart_sen
nightseas 1:0c1053275589 8 //#define uart_db uart_pc
nightseas 0:3dac4f477e98 9
nightseas 0:3dac4f477e98 10 int ZPH01_Init(void)
nightseas 0:3dac4f477e98 11 {
nightseas 0:3dac4f477e98 12 return 0;
nightseas 0:3dac4f477e98 13 }
nightseas 0:3dac4f477e98 14
nightseas 0:3dac4f477e98 15 float ZPH01_ReadPM25(void)
nightseas 0:3dac4f477e98 16 {
nightseas 0:3dac4f477e98 17 int tmp, sum, i;
nightseas 0:3dac4f477e98 18 uint8_t data[ZPH01_DAT_LEN];
nightseas 0:3dac4f477e98 19
nightseas 0:3dac4f477e98 20 #if defined uart_db
nightseas 0:3dac4f477e98 21 uart_db.printf("\n\rZPH01 return ");
nightseas 0:3dac4f477e98 22 #endif
nightseas 0:3dac4f477e98 23
nightseas 0:3dac4f477e98 24 do{
nightseas 0:3dac4f477e98 25 while(!uart_zph01.readable());
nightseas 0:3dac4f477e98 26
nightseas 0:3dac4f477e98 27 tmp = uart_zph01.getc();
nightseas 0:3dac4f477e98 28 #if defined uart_db
nightseas 0:3dac4f477e98 29 uart_db.printf("0x%02X ", tmp);
nightseas 0:3dac4f477e98 30 #endif
nightseas 0:3dac4f477e98 31 }while(tmp != 0xFF);
nightseas 0:3dac4f477e98 32 data[0] = 0xFF;
nightseas 0:3dac4f477e98 33
nightseas 0:3dac4f477e98 34 do{
nightseas 0:3dac4f477e98 35 while(!uart_zph01.readable());
nightseas 0:3dac4f477e98 36 tmp = uart_zph01.getc();
nightseas 0:3dac4f477e98 37 #if defined uart_db
nightseas 0:3dac4f477e98 38 uart_db.printf("0x%02X ", tmp);
nightseas 0:3dac4f477e98 39 #endif
nightseas 0:3dac4f477e98 40 }while(tmp != ZPH01_GAS_ID);
nightseas 0:3dac4f477e98 41 data[1] = ZPH01_GAS_ID;
nightseas 0:3dac4f477e98 42
nightseas 0:3dac4f477e98 43 for(i=2; i<ZPH01_DAT_LEN; i++)
nightseas 0:3dac4f477e98 44 {
nightseas 0:3dac4f477e98 45 data[i] = uart_zph01.getc();
nightseas 0:3dac4f477e98 46 #if defined uart_db
nightseas 0:3dac4f477e98 47 uart_db.printf("0x%02X ", data[i]);
nightseas 0:3dac4f477e98 48 #endif
nightseas 0:3dac4f477e98 49 }
nightseas 0:3dac4f477e98 50
nightseas 0:3dac4f477e98 51 #if defined uart_db
nightseas 0:3dac4f477e98 52 uart_db.printf(".\n\r");
nightseas 0:3dac4f477e98 53 #endif
nightseas 0:3dac4f477e98 54
nightseas 0:3dac4f477e98 55 sum = ZPH01_CalCheckSum(data);
nightseas 0:3dac4f477e98 56 if(data[ZPH01_DAT_LEN - 1] == sum)
nightseas 0:3dac4f477e98 57 {
nightseas 0:3dac4f477e98 58 //Unit: percents(%)
nightseas 1:0c1053275589 59 float ratioPM25 = data[3] * 1.0 + data[4] / 100.0;
nightseas 1:0c1053275589 60 #if defined uart_db
nightseas 1:0c1053275589 61 uart_db.printf("PM2.5 PWM ratio is %f%%.\n\r", ratioPM25);
nightseas 1:0c1053275589 62 #endif
nightseas 0:3dac4f477e98 63 //Unit: ug/m^3
nightseas 0:3dac4f477e98 64 float volPM25 = ratioPM25 * 20.43;
nightseas 0:3dac4f477e98 65 return volPM25;
nightseas 0:3dac4f477e98 66 }
nightseas 0:3dac4f477e98 67 else
nightseas 0:3dac4f477e98 68 {
nightseas 0:3dac4f477e98 69 #if defined uart_db
nightseas 0:3dac4f477e98 70 uart_db.printf("Incorrect checksum 0x%02X, expect 0x%02X.\n\r", sum, data[ZPH01_DAT_LEN - 1]);
nightseas 0:3dac4f477e98 71 #endif
nightseas 0:3dac4f477e98 72 return -1;
nightseas 0:3dac4f477e98 73 }
nightseas 0:3dac4f477e98 74 }
nightseas 0:3dac4f477e98 75
nightseas 0:3dac4f477e98 76 uint8_t ZPH01_CalCheckSum(uint8_t *packet)
nightseas 0:3dac4f477e98 77 {
nightseas 0:3dac4f477e98 78 uint8_t i, checksum = 0;
nightseas 0:3dac4f477e98 79 for( i = 1; i < 8; i++)
nightseas 0:3dac4f477e98 80 {
nightseas 0:3dac4f477e98 81 checksum += packet[i];
nightseas 0:3dac4f477e98 82 }
nightseas 0:3dac4f477e98 83 checksum = 0xff - checksum;
nightseas 0:3dac4f477e98 84 checksum += 1;
nightseas 0:3dac4f477e98 85 return checksum;
nightseas 0:3dac4f477e98 86 }