Xiaohai Li / Mbed 2 deprecated AirBoxProto

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers PeMod_MHZ19.cpp Source File

PeMod_MHZ19.cpp

00001 #include "SysConfig.h"
00002 
00003 #define MHZ19_CMD_LEN   9
00004 #define MHZ19_DAT_LEN   9
00005 
00006 #define uart_mhz19 uart_sen
00007 //#define uart_db uart_pc
00008 
00009 const uint8_t MHZ19_CmdMCalZero[MHZ19_CMD_LEN] = {0xFF, 0x01, 0x87, 0x00, 0x00, 0x00, 0x00, 0x00, 0x78};
00010 const uint8_t MHZ19_CmdMCalFull[MHZ19_CMD_LEN] = {0xFF, 0x01, 0x88, 0x07, 0xD0, 0x00, 0x00, 0x00, 0xA0};
00011 const uint8_t MHZ19_CmdReadCO2[MHZ19_CMD_LEN] = {0xFF, 0x01, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00, 0x79};
00012 
00013 int MHZ19_Init(void)
00014 {
00015     return 0;   
00016 }
00017 
00018 int MHZ19_ReadCO2(void)
00019 {
00020     int co2Vol, i, sum = 0; 
00021     uint8_t data[MHZ19_DAT_LEN];
00022            
00023     for(i=0; i<MHZ19_CMD_LEN; i++)
00024         uart_mhz19.putc(MHZ19_CmdReadCO2[i]);
00025         
00026     #if defined uart_db
00027     uart_db.printf("\n\rMHZ19 return ");
00028     #endif
00029         
00030     for(i=0; i<MHZ19_DAT_LEN; i++)
00031     {
00032         while(!uart_mhz19.readable());
00033         data[i] = uart_mhz19.getc();
00034         #if defined uart_db
00035         uart_db.printf("0x%02X ", data[i]);
00036         #endif
00037     }    
00038     
00039     #if defined uart_db
00040     uart_db.printf(".\n\r");
00041     #endif
00042     
00043     sum = MHZ19_CalCheckSum(data);
00044     if(data[MHZ19_DAT_LEN - 1] == sum)
00045     {
00046         co2Vol = data[2] *256 + data[3];
00047         return co2Vol;
00048     }
00049     else
00050     {
00051         #if defined uart_db
00052         uart_db.printf("Incorrect checksum 0x%02X, expect 0x%02X.\n\r", sum, data[MHZ19_DAT_LEN - 1]);
00053         #endif
00054         return -1;
00055     }
00056 }
00057 
00058 int MHZ19_CalZero(void)
00059 { 
00060     for(int i=0; i<MHZ19_CMD_LEN; i++)
00061         uart_mhz19.putc(MHZ19_CmdMCalZero[i]);
00062         
00063     return 0;        
00064 }
00065 
00066 int MHZ19_CalFull(void)
00067 {
00068     for(int i=0; i<MHZ19_CMD_LEN; i++)
00069         uart_mhz19.putc(MHZ19_CmdMCalFull[i]);
00070         
00071     return 0;        
00072 }
00073 
00074 uint8_t MHZ19_CalCheckSum(uint8_t *packet)
00075 {
00076     uint8_t i, checksum = 0;
00077     for( i = 1; i < 8; i++)
00078     {
00079         checksum += packet[i];
00080     }
00081     checksum = 0xff - checksum;
00082     checksum += 1;
00083     return checksum;
00084 }