Microchip MCP342x ADC library

mcp342x.cpp

Committer:
antoniogonzalez
Date:
2016-06-15
Revision:
0:7dbf7356da6b
Child:
1:c4da9889ff85

File content as of revision 0:7dbf7356da6b:

#include "mcp342x.h"

#define LEN_ONE_BYTE 1
#define REQUEST_N_BYTES 4

MCP342x::MCP342x(I2C *i2c, uint8_t device_address)
{
    // 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.
    set_channel(CHANNEL_1);
    set_resolution(RESOLUTION_12);
    set_pga(PGA_1);
}

void MCP342x::set_channel(mcp342x_channel_t channel)
{
    _configuration[0] &= REG_CHANNEL_Clear;
    _configuration[0] |= 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;
    _write_configuration();
}

void MCP342x::set_resolution(mcp342x_resolution_t resolution)
{
    switch(resolution) {
        case RESOLUTION_12:
            _resolution = 12;
            break;
        case RESOLUTION_14:
            _resolution = 14;
            break;
        case RESOLUTION_16:
            _resolution = 16;
            break;
        case RESOLUTION_18:
            _resolution = 18;
            break;
        }
    _configuration[0] &= REG_RESOLUTION_Clear;
    _configuration[0] |= resolution << REG_RESOLUTION_Pos;
    _write_configuration();
}

void MCP342x::set_pga(mcp342x_pga_t pga)
{
    switch(pga) {
        case PGA_1:
            _pga = 1;
            break;
        case PGA_2:
            _pga = 2;
            break;
        case PGA_4:
            _pga = 4;
            break;
        case PGA_8:
            _pga = 8;
            break;
        }
    _configuration[0] &= REG_PGA_Clear;
    _configuration[0] |= pga << REG_PGA_Pos;
    _write_configuration();
}

void MCP342x::_write_configuration()
{
    _i2c->write(_address, _configuration, 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()
{
    char raw_data[REQUEST_N_BYTES];
    long adc_value = 0;

    _i2c->read(_address, raw_data, REQUEST_N_BYTES);

    switch (_resolution) {
        case 12:
            adc_value = (raw_data[1] << 8) | raw_data[0];
            adc_value &= 0xfff;
            break;

        case 14:
            adc_value = (raw_data[1] << 8) | raw_data[0];
            adc_value &= 0x3fff;
            break;

        case 16:
            adc_value = (raw_data[1] << 8) | raw_data[0];
            adc_value &= 0xffff;
            break;

        case 18:
            adc_value = (raw_data[2] << 16) | (raw_data[1] << 8) | raw_data[0];
            adc_value &= 0x3ffff;
            break;
    }
    return adc_value;
}