Microchip MCP342x ADC library

Files at this revision

API Documentation at this revision

Comitter:
antoniogonzalez
Date:
Wed Jun 22 12:22:40 2016 +0000
Parent:
0:7dbf7356da6b
Child:
2:639a5612903f
Commit message:
Several fixes to read() and read_volts()

Changed in this revision

mcp342x.cpp Show annotated file Show diff for this revision Revisions of this file
mcp342x.h Show annotated file Show diff for this revision Revisions of this file
--- a/mcp342x.cpp	Wed Jun 15 10:37:24 2016 +0000
+++ b/mcp342x.cpp	Wed Jun 22 12:22:40 2016 +0000
@@ -1,16 +1,20 @@
 #include "mcp342x.h"
 
 #define LEN_ONE_BYTE 1
-#define REQUEST_N_BYTES 4
 
 MCP342x::MCP342x(I2C *i2c, uint8_t device_address)
 {
+    _i2c = i2c;
+
     // The address byte is the device code (4 bits, hardcoded in
     // factory) and the device address (3 bits). These are shifted one
     // bit to the left because mbed uses 8-bit addresses.
     _address = (_device_code << 4) | (device_address << 1);
-    _i2c = i2c;
-    // Initialise to default settings.
+
+    // Initialise to default settings: channel 1, gain 1x, 12 bits.
+    // It is necessary to do this to ensure that the variables
+    // _resolution and _pga are properly set.
+    _configuration = 0x10;
     set_channel(CHANNEL_1);
     set_resolution(RESOLUTION_12);
     set_pga(PGA_1);
@@ -18,42 +22,66 @@
 
 void MCP342x::set_channel(mcp342x_channel_t channel)
 {
-    _configuration[0] &= REG_CHANNEL_Clear;
-    _configuration[0] |= channel << REG_CHANNEL_Pos;
+    _configuration &= REG_CHANNEL_Clear;
+    _configuration |= channel << REG_CHANNEL_Pos;
     _write_configuration();
 }
 
 void MCP342x::set_conversion_mode(mcp342x_conversion_mode_t mode)
 {
-    _configuration[0] &= REG_MODE_Clear;
-    _configuration[0] |= mode << REG_MODE_Pos;
+    _configuration &= REG_MODE_Clear;
+    _configuration |= mode << REG_MODE_Pos;
     _write_configuration();
 }
 
 void MCP342x::set_resolution(mcp342x_resolution_t resolution)
 {
-    switch(resolution) {
+    _resolution = resolution;
+
+    // _lsb and _max_code are variables required for converting the ADC
+    // data into volts; see Section 4.9 of the MCP342x datasheet. Their
+    // value depends on the resolution chosen, so it is useful to
+    // calculate these values here, whenever the resolution setting is
+    // changed.
+    //
+    // _lsb is the magnitude (in volts) of the last significant byte,
+    // and it is calculated as 2 * 2.048 / (2^N), where N is the
+    // resolution (datasheet Eq. 4-3).
+    //
+    // _max_code is the maximum output code, and it is equal to
+    // 2^(N-1) - 1 (datasheet Table 4-3).
+    switch(_resolution){
         case RESOLUTION_12:
-            _resolution = 12;
+            _lsb = 2 * 2.048 / 4096;
+            _max_code = 2047;
             break;
         case RESOLUTION_14:
-            _resolution = 14;
+            _lsb = 2 * 2.048 / 16384;
+            _max_code = 8191;
             break;
         case RESOLUTION_16:
-            _resolution = 16;
+            _lsb = 2 * 2.048 / 65536;
+            _max_code = 32767;
             break;
         case RESOLUTION_18:
-            _resolution = 18;
+            _lsb = 2 * 2.048 / 262144;
+            _max_code = 131071;
             break;
-        }
-    _configuration[0] &= REG_RESOLUTION_Clear;
-    _configuration[0] |= resolution << REG_RESOLUTION_Pos;
+    }
+
+    _configuration &= REG_RESOLUTION_Clear;
+    _configuration |= _resolution << REG_RESOLUTION_Pos;
     _write_configuration();
 }
 
 void MCP342x::set_pga(mcp342x_pga_t pga)
 {
-    switch(pga) {
+    // The gain value (1, 2, 4 or 8) is required for converting digital
+    // output codes to voltage. For this purpose the actual PGA value is
+    // kept in the variable _pga, instead of keeping the *position* in
+    // the register of PGA, which is what the variable type
+    // mcp342x_pga_t  represents.
+    switch (pga) {
         case PGA_1:
             _pga = 1;
             break;
@@ -66,60 +94,69 @@
         case PGA_8:
             _pga = 8;
             break;
-        }
-    _configuration[0] &= REG_PGA_Clear;
-    _configuration[0] |= pga << REG_PGA_Pos;
+    }
+
+    _configuration &= REG_PGA_Clear;
+    _configuration |= pga << REG_PGA_Pos;
     _write_configuration();
 }
 
 void MCP342x::_write_configuration()
 {
-    _i2c->write(_address, _configuration, LEN_ONE_BYTE);
+    _i2c_command[0] = _configuration;
+    _i2c->write(_address, _i2c_command, LEN_ONE_BYTE);
 }
 
-float MCP342x::read_volts(){
-    long adc_value = read();
-    float volts = 0.0;
-    float lsb = 2 * 2.048 / 2^_resolution;
-    int max_code = 2^(_resolution-1) - 1;
-
-    if (adc_value > max_code) {
-        volts = (~adc_value & max_code) + 1;
-        volts *= -1;
-    } else {
-        volts = (float)adc_value;
-    }
-
-    return volts * lsb/(float)_pga;
-}
-
-long MCP342x::read()
+uint32_t MCP342x::read()
 {
-    char raw_data[REQUEST_N_BYTES];
-    long adc_value = 0;
-
-    _i2c->read(_address, raw_data, REQUEST_N_BYTES);
+    uint32_t adc_value = 0;
+    _i2c->read(_address, _i2c_command, COMMAND_N_BYTES);
 
     switch (_resolution) {
-        case 12:
-            adc_value = (raw_data[1] << 8) | raw_data[0];
+        case RESOLUTION_12:
+            adc_value = (_i2c_command[0] << 8) | _i2c_command[1];
             adc_value &= 0xfff;
             break;
 
-        case 14:
-            adc_value = (raw_data[1] << 8) | raw_data[0];
+        case RESOLUTION_14:
+            adc_value = (_i2c_command[0] << 8) | _i2c_command[1];
             adc_value &= 0x3fff;
             break;
 
-        case 16:
-            adc_value = (raw_data[1] << 8) | raw_data[0];
+        case RESOLUTION_16:
+            adc_value = (_i2c_command[0] << 8) | _i2c_command[1];
             adc_value &= 0xffff;
             break;
 
-        case 18:
-            adc_value = (raw_data[2] << 16) | (raw_data[1] << 8) | raw_data[0];
+        case RESOLUTION_18:
+            adc_value = (_i2c_command[0] << 16) |
+                (_i2c_command[1] << 8) | _i2c_command[2];
             adc_value &= 0x3ffff;
             break;
     }
     return adc_value;
 }
+
+float MCP342x::read_volts(){
+    float volts = 0.0;
+    uint32_t adc_value = read();
+
+    // The digital output of the MCP342x is in two's complement format;
+    // see datasheet Section 4.9. This 'if... else' construction
+    // determines whether the digital code is negative or positive; if
+    // it is the former, it's two's complement is calculated.
+    if (adc_value > _max_code) {
+        // if the output code is negative...
+        volts = (~adc_value & _max_code) + 1;
+        volts *= -1;
+    } else {
+        // if the output code is positive...
+        volts = (float)adc_value;
+    }
+
+    // The actual voltage is proportional to the resolution and PGA
+    // settings. This equation corresponds to Equation 4-4 in the
+    // datasheet. The variables _lsb and _pga are calculated whenever
+    // the user changes the resolution or PGA parameters.
+    return volts * _lsb / _pga;
+}
--- a/mcp342x.h	Wed Jun 15 10:37:24 2016 +0000
+++ b/mcp342x.h	Wed Jun 22 12:22:40 2016 +0000
@@ -21,6 +21,8 @@
 
 #include "mbed.h"
 
+#define COMMAND_N_BYTES 5
+
 /**
  * @brief Microchip analog-to-digital converter MCP3422/3/4
  *
@@ -53,13 +55,13 @@
  *
  *     while(1){
  *         // Channel 2 was selected above. Read data from this channel.
- *         long chan2_val = mcp_adc.read();
+ *         float chan2_val = mcp_adc.read_volts();
  *
  *         // Select now channel 1 and read it.
  *         mcp_adc.set_channel(MCP342x::CHANNEL_1);
- *         long chan1_val = mcp_adc.read();
+ *         float chan1_val = mcp_adc.read_volts();
  *
- *         printf("CH1 %i  CH2 %i\r\n", chan1_val, chan2_val);
+ *         printf("CH1 %.3f; CH2 %.3f\r\n", chan1_val, chan2_val);
  *         wait(5);
  *     }
  * }
@@ -160,36 +162,43 @@
     void set_pga(mcp342x_pga_t pga);
 
     /**
-    * Read the ADC value. The value will be that read from the channel
-    * set previously (`set_channel`)
+    * Read the ADC value. The value will be that read from whatever
+    * channel was set previously (`set_channel`).
     *
     * @return Analog measurement in raw data (integer)
     */
-    long read();
+    uint32_t read();
 
     /**
-    * Read the ADC value in volts. The value will be that read from the
-    * channel set previously (`set_channel`).
+    * Read the ADC value in volts. The value will be that read from
+    * whatever channel was set previously (`set_channel`).
     *
     * The data are coded in two's complements format, and the final
     * voltage is also a function of the resolution and gain settings.
-    * This function follows the equations presented in the device's
-    * datasheet (Microchip DS22088C), Section 4.9.
+    * This function follows the equations presented in Section 4.9 of
+    * the device's datasheet (Microchip DS22088C).
     *
     * @return Analog measurement in volts (float)
     */
     float read_volts();
 
+    //void start_conversion(void); For one-shot mode.
+
 private:
-    uint8_t _address;
+    // Variables and functions for I2C communication.
     I2C *_i2c;
-    uint8_t _resolution;
-    uint8_t _pga;
-    char _configuration[1];
+    uint8_t _address;
+    char _i2c_command[COMMAND_N_BYTES];
+    uint8_t _configuration;
     static const uint8_t _device_code = 0b1101; // Hardcoded in factory
     void _write_configuration();
+
+    // Variables needed for converting digital output codes to real
+    // values (i.e. voltage).
+    mcp342x_resolution_t _resolution;
+    uint8_t _pga;
+    float _lsb;
+    uint32_t _max_code;
 };
 
-//void mcp342x_start_conversion(void); For one-shot mode.
-
 #endif