Crate mcp3425 [−] [src]
A platform agnostic Rust driver for the MCP3425, 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.
Usage
Instantiating
Import this crate and an embedded_hal
implementation:
extern crate linux_embedded_hal as hal; extern crate mcp3425;
Then instantiate the device in either
ContinuousMode
or
OneShotMode
:
use 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.
let config = Config::new(Resolution::Bits12Sps240, Gain::Gain1); let high_res = config.with_resolution(Resolution::Bits16Sps15); let high_gain = high_res.with_gain(Gain::Gain8);
Measurements
You can trigger a one-shot measurement:
let mut adc = MCP3425::oneshot(dev, address, Delay); let config = Config::new(Resolution::Bits12Sps240, Gain::Gain1); match adc.measure(&config) { Ok(mv) => println!("ADC measured {} mV", mv), 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::NotInitialized) => unreachable!(), }
As you can see, the saturation values are automatically converted to proper errors.
Structs
Config |
Device configuration: Resolution and gain |
ContinuousMode |
Use the MCP3425 in Continuous Conversion mode. |
MCP3425 |
Driver for the MCP3425 ADC |
OneShotMode |
Use the MCP3425 in One-Shot mode. |
Enums
Error |
All possible errors in this crate |
Gain |
Programmable gain amplifier (PGA) |
Measurement |
This enum wraps the measurements read in continuous conversion mode. |
Resolution |
Conversion bit resolution and sample rate |
Traits
ConversionMode |
The two conversion mode structs implement this trait. |