summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorxengineering <me@xengineering.eu>2021-09-28 14:27:33 +0200
committerxengineering <me@xengineering.eu>2021-09-28 14:27:33 +0200
commit3aef86531398a98258bdb21a73b46fc2914db08e (patch)
treede9d04f71c8257112f26a6bfc7fd87d5b4fd79a0
parent9704cdaa3d48fe1deceaba1189706326bd41f306 (diff)
downloadledcontrol-3aef86531398a98258bdb21a73b46fc2914db08e.tar
ledcontrol-3aef86531398a98258bdb21a73b46fc2914db08e.tar.zst
ledcontrol-3aef86531398a98258bdb21a73b46fc2914db08e.zip
Timing Improvements for PWM Demonstration
-rw-r--r--README.md1
-rw-r--r--src/main.c17
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;