SenseAir LP8 connected to BLE Nano
Dependencies: BLE_API mbed nRF51822
Fork of SenseAirLP8 by
LP8.h@6:7ce8b52c2c5e, 2017-08-28 (annotated)
- Committer:
- rodjegard
- Date:
- Mon Aug 28 14:05:01 2017 +0000
- Revision:
- 6:7ce8b52c2c5e
- Parent:
- 5:2c80954571b6
SenseAir LP8 and BLE Nano firmware
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jony1401 | 0:ee3787c8e209 | 1 | #ifndef LP8_H |
jony1401 | 0:ee3787c8e209 | 2 | #define LP8_H |
jony1401 | 0:ee3787c8e209 | 3 | |
jony1401 | 2:d02255d8c36f | 4 | /* To initialize the lp8 object, you need to pass a serial (tx, rx), a DigitalOut signal for the en_vbb, |
jony1401 | 2:d02255d8c36f | 5 | an DigitalIn for the ready signal, DigitalOut for Reset and a timer object. */ |
jony1401 | 1:b512a405b584 | 6 | |
jony1401 | 0:ee3787c8e209 | 7 | class LP8 |
jony1401 | 0:ee3787c8e209 | 8 | { |
jony1401 | 0:ee3787c8e209 | 9 | public: |
jony1401 | 1:b512a405b584 | 10 | //constructor |
jony1401 | 2:d02255d8c36f | 11 | LP8(Serial &device, DigitalOut &vbb_en, DigitalIn &rdy, DigitalOut &res ,Timer &_timer): |
jony1401 | 1:b512a405b584 | 12 | Device(device), |
jony1401 | 1:b512a405b584 | 13 | VBB_EN(vbb_en), |
jony1401 | 1:b512a405b584 | 14 | RDY(rdy), |
jony1401 | 2:d02255d8c36f | 15 | RES(res), |
jony1401 | 1:b512a405b584 | 16 | lp8Wait(_timer) |
jony1401 | 1:b512a405b584 | 17 | { |
jony1401 | 3:933dd59ad44d | 18 | Device.baud(9600); //set baud rate to 9600 Hz |
jony1401 | 3:933dd59ad44d | 19 | Device.format(8, SerialBase::None, 2); //set bits, parity and stop bits |
jony1401 | 0:ee3787c8e209 | 20 | |
jony1401 | 1:b512a405b584 | 21 | //initialize arrays with lp8 modbus commands. |
jony1401 | 1:b512a405b584 | 22 | //initial startup command: |
jony1401 | 1:b512a405b584 | 23 | firstWrite[0] = 0xfe; //device adress |
jony1401 | 1:b512a405b584 | 24 | firstWrite[1] = 0x41; //write to ram |
jony1401 | 1:b512a405b584 | 25 | firstWrite[2] = 0x00; // |
jony1401 | 1:b512a405b584 | 26 | firstWrite[3] = 0x80; //starting adress |
jony1401 | 1:b512a405b584 | 27 | firstWrite[4] = 0x01; //nr of bytes to send |
jony1401 | 1:b512a405b584 | 28 | firstWrite[5] = 0x10; //calculation control byte |
jony1401 | 1:b512a405b584 | 29 | firstWrite[6] = 0x28; //crc low |
jony1401 | 1:b512a405b584 | 30 | firstWrite[7] = 0x7e; //crc high |
jony1401 | 0:ee3787c8e209 | 31 | |
jony1401 | 1:b512a405b584 | 32 | //write previous sensor state to lp8, command: |
jony1401 | 1:b512a405b584 | 33 | stateWrite[0] = 0xfe; //device adress |
jony1401 | 1:b512a405b584 | 34 | stateWrite[1] = 0x41; //write to ram |
jony1401 | 1:b512a405b584 | 35 | stateWrite[2] = 0x00; // |
jony1401 | 1:b512a405b584 | 36 | stateWrite[3] = 0x80; //starting adress |
jony1401 | 1:b512a405b584 | 37 | stateWrite[4] = 0x18; //nr of bytes to send |
jony1401 | 1:b512a405b584 | 38 | stateWrite[5] = 0x20; //Calculation Control |
jony1401 | 1:b512a405b584 | 39 | for(int k = 6; k < 31; k++) { stateWrite[k] = 0x00; } // |
jony1401 | 0:ee3787c8e209 | 40 | |
jony1401 | 1:b512a405b584 | 41 | //read request from the lp8: |
jony1401 | 1:b512a405b584 | 42 | stateRead[0] = 0xfe; //adress |
jony1401 | 1:b512a405b584 | 43 | stateRead[1] = 0x44; //read from ram |
jony1401 | 1:b512a405b584 | 44 | stateRead[2] = 0x00; // |
jony1401 | 1:b512a405b584 | 45 | stateRead[3] = 0x80; //starting adress |
jony1401 | 1:b512a405b584 | 46 | stateRead[4] = 0x2c; //number of bytes to read |
jony1401 | 1:b512a405b584 | 47 | stateRead[5] = 0x00; //crc_l |
jony1401 | 3:933dd59ad44d | 48 | stateRead[6] = 0x00; //crc_h |
jony1401 | 3:933dd59ad44d | 49 | |
jony1401 | 3:933dd59ad44d | 50 | //communication confirmation sequence (response from lp8) |
jony1401 | 3:933dd59ad44d | 51 | confirmation[0] = 0xfe; |
jony1401 | 3:933dd59ad44d | 52 | confirmation[1] = 0x41; |
jony1401 | 3:933dd59ad44d | 53 | confirmation[2] = 0x81; |
jony1401 | 3:933dd59ad44d | 54 | confirmation[3] = 0xe0; |
jony1401 | 1:b512a405b584 | 55 | |
jony1401 | 1:b512a405b584 | 56 | //response buffer |
jony1401 | 1:b512a405b584 | 57 | for(int k = 0; k < 60; k++) { response[k] = 0x00; } |
jony1401 | 0:ee3787c8e209 | 58 | |
jony1401 | 1:b512a405b584 | 59 | //variable initialization |
jony1401 | 3:933dd59ad44d | 60 | co2 = 400; // |
jony1401 | 3:933dd59ad44d | 61 | CRC = 0x0000; // |
jony1401 | 2:d02255d8c36f | 62 | |
jony1401 | 1:b512a405b584 | 63 | }; |
jony1401 | 0:ee3787c8e209 | 64 | |
jony1401 | 0:ee3787c8e209 | 65 | |
jony1401 | 2:d02255d8c36f | 66 | //LP8 Initialization and first message |
jony1401 | 0:ee3787c8e209 | 67 | bool lp8Init(){ |
jony1401 | 2:d02255d8c36f | 68 | |
jony1401 | 3:933dd59ad44d | 69 | Device.format(8, SerialBase::None, 2); |
jony1401 | 2:d02255d8c36f | 70 | |
jony1401 | 2:d02255d8c36f | 71 | //Enable Sensor |
jony1401 | 0:ee3787c8e209 | 72 | VBB_EN.write( 1 ); //power on |
jony1401 | 3:933dd59ad44d | 73 | |
jony1401 | 1:b512a405b584 | 74 | //wait for rdy signal |
dimion | 5:2c80954571b6 | 75 | timeIt( 0.21 ); //wait for lp8 rdy signal |
jony1401 | 3:933dd59ad44d | 76 | // while(RDY.read() != 0 ) { /* wait for rdy to go low */} |
jony1401 | 3:933dd59ad44d | 77 | |
jony1401 | 3:933dd59ad44d | 78 | //transmit first packet |
jony1401 | 1:b512a405b584 | 79 | transmitPacket(firstWrite, 8); //Send out msg (and nr of bytes) over serial line |
jony1401 | 0:ee3787c8e209 | 80 | Response( 4 ); //read 4 bytes response |
jony1401 | 3:933dd59ad44d | 81 | |
jony1401 | 3:933dd59ad44d | 82 | //check response |
jony1401 | 3:933dd59ad44d | 83 | if ( responseCompare() != true){ |
dimion | 5:2c80954571b6 | 84 | VBB_EN.write(0); |
jony1401 | 3:933dd59ad44d | 85 | return false; |
dimion | 5:2c80954571b6 | 86 | } |
jony1401 | 3:933dd59ad44d | 87 | |
jony1401 | 1:b512a405b584 | 88 | //compute crc |
jony1401 | 0:ee3787c8e209 | 89 | CRC = modbusCrc(stateRead, 5); |
jony1401 | 3:933dd59ad44d | 90 | |
jony1401 | 1:b512a405b584 | 91 | //add crc value to the transmit package |
jony1401 | 1:b512a405b584 | 92 | stateRead[5] = (uint8_t)CRC; //crc_l |
jony1401 | 1:b512a405b584 | 93 | stateRead[6] = (uint8_t)(CRC >> 8); //crc_h |
jony1401 | 3:933dd59ad44d | 94 | |
jony1401 | 1:b512a405b584 | 95 | //wait for rdy |
dimion | 5:2c80954571b6 | 96 | timeIt( 0.25 ); // |
jony1401 | 3:933dd59ad44d | 97 | // while(RDY.read() != 1 ) { /*wait for rdy to go high */} |
jony1401 | 3:933dd59ad44d | 98 | |
jony1401 | 0:ee3787c8e209 | 99 | transmitPacket(stateRead, 7); //transmit packet |
jony1401 | 3:933dd59ad44d | 100 | Response( 49 ); //get sensor state and co2 value(s) |
jony1401 | 1:b512a405b584 | 101 | VBB_EN.write( 0 ); //power off lp8 |
jony1401 | 2:d02255d8c36f | 102 | |
jony1401 | 3:933dd59ad44d | 103 | return true; |
jony1401 | 0:ee3787c8e209 | 104 | }; |
jony1401 | 0:ee3787c8e209 | 105 | |
jony1401 | 1:b512a405b584 | 106 | //send subsequent messages to the lp8 |
jony1401 | 3:933dd59ad44d | 107 | bool lp8Talk(uint8_t inc_ccByte){ |
jony1401 | 2:d02255d8c36f | 108 | |
jony1401 | 3:933dd59ad44d | 109 | static const uint8_t A[] = {0xFE,0x41,0x00,0x80,0x20,0x00,0x00,0x02,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00, |
jony1401 | 3:933dd59ad44d | 110 | 0x00,0x7F,0xFF,0x00,0x00,0x95,0xED,0x00,0x03,0x29,0x00,0x27,0x8C, 0xf5, 0x63}; |
jony1401 | 3:933dd59ad44d | 111 | |
jony1401 | 3:933dd59ad44d | 112 | Device.format(8, SerialBase::None, 2); |
jony1401 | 0:ee3787c8e209 | 113 | //transfer previous sensor state to the new msg out |
jony1401 | 0:ee3787c8e209 | 114 | for (int u = 4; u < 23+4; u++) { |
jony1401 | 0:ee3787c8e209 | 115 | stateWrite[u+2] = response[u]; |
jony1401 | 0:ee3787c8e209 | 116 | } |
jony1401 | 3:933dd59ad44d | 117 | //Set Calibration Control Byte (see end of page for explantion), |
jony1401 | 3:933dd59ad44d | 118 | /* remove this statement if you want to be able to send anything to lp8 calculation control */ |
jony1401 | 3:933dd59ad44d | 119 | if( inc_ccByte != 0x20 ){ /* 0x52 is "simple recalibration, needs only one sample */ |
jony1401 | 3:933dd59ad44d | 120 | stateWrite[5] = inc_ccByte; |
jony1401 | 3:933dd59ad44d | 121 | } |
jony1401 | 3:933dd59ad44d | 122 | else { |
jony1401 | 3:933dd59ad44d | 123 | stateWrite[5] = 0x20; |
jony1401 | 3:933dd59ad44d | 124 | } |
jony1401 | 3:933dd59ad44d | 125 | |
jony1401 | 3:933dd59ad44d | 126 | |
jony1401 | 0:ee3787c8e209 | 127 | //compute new crc value |
jony1401 | 3:933dd59ad44d | 128 | CRC = modbusCrc((uint8_t*)stateWrite, 29); |
jony1401 | 3:933dd59ad44d | 129 | |
jony1401 | 0:ee3787c8e209 | 130 | //add new crc value to send list |
jony1401 | 0:ee3787c8e209 | 131 | stateWrite[29] = (uint8_t)CRC; |
jony1401 | 0:ee3787c8e209 | 132 | stateWrite[30] = (uint8_t)(CRC >> 8); |
jony1401 | 2:d02255d8c36f | 133 | |
jony1401 | 0:ee3787c8e209 | 134 | //initialize new transfer sequence |
jony1401 | 0:ee3787c8e209 | 135 | VBB_EN.write( 1 ); //power on sensor |
jony1401 | 3:933dd59ad44d | 136 | |
jony1401 | 2:d02255d8c36f | 137 | timeIt( 0.35 ); |
jony1401 | 3:933dd59ad44d | 138 | // while(RDY.read() != 0 ) { /* wait for rdy */} |
jony1401 | 3:933dd59ad44d | 139 | |
jony1401 | 3:933dd59ad44d | 140 | transmitPacket(/*(uint8_t*)A*/(uint8_t*)stateWrite, 31); //Send out msg with previous state (and nr of elements) over serial line |
jony1401 | 3:933dd59ad44d | 141 | Response( 4 ); //read 4 bytes response |
jony1401 | 3:933dd59ad44d | 142 | |
jony1401 | 3:933dd59ad44d | 143 | //compare to confirmation sequence |
jony1401 | 3:933dd59ad44d | 144 | if ( responseCompare() != true){ |
dimion | 5:2c80954571b6 | 145 | VBB_EN.write( 0 ); |
jony1401 | 3:933dd59ad44d | 146 | return false; |
jony1401 | 3:933dd59ad44d | 147 | } |
jony1401 | 3:933dd59ad44d | 148 | |
jony1401 | 0:ee3787c8e209 | 149 | //compute crc |
jony1401 | 0:ee3787c8e209 | 150 | CRC = modbusCrc(stateRead, 5); |
jony1401 | 3:933dd59ad44d | 151 | |
jony1401 | 0:ee3787c8e209 | 152 | //add crc value to the read request transmit package |
jony1401 | 0:ee3787c8e209 | 153 | stateRead[5] = (uint8_t)CRC; //crc_l |
jony1401 | 0:ee3787c8e209 | 154 | stateRead[6] = (uint8_t)(CRC >> 8); //crc_h |
jony1401 | 3:933dd59ad44d | 155 | |
jony1401 | 3:933dd59ad44d | 156 | timeIt( 0.20 ); |
jony1401 | 3:933dd59ad44d | 157 | // while(RDY.read() != 1 ) { /* wait for rdy to go high */} |
jony1401 | 3:933dd59ad44d | 158 | |
jony1401 | 0:ee3787c8e209 | 159 | //send read request |
jony1401 | 0:ee3787c8e209 | 160 | transmitPacket(stateRead, 7); //transmit packet |
jony1401 | 3:933dd59ad44d | 161 | |
jony1401 | 0:ee3787c8e209 | 162 | //read sensor response |
jony1401 | 3:933dd59ad44d | 163 | Response( 49 ); //get sensor state |
jony1401 | 3:933dd59ad44d | 164 | VBB_EN.write( 0 ); //power off |
jony1401 | 3:933dd59ad44d | 165 | |
jony1401 | 3:933dd59ad44d | 166 | return true; |
jony1401 | 0:ee3787c8e209 | 167 | }; |
jony1401 | 1:b512a405b584 | 168 | |
jony1401 | 2:d02255d8c36f | 169 | //get value from lp8 response |
jony1401 | 2:d02255d8c36f | 170 | unsigned long getValue() /* CO2 Value */ |
jony1401 | 0:ee3787c8e209 | 171 | { |
jony1401 | 1:b512a405b584 | 172 | int high = response[29]; |
jony1401 | 1:b512a405b584 | 173 | int low = response[30]; |
jony1401 | 0:ee3787c8e209 | 174 | unsigned long val = high * 256 + low; |
jony1401 | 0:ee3787c8e209 | 175 | |
jony1401 | 0:ee3787c8e209 | 176 | return val; |
jony1401 | 0:ee3787c8e209 | 177 | } |
jony1401 | 2:d02255d8c36f | 178 | |
jony1401 | 3:933dd59ad44d | 179 | float getTempValue() |
jony1401 | 2:d02255d8c36f | 180 | { |
jony1401 | 2:d02255d8c36f | 181 | int h = response[33]; |
jony1401 | 2:d02255d8c36f | 182 | int l = response[34]; |
jony1401 | 2:d02255d8c36f | 183 | unsigned long _temp = h * 256 + l; |
jony1401 | 2:d02255d8c36f | 184 | |
jony1401 | 3:933dd59ad44d | 185 | float _tempVal = 0.01 * _temp; |
jony1401 | 2:d02255d8c36f | 186 | |
jony1401 | 2:d02255d8c36f | 187 | return _tempVal; |
jony1401 | 2:d02255d8c36f | 188 | } |
jony1401 | 2:d02255d8c36f | 189 | |
jony1401 | 3:933dd59ad44d | 190 | /* get Vcap value for at current measurement, [35],[36] is previous Vcap value */ |
jony1401 | 2:d02255d8c36f | 191 | int getVcapValue(){ |
jony1401 | 2:d02255d8c36f | 192 | |
jony1401 | 3:933dd59ad44d | 193 | int hB = response[37]; |
jony1401 | 3:933dd59ad44d | 194 | int lB = response[38]; |
jony1401 | 2:d02255d8c36f | 195 | unsigned long temp = hB * 256 + lB; |
jony1401 | 2:d02255d8c36f | 196 | |
jony1401 | 2:d02255d8c36f | 197 | return temp; |
jony1401 | 2:d02255d8c36f | 198 | } |
jony1401 | 2:d02255d8c36f | 199 | |
jony1401 | 2:d02255d8c36f | 200 | uint32_t getErrorStatus(){ |
jony1401 | 2:d02255d8c36f | 201 | |
jony1401 | 2:d02255d8c36f | 202 | uint32_t tmp = 0; |
jony1401 | 2:d02255d8c36f | 203 | tmp += response[39] << (32-8); |
jony1401 | 2:d02255d8c36f | 204 | tmp += response[40] << (32-16); |
jony1401 | 2:d02255d8c36f | 205 | tmp += response[41] << (32-24); |
jony1401 | 2:d02255d8c36f | 206 | tmp += response[42]; |
jony1401 | 2:d02255d8c36f | 207 | |
jony1401 | 2:d02255d8c36f | 208 | return tmp; |
jony1401 | 2:d02255d8c36f | 209 | } |
jony1401 | 2:d02255d8c36f | 210 | |
jony1401 | 2:d02255d8c36f | 211 | //get calculation Control byte from lp8 response |
jony1401 | 2:d02255d8c36f | 212 | uint8_t getCCbyte(){ |
jony1401 | 3:933dd59ad44d | 213 | uint8_t rCCbyte = stateWrite[5]; |
jony1401 | 3:933dd59ad44d | 214 | return rCCbyte; |
jony1401 | 2:d02255d8c36f | 215 | } |
jony1401 | 2:d02255d8c36f | 216 | |
jony1401 | 1:b512a405b584 | 217 | |
jony1401 | 1:b512a405b584 | 218 | /************************************************* Helper Functions ********************************************/ |
jony1401 | 2:d02255d8c36f | 219 | |
jony1401 | 1:b512a405b584 | 220 | //purge response buffer |
jony1401 | 0:ee3787c8e209 | 221 | void responsePurge(int bytesToPurge){ |
jony1401 | 0:ee3787c8e209 | 222 | for (int j = 0; j < bytesToPurge; j++) { |
jony1401 | 0:ee3787c8e209 | 223 | response[j] = 0x00; |
jony1401 | 0:ee3787c8e209 | 224 | } |
jony1401 | 3:933dd59ad44d | 225 | //for(int k = 6; k < 31; k++) { stateWrite[k] = 0x00; } //purge sensor state |
jony1401 | 0:ee3787c8e209 | 226 | }; |
jony1401 | 1:b512a405b584 | 227 | |
jony1401 | 3:933dd59ad44d | 228 | //read response from lp8 |
jony1401 | 3:933dd59ad44d | 229 | void Response( int bytesToRead ){ |
jony1401 | 3:933dd59ad44d | 230 | int Count = 0; |
jony1401 | 4:320febe026ed | 231 | |
jony1401 | 4:320febe026ed | 232 | lp8Wait.start(); /* timeout timer if something goes wrong */ |
jony1401 | 4:320febe026ed | 233 | |
jony1401 | 0:ee3787c8e209 | 234 | do { |
jony1401 | 0:ee3787c8e209 | 235 | if(Device.readable()) { |
jony1401 | 3:933dd59ad44d | 236 | response[Count] = Device.getc(); |
jony1401 | 3:933dd59ad44d | 237 | Count++; |
jony1401 | 3:933dd59ad44d | 238 | --bytesToRead; |
jony1401 | 0:ee3787c8e209 | 239 | } |
jony1401 | 0:ee3787c8e209 | 240 | } |
jony1401 | 4:320febe026ed | 241 | while( (bytesToRead > 0) || (lp8Wait.read() < 0.2 )); |
jony1401 | 4:320febe026ed | 242 | |
jony1401 | 4:320febe026ed | 243 | lp8Wait.stop(); |
jony1401 | 4:320febe026ed | 244 | lp8Wait.reset(); |
jony1401 | 0:ee3787c8e209 | 245 | }; |
jony1401 | 0:ee3787c8e209 | 246 | |
jony1401 | 1:b512a405b584 | 247 | //transmit data over serial lines |
jony1401 | 0:ee3787c8e209 | 248 | void transmitPacket(uint8_t msg[], int le ){ |
jony1401 | 0:ee3787c8e209 | 249 | //Send out msg over serial line: |
jony1401 | 0:ee3787c8e209 | 250 | while(!Device.writeable() ) { /* wait for serial available*/ } |
jony1401 | 0:ee3787c8e209 | 251 | for(int pos = 0; pos < le; pos++) { |
jony1401 | 0:ee3787c8e209 | 252 | Device.putc(msg[pos]); |
jony1401 | 0:ee3787c8e209 | 253 | } |
jony1401 | 0:ee3787c8e209 | 254 | |
jony1401 | 0:ee3787c8e209 | 255 | }; |
jony1401 | 0:ee3787c8e209 | 256 | |
jony1401 | 1:b512a405b584 | 257 | //timer |
jony1401 | 2:d02255d8c36f | 258 | void timeIt(float timeMe){ |
jony1401 | 0:ee3787c8e209 | 259 | //start amd stop timer... |
jony1401 | 0:ee3787c8e209 | 260 | lp8Wait.start(); |
jony1401 | 0:ee3787c8e209 | 261 | while (lp8Wait.read() < timeMe ) { /* W A I T I N G */ } |
jony1401 | 0:ee3787c8e209 | 262 | lp8Wait.stop(); |
jony1401 | 0:ee3787c8e209 | 263 | lp8Wait.reset(); |
jony1401 | 0:ee3787c8e209 | 264 | }; |
jony1401 | 0:ee3787c8e209 | 265 | |
jony1401 | 1:b512a405b584 | 266 | // Computation for the modbus 16-bit crc |
jony1401 | 0:ee3787c8e209 | 267 | uint16_t modbusCrc(uint8_t buffer[], int len){ |
jony1401 | 0:ee3787c8e209 | 268 | uint16_t crc = 0xFFFF; |
jony1401 | 0:ee3787c8e209 | 269 | |
jony1401 | 0:ee3787c8e209 | 270 | for (int pos = 0; pos < len; pos++) { |
jony1401 | 0:ee3787c8e209 | 271 | crc ^= (uint16_t)buffer[pos]; // XOR byte into least sig. byte of crc |
jony1401 | 0:ee3787c8e209 | 272 | |
jony1401 | 0:ee3787c8e209 | 273 | for (int i = 8; i != 0; i--) { // Loop over each bit |
jony1401 | 0:ee3787c8e209 | 274 | if ((crc & 0x0001) != 0) { // If the LSB is set |
jony1401 | 0:ee3787c8e209 | 275 | crc >>= 1; // shift right and XOR 0xA001 |
jony1401 | 0:ee3787c8e209 | 276 | crc ^= 0xA001; |
jony1401 | 0:ee3787c8e209 | 277 | } |
jony1401 | 0:ee3787c8e209 | 278 | else // Else LSB is not set |
jony1401 | 0:ee3787c8e209 | 279 | crc >>= 1; // shift right |
jony1401 | 0:ee3787c8e209 | 280 | } |
jony1401 | 0:ee3787c8e209 | 281 | } |
jony1401 | 0:ee3787c8e209 | 282 | // Note, this number has low and high bytes swapped |
jony1401 | 0:ee3787c8e209 | 283 | return crc; |
jony1401 | 0:ee3787c8e209 | 284 | }; |
jony1401 | 1:b512a405b584 | 285 | |
jony1401 | 3:933dd59ad44d | 286 | bool responseCompare(){ |
jony1401 | 3:933dd59ad44d | 287 | |
jony1401 | 3:933dd59ad44d | 288 | short seq = 0; |
jony1401 | 3:933dd59ad44d | 289 | |
jony1401 | 3:933dd59ad44d | 290 | for(int j=0; j < 4; j++){ |
jony1401 | 3:933dd59ad44d | 291 | if(response[j] == confirmation[j]){ |
jony1401 | 3:933dd59ad44d | 292 | seq++; |
jony1401 | 3:933dd59ad44d | 293 | } |
jony1401 | 3:933dd59ad44d | 294 | } |
jony1401 | 3:933dd59ad44d | 295 | //return false if check sequence fails |
jony1401 | 3:933dd59ad44d | 296 | if( seq != 4 ){ |
jony1401 | 3:933dd59ad44d | 297 | return false; |
jony1401 | 3:933dd59ad44d | 298 | } |
jony1401 | 3:933dd59ad44d | 299 | |
jony1401 | 3:933dd59ad44d | 300 | return true; |
jony1401 | 3:933dd59ad44d | 301 | } |
jony1401 | 3:933dd59ad44d | 302 | |
jony1401 | 1:b512a405b584 | 303 | //variables and buffers |
jony1401 | 0:ee3787c8e209 | 304 | private: |
jony1401 | 2:d02255d8c36f | 305 | //pins |
jony1401 | 0:ee3787c8e209 | 306 | Serial &Device; |
jony1401 | 0:ee3787c8e209 | 307 | DigitalOut &VBB_EN; |
jony1401 | 0:ee3787c8e209 | 308 | DigitalIn &RDY; |
jony1401 | 2:d02255d8c36f | 309 | DigitalOut &RES; |
jony1401 | 2:d02255d8c36f | 310 | |
jony1401 | 0:ee3787c8e209 | 311 | Timer &lp8Wait; |
jony1401 | 0:ee3787c8e209 | 312 | |
jony1401 | 1:b512a405b584 | 313 | //msg containers |
jony1401 | 3:933dd59ad44d | 314 | |
jony1401 | 0:ee3787c8e209 | 315 | uint8_t firstWrite[8]; |
jony1401 | 3:933dd59ad44d | 316 | volatile uint8_t stateWrite[31]; |
jony1401 | 0:ee3787c8e209 | 317 | uint8_t stateRead[7]; |
jony1401 | 3:933dd59ad44d | 318 | uint8_t confirmation[4]; |
jony1401 | 3:933dd59ad44d | 319 | |
jony1401 | 3:933dd59ad44d | 320 | volatile uint8_t response[60]; |
jony1401 | 3:933dd59ad44d | 321 | |
jony1401 | 2:d02255d8c36f | 322 | // |
jony1401 | 2:d02255d8c36f | 323 | int co2; //CO2 initial value |
jony1401 | 2:d02255d8c36f | 324 | int tempValue; |
jony1401 | 0:ee3787c8e209 | 325 | uint16_t CRC; //modbus crc value |
jony1401 | 0:ee3787c8e209 | 326 | |
jony1401 | 0:ee3787c8e209 | 327 | |
jony1401 | 0:ee3787c8e209 | 328 | }; |
jony1401 | 0:ee3787c8e209 | 329 | #endif |
jony1401 | 0:ee3787c8e209 | 330 | |
jony1401 | 0:ee3787c8e209 | 331 | /* |
jony1401 | 1:b512a405b584 | 332 | LP8 Modbus Communication Protocol (With no external Pressure Sensor): |
jony1401 | 0:ee3787c8e209 | 333 | -------------------------------------------------------------------------------- |
jony1401 | 0:ee3787c8e209 | 334 | Initial Measurement (first read after powerup): COMMANDS: |
jony1401 | 0:ee3787c8e209 | 335 | -------------------------------------------------------------------------------- |
jony1401 | 0:ee3787c8e209 | 336 | 1) host powers up sensor: VBB_EN = 1 |
jony1401 | 0:ee3787c8e209 | 337 | 2) host waits until rdy signal goes low: RDY = 0; |
jony1401 | 0:ee3787c8e209 | 338 | 3) host writes command, "write 24 bytes starting from adress 0x0080": { 0xfe, 0x41, 0x00, 0x80, 0x18, 0x10, ... [any 23 bytes] ..., CRC_low, CRC_high }; |
jony1401 | 0:ee3787c8e209 | 339 | 4) host reads response: { 0xfe, 0x41, 0x81, 0xe0 }; |
jony1401 | 0:ee3787c8e209 | 340 | 5) host waits until rdy signal goes high: RDY = 1; |
jony1401 | 0:ee3787c8e209 | 341 | 6) host writes command "read 44 bytes starting from adress 0x0080": { 0xfe, 0x44, 0x00, 0x80, 0x2c, CRC_low, CRC_high }; |
jony1401 | 0:ee3787c8e209 | 342 | 7) host reads response: { 0xfe, 0x44, 0x2c, 0x00, SS1, SS2, ..., SS23, PP_H, PP_L, D1, D2, ..., D18, CRC_low, CRC_high }; |
jony1401 | 0:ee3787c8e209 | 343 | 8) host powers down sensor: VBB_EN = 0; |
jony1401 | 0:ee3787c8e209 | 344 | |
jony1401 | 0:ee3787c8e209 | 345 | [------------------------------------------------------------------------------] |
jony1401 | 0:ee3787c8e209 | 346 | Optional first reading W/O pressure sensor |
jony1401 | 0:ee3787c8e209 | 347 | [------------------------------------------------------------------------------] |
jony1401 | 0:ee3787c8e209 | 348 | 1) host powers up sensor: VBB_EN = 1 |
jony1401 | 0:ee3787c8e209 | 349 | 2) host waits until rdy signal goes low: RDY = 0; |
jony1401 | 0:ee3787c8e209 | 350 | 3) host writes command, "write 24 bytes starting from adress 0x0080": { 0xfe, 0x41, 0x00, 0x80, 0x18, 0x10, 0x28, 0x7e }; |
jony1401 | 0:ee3787c8e209 | 351 | 4) host reads response: { 0xfe, 0x41, 0x81, 0xe0 }; |
jony1401 | 0:ee3787c8e209 | 352 | 5) host waits until rdy signal goes high: RDY = 1; |
jony1401 | 0:ee3787c8e209 | 353 | 6) host writes command "read 44 bytes starting from adress 0x0080": { 0xfe, 0x44, 0x00, 0x80, 0x2c, CRC_low, CRC_high }; |
jony1401 | 0:ee3787c8e209 | 354 | 7) host reads response: { 0xfe, 0x44, 0x2c, 0x00, SS1, SS2, ..., SS23, PP_H, PP_L, D1, D2, ..., D18, CRC_low, CRC_high }; |
jony1401 | 0:ee3787c8e209 | 355 | 8) host powers down sensor: VBB_EN = 0; |
jony1401 | 0:ee3787c8e209 | 356 | |
jony1401 | 0:ee3787c8e209 | 357 | |
jony1401 | 0:ee3787c8e209 | 358 | -------------------------------------------------------------------------------- |
jony1401 | 0:ee3787c8e209 | 359 | Subsequent readings: |
jony1401 | 0:ee3787c8e209 | 360 | -------------------------------------------------------------------------------- |
jony1401 | 0:ee3787c8e209 | 361 | 1) host powers up sensor: VBB_EN = 1 |
jony1401 | 0:ee3787c8e209 | 362 | 2) host waits until rdy signal goes low: RDY = 0; |
jony1401 | 0:ee3787c8e209 | 363 | 3) host writes command, "write 24 bytes starting from adress 0x0080": { 0xfe, 0x41, 0x00, 0x80, 0x18, CC, SS1, ..., SS23, CRC_low, CRC_high }; |
jony1401 | 0:ee3787c8e209 | 364 | 4) host reads response: { 0xfe, 0x41, 0x81, 0xe0 }; |
jony1401 | 0:ee3787c8e209 | 365 | 5) host waits until rdy signal goes high: RDY = 1; |
jony1401 | 0:ee3787c8e209 | 366 | 6) host writes command "read 44 bytes starting from adress 0x0080": { 0xfe, 0x44, 0x00, 0x80, 0x2c, CRC_low, CRC_high }; |
jony1401 | 0:ee3787c8e209 | 367 | 7) host reads response: { 0xfe, 0x44, 0x2c, 0x00, SS1, SS2, ..., SS23, PP_H, PP_L, D1, D2, ..., D18, CRC_low, CRC_high }; |
jony1401 | 0:ee3787c8e209 | 368 | 8) host powers down sensor: VBB_EN = 0; |
jony1401 | 0:ee3787c8e209 | 369 | |
jony1401 | 0:ee3787c8e209 | 370 | |
jony1401 | 0:ee3787c8e209 | 371 | -------------------------------------------------------------------------------- |
jony1401 | 1:b512a405b584 | 372 | LP8 Background Calibration, Calculation Control Byte |
jony1401 | 1:b512a405b584 | 373 | -------------------------------------------------------------------------------- |
jony1401 | 1:b512a405b584 | 374 | To calibrate the lp8, commands needs to be sent with the calculation control byte |
jony1401 | 1:b512a405b584 | 375 | Calculation: |
jony1401 | 1:b512a405b584 | 376 | CC = |
jony1401 | 1:b512a405b584 | 377 | 0x10 Initial measurement (filters reset, ABC sample reset and other, |
jony1401 | 1:b512a405b584 | 378 | 0x20 Subsequent measurement, |
jony1401 | 1:b512a405b584 | 379 | 0x40 Zero calibration using unfiltered data, |
jony1401 | 1:b512a405b584 | 380 | 0x41 Zero calibration using filtered data, |
jony1401 | 1:b512a405b584 | 381 | 0x42 Zero calibration using unfiltered data + reset filters, |
jony1401 | 1:b512a405b584 | 382 | 0x43 Zero calibration using filtered data + reset filters, |
jony1401 | 1:b512a405b584 | 383 | 0x50 Background calibration using unfiltered data, |
jony1401 | 1:b512a405b584 | 384 | 0x51 Background calibration using filtered data, |
jony1401 | 1:b512a405b584 | 385 | 0x52 Background calibration using unfiltered data + reset filters, |
jony1401 | 1:b512a405b584 | 386 | 0x53 Background calibration using filtered data + reset filters, |
jony1401 | 1:b512a405b584 | 387 | 0x70 ABC (based on filtered data), |
jony1401 | 1:b512a405b584 | 388 | 0x72 ABC (based on filtered data) + reset filters |
jony1401 | 1:b512a405b584 | 389 | |
jony1401 | 1:b512a405b584 | 390 | |
jony1401 | 1:b512a405b584 | 391 | -------------------------------------------------------------------------------- |
jony1401 | 0:ee3787c8e209 | 392 | -------------------------------------------------------------------------------- |
jony1401 | 0:ee3787c8e209 | 393 | CC = Calculation Control, 1 byte |
jony1401 | 0:ee3787c8e209 | 394 | SS = Sensor State, Sensor State, 23 bytes |
jony1401 | 0:ee3787c8e209 | 395 | D = Measured data and Sensor Status, 23 bytes |
jony1401 | 0:ee3787c8e209 | 396 | PP = Host Pressure Value |
jony1401 | 0:ee3787c8e209 | 397 | CRC= 16 bit CRC error check |
jony1401 | 0:ee3787c8e209 | 398 | -------------------------------------------------------------------------------- |
jony1401 | 0:ee3787c8e209 | 399 | -------------------------------------------------------------------------------- |
jony1401 | 0:ee3787c8e209 | 400 | */ |
jony1401 | 0:ee3787c8e209 | 401 |