diff options
author | xengineering <me@xengineering.eu> | 2021-09-28 14:27:33 +0200 |
---|---|---|
committer | xengineering <me@xengineering.eu> | 2021-09-28 14:27:33 +0200 |
commit | 3aef86531398a98258bdb21a73b46fc2914db08e (patch) | |
tree | de9d04f71c8257112f26a6bfc7fd87d5b4fd79a0 /src/main.c | |
parent | 9704cdaa3d48fe1deceaba1189706326bd41f306 (diff) | |
download | ledcontrol-3aef86531398a98258bdb21a73b46fc2914db08e.tar ledcontrol-3aef86531398a98258bdb21a73b46fc2914db08e.tar.zst ledcontrol-3aef86531398a98258bdb21a73b46fc2914db08e.zip |
Timing Improvements for PWM Demonstration
Diffstat (limited to 'src/main.c')
-rw-r--r-- | src/main.c | 17 |
1 files changed, 12 insertions, 5 deletions
@@ -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; |