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