3 releases (1 stable)

1.0.0 Jun 24, 2024
0.2.0 Sep 23, 2021
0.1.0 Feb 9, 2019

#1874 in Embedded development

Download history 12/week @ 2024-09-12 14/week @ 2024-09-19 6/week @ 2024-09-26

156 downloads per month

MIT/Apache

18KB
174 lines

Rust MCP4x digital potentiometer (digipot) driver

crates.io Docs Minimum Supported Rust Version Build Status Coverage Status

This is a platform-agnostic Rust driver for the MCP41xxx and MCP42xxx SPI digital potentiometers (digipot), based on the embedded-hal traits.

This driver allows you to:

  • Set a channel (or all of them) to a position. See: set_position().
  • Shutdown a channel (or all of them). See: shutdown().

The devices

The MCP41XXX and MCP42XXX devices are 256-position, digital potentiometers available in 10 kΩ, 50 kΩ and 100 kΩ resistance versions. The MCP41XXX is a single-channel device and is offered in an 8-pin PDIP or SOIC package. The MCP42XXX contains two independent channels in a 14-pin PDIP, SOIC or TSSOP package. The wiper position of the MCP41XXX/42XXX varies linearly and is controlled via an industry-standard SPI interface.

The devices consume <1 μA during static operation. A software shutdown feature is provided that disconnects the "A" terminal from the resistor stack and simultaneously connects the wiper to the "B" terminal. In addition, the dual MCP42XXX has a SHDN pin that performs the same function in hardware. During shutdown mode, the contents of the wiper register can be changed and the potentiometer returns from shutdown to the new value. The wiper is reset to the mid-scale position (80h) upon power-up. The RS (reset) pin implements a hardware reset and also returns the wiper to mid-scale.

The MCP42XXX SPI interface includes both the SI and SO pins, allowing daisy-chaining of multiple devices. Channel-to-channel resistance matching on the MCP42XXX varies by less than 1%.

This driver should be compatible at least with the devices: MCP41010, MCP41050, MCP41100, MCP42010, MCP42050 and MCP42100.

Datasheet:

Usage

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 MCP41x will be created as an example. Other devices can be created with similar methods like: Mcp4x::new_mcp42x(...).

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

use mcp4x::{Channel, Mcp4x};
use embedded_hal_bus::spi::ExclusiveDevice;
use linux_embedded_hal::{Delay, SpidevBus, SysfsPin};

fn main() {
    let spi = SpidevBus::open("/dev/spidev0.0").unwrap();
    let chip_select = SysfsPin::new(25);
    let dev = ExclusiveDevice::new(spi, chip_select, Delay);
    let mut mcp41x = Mcp4x::new_mcp41x(dev);

    mcp41x.set_position(Channel::Ch0, 50).unwrap();

    // Get SPI device back
    let _dev = mcp41x.destroy_mcp41x();
}

Support

For questions, issues, feature requests like compatibility with similar devices and other changes, please file an issue in the github project.

License

Licensed under either of

at your option.

Contributing

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~56KB