Expand description
A platform agnostic Rust driver for the MCP3425 (and newer variants
MCP3426/MCP3427/MCP3428 as well), based on the
embedded-hal traits.
§The Device
The Microchip MCP3425 is a low-current 16-bit analog-to-digital converter. The device has an I²C interface and an on-board ±2048mV reference. For more information, see the datasheet.
Variants MCP3426/7/8 are very similar, but support multiple input channels. They are supported as well, but require to enable one of the following Cargo features:
dual_channelfor MCP3426/7quad_channelfor MCP3428
§Cargo Features
The following feature flags exists:
dual_channelfor dual-channel support (MCP3426/7/8)quad_channelfor dual-channel support (MCP3428)measurements: Use the measurements crate to represent voltages instead of the customVoltagewrapper
§Usage
§Instantiating
Import this crate and an embedded_hal implementation (e.g.
linux-embedded-hal).
Then instantiate the device in either
ContinuousMode or
OneShotMode:
use linux_embedded_hal::{Delay, I2cdev};
use mcp3425::{MCP3425, Config, Resolution, Gain, Error, OneShotMode};
let dev = I2cdev::new("/dev/i2c-1").unwrap();
let address = 0x68;
let mut adc = MCP3425::new(dev, address, Delay, OneShotMode);(You can also use the shortcut functions
oneshot or
continuous to create instances
of the MCP3425 type without having to specify the
type as parameter.)
§Configuration
You can choose the conversion resolution / sample rate and the PGA gain
with a Config object.
Use the methods starting with with_ to create a (side-effect free) new
instance of the configuration where the specified setting has been
replaced.
use mcp3425::Channel;
let config = Config::default()
.with_resolution(Resolution::Bits12Sps240)
.with_gain(Gain::Gain1);
let high_res = config.with_resolution(Resolution::Bits16Sps15);
let high_gain = high_res.with_gain(Gain::Gain8);Note: If you enable the dual_channel or quad_channel Cargo features,
you can also use the method .with_channel(...) on the Config struct (if
your model supports multiple input channels).
§Measurements
One-Shot
You can trigger a one-shot measurement:
let dev = I2cdev::new("/dev/i2c-1").unwrap();
let mut adc = MCP3425::oneshot(dev, address, Delay);
let config = Config::default();
match adc.measure(&config) {
Ok(voltage) => println!("ADC measured {} mV", voltage.as_millivolts()),
Err(Error::I2c(e)) => println!("An I2C error happened: {}", e),
Err(Error::VoltageTooHigh) => println!("Voltage is too high to measure"),
Err(Error::VoltageTooLow) => println!("Voltage is too low to measure"),
Err(Error::NotReady) => println!("Measurement not yet ready. This is a driver bug."),
Err(Error::NotInitialized) => unreachable!(),
}As you can see, the saturation values are automatically converted to proper errors.
Continuous
You can also configure the ADC in continuous mode:
let dev = I2cdev::new("/dev/i2c-1").unwrap();
let mut adc = MCP3425::continuous(dev, address, Delay);
let config = Config::default();
adc.set_config(&config).unwrap();
match adc.read_measurement() {
Ok(voltage) => println!("ADC measured {} mV", voltage.as_millivolts()),
Err(Error::I2c(e)) => println!("An I2C error happened: {}", e),
Err(Error::VoltageTooHigh) => println!("Voltage is too high to measure"),
Err(Error::VoltageTooLow) => println!("Voltage is too low to measure"),
Err(Error::NotReady) => println!("Measurement not yet ready. Polling too fast?"),
Err(Error::NotInitialized) => println!("You forgot to call .set_config"),
}Structs§
- Config
- Device configuration: Resolution, gain and input channel.
- Continuous
Mode - Use the MCP3425 in Continuous Conversion mode.
- MCP3425
- Driver for the MCP3425 ADC
- OneShot
Mode - Use the MCP3425 in One-Shot mode.
- Voltage
- A voltage measurement.
Enums§
- Channel
- Selected ADC channel
- Error
- All possible errors in this crate
- Gain
- Programmable gain amplifier (PGA)
- Resolution
- Conversion bit resolution and sample rate
Traits§
- Conversion
Mode - The two conversion mode structs implement this trait.