Xiaohai Li
/
AirBoxProto
Demo
PeripheralLayer/PeMod_ZE08.cpp@2:0ee90da44162, 2016-05-19 (annotated)
- Committer:
- nightseas
- Date:
- Thu May 19 15:52:24 2016 +0000
- Revision:
- 2:0ee90da44162
- Parent:
- 1:0c1053275589
AirBoxProtoDemo
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nightseas | 0:3dac4f477e98 | 1 | #include "SysConfig.h" |
nightseas | 0:3dac4f477e98 | 2 | |
nightseas | 0:3dac4f477e98 | 3 | #define ZE08_CMD_LEN 9 |
nightseas | 0:3dac4f477e98 | 4 | #define ZE08_DAT_LEN 9 |
nightseas | 0:3dac4f477e98 | 5 | |
nightseas | 0:3dac4f477e98 | 6 | #define uart_ze08 uart_sen |
nightseas | 1:0c1053275589 | 7 | //#define uart_db uart_pc |
nightseas | 0:3dac4f477e98 | 8 | |
nightseas | 0:3dac4f477e98 | 9 | const uint8_t ZE08_CmdManualMode[ZE08_CMD_LEN] = {0xFF, 0x01, 0x78, 0x41, 0x00, 0x00, 0x00, 0x00, 0x46}; |
nightseas | 0:3dac4f477e98 | 10 | const uint8_t ZE08_CmdReadCH2O[ZE08_CMD_LEN] = {0xFF, 0x01, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00, 0x79}; |
nightseas | 0:3dac4f477e98 | 11 | |
nightseas | 0:3dac4f477e98 | 12 | int ZE08_Init(void) |
nightseas | 0:3dac4f477e98 | 13 | { |
nightseas | 0:3dac4f477e98 | 14 | //Change ZE08 to manual request & reply mode |
nightseas | 0:3dac4f477e98 | 15 | for(int i=0; i<ZE08_CMD_LEN; i++) |
nightseas | 0:3dac4f477e98 | 16 | uart_ze08.putc(ZE08_CmdManualMode[i]); |
nightseas | 0:3dac4f477e98 | 17 | |
nightseas | 0:3dac4f477e98 | 18 | return 0; |
nightseas | 0:3dac4f477e98 | 19 | } |
nightseas | 0:3dac4f477e98 | 20 | |
nightseas | 0:3dac4f477e98 | 21 | int ZE08_ReadCH2O(void) |
nightseas | 0:3dac4f477e98 | 22 | { |
nightseas | 0:3dac4f477e98 | 23 | int ch2oVol, i, sum = 0; |
nightseas | 0:3dac4f477e98 | 24 | uint8_t data[ZE08_DAT_LEN]; |
nightseas | 0:3dac4f477e98 | 25 | |
nightseas | 0:3dac4f477e98 | 26 | for(i=0; i<ZE08_CMD_LEN; i++) |
nightseas | 0:3dac4f477e98 | 27 | uart_ze08.putc(ZE08_CmdReadCH2O[i]); |
nightseas | 0:3dac4f477e98 | 28 | |
nightseas | 0:3dac4f477e98 | 29 | #if defined uart_db |
nightseas | 0:3dac4f477e98 | 30 | uart_db.printf("\n\rZE08 return "); |
nightseas | 0:3dac4f477e98 | 31 | #endif |
nightseas | 0:3dac4f477e98 | 32 | |
nightseas | 0:3dac4f477e98 | 33 | for(i=0; i<ZE08_DAT_LEN; i++) |
nightseas | 0:3dac4f477e98 | 34 | { |
nightseas | 0:3dac4f477e98 | 35 | while(!uart_ze08.readable()); |
nightseas | 0:3dac4f477e98 | 36 | data[i] = uart_ze08.getc(); |
nightseas | 0:3dac4f477e98 | 37 | #if defined uart_db |
nightseas | 0:3dac4f477e98 | 38 | uart_db.printf("0x%02X ", data[i]); |
nightseas | 0:3dac4f477e98 | 39 | #endif |
nightseas | 0:3dac4f477e98 | 40 | } |
nightseas | 0:3dac4f477e98 | 41 | |
nightseas | 0:3dac4f477e98 | 42 | #if defined uart_db |
nightseas | 0:3dac4f477e98 | 43 | uart_db.printf(".\n\r"); |
nightseas | 0:3dac4f477e98 | 44 | #endif |
nightseas | 0:3dac4f477e98 | 45 | |
nightseas | 0:3dac4f477e98 | 46 | sum = ZE08_CalCheckSum(data); |
nightseas | 0:3dac4f477e98 | 47 | if(data[ZE08_DAT_LEN - 1] == sum) |
nightseas | 0:3dac4f477e98 | 48 | { |
nightseas | 0:3dac4f477e98 | 49 | ch2oVol = data[2] *256 + data[3]; |
nightseas | 0:3dac4f477e98 | 50 | return ch2oVol; |
nightseas | 0:3dac4f477e98 | 51 | } |
nightseas | 0:3dac4f477e98 | 52 | else |
nightseas | 0:3dac4f477e98 | 53 | { |
nightseas | 0:3dac4f477e98 | 54 | #if defined uart_db |
nightseas | 0:3dac4f477e98 | 55 | uart_db.printf("Incorrect checksum 0x%02X, expect 0x%02X.\n\r", sum, data[ZE08_DAT_LEN - 1]); |
nightseas | 0:3dac4f477e98 | 56 | #endif |
nightseas | 0:3dac4f477e98 | 57 | return -1; |
nightseas | 0:3dac4f477e98 | 58 | } |
nightseas | 0:3dac4f477e98 | 59 | } |
nightseas | 0:3dac4f477e98 | 60 | |
nightseas | 0:3dac4f477e98 | 61 | uint8_t ZE08_CalCheckSum(uint8_t *packet) |
nightseas | 0:3dac4f477e98 | 62 | { |
nightseas | 0:3dac4f477e98 | 63 | uint8_t i, checksum = 0; |
nightseas | 0:3dac4f477e98 | 64 | for( i = 1; i < 8; i++) |
nightseas | 0:3dac4f477e98 | 65 | { |
nightseas | 0:3dac4f477e98 | 66 | checksum += packet[i]; |
nightseas | 0:3dac4f477e98 | 67 | } |
nightseas | 0:3dac4f477e98 | 68 | checksum = 0xff - checksum; |
nightseas | 0:3dac4f477e98 | 69 | checksum += 1; |
nightseas | 0:3dac4f477e98 | 70 | return checksum; |
nightseas | 0:3dac4f477e98 | 71 | } |