From 3aef86531398a98258bdb21a73b46fc2914db08e Mon Sep 17 00:00:00 2001 From: xengineering Date: Tue, 28 Sep 2021 14:27:33 +0200 Subject: Timing Improvements for PWM Demonstration --- README.md | 1 + src/main.c | 17 ++++++++++++----- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 36228fa..1ddab7f 100644 --- a/README.md +++ b/README.md @@ -44,4 +44,5 @@ call `load` and `run` again. - Nice [tutorial](https://jacobmossberg.se/posts/2018/08/11/run-c-program-bare-metal-on-arm-cortex-m3.html) about C programs on bare-metal ARM chips - Another useful [tutorial](https://www.rhye.org/post/stm32-with-opencm3-0-compiling-and-uploading/) about STM32 programming with libopencm3 +- A nice [PWM example](https://github.com/libopencm3/libopencm3-examples/blob/master/examples/stm32/f1/obldc/pwmleds/pwmleds.c) from the libopencm3 project diff --git a/src/main.c b/src/main.c index 18a6e4a..8e39278 100644 --- a/src/main.c +++ b/src/main.c @@ -80,7 +80,7 @@ static void timer_init(void) // setup timer 2 for blinking timer_set_counter(TIM2, 1); timer_set_prescaler(TIM2, 1440); - timer_set_period(TIM2, 25000); + timer_set_period(TIM2, 50); // enable timer / counter 2 and the corresponding interrupt for blinking timer_enable_irq(TIM2, TIM_DIER_UIE); @@ -122,14 +122,21 @@ void tim2_isr(void) { // PWM handling static uint32_t pwm_value; - pwm_value += 20000; - if (pwm_value > 60000) { - pwm_value = 0; + if (pwm_value == 0) { + pwm_value = 65535; + } + else { + pwm_value -= 20; } TIM3_CCR3 = pwm_value; // toggle blink LED - gpio_toggle(GPIOC, GPIO13); + static uint32_t counter; + counter += 1; + if (counter > 500) { + gpio_toggle(GPIOC, GPIO13); + counter = 0; + } // clear interrrupt flag TIM_SR(TIM2) &= ~TIM_SR_UIF; -- cgit v1.2.3-70-g09d2