rust 嵌入式esp23 《点灯》

当你踏入 Rust 嵌入式开发的世界,控制 ESP32 单片机点亮 LED 灯并让它规律闪烁,就像与硬件进行的第一次对话。

在上一篇 rust 嵌入式esp23 《hello word》的的基础上

修改代码

添加引脚配置
rs 复制代码
use esp_hal::{
    clock::CpuClock,
    gpio::{Output, OutputConfig, Level}, //新增的引脚配置
    main,
    time::{Duration, Instant},
};
声明引脚变量

config:默认的输出引脚配置

Level::High:高电压输出

peripherals.GPIO1:绑定G1引脚

js 复制代码
    ...
    let peripherals = esp_hal::init(config);
    let config = OutputConfig::default(); //声明输出引脚默认配置
    let mut led = Output::new(peripherals.GPIO1, Level::High, config); //引脚绑定G1 并配置高电压输出
    ...
控制led灯闪烁

led.toggle():实现高低电压的切换

rs 复制代码
 ...
    loop {
        led.toggle();//切换电压输出模式
        let delay_start = Instant::now();
  ...

完整代码

rs 复制代码
#![no_std]
#![no_main]
#![deny(
    clippy::mem_forget,
    reason = "mem::forget is generally not safe to do with esp_hal types, especially those \
    holding buffers for the duration of a data transfer."
)]

use esp_hal::{
    clock::CpuClock,
    gpio::{Output, OutputConfig, Level},
    main,
    time::{Duration, Instant},
};
use esp_println::println;

#[panic_handler]
fn panic(_: &core::panic::PanicInfo) -> ! {
    loop {
        println!("111 world!");
        println!("Panic!");
    }
}

// This creates a default app-descriptor required by the esp-idf bootloader.
// For more information see: <https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-reference/system/app_image_format.html#application-description>
esp_bootloader_esp_idf::esp_app_desc!();

#[main]
fn main() -> ! {
    // generator version: 0.5.0
    let config = esp_hal::Config::default().with_cpu_clock(CpuClock::max());
    let peripherals = esp_hal::init(config);
    let config = OutputConfig::default();
    let mut led = Output::new(peripherals.GPIO1, Level::High, config);

    loop {
        led.toggle();
        let delay_start = Instant::now();
        while delay_start.elapsed() < Duration::from_millis(500) {}
    }

    // for inspiration have a look at the examples at https://github.com/esp-rs/esp-hal/tree/esp-hal-v1.0.0-rc.0/examples/src/bin
}
硬件连接

将led灯的一个角接G1,另外一个接GND(接地)

usb连接电脑

运行命令

shell 复制代码
cargo run 

结果如图所示

成果展示
相关推荐
Source.Liu19 小时前
【ISO8601】ISO8601 Rust 库完整学习指南
rust·time
像风一样自由202019 小时前
Rust与Python完全指南:从零开始理解两门语言的区别与关系
开发语言·python·rust
阿源-21 小时前
UEFI 启动的各阶段介绍
嵌入式·uefi·edk2·固件
2301_7965125221 小时前
Rust编程学习 - 如何学习有关函数和闭包的高级特性,这包括函数指针以及返回闭包
服务器·学习·rust
小灰灰搞电子1 天前
Rust Slint实现控件尺寸的扩展与收缩源码分享
开发语言·后端·rust
林太白1 天前
rust13-字典类型
后端·rust
tung tung tung sahur1 天前
领略 Rust 抽象之美:自定义迭代器实现全解析
开发语言·后端·rust
微小冷1 天前
Rust图形界面egui初步教程
rust·编程语言·egui·用户图形界面·示例项目
ftpeak1 天前
《Rust MP4视频技术开发》第八章:生成MP4
开发语言·rust·音视频·mp4
大聪明-PLUS1 天前
io_uring:Linux 上的高性能异步 I/O
linux·嵌入式·arm·smarc