[][src]Crate analog_multiplexer

This crate provides an interface, Multiplexer that makes it trivially easy to select channels on any given 74HC4051 or 74HC4067 series analog multiplexer. Internally it keeps track of each multiplexer's state, allowing you to check what channel is presently active or to enable/disable the multiplexer at will.

Example using a 74HC4067 with a Blue Pill (stm32f104) board

// NOTE: This is pseudocode. It's just meant to get the concept across :)
use analog_multiplexer::Multiplexer; // Important part

use stm32f1xx_hal::gpio::State;
// The pins we're using:
use stm32f1xx_hal::gpio::gpiob::{PB0, PB5, PB12, PB13, PB14, PB15};
use stm32f1xx_hal::{adc}; // So we can read an analog pin (PB0)

fn main() {
    // stm32f1xx_hal boilerplate...
    let device = pac::Peripherals::take().unwrap();
    let mut flash = device.FLASH.constrain();
    let mut rcc = device.RCC.constrain();
    let mut _afio = device.AFIO.constrain(&mut rcc.apb2);
    let _clocks = rcc
        .freeze(&mut flash.acr);
    // Setup ADC (we're using ADC1 for this example since we're reading PB0)
    let adc1 = adc::Adc::adc1(device.ADC1, &mut rcc.apb2, _clocks);
    // Setup GPIOB (so we can access the ADC via PB0)
    let mut gpiob = device.GPIOB.split(&mut rcc.apb2);
    // Configure PB0 as an analog input (all channels lead to this analog input pin!)
    let analog_pin = gpiob.pb0.into_analog(&mut gpiob.crl);
    // Setup PB12-PB15 for accessing S0-S3 on the 74HC4067 multiplexer
    let s0 = gpiob
        .into_push_pull_output_with_state(&mut gpiob.crh, State::Low);
    let s1 = gpiob
        .into_push_pull_output_with_state(&mut gpiob.crh, State::Low);
    let s2 = gpiob
        .into_push_pull_output_with_state(&mut gpiob.crh, State::Low);
    let s3 = gpiob
        .into_push_pull_output_with_state(&mut gpiob.crh, State::Low);
    // NOTE: On some multiplexers the S0-S3 pins are labeled A, B, C, D
    // Enable pin...  If you want to be able to enable/disable the multiplexer on-the-fly
    let en = gpiob
        .into_push_pull_output_with_state(&mut gpiob.crl, State::Low);
    // TIP: Just run a wire from EN to GND to keep it enabled all the time
    // Multiplexer pins are given as a tuple in the order S0-S3 then enable pin (EN):
    let pins = (s0,s1,s2,s3,en); // For 16-channel
    // let pins = (s0,s1,s2,en); // For 8-channel
    let mut multiplexer = Multiplexer::new(pins); // The important part!
    multiplexer.enable(); // Make sure it's enabled (if using EN pin)
    loop {
        for chan in 0..multiplexer.num_channels {
            multiplexer.set_channel(chan); // Change the channel
            let data: u16 = adc1.read(&mut *analog_pin).unwrap();
            // Do something with the data here

NOTE: There's a working Blue Pill/RTIC example in the examples directory.



Provides an interface for setting the active channel and enabling/disabling an 8-channel (74HC4051) or 16-channel (74HC4067) analog multiplexer. It also keeps track of which channel is currently active (active_channel) and provides a convenient num_channels field that can be used to iterate over all the multiplexer's channels.



A trait so we can support both 8-channel and 16-channel multiplexers simultaneously by merely instantiating them with a 5 (16-channel) or 4 (8-channel) member tuple of OutputPins.