3个稳定版本
1.0.2 | 2020年11月9日 |
---|---|
1.0.1 | 2020年9月19日 |
#489 在 嵌入式开发
46 每月下载量
15KB
129 行
模拟多路复用器
一个针对Rust嵌入式-hal的4051和4067系列模拟多路复用器的不依赖于平台的驱动程序
此crate提供了一个接口Multiplexer
,它使得在任何74HC4051或74HC4067系列模拟多路复用器上选择通道变得非常简单。内部它会跟踪每个多路复用器的状态,允许你检查当前哪个通道是激活的,或者根据需要启用/禁用多路复用器。
支持的硬件(模拟多路复用器)
用法
以下是一个使用Blue Pill(stm32f104)板的74HC4067的虚构示例...
// 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
.cfgr
.use_hse(8.mhz())
.sysclk(72.mhz())
.pclk1(36.mhz())
.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
.pb12
.into_push_pull_output_with_state(&mut gpiob.crh, State::Low);
let s1 = gpiob
.pb13
.into_push_pull_output_with_state(&mut gpiob.crh, State::Low);
let s2 = gpiob
.pb14
.into_push_pull_output_with_state(&mut gpiob.crh, State::Low);
let s3 = gpiob
.pb15
.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
.pb5
.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
}
}
}
工作示例
在examples
目录中有一个正确的工作示例(read_all
),它使用RTIC和probe-rs,效果很好。它需要一个ST-LINK编程器,一个Blue Pill板和probe-run。
以下是我用它来读取通道15上的霍尔效应传感器的示例
许可证
许可协议
- Apache License,版本2.0(LICENSE-APACHE或https://apache.ac.cn/licenses/LICENSE-2.0)
依赖关系
~71KB