2个版本
0.1.1 | 2020年1月18日 |
---|---|
0.1.0 | 2020年1月17日 |
#1219 in 嵌入式开发
每月472次下载
用于ds18b20
20KB
385 行
单总线
快速开始
以下示例省略了错误处理以保持简洁。您应该检查所有结果并适当处理。
1-wire总线需要一个配置为开漏输出的单个数字引脚(它要么是开路,要么连接到地),并且总线应连接一个约5K欧姆的上拉电阻。如何从您的具体设备中获取此引脚取决于该设备的嵌入式-hal实现,但它必须实现InputPin
和OutputPin
use embedded_hal::blocking::delay::DelayUs;
use embedded_hal::digital::v2::{InputPin, OutputPin};
use core::fmt::{Debug, Write};
use one_wire_bus::OneWire;
fn find_devices<P, E>(
delay: &mut impl DelayUs<u16>,
tx: &mut impl Write,
one_wire_pin: P,
)
where
P: OutputPin<Error=E> + InputPin<Error=E>,
E: Debug
{
let mut one_wire_bus = OneWire::new(one_wire_pin).unwrap();
for device_address in one_wire_bus.devices(false, delay) {
// The search could fail at any time, so check each result. The iterator automatically
// ends after an error.
let device_address = device_address.unwrap();
// The family code can be used to identify the type of device
// If supported, another crate can be used to interact with that device at the given address
writeln!(tx, "Found device at address {:?} with family code: {:#x?}",
device_address, device_address.family_code()).unwrap();
}
}
示例输出
Found device at address E800000B1FCD1028 with family code: 0x28
Found device at address 70000008AC851628 with family code: 0x28
Found device at address 0B00000B20687E28 with family code: 0x28
Found device at address 5700000B2015FF28 with family code: 0x28
依赖项
~71KB