-
Notifications
You must be signed in to change notification settings - Fork 60
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to use SysTick timer to count ticks from a rotary encoder? #153
Comments
I gave it a quick skim, it looks like the SysTick is simply used to calculate the speed of the encoder: stm32f1xx_it.c#L197 The actual position comes from a different mechanism, Assuming you have that position the SysTick handler could be written in rust along the lines of: // probably missing some use statements, I didn't actually compile this.
use cortex_m_rt::{entry, exception};
static OLDPOS: i16 = 0;
static INDX: i32 = 0;
#[exception]
fn SysTick() {
unsafe {
INDX += 1;
if (INDX == 500) {
SPEED = ((POSITION - OLDPOS) * 2);
OLDPOS = POSITION;
INDX = 0;
}
}
// whatever else here
}
#[entry]
fn main() -> ! {
let p = cortex_m::Peripherals::take().unwrap();
let mut syst = p.SYST;
// configures the system timer to trigger a SysTick exception every second
syst.set_clock_source(SystClkSource::Core);
// CPU clock of 8 MHz
syst.set_reload(8_000_000);
syst.clear_current();
syst.enable_counter();
syst.enable_interrupt();
loop {
compiler_fence(SeqCst);
}
} Actually getting the position is a bit more effort that I don't know about off the top of my head. That C example you linked isn't too big, you could probably recreate that using the PAC to get started. |
@newAM Thanks very much for this! I saw in the examples that the |
This is the exception number definition for ARMv6-M: https://developer.arm.com/documentation/ddi0419/c/System-Level-Architecture/System-Level-Programmers--Model/ARMv6-M-exception-model/Exception-number-definition?lang=en 2 is |
I have seen a couple of examples that use the SysTick timer to count the rotations on a rotary encoder, like this example for the blue pill .
I was able to port that example to ESP32 (based on ESP32-Arduino code) as:
I saw there's a systick timer in this repo and a flash example based on it, but I can't reason around how I would use it for the rotary encoder.
Any help with this would be highly appreciated!
The text was updated successfully, but these errors were encountered: