Driver for the Microchip MCP432x series of ADCs.

Dependents:   AKDP-RevD7_014

Fork of MCP342x by Osamu Koizumi

Committer:
tkstreet
Date:
Fri Aug 10 17:32:01 2018 +0000
Revision:
10:9e1bb1e16e68
Parent:
9:9e70e215d39a
Removed calls to Debug library.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
coisme 1:bc877c37027c 1 #include "mcp342x.h"
coisme 1:bc877c37027c 2
coisme 1:bc877c37027c 3 #define LEN_DATA_REGISTER 3 // Length of the data registers.
coisme 1:bc877c37027c 4 #define LEN_ONE_BYTE 1
coisme 1:bc877c37027c 5
coisme 1:bc877c37027c 6 MCP342X::MCP342X(I2C *conn, SlaveAddress addr) {
coisme 1:bc877c37027c 7 slaveAddress = addr;
coisme 1:bc877c37027c 8 connection = conn;
coisme 1:bc877c37027c 9
coisme 1:bc877c37027c 10 init();
coisme 1:bc877c37027c 11 }
coisme 1:bc877c37027c 12
masahikofukasawa 4:6215f50b3297 13 /**
masahikofukasawa 4:6215f50b3297 14 * Destructor.
masahikofukasawa 4:6215f50b3297 15 *
masahikofukasawa 4:6215f50b3297 16 */
masahikofukasawa 4:6215f50b3297 17 //MCP342X::~MCP342X(){
masahikofukasawa 4:6215f50b3297 18 // if (connection) delete connection;
masahikofukasawa 4:6215f50b3297 19 //}
masahikofukasawa 4:6215f50b3297 20
coisme 1:bc877c37027c 21 MCP342X::Status MCP342X::init() {
coisme 1:bc877c37027c 22 Status status;
masahikofukasawa 5:0ecc54b7fb2e 23
masahikofukasawa 5:0ecc54b7fb2e 24 // general call reset (software POR)
masahikofukasawa 6:ed68eca49e70 25 // char general_reset = 0x06;
masahikofukasawa 6:ed68eca49e70 26 // connection->write(0, &general_reset, LEN_ONE_BYTE);
masahikofukasawa 5:0ecc54b7fb2e 27
coisme 1:bc877c37027c 28 int16_t val = 0;
coisme 1:bc877c37027c 29 // Reads the current configuration.
coisme 1:bc877c37027c 30 if ((status=readData(&val, &currentConfig)) != SUCCESS) {
coisme 1:bc877c37027c 31 return status;
coisme 1:bc877c37027c 32 }
coisme 1:bc877c37027c 33
coisme 1:bc877c37027c 34 // Initialize internal variable
coisme 1:bc877c37027c 35 currentConfig.measurementTrigger = NONE;
coisme 1:bc877c37027c 36
coisme 1:bc877c37027c 37 return status;
coisme 1:bc877c37027c 38 }
coisme 1:bc877c37027c 39
coisme 1:bc877c37027c 40 MCP342X::Status MCP342X::readData(int16_t *val, Config *config) {
tkstreet 10:9e1bb1e16e68 41
coisme 1:bc877c37027c 42 char buf[LEN_DATA_REGISTER];
coisme 1:bc877c37027c 43
coisme 1:bc877c37027c 44 // Reads data registers.
coisme 1:bc877c37027c 45 if (connection->read((slaveAddress << 1), buf, LEN_DATA_REGISTER) != 0) {
coisme 1:bc877c37027c 46 return ERROR_I2C_READ;
coisme 1:bc877c37027c 47 }
coisme 1:bc877c37027c 48
coisme 1:bc877c37027c 49 // Decodes configuration register data.
coisme 1:bc877c37027c 50 decodeConfigurationRegister(config, buf[2]);
coisme 1:bc877c37027c 51
coisme 1:bc877c37027c 52 // Converts AD value
coisme 1:bc877c37027c 53 *val = (int16_t)((buf[0] << 8) | buf[1]);
coisme 1:bc877c37027c 54
coisme 1:bc877c37027c 55 return SUCCESS;
coisme 1:bc877c37027c 56 }
coisme 1:bc877c37027c 57
coisme 1:bc877c37027c 58 void MCP342X::decodeConfigurationRegister(Config *config, uint8_t regVal) {
coisme 1:bc877c37027c 59 uint8_t tmp = 0;
coisme 1:bc877c37027c 60
coisme 1:bc877c37027c 61 // For the meaning of each bit, see the section 5.2 in the datasheet.
coisme 1:bc877c37027c 62
coisme 1:bc877c37027c 63 // Decodes Ready Bit (~RDY), Bit 7
coisme 1:bc877c37027c 64 tmp = ((0x80 & regVal) >> 7);
coisme 1:bc877c37027c 65 if (tmp == 0x00) {
coisme 1:bc877c37027c 66 config->dataStatus = DATA_UPDATED;
coisme 1:bc877c37027c 67 } else {
coisme 1:bc877c37027c 68 config->dataStatus = DATA_NOT_UPDATED;
coisme 1:bc877c37027c 69 }
coisme 1:bc877c37027c 70
coisme 1:bc877c37027c 71 // Decodes Channel Selection Bits, Bit 6-5
coisme 1:bc877c37027c 72 tmp = ((0x60 & regVal) >> 5);
coisme 1:bc877c37027c 73 if (tmp == 0x00) {
coisme 1:bc877c37027c 74 config->adcChannel = ADC_CH1;
coisme 1:bc877c37027c 75 } else if (tmp == 0x01) {
coisme 1:bc877c37027c 76 config->adcChannel = ADC_CH2;
coisme 1:bc877c37027c 77 } else if (tmp == 0x02) {
coisme 1:bc877c37027c 78 config->adcChannel = ADC_CH3;
coisme 1:bc877c37027c 79 } else { // ch == 0x03
coisme 1:bc877c37027c 80 config->adcChannel = ADC_CH4;
coisme 1:bc877c37027c 81 }
coisme 1:bc877c37027c 82
coisme 1:bc877c37027c 83 // Decodes Conversion Mode Bit, Bit 4
coisme 1:bc877c37027c 84 tmp = ((0x10 & regVal) >> 4);
coisme 1:bc877c37027c 85 if (tmp == 0x01) {
coisme 1:bc877c37027c 86 config->conversionMode = CONTINUOUS;
coisme 1:bc877c37027c 87 } else {
coisme 1:bc877c37027c 88 config->conversionMode = ONE_SHOT;
coisme 1:bc877c37027c 89 }
coisme 1:bc877c37027c 90
coisme 1:bc877c37027c 91 // Decodes Sample Rate Selection Bit
coisme 1:bc877c37027c 92 tmp = ((0x0C & regVal) >> 2);
coisme 1:bc877c37027c 93 if (tmp == 0x00) {
coisme 1:bc877c37027c 94 config->sampleSetting = SAMPLE_240HZ_12BIT;
coisme 1:bc877c37027c 95 } else if (tmp == 0x01) {
coisme 1:bc877c37027c 96 config->sampleSetting = SAMPLE_60HZ_14BIT;
coisme 1:bc877c37027c 97 } else {
coisme 1:bc877c37027c 98 config->sampleSetting = SAMPLE_15HZ_16BIT;
coisme 1:bc877c37027c 99 }
coisme 1:bc877c37027c 100
coisme 1:bc877c37027c 101 // Decodes PGA Gain Selection Bits
coisme 1:bc877c37027c 102 tmp = (0x03 & regVal);
coisme 1:bc877c37027c 103 if (tmp == 0x00) {
coisme 1:bc877c37027c 104 config->pgaSetting = PGA_1X;
coisme 1:bc877c37027c 105 } else if (tmp == 0x01) {
coisme 1:bc877c37027c 106 config->pgaSetting = PGA_2X;
coisme 1:bc877c37027c 107 } else if (tmp == 0x02) {
coisme 1:bc877c37027c 108 config->pgaSetting = PGA_4X;
coisme 1:bc877c37027c 109 } else {
coisme 1:bc877c37027c 110 config->pgaSetting = PGA_8X;
coisme 1:bc877c37027c 111 }
coisme 1:bc877c37027c 112 }
coisme 1:bc877c37027c 113
coisme 1:bc877c37027c 114 MCP342X::Status MCP342X::setConfig(const Config *config) {
coisme 1:bc877c37027c 115 char val = 0;
coisme 1:bc877c37027c 116
coisme 1:bc877c37027c 117 // Measurement trigger
coisme 1:bc877c37027c 118 if (config->measurementTrigger == TRIGGER) {
coisme 1:bc877c37027c 119 val |= 0x80;
coisme 1:bc877c37027c 120 } else {
coisme 1:bc877c37027c 121 val |= 0x00;
coisme 1:bc877c37027c 122 }
coisme 1:bc877c37027c 123
coisme 1:bc877c37027c 124 // Channel Selection
coisme 1:bc877c37027c 125 if (config->adcChannel == ADC_CH1) {
coisme 1:bc877c37027c 126 val |= 0x00;
coisme 1:bc877c37027c 127 } else if (config->adcChannel == ADC_CH2) {
coisme 1:bc877c37027c 128 val |= 0x20;
coisme 1:bc877c37027c 129 } else if (config->adcChannel == ADC_CH3) {
coisme 1:bc877c37027c 130 val |= 0x40;
coisme 1:bc877c37027c 131 } else { // config->adcChannel == ADC_CH4
coisme 1:bc877c37027c 132 val |= 0x60;
coisme 1:bc877c37027c 133 }
coisme 1:bc877c37027c 134
coisme 1:bc877c37027c 135 // Conversion Mode
coisme 1:bc877c37027c 136 if (config->conversionMode == CONTINUOUS) {
coisme 1:bc877c37027c 137 val |= 0x10;
coisme 1:bc877c37027c 138 } else if (config->conversionMode == ONE_SHOT) {
coisme 1:bc877c37027c 139 val |= 0x00;
coisme 1:bc877c37027c 140 }
coisme 1:bc877c37027c 141
coisme 1:bc877c37027c 142 // Sample Rate
coisme 1:bc877c37027c 143 if (config->sampleSetting == SAMPLE_240HZ_12BIT) {
coisme 1:bc877c37027c 144 val |= 0x00;
coisme 1:bc877c37027c 145 } else if (config->sampleSetting == SAMPLE_60HZ_14BIT) {
coisme 1:bc877c37027c 146 val |= 0x04;
coisme 1:bc877c37027c 147 } else { //config->sampleSetting == SAMPLE_15HZ_16BIT
coisme 1:bc877c37027c 148 val |= 0x08;
coisme 1:bc877c37027c 149 }
coisme 1:bc877c37027c 150
coisme 1:bc877c37027c 151 // PGA Gain Selection
coisme 1:bc877c37027c 152 if (config->pgaSetting == PGA_1X) {
coisme 1:bc877c37027c 153 val |= 0x00;
coisme 1:bc877c37027c 154 } else if (config->pgaSetting == PGA_2X) {
coisme 1:bc877c37027c 155 val |= 0x01;
coisme 1:bc877c37027c 156 } else if (config->pgaSetting == PGA_4X) {
coisme 1:bc877c37027c 157 val |= 0x02;
coisme 1:bc877c37027c 158 } else { // config->pgaSetting == PGA_8X) {
coisme 1:bc877c37027c 159 val |= 0x03;
coisme 1:bc877c37027c 160 }
coisme 1:bc877c37027c 161
coisme 1:bc877c37027c 162 // Write to the device.
coisme 1:bc877c37027c 163 if (connection->write((slaveAddress << 1), &val, LEN_ONE_BYTE) != 0) {
coisme 1:bc877c37027c 164 return ERROR_I2C_WRITE;
coisme 1:bc877c37027c 165 }
coisme 1:bc877c37027c 166
coisme 1:bc877c37027c 167 return SUCCESS;
coisme 1:bc877c37027c 168 }
coisme 1:bc877c37027c 169
coisme 1:bc877c37027c 170
coisme 1:bc877c37027c 171 MCP342X::Status MCP342X::setChannel(AdcChannel ch) {
coisme 1:bc877c37027c 172 currentConfig.adcChannel = ch;
coisme 1:bc877c37027c 173 return setConfig(&currentConfig);
coisme 1:bc877c37027c 174 }
coisme 1:bc877c37027c 175
coisme 1:bc877c37027c 176 MCP342X::AdcChannel MCP342X::getChannel() {
coisme 1:bc877c37027c 177 return currentConfig.adcChannel;
coisme 1:bc877c37027c 178 }
coisme 1:bc877c37027c 179
coisme 1:bc877c37027c 180 MCP342X::Status MCP342X::setConversionMode(ConversionMode mode) {
coisme 1:bc877c37027c 181 currentConfig.conversionMode = mode;
coisme 1:bc877c37027c 182 return setConfig(&currentConfig);
coisme 1:bc877c37027c 183 }
coisme 1:bc877c37027c 184
coisme 1:bc877c37027c 185 MCP342X::ConversionMode MCP342X::getConversionMode() {
coisme 1:bc877c37027c 186 return currentConfig.conversionMode;
coisme 1:bc877c37027c 187 }
coisme 1:bc877c37027c 188
coisme 1:bc877c37027c 189 MCP342X::Status MCP342X::setSampleSetting(SampleSetting s) {
coisme 1:bc877c37027c 190 currentConfig.sampleSetting = s;
coisme 1:bc877c37027c 191 return setConfig(&currentConfig);
coisme 1:bc877c37027c 192 }
coisme 1:bc877c37027c 193
coisme 1:bc877c37027c 194 MCP342X::SampleSetting MCP342X::getSampleSetting() {
coisme 1:bc877c37027c 195 return currentConfig.sampleSetting;
coisme 1:bc877c37027c 196 }
coisme 1:bc877c37027c 197
coisme 1:bc877c37027c 198 MCP342X::Status MCP342X::setPgaSetting(PgaSetting s) {
coisme 1:bc877c37027c 199 currentConfig.pgaSetting = s;
coisme 1:bc877c37027c 200 return setConfig(&currentConfig);
coisme 1:bc877c37027c 201 }
coisme 1:bc877c37027c 202
coisme 1:bc877c37027c 203 MCP342X::PgaSetting MCP342X::getPgaSetting() {
coisme 1:bc877c37027c 204 return currentConfig.pgaSetting;
coisme 1:bc877c37027c 205 }
coisme 1:bc877c37027c 206
coisme 1:bc877c37027c 207 MCP342X::Status MCP342X::getData(Data *pt) {
coisme 1:bc877c37027c 208 Status status;
coisme 1:bc877c37027c 209
coisme 1:bc877c37027c 210 int16_t val = 0;
coisme 1:bc877c37027c 211 if ((status=readData(&val, &currentConfig)) != SUCCESS) {
coisme 1:bc877c37027c 212 return status;
coisme 1:bc877c37027c 213 }
coisme 1:bc877c37027c 214
coisme 1:bc877c37027c 215 pt->st = currentConfig.dataStatus;
coisme 1:bc877c37027c 216 pt->value = val;
coisme 1:bc877c37027c 217
coisme 1:bc877c37027c 218 return status;
coisme 1:bc877c37027c 219 }
coisme 1:bc877c37027c 220
coisme 1:bc877c37027c 221 MCP342X::Status MCP342X::trigger(){
coisme 1:bc877c37027c 222 Status status;
coisme 1:bc877c37027c 223
coisme 1:bc877c37027c 224 currentConfig.measurementTrigger = TRIGGER;
coisme 1:bc877c37027c 225 status = setConfig(&currentConfig);
coisme 1:bc877c37027c 226 currentConfig.measurementTrigger = NONE;
coisme 1:bc877c37027c 227
coisme 1:bc877c37027c 228 return status;
coisme 2:96d9bfe25b03 229 }
coisme 2:96d9bfe25b03 230