Expand description

This is a platform-agnostic Rust driver for the MCP49xx and MCP48xx SPI digital-to-analog converters (DAC), based on the embedded-hal traits.

This driver allows you to:

  • Set a channel to a value.
  • Shutdown a channel.
  • Use buffering on commands.
  • Select gain.

The devices

The Microchip Technology Inc. MCP49xx and MCP48xx devices are single/dual channel 8-bit, 10-bit and 12-bit buffered voltage output Digital-to-Analog Converters (DACs). The devices operate from a single 2.7V to 5.5V supply with an SPI compatible Serial Peripheral Interface. The user can configure the full-scale range of the device to be Vref or 2*Vref by setting the gain selection option bit (gain of 1 of 2).

The user can shut down the device by setting the Configuration Register bit. In Shutdown mode, most of the internal circuits are turned off for power savings, and the output amplifier is configured to present a known high resistance output load (500 kΩ, typical).

The devices include double-buffered registers, allowing synchronous updates of the DAC output using the LDAC pin. These devices also incorporate a Power-on Reset (POR) circuit to ensure reliable power-up.

The devices utilize a resistive string architecture, with its inherent advantages of low Differential Non-Linearity (DNL) error and fast settling time. These devices are specified over the extended temperature range (+125°C).

The devices provide high accuracy and low noise performance for consumer and industrial applications where calibration or compensation of signals (such as temperature, pressure and humidity) are required.

This driver is compatible with these devices:

DeviceResolutionChannelsBuffering
MCP48018-bit1Not available
MCP48028-bit2Not available
MCP481110-bit1Not available
MCP481210-bit2Not available
MCP482112-bit1Not available
MCP482212-bit2Not available
MCP49018-bit1Available
MCP49028-bit2Available
MCP491110-bit1Available
MCP491210-bit2Available
MCP492112-bit1Available
MCP492212-bit2Available

Datasheets:

The interface

These devices support changing all configuration flags in each command sent. In order to keep this flexibility, this driver does not provide individual methods to set the settings but provides a Command struct which can be used to specify all settings. Then commands can be sent to the device through the send() method.

Usage examples (see also examples folder)

To use this driver, import this crate and an embedded_hal implementation, then instantiate the appropriate device. In the following examples an instance of the device MCP4921 will be created as an example. Other devices can be created with similar methods like: Mcp49xx::new_mcp4822(...).

Please find additional examples using hardware in this repository: driver-examples

Set channel 0 to position 1024 in a MCP4921 device

use mcp49xx::{Channel, Command, Mcp49xx};
use linux_embedded_hal::{Pin, Spidev};

let mut spi = Spidev::open("/dev/spidev0.0").unwrap();
let chip_select = Pin::new(25);

let mut dac = Mcp49xx::new_mcp4921(chip_select);

let cmd = Command::default();
let cmd = cmd.channel(Channel::Ch0).value(1024);
dac.send(&mut spi, cmd).unwrap();

// Get CS pin back
let _chip_select = dac.destroy();

Set position and shutdown channels in a MCP4822 device

use mcp49xx::{Channel, Command, Mcp49xx};
use linux_embedded_hal::{Pin, Spidev};

let mut spi = Spidev::open("/dev/spidev0.0").unwrap();
let chip_select = Pin::new(25);

let mut dac = Mcp49xx::new_mcp4822(chip_select);

let cmd = Command::default();
let cmd = cmd.channel(Channel::Ch1).value(1024);
dac.send(&mut spi, cmd).unwrap();

let cmd = Command::default();
let cmd = cmd.channel(Channel::Ch0).shutdown();
dac.send(&mut spi, cmd).unwrap();

// Get CS pin back
let _chip_select = dac.destroy();

Set position and activate buffering and double gain in a MCP4911 device

use mcp49xx::{Channel, Command, Mcp49xx};
use linux_embedded_hal::{Pin, Spidev};

let mut spi = Spidev::open("/dev/spidev0.0").unwrap();
let chip_select = Pin::new(25);

let mut dac = Mcp49xx::new_mcp4911(chip_select);

let cmd = Command::default();
let cmd = cmd.channel(Channel::Ch0).buffered().double_gain().value(511);
dac.send(&mut spi, cmd).unwrap();

// Get CS pin back
let _chip_select = dac.destroy();

Modules

Markers

Structs

Configurable command that can be sent to the device

MCP49xx digital potentiometer driver

Enums

Channel selector

All possible errors in this crate

Constants

Helper for CPOL = 0, CPHA = 0

Helper for CPOL = 1, CPHA = 1