Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
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 |
| enum ConversionMode |
| enum DataStatus |
| enum MeasurementTrigger |
| enum PgaSetting |
| enum SampleSetting |
| enum SlaveAddress |
Slave addresses.
- Enumerator:
| enum Status |
Constructor & Destructor Documentation
| MCP342X | ( | I2C * | conn, |
| SlaveAddress | addr | ||
| ) |
Constructor.
- Parameters:
-
conn Pointer to an instance of I2C. addr Slave 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 ) |
| 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:
-
ch ADC 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:
-
mode Conversion 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:
-
s PGA 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:
-
s Sample 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.
Generated on Thu Jul 14 2022 03:57:06 by
1.7.2