Comments (3)
Hi! I would suggest using the matrix channel (https://matrix.to/#/#esp-rs:matrix.org) for this kind of questions as they are not issues per se and not strictly related with the training. Also, more people could jump in to help you.
from std-training.
Hi! I would suggest using the matrix channel (https://matrix.to/#/#esp-rs:matrix.org) for this kind of questions as they are not issues per se and not strictly related with the training. Also, more people could jump in to help you.
ok
from std-training.
is ok
use embedded_hal::digital;
use esp_idf_svc::hal::delay;
use esp_idf_svc::hal::gpio;
use esp_idf_svc::hal::peripherals;
use esp_idf_svc::hal::uart;
use esp_idf_svc::hal::units;
use std::fmt::Write;
const CMD_SKIP: u8 = 0xcc;
const CMD_WRITE_REG: u8 = 0x4e;
const CMD_TEMP_START: u8 = 0x44;
const CMD_READ_REG: u8 = 0xbe;
fn main() {
// It is necessary to call this function once. Otherwise some patches to the runtime
// implemented by esp-idf-sys might not link properly. See https://github.com/esp-rs/esp-idf-template/issues/71
esp_idf_svc::sys::link_patches();
// Bind the log crate to the ESP Logging facilities
esp_idf_svc::log::EspLogger::initialize_default();
log::info!("Hello, world!");
let peripherals = peripherals::Peripherals::take().unwrap();
// onewire
let mut onewire = gpio::PinDriver::input_output_od(peripherals.pins.gpio0).unwrap();
onewire.set_pull(gpio::Pull::Floating).unwrap(); // use external pull-up
// uart
let config = uart::config::Config::default().baudrate(units::Hertz(115_200));
let mut uart = uart::UartDriver::new(
peripherals.uart1,
peripherals.pins.gpio1,
peripherals.pins.gpio2,
Option::<gpio::AnyIOPin>::None,
Option::<gpio::AnyIOPin>::None,
&config,
)
.unwrap();
// delay
let delay = delay::Delay::new_default();
// ds18b20
// wait reset
while !onewire_reset(&mut onewire, &delay) {
delay::FreeRtos::delay_ms(1000);
}
// set accuracy
onewire_write_byte(&mut onewire, &delay, CMD_SKIP);
onewire_write_byte(&mut onewire, &delay, CMD_WRITE_REG);
onewire_write_byte(&mut onewire, &delay, 0xff);
onewire_write_byte(&mut onewire, &delay, 0x00);
onewire_write_byte(&mut onewire, &delay, 0x7f);
loop {
delay::FreeRtos::delay_ms(1000);
// ds18b20
// measurement temp
if !onewire_reset(&mut onewire, &delay) {
continue;
}
onewire_write_byte(&mut onewire, &delay, CMD_SKIP);
onewire_write_byte(&mut onewire, &delay, CMD_TEMP_START);
// read temp
if !onewire_reset(&mut onewire, &delay) {
continue;
}
onewire_write_byte(&mut onewire, &delay, CMD_SKIP);
onewire_write_byte(&mut onewire, &delay, CMD_READ_REG);
let temp_l = onewire_read_byte(&mut onewire, &delay);
let temp_h = onewire_read_byte(&mut onewire, &delay);
let temp = (((temp_h as i16) << 8 | (temp_l as i16)) as f64) * 0.0625;
log::info!("temp: {:.1}", temp);
// uart
writeln!(uart, "temp: {:.1}", temp).unwrap();
}
}
fn onewire_reset<T>(pin: &mut T, delay: &delay::Delay) -> bool
where
T: digital::InputPin + digital::OutputPin,
{
pin.set_low().unwrap();
delay.delay_us(600);
pin.set_high().unwrap();
delay.delay_us(80);
if pin.is_low().unwrap() {
log::info!("onewire: is exist");
delay.delay_us(900);
if pin.is_high().unwrap() {
log::info!("onewire: reset ok");
true
} else {
log::warn!("onewire: reset err");
false
}
} else {
log::warn!("onewire: reset err");
false
}
}
fn onewire_write_byte<T>(pin: &mut T, delay: &delay::Delay, mut byte: u8)
where
T: digital::InputPin + digital::OutputPin,
{
for _ in 0..8 {
let bit = byte & 0x01 != 0;
byte >>= 1;
if bit {
pin.set_low().unwrap();
delay.delay_us(5);
pin.set_high().unwrap();
delay.delay_us(90);
} else {
pin.set_low().unwrap();
delay.delay_us(90);
pin.set_high().unwrap();
delay.delay_us(5);
}
}
}
fn onewire_read_byte<T>(pin: &mut T, delay: &delay::Delay) -> u8
where
T: digital::InputPin + digital::OutputPin,
{
let mut byte: u8 = 0;
for _ in 0..8 {
byte >>= 1;
pin.set_low().unwrap();
delay.delay_us(5);
pin.set_high().unwrap();
delay.delay_us(5);
if pin.is_high().unwrap() {
byte |= 0x80;
}
delay.delay_us(60);
}
byte
}
from std-training.
Related Issues (20)
- Use mdBook anchors instead of duplicating code
- Common crates HOT 1
- Async material
- ESP32-C6 and ZigBee examples HOT 4
- The MQTT lesson is not for ESP32? HOT 3
- Running "hardware-check" reports "This script was called from a virtual environment, can not create a virtual environment again" error HOT 2
- Compilation error when building hardware-check example for ESP32 HOT 2
- Building `esp-idf-sys` failed because of uninitialized submodules in esp-idf HOT 3
- error: unrecognized command-line option '--target=riscv32imc_zicsr_zifencei-esp-espidf HOT 2
- How to modify the configuration of SDK,such as the port port port rate HOT 4
- esp32c2 uses 26M crystal oscillator, resulting in delay errors HOT 2
- WIFI Example does not compile with the esp_idf_svc crate recommended in the `esp-rs` book HOT 6
- How create multiple asynchronous tasks,task have "loop{}" HOT 3
- 'None' cannot be used for the 'Option' type in 'SpiDeviceDriver::new_single()' HOT 1
- Button interrupt on edge fired repeatedly
- Guide on interoperability with C
- hardware-check: Segment 0 0x3c090020-0x3c09fff8 invalid: bad load address range HOT 5
- RGB LED is not flashing if esp-idf-svc is 0.48.* HOT 3
- Does esp-rs support speech recognition now, or can I use the existing esp-sr?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from std-training.