Demo

Dependencies:   mbed

Committer:
nightseas
Date:
Thu May 19 15:52:24 2016 +0000
Revision:
2:0ee90da44162
Parent:
PeripheralLayer/PeMod_ESP01.cpp@1:0c1053275589
AirBoxProtoDemo

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nightseas 1:0c1053275589 1 #include "SysConfig.h"
nightseas 1:0c1053275589 2
nightseas 2:0ee90da44162 3 #define MHZ19_CMD_LEN 9
nightseas 2:0ee90da44162 4 #define MHZ19_DAT_LEN 9
nightseas 1:0c1053275589 5
nightseas 2:0ee90da44162 6 #define uart_mhz19 uart_sen
nightseas 1:0c1053275589 7 //#define uart_db uart_pc
nightseas 1:0c1053275589 8
nightseas 2:0ee90da44162 9 const uint8_t MHZ19_CmdMCalZero[MHZ19_CMD_LEN] = {0xFF, 0x01, 0x87, 0x00, 0x00, 0x00, 0x00, 0x00, 0x78};
nightseas 2:0ee90da44162 10 const uint8_t MHZ19_CmdMCalFull[MHZ19_CMD_LEN] = {0xFF, 0x01, 0x88, 0x07, 0xD0, 0x00, 0x00, 0x00, 0xA0};
nightseas 2:0ee90da44162 11 const uint8_t MHZ19_CmdReadCO2[MHZ19_CMD_LEN] = {0xFF, 0x01, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00, 0x79};
nightseas 1:0c1053275589 12
nightseas 2:0ee90da44162 13 int MHZ19_Init(void)
nightseas 1:0c1053275589 14 {
nightseas 1:0c1053275589 15 return 0;
nightseas 1:0c1053275589 16 }
nightseas 1:0c1053275589 17
nightseas 2:0ee90da44162 18 int MHZ19_ReadCO2(void)
nightseas 1:0c1053275589 19 {
nightseas 1:0c1053275589 20 int co2Vol, i, sum = 0;
nightseas 2:0ee90da44162 21 uint8_t data[MHZ19_DAT_LEN];
nightseas 1:0c1053275589 22
nightseas 2:0ee90da44162 23 for(i=0; i<MHZ19_CMD_LEN; i++)
nightseas 2:0ee90da44162 24 uart_mhz19.putc(MHZ19_CmdReadCO2[i]);
nightseas 1:0c1053275589 25
nightseas 1:0c1053275589 26 #if defined uart_db
nightseas 2:0ee90da44162 27 uart_db.printf("\n\rMHZ19 return ");
nightseas 1:0c1053275589 28 #endif
nightseas 1:0c1053275589 29
nightseas 2:0ee90da44162 30 for(i=0; i<MHZ19_DAT_LEN; i++)
nightseas 1:0c1053275589 31 {
nightseas 2:0ee90da44162 32 while(!uart_mhz19.readable());
nightseas 2:0ee90da44162 33 data[i] = uart_mhz19.getc();
nightseas 1:0c1053275589 34 #if defined uart_db
nightseas 1:0c1053275589 35 uart_db.printf("0x%02X ", data[i]);
nightseas 1:0c1053275589 36 #endif
nightseas 1:0c1053275589 37 }
nightseas 1:0c1053275589 38
nightseas 1:0c1053275589 39 #if defined uart_db
nightseas 1:0c1053275589 40 uart_db.printf(".\n\r");
nightseas 1:0c1053275589 41 #endif
nightseas 1:0c1053275589 42
nightseas 2:0ee90da44162 43 sum = MHZ19_CalCheckSum(data);
nightseas 2:0ee90da44162 44 if(data[MHZ19_DAT_LEN - 1] == sum)
nightseas 1:0c1053275589 45 {
nightseas 1:0c1053275589 46 co2Vol = data[2] *256 + data[3];
nightseas 1:0c1053275589 47 return co2Vol;
nightseas 1:0c1053275589 48 }
nightseas 1:0c1053275589 49 else
nightseas 1:0c1053275589 50 {
nightseas 1:0c1053275589 51 #if defined uart_db
nightseas 2:0ee90da44162 52 uart_db.printf("Incorrect checksum 0x%02X, expect 0x%02X.\n\r", sum, data[MHZ19_DAT_LEN - 1]);
nightseas 1:0c1053275589 53 #endif
nightseas 1:0c1053275589 54 return -1;
nightseas 1:0c1053275589 55 }
nightseas 1:0c1053275589 56 }
nightseas 1:0c1053275589 57
nightseas 2:0ee90da44162 58 int MHZ19_CalZero(void)
nightseas 2:0ee90da44162 59 {
nightseas 2:0ee90da44162 60 for(int i=0; i<MHZ19_CMD_LEN; i++)
nightseas 2:0ee90da44162 61 uart_mhz19.putc(MHZ19_CmdMCalZero[i]);
nightseas 2:0ee90da44162 62
nightseas 2:0ee90da44162 63 return 0;
nightseas 2:0ee90da44162 64 }
nightseas 2:0ee90da44162 65
nightseas 2:0ee90da44162 66 int MHZ19_CalFull(void)
nightseas 2:0ee90da44162 67 {
nightseas 2:0ee90da44162 68 for(int i=0; i<MHZ19_CMD_LEN; i++)
nightseas 2:0ee90da44162 69 uart_mhz19.putc(MHZ19_CmdMCalFull[i]);
nightseas 2:0ee90da44162 70
nightseas 2:0ee90da44162 71 return 0;
nightseas 2:0ee90da44162 72 }
nightseas 2:0ee90da44162 73
nightseas 2:0ee90da44162 74 uint8_t MHZ19_CalCheckSum(uint8_t *packet)
nightseas 1:0c1053275589 75 {
nightseas 1:0c1053275589 76 uint8_t i, checksum = 0;
nightseas 1:0c1053275589 77 for( i = 1; i < 8; i++)
nightseas 1:0c1053275589 78 {
nightseas 1:0c1053275589 79 checksum += packet[i];
nightseas 1:0c1053275589 80 }
nightseas 1:0c1053275589 81 checksum = 0xff - checksum;
nightseas 1:0c1053275589 82 checksum += 1;
nightseas 1:0c1053275589 83 return checksum;
nightseas 1:0c1053275589 84 }