Osamu Koizumi / MCP342x
Embed: (wiki syntax)

« Back to documentation index

MCP342X Class Reference

Device driver for MCP3426, MCP3427, and MCP3428. More...

#include <mcp342x.h>

Data Structures

struct  Data
 ADC result. More...

Public Types

enum  SlaveAddress {
  SLAVE_ADDRESS_68H = 0x68, SLAVE_ADDRESS_69H = 0x69, SLAVE_ADDRESS_6AH = 0x6A, SLAVE_ADDRESS_6CH = 0x6C,
  SLAVE_ADDRESS_6DH = 0x6D, SLAVE_ADDRESS_6EH = 0x6E, SLAVE_ADDRESS_6BH = 0x6B, SLAVE_ADDRESS_6FH = 0x6F
}
 

Slave addresses.

More...
enum  Status { SUCCESS, ERROR_I2C_READ, ERROR_I2C_WRITE, ERROR }
 

Status of function.

More...
enum  ConversionMode { CONTINUOUS, ONE_SHOT }
 

Conversion mode setting.

More...
enum  DataStatus { DATA_NOT_UPDATED, DATA_UPDATED }
 

Data ready status.

More...
enum  MeasurementTrigger { TRIGGER, NONE }
 

Measurement trigger command.

More...
enum  SampleSetting { SAMPLE_240HZ_12BIT, SAMPLE_60HZ_14BIT, SAMPLE_15HZ_16BIT }
 

Sample rate and resolution setting.

More...
enum  AdcChannel { ADC_CH1 = 0, ADC_CH2 = 1, ADC_CH3 = 2, ADC_CH4 = 3 }
 

ADC channel selection.

More...
enum  PgaSetting { PGA_1X, PGA_2X, PGA_4X, PGA_8X }
 

Programmable Gain Amplifier setting.

More...

Public Member Functions

 MCP342X (I2C *conn, SlaveAddress addr)
 Constructor.
Status setChannel (AdcChannel ch)
 Sets a ADC channel.
AdcChannel getChannel ()
 Gets the current selected ADC channel.
Status setConversionMode (ConversionMode mode)
 Sets a conversion mode.
ConversionMode getConversionMode ()
 Gets the current conversion mode.
Status setSampleSetting (SampleSetting s)
 Sets sample setting, i.e.
SampleSetting getSampleSetting ()
 Gets the current sample setting.
Status setPgaSetting (PgaSetting s)
 Sets the gain of Programmable Gain Amplifier (PGA).
PgaSetting getPgaSetting ()
 Gets the current Programmable Gain Amplifier (PGA) setting.
Status getData (Data *pt)
 Gets the AD value.
Status trigger ()
 Trigger AD conversion.

Detailed Description

Device driver for MCP3426, MCP3427, and MCP3428.

Note:
MCP342x is Analog-to-Digital Converter (ADC) IC with I2C interface.

Example:

 #include "mbed.h"
 #include "mcp342x.h"

 #define I2C_SPEED_100KHZ    100000
 #define I2C_SPEED_400KHZ    400000
 #define PIN_SERIAL_TX   P0_4
 #define PIN_SERIAL_RX   P0_5 

 int16_t getAdcData(MCP342X *mcp3428, MCP342X::AdcChannel ch, MCP342X::SampleSetting s) {
     // Configure channel and trigger.
     mcp3428->setChannel(ch);
     mcp3428->setSampleSetting(s);
     mcp3428->trigger();

     // polling data
     MCP342X::Data data;
     do {
         wait_ms(WAIT_ADC_MS);
         mcp3428->getData(&data);
     } while(data.st == MCP342X::DATA_NOT_UPDATED);

     return data.value;
 }

 int main(void) {
     // Instanciate I2C
     I2C i2c(I2C_SDA0, I2C_SCL0);
     i2c.frequency(I2C_SPEED_400KHZ);

     // Serial output for debug. (optional)
     Serial serial(PIN_SERIAL_TX, PIN_SERIAL_RX);

     // Instanciate MCP342x
     // Suppose that the slave address of MCP342x on your board is .
     MCP342X mcp342x(&i2c, MCP342X::SLAVE_ADDRESS_68H);

     // Sets MCP342x one-shot measurement mode.
     mcp342x.setSampleSetting(MCP342X::ONE_SHOT); 

     while(true) {
         // Supposes that the device is MCP3428, which has 4 channels.
         const uint8_t CHANNEL_NUM = 4;
         // Sampling setting. Ch1 is 12-bit, Ch2 is 14-bit, Ch3 is 16-bit, Ch4 is 16-bit.
         const MCP342X::SampleSetting sampleSetting[CHANNEL_NUM] = 
                 {MCP342X::SAMPLE_240HZ_12BIT, MCP342X::SAMPLE_60HZ_14BIT,
                  MCP342X::SAMPLE_15HZ_16BIT, MCP342X::SAMPLE_15HZ_16BIT};
         // Data buffer.
         int16_t data[CHANNEL_NUM];
         // Measures each channel.
         for (int i=0; i < CHANNEL_NUM; i++) {
             mcp342x.getAdcData(&data[i], (MCP342X::AdcChannel)i, sampleSetting[i]);
         }
         // Prints out the ADC results.
         serial.printf("%d, %d, %d, %d\r\n", data[0], data[1], data[2], data[3]);
     }
 }

Definition at line 70 of file mcp342x.h.


Member Enumeration Documentation

enum AdcChannel

ADC channel selection.

Enumerator:
ADC_CH1 

Channel 1, default.

ADC_CH2 

Channel 2.

ADC_CH3 

Channel 3, MCP3428 only, treated as channel 1 by the MCP3426/MCP3427.

ADC_CH4 

Channel 4, MCP3428 only, treated as channel 2 by the MCP3426/MCP3427.

Definition at line 134 of file mcp342x.h.

Conversion mode setting.

Enumerator:
CONTINUOUS 

Continuous conversion mode.

Default.

ONE_SHOT 

One-shot conversion mode.

Definition at line 100 of file mcp342x.h.

enum DataStatus

Data ready status.

Enumerator:
DATA_NOT_UPDATED 

Output register has not been updated.

DATA_UPDATED 

Output register has been updated with the latest conversion result.

Definition at line 108 of file mcp342x.h.

Measurement trigger command.

Enumerator:
TRIGGER 

Initiate a new conversion.

NONE 

No effect.

Definition at line 116 of file mcp342x.h.

enum PgaSetting

Programmable Gain Amplifier setting.

Enumerator:
PGA_1X 

Gain 1x, Default.

PGA_2X 

Gain 2x.

PGA_4X 

Gain 4x.

PGA_8X 

Gain 8x.

Definition at line 144 of file mcp342x.h.

Sample rate and resolution setting.

Enumerator:
SAMPLE_240HZ_12BIT 

240 sample per second with 12 bit data.

Default.

SAMPLE_60HZ_14BIT 

60 sample per second with 14 bit data.

SAMPLE_15HZ_16BIT 

15 sample per second with 16 bit data.

Definition at line 124 of file mcp342x.h.

Slave addresses.

Enumerator:
SLAVE_ADDRESS_68H 

When Adr0 pin = L and Adr1 pin = L, or Adr0 pin = float and Adr1 pin = float.

SLAVE_ADDRESS_69H 

When Adr0 pin = L and Adr1 pin = float.

SLAVE_ADDRESS_6AH 

When Adr0 pin = L and Adr1 pin = H.

SLAVE_ADDRESS_6CH 

When Adr0 pin = H and Adr1 pin = L.

SLAVE_ADDRESS_6DH 

When Adr0 pin = H and Adr1 pin = float.

SLAVE_ADDRESS_6EH 

When Adr0 pin = H and Adr1 pin = H.

SLAVE_ADDRESS_6BH 

When Adr0 pin = float and Adr1 pin = L.

SLAVE_ADDRESS_6FH 

When Adr0 pin = float and Adr1 pin = H.

Definition at line 76 of file mcp342x.h.

enum Status

Status of function.

Enumerator:
SUCCESS 

The function processed successfully.

ERROR_I2C_READ 

Error related to I2C read.

ERROR_I2C_WRITE 

Error related to I2C write.

ERROR 

General Error.

Definition at line 90 of file mcp342x.h.


Constructor & Destructor Documentation

MCP342X ( I2C *  conn,
SlaveAddress  addr 
)

Constructor.

Parameters:
connPointer to an instance of I2C.
addrSlave address of the device.

Definition at line 7 of file mcp342x.cpp.


Member Function Documentation

MCP342X::AdcChannel getChannel (  )

Gets the current selected ADC channel.

Returns:
ADC channel currently set.

Definition at line 164 of file mcp342x.cpp.

MCP342X::ConversionMode getConversionMode (  )

Gets the current conversion mode.

Returns:
Current conversion mode.

Definition at line 173 of file mcp342x.cpp.

MCP342X::Status getData ( Data pt )

Gets the AD value.

Returns:
AD value.

Definition at line 195 of file mcp342x.cpp.

MCP342X::PgaSetting getPgaSetting (  )

Gets the current Programmable Gain Amplifier (PGA) setting.

Returns:
Current PGA setting.

Definition at line 191 of file mcp342x.cpp.

MCP342X::SampleSetting getSampleSetting (  )

Gets the current sample setting.

Returns:
Current sample setting.

Definition at line 182 of file mcp342x.cpp.

MCP342X::Status setChannel ( AdcChannel  ch )

Sets a ADC channel.

Parameters:
chADC channel which to be the input.
Returns:
SUCCESS when succeeded. Other value will be returned when error.

Definition at line 159 of file mcp342x.cpp.

MCP342X::Status setConversionMode ( ConversionMode  mode )

Sets a conversion mode.

Parameters:
modeConversion mode which to be set.
Returns:
SUCCESS when succeeded. Other value will be returned when error.

Definition at line 168 of file mcp342x.cpp.

MCP342X::Status setPgaSetting ( PgaSetting  s )

Sets the gain of Programmable Gain Amplifier (PGA).

Parameters:
sPGA seeting to be set.
Returns:
SUCCESS when succeeded. Other value will be returned when error.

Definition at line 186 of file mcp342x.cpp.

MCP342X::Status setSampleSetting ( SampleSetting  s )

Sets sample setting, i.e.

sampling frequency and resolution bits.

Parameters:
sSample setting to be set.
Returns:
SUCCESS when succeeded. Other value will be returned when error.

Definition at line 177 of file mcp342x.cpp.

MCP342X::Status trigger (  )

Trigger AD conversion.

In continuous measurement mode, this function has no effect.

Returns:
SUCCESS when succeeded. Other value will be returned when error.

Definition at line 209 of file mcp342x.cpp.