AnsweredAssumed Answered

Timer channel 1 running "regular timer" and channel 2 generating square wave. How?

Question asked by arnold_w on Nov 15, 2017
Latest reply on Nov 16, 2017 by KIC8462852 EPIC204278916

I am working with the stm32f4-discovery development board and I need to share timer 3 for two purposes:

1.  Regular timer that can be scheduled with 1 ms accuracy:

scheduleTimer3(uint16_t delayMs, EventCallback_t callback)

This is easy to implement, I have set the prescalar (TIM3->PSC) so that the timer increments every 1 ms and then I set TIM3->CCR1 to the desired delayMs. TIM3->ARR is set to 0xFFFF which means the biggest delay I can accomplish is 65535 ms and that's fine.

2.  A square wave with frequency of roughly 60 Hz (55 - 64 Hz is fine) on the pin connected to timer 3 channel 2. This is also fairly easy to achieve by itself, I just use PWM and set TIM3->ARR to 1000/60 = 16 and TIM3->CCR2 = TIM3->ARR/2.

 

But how can I do both 1 and 2 at the same time? The problem with this approach is that they require different TIM3->ARR values at the same time. Of course, I can use the lower TIM3-ARR value and then increment a counter in interrupts and only call the callback when a number of interrupts has occurred, but it seems inefficient. Surely there must be a better way to do this. Can someone please help me?

Outcomes