rtic/examples/stm32f411_encoder_polling/README.md
Milton Eduardo Sosa 87a8b74908
Add example to poll encoder and display value on I2C display (#963)
Co-authored-by: Milton Eduardo Sosa <milton@Miltons-MacBook-Pro.local>
2024-10-02 18:46:42 +00:00

64 lines
2.3 KiB
Markdown

# STM32F411CEU6 encoder polling and LCD display
This example uses an STM32F411CEU6 microcontroller on the Blackpill board to interface with an I2C LCD display and a rotary encoder.
## Features
- **Rotary Encoder Integration**: Reads input from a rotary encoder to adjust a value.
- **I2C LCD Display**: Displays the current encoder value on an I2C-connected HD44780 LCD.
## Working principle
The hardware task `tim2_timeout_interrupt_handler` polls the encoder state on each interrupt generated by TIM2, and updates the displayed value accordingly.
## Code Overview
### Key Components
- **Knob Struct**: Placeholder for the rotary encoder and its current value.
- **LCD I2C Communication**: Uses I2C protocol to communicate with the HD44780 LCD.
- **Timers**: Utilizes TIM1 for delays and TIM2 for polling the rotary encoder.
### Important Constants
- `LCD_I2C_ADDRESS`: The I2C address of the LCD display.
### Initialization
- **System Clock**: Configured to use an external 25 MHz clock.
- **GPIO Setup**: Configures GPIO pins for the LED, rotary encoder, and I2C communication.
- **I2C Initialization**: Sets up the I2C bus for LCD communication.
- **LCD Initialization**: Initializes the LCD and displays initial messages.
- **Timer Setup**: Configures TIM2 at 2 kHz and generate interrupts.
### Tasks and Handlers
- **idle**: Background task that runs when no other tasks are active.
- **tim2_timeout_interrupt_handler**: Handles interrupts generated by TIM2, updates the rotary encoder value, and displays the value on the LCD.
### Utility Functions
- `u8_to_str(n: u8) -> [u8; 3]`: Converts an 8-bit integer to a 3-character string representation.
## Dependencies
- **hd44780-driver**: Driver for HD44780 LCD over I2C.
- **rotary_encoder_embedded**: Library for handling rotary encoders.
- **defmt**: Logging framework for embedded systems.
## How-to
### Build
Run `cargo build --release` to compile the code. If you run it for the first time, it will take some time to download and compile dependencies.
### Run
Install `probe-rs` and configure it using the [debugging extension for VScode](https://probe.rs/docs/tools/debugger/).
The output should look like this:
[![IMAGE ALT TEXT HERE](https://img.youtube.com/vi/nn75gWDhHn0/0.jpg)](https://www.youtube.com/watch?v=nn75gWDhHn0)