Osamu Koizumi / MCP342x
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers mcp342x.cpp Source File

mcp342x.cpp

00001 #include "mcp342x.h"
00002 
00003 
00004 #define LEN_DATA_REGISTER  3          // Length of the data registers.
00005 #define LEN_ONE_BYTE       1
00006 
00007 MCP342X::MCP342X(I2C *conn, SlaveAddress addr) {
00008     slaveAddress = addr;
00009     connection = conn;
00010     
00011     init();
00012 }
00013 
00014 MCP342X::Status MCP342X::init() {
00015     Status status;
00016     int16_t val = 0;
00017     
00018     // Reads the current configuration.
00019     if ((status=readData(&val, &currentConfig)) != SUCCESS) {
00020         return status;
00021     }
00022 
00023     // Initialize internal variable    
00024     currentConfig.measurementTrigger = NONE;
00025     
00026     return status;
00027 }
00028 
00029 MCP342X::Status MCP342X::readData(int16_t *val, Config *config) {
00030     char buf[LEN_DATA_REGISTER];
00031     
00032     // Reads data registers.
00033     if (connection->read((slaveAddress << 1), buf, LEN_DATA_REGISTER) != 0) {
00034         return ERROR_I2C_READ;
00035     }
00036     
00037     // Decodes configuration register data.
00038     decodeConfigurationRegister(config, buf[2]);   
00039 
00040     // Converts AD value
00041     *val = (int16_t)((buf[0] << 8) | buf[1]);
00042     
00043     return SUCCESS;
00044 }
00045 
00046 void MCP342X::decodeConfigurationRegister(Config *config, uint8_t regVal) {
00047     uint8_t tmp = 0;
00048 
00049     // For the meaning of each bit, see the section 5.2 in the datasheet.
00050     
00051     // Decodes Ready Bit (~RDY), Bit 7
00052     tmp = ((0x80 & regVal) >> 7);
00053     if (tmp == 0x00) {
00054         config->dataStatus = DATA_UPDATED;
00055     } else {
00056         config->dataStatus = DATA_NOT_UPDATED;
00057     }
00058     
00059     // Decodes Channel Selection Bits, Bit 6-5
00060     tmp = ((0x60 & regVal) >> 5);
00061     if (tmp == 0x00) {
00062         config->adcChannel = ADC_CH1;
00063     } else if (tmp == 0x01) {
00064         config->adcChannel = ADC_CH2;
00065     } else if (tmp == 0x02) {
00066         config->adcChannel = ADC_CH3;
00067     } else {  //  ch == 0x03
00068         config->adcChannel = ADC_CH4;
00069     }
00070 
00071     // Decodes Conversion Mode Bit, Bit 4
00072     tmp = ((0x10 & regVal) >> 4);
00073     if (tmp == 0x01) {
00074         config->conversionMode = CONTINUOUS;
00075     } else {
00076         config->conversionMode = ONE_SHOT;
00077     }
00078 
00079     // Decodes Sample Rate Selection Bit
00080     tmp = ((0x0C & regVal) >> 2);
00081     if (tmp == 0x00) {
00082         config->sampleSetting = SAMPLE_240HZ_12BIT;
00083     } else if (tmp == 0x01) {
00084         config->sampleSetting = SAMPLE_60HZ_14BIT;
00085     } else {
00086         config->sampleSetting = SAMPLE_15HZ_16BIT;
00087     }
00088     
00089     // Decodes PGA Gain Selection Bits
00090     tmp = (0x03 & regVal);
00091     if (tmp == 0x00) {
00092         config->pgaSetting = PGA_1X;
00093     } else if (tmp == 0x01) {
00094         config->pgaSetting = PGA_2X;
00095     } else if (tmp == 0x02) {
00096         config->pgaSetting = PGA_4X;
00097     } else {
00098         config->pgaSetting = PGA_8X;
00099     }
00100 }
00101 
00102 MCP342X::Status MCP342X::setConfig(const Config *config) {
00103     char val = 0;
00104     
00105     // Measurement trigger
00106     if (config->measurementTrigger == TRIGGER) {
00107         val |= 0x80;
00108     } else {
00109         val |= 0x00;
00110     }
00111 
00112     // Channel Selection
00113     if (config->adcChannel == ADC_CH1) {
00114         val |= 0x00;
00115     } else if (config->adcChannel == ADC_CH2) {
00116         val |= 0x20;
00117     } else if (config->adcChannel == ADC_CH3) {
00118         val |= 0x40;
00119     } else {  // config->adcChannel == ADC_CH4
00120         val |= 0x60;
00121     }
00122     
00123     // Conversion Mode
00124     if (config->conversionMode == CONTINUOUS) {
00125         val |= 0x10;
00126     } else if (config->conversionMode == ONE_SHOT) {
00127         val |= 0x00;
00128     }
00129     
00130     // Sample Rate
00131     if (config->sampleSetting == SAMPLE_240HZ_12BIT) {
00132         val |= 0x00;
00133     } else if (config->sampleSetting == SAMPLE_60HZ_14BIT) {
00134         val |= 0x04;
00135     } else { //config->sampleSetting == SAMPLE_15HZ_16BIT
00136         val |= 0x08;
00137     }
00138     
00139     // PGA Gain Selection
00140     if (config->pgaSetting == PGA_1X) {
00141         val |= 0x00;
00142     } else if (config->pgaSetting == PGA_2X) {
00143         val |= 0x01;
00144     } else if (config->pgaSetting == PGA_4X) {
00145         val |= 0x02;
00146     } else { // config->pgaSetting == PGA_8X) {
00147         val |= 0x03;
00148     }
00149     
00150     // Write to the device.
00151     if (connection->write((slaveAddress << 1), &val, LEN_ONE_BYTE) != 0) {
00152         return ERROR_I2C_WRITE;
00153     }
00154     
00155     return SUCCESS;
00156 }
00157 
00158 
00159 MCP342X::Status MCP342X::setChannel(AdcChannel ch) {
00160     currentConfig.adcChannel = ch;
00161     return setConfig(&currentConfig);
00162 }
00163 
00164 MCP342X::AdcChannel MCP342X::getChannel() {
00165     return currentConfig.adcChannel;
00166 }
00167 
00168 MCP342X::Status MCP342X::setConversionMode(ConversionMode mode) {
00169     currentConfig.conversionMode = mode;
00170     return setConfig(&currentConfig);
00171 }
00172 
00173 MCP342X::ConversionMode MCP342X::getConversionMode() {
00174     return currentConfig.conversionMode;
00175 }
00176 
00177 MCP342X::Status MCP342X::setSampleSetting(SampleSetting s) {
00178     currentConfig.sampleSetting = s;
00179     return setConfig(&currentConfig);
00180 }
00181 
00182 MCP342X::SampleSetting MCP342X::getSampleSetting() {
00183     return currentConfig.sampleSetting;
00184 }
00185 
00186 MCP342X::Status MCP342X::setPgaSetting(PgaSetting s) {
00187     currentConfig.pgaSetting = s;
00188     return setConfig(&currentConfig);
00189 }
00190 
00191 MCP342X::PgaSetting MCP342X::getPgaSetting() {
00192     return currentConfig.pgaSetting;
00193 }
00194 
00195 MCP342X::Status MCP342X::getData(Data *pt) {
00196     Status status;
00197     
00198     int16_t val = 0;
00199     if ((status=readData(&val, &currentConfig)) != SUCCESS) {
00200         return status;
00201     }
00202     
00203     pt->st = currentConfig.dataStatus;
00204     pt->value = val;
00205     
00206     return status;
00207 }
00208 
00209 MCP342X::Status MCP342X::trigger(){
00210     Status status;
00211     
00212     currentConfig.measurementTrigger = TRIGGER;
00213     status = setConfig(&currentConfig);
00214     currentConfig.measurementTrigger = NONE;
00215     
00216     return status;
00217 }
00218