nucleo-l053r8/examples/lcd.rs
2025-05-12 16:05:32 +02:00

85 lines
2.5 KiB
Rust
Executable file

#![no_main]
#![no_std]
use defmt::{debug, error, info, println, trace, warn};
use hal::pwm::Pin;
use panic_probe as _;
use defmt_rtt as _; // global logger
use core::fmt::Write;
use cortex_m_rt::entry;
use hal::{pac, prelude::*, rcc::Config};
use hd44780_driver::HD44780;
use heapless::Vec;
#[defmt::panic_handler]
fn panic() -> ! {
cortex_m::asm::udf()
}
#[entry]
fn main() -> ! {
let dp = pac::Peripherals::take().unwrap();
let cp = cortex_m::Peripherals::take().unwrap();
let mut rcc = dp.RCC.freeze(Config::hsi16());
let gpioa = dp.GPIOA.split(&mut rcc);
let gpiob = dp.GPIOB.split(&mut rcc);
let gpioc = dp.GPIOC.split(&mut rcc);
// contrast over A0 analog port
// analog output seems to not be supported somehow?
let mut vO = gpioa.pa0.into_push_pull_output();
// literal D4-D7 ports etc as written on the nucleo board, mapped to the D4-D7 ports of the LCD
// controller
let d4 = gpiob.pb5.into_push_pull_output();
let d5 = gpiob.pb4.into_push_pull_output();
let d6 = gpiob.pb10.into_push_pull_output();
let d7 = gpioa.pa8.into_push_pull_output();
// clock enable on D8
let en = gpioa.pa9.into_push_pull_output();
// register select on D9, the lib wants that but I'd just put it on ground otherwise
let rs = gpioc.pc7.into_push_pull_output();
// See https://en.wikipedia.org/wiki/Hitachi_HD44780_LCD_controller#Interface
// for the pins of the LCD
let mut delay = cp.SYST.delay(rcc.clocks);
let mut led = gpioa.pa5.into_push_pull_output();
let mut lcd = HD44780::new_4bit(rs, en, d4, d5, d6, d7, &mut delay)
.expect("could not init HD44780 driver");
lcd.set_display_mode(
hd44780_driver::DisplayMode {
cursor_visibility: hd44780_driver::Cursor::Visible,
cursor_blink: hd44780_driver::CursorBlink::On,
display: hd44780_driver::Display::On,
},
&mut delay,
)
.expect("could not set display properties");
lcd.reset(&mut delay).expect("could not reset the lcd");
lcd.write_str("Hello world!", &mut delay)
.expect("could not write to LCD");
let mut i = 0;
loop {
vO.set_low().expect("set contrast low boom?");
led.set_high().unwrap();
info!("Writing to LCD...");
lcd.write_str("Hello world!\nGoing on", &mut delay)
.expect("could not write to LCD");
info!("Done!");
led.set_low().unwrap();
delay.delay_ms(500_u16);
i += 1;
}
}