A compilation of some hardware sensors and their shared programming interfaces.
MAX17043.cpp@0:8d34cc2ff388, 2014-03-16 (annotated)
- Committer:
- mgottscho
- Date:
- Sun Mar 16 01:48:59 2014 +0000
- Revision:
- 0:8d34cc2ff388
A compilation of various hardware sensors and some shared programming interfaces.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mgottscho | 0:8d34cc2ff388 | 1 | /* MAX17043.cpp |
mgottscho | 0:8d34cc2ff388 | 2 | * Tested with mbed board: FRDM-KL46Z |
mgottscho | 0:8d34cc2ff388 | 3 | * Author: Mark Gottscho |
mgottscho | 0:8d34cc2ff388 | 4 | * mgottscho@ucla.edu |
mgottscho | 0:8d34cc2ff388 | 5 | */ |
mgottscho | 0:8d34cc2ff388 | 6 | |
mgottscho | 0:8d34cc2ff388 | 7 | |
mgottscho | 0:8d34cc2ff388 | 8 | #include "mbed.h" |
mgottscho | 0:8d34cc2ff388 | 9 | #include "I2CSensor.h" |
mgottscho | 0:8d34cc2ff388 | 10 | #include "PeriodicSensor.h" |
mgottscho | 0:8d34cc2ff388 | 11 | #include "MAX17043.h" |
mgottscho | 0:8d34cc2ff388 | 12 | |
mgottscho | 0:8d34cc2ff388 | 13 | |
mgottscho | 0:8d34cc2ff388 | 14 | MAX17043::MAX17043(PinName sda, PinName scl, int i2c_addr) : |
mgottscho | 0:8d34cc2ff388 | 15 | I2CSensor(sda, scl, i2c_addr), |
mgottscho | 0:8d34cc2ff388 | 16 | PeriodicSensor(0.5), //Default max sampling rate of 2 Hz, because the device internally samples every 500ms after initial POS report |
mgottscho | 0:8d34cc2ff388 | 17 | __soc(0), |
mgottscho | 0:8d34cc2ff388 | 18 | __vcell(0) |
mgottscho | 0:8d34cc2ff388 | 19 | { |
mgottscho | 0:8d34cc2ff388 | 20 | } |
mgottscho | 0:8d34cc2ff388 | 21 | |
mgottscho | 0:8d34cc2ff388 | 22 | MAX17043::~MAX17043() { } |
mgottscho | 0:8d34cc2ff388 | 23 | |
mgottscho | 0:8d34cc2ff388 | 24 | void MAX17043::selfInit() { |
mgottscho | 0:8d34cc2ff388 | 25 | __i2c.frequency(400000); |
mgottscho | 0:8d34cc2ff388 | 26 | reset(); |
mgottscho | 0:8d34cc2ff388 | 27 | } |
mgottscho | 0:8d34cc2ff388 | 28 | |
mgottscho | 0:8d34cc2ff388 | 29 | void MAX17043::reset() { |
mgottscho | 0:8d34cc2ff388 | 30 | __disable_irq(); |
mgottscho | 0:8d34cc2ff388 | 31 | uint16_t data = RST_CODE; |
mgottscho | 0:8d34cc2ff388 | 32 | setRegister16b(COMMAND_MSB, data); |
mgottscho | 0:8d34cc2ff388 | 33 | __enable_irq(); |
mgottscho | 0:8d34cc2ff388 | 34 | wait(0.130); //wait 130ms until first readings are valid (125ms est) |
mgottscho | 0:8d34cc2ff388 | 35 | } |
mgottscho | 0:8d34cc2ff388 | 36 | |
mgottscho | 0:8d34cc2ff388 | 37 | uint16_t MAX17043::getVersion() { |
mgottscho | 0:8d34cc2ff388 | 38 | uint8_t data = getRegister(VERSION_MSB); |
mgottscho | 0:8d34cc2ff388 | 39 | return (data << 8) | (getRegister(VERSION_LSB)); |
mgottscho | 0:8d34cc2ff388 | 40 | } |
mgottscho | 0:8d34cc2ff388 | 41 | |
mgottscho | 0:8d34cc2ff388 | 42 | uint16_t MAX17043::getVCell(bool sampleNow) { |
mgottscho | 0:8d34cc2ff388 | 43 | __disable_irq(); |
mgottscho | 0:8d34cc2ff388 | 44 | |
mgottscho | 0:8d34cc2ff388 | 45 | if (sampleNow) { |
mgottscho | 0:8d34cc2ff388 | 46 | uint16_t data = getRegister16b(VCELL_MSB); |
mgottscho | 0:8d34cc2ff388 | 47 | __vcell = data >> 4; //right shift by 4 to throw out the don't care bits |
mgottscho | 0:8d34cc2ff388 | 48 | } |
mgottscho | 0:8d34cc2ff388 | 49 | __dataReady = false; |
mgottscho | 0:8d34cc2ff388 | 50 | |
mgottscho | 0:8d34cc2ff388 | 51 | __enable_irq(); |
mgottscho | 0:8d34cc2ff388 | 52 | |
mgottscho | 0:8d34cc2ff388 | 53 | return __vcell; |
mgottscho | 0:8d34cc2ff388 | 54 | } |
mgottscho | 0:8d34cc2ff388 | 55 | |
mgottscho | 0:8d34cc2ff388 | 56 | float MAX17043::getFloatVCell(bool sampleNow) { |
mgottscho | 0:8d34cc2ff388 | 57 | return getVCell(sampleNow) * DIV_VCELL; |
mgottscho | 0:8d34cc2ff388 | 58 | } |
mgottscho | 0:8d34cc2ff388 | 59 | |
mgottscho | 0:8d34cc2ff388 | 60 | uint16_t MAX17043::getSOC(bool sampleNow) { |
mgottscho | 0:8d34cc2ff388 | 61 | __disable_irq(); |
mgottscho | 0:8d34cc2ff388 | 62 | |
mgottscho | 0:8d34cc2ff388 | 63 | if (sampleNow) { |
mgottscho | 0:8d34cc2ff388 | 64 | uint8_t data = getRegister(SOC_MSB); |
mgottscho | 0:8d34cc2ff388 | 65 | __soc = (data << 8) | (getRegister(SOC_LSB)); |
mgottscho | 0:8d34cc2ff388 | 66 | } |
mgottscho | 0:8d34cc2ff388 | 67 | __dataReady = false; |
mgottscho | 0:8d34cc2ff388 | 68 | |
mgottscho | 0:8d34cc2ff388 | 69 | __enable_irq(); |
mgottscho | 0:8d34cc2ff388 | 70 | |
mgottscho | 0:8d34cc2ff388 | 71 | return __soc; |
mgottscho | 0:8d34cc2ff388 | 72 | } |
mgottscho | 0:8d34cc2ff388 | 73 | |
mgottscho | 0:8d34cc2ff388 | 74 | float MAX17043::getFloatSOC(bool sampleNow) { |
mgottscho | 0:8d34cc2ff388 | 75 | return getSOC(sampleNow) * DIV_SOC; |
mgottscho | 0:8d34cc2ff388 | 76 | } |
mgottscho | 0:8d34cc2ff388 | 77 | |
mgottscho | 0:8d34cc2ff388 | 78 | void MAX17043::__sample_data_ISR() { |
mgottscho | 0:8d34cc2ff388 | 79 | getSOC(true); |
mgottscho | 0:8d34cc2ff388 | 80 | getVCell(true); |
mgottscho | 0:8d34cc2ff388 | 81 | __dataReady = true; |
mgottscho | 0:8d34cc2ff388 | 82 | } |