Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
PeMod_ZPH01.cpp
00001 #include "SysConfig.h" 00002 00003 #define ZPH01_CMD_LEN 0 00004 #define ZPH01_DAT_LEN 9 00005 #define ZPH01_GAS_ID 0x18 00006 00007 #define uart_zph01 uart_sen 00008 //#define uart_db uart_pc 00009 00010 int ZPH01_Init(void) 00011 { 00012 return 0; 00013 } 00014 00015 float ZPH01_ReadPM25(void) 00016 { 00017 int tmp, sum, i; 00018 uint8_t data[ZPH01_DAT_LEN]; 00019 00020 #if defined uart_db 00021 uart_db.printf("\n\rZPH01 return "); 00022 #endif 00023 00024 do{ 00025 while(!uart_zph01.readable()); 00026 00027 tmp = uart_zph01.getc(); 00028 #if defined uart_db 00029 uart_db.printf("0x%02X ", tmp); 00030 #endif 00031 }while(tmp != 0xFF); 00032 data[0] = 0xFF; 00033 00034 do{ 00035 while(!uart_zph01.readable()); 00036 tmp = uart_zph01.getc(); 00037 #if defined uart_db 00038 uart_db.printf("0x%02X ", tmp); 00039 #endif 00040 }while(tmp != ZPH01_GAS_ID); 00041 data[1] = ZPH01_GAS_ID; 00042 00043 for(i=2; i<ZPH01_DAT_LEN; i++) 00044 { 00045 data[i] = uart_zph01.getc(); 00046 #if defined uart_db 00047 uart_db.printf("0x%02X ", data[i]); 00048 #endif 00049 } 00050 00051 #if defined uart_db 00052 uart_db.printf(".\n\r"); 00053 #endif 00054 00055 sum = ZPH01_CalCheckSum(data); 00056 if(data[ZPH01_DAT_LEN - 1] == sum) 00057 { 00058 //Unit: percents(%) 00059 float ratioPM25 = data[3] * 1.0 + data[4] / 100.0; 00060 #if defined uart_db 00061 uart_db.printf("PM2.5 PWM ratio is %f%%.\n\r", ratioPM25); 00062 #endif 00063 //Unit: ug/m^3 00064 float volPM25 = ratioPM25 * 20.43; 00065 return volPM25; 00066 } 00067 else 00068 { 00069 #if defined uart_db 00070 uart_db.printf("Incorrect checksum 0x%02X, expect 0x%02X.\n\r", sum, data[ZPH01_DAT_LEN - 1]); 00071 #endif 00072 return -1; 00073 } 00074 } 00075 00076 uint8_t ZPH01_CalCheckSum(uint8_t *packet) 00077 { 00078 uint8_t i, checksum = 0; 00079 for( i = 1; i < 8; i++) 00080 { 00081 checksum += packet[i]; 00082 } 00083 checksum = 0xff - checksum; 00084 checksum += 1; 00085 return checksum; 00086 }
Generated on Fri Jul 15 2022 14:37:21 by
1.7.2