The STM32F1xx has some neat features that allow you to cross-couple timers in interesting ways.
In the last post on timers, I synchronized 2 timers by using a master-slave configuration where the master resets the slave on every cycle. This time I use 2 cross-coupled channels of one timer in order to measure a PWM train frequency and mark:space ratio.
The trick to measure PWM is to use one counter to measure the period and the second to measure mark time, the latter reset by the first period counter on every cycle:
Here’s how it works:
The pulse train is applied to the pin of Timer 1 channel 1 (T1C1)
T1C1 edge detector outputs rising and falling edge signals as shown below.
The MUXs are cleverly designed so that you can use the edge signals from one channel
This post pulls together the work from prior posts on the timers, ADC, DMA and GPIU for the STM32F372.
The idea is to get the MCU to do periodic ADC measurements and store the results away on its own without needing the CPU to intervene. This means no interrupts or polling, just the peripheral blocks working together on their own. This type of design is becoming more and more common in the low power world where the CPU and its subsystem is kept asleep, leaving the peripherals to do their job with the CPU only woken when some control needs doing.
The demo works like this:
Timer 2 is set up to produce a PWM train with a 5 second period. I use 2 channels in the same counter with the same mark:space ratio settings so that the first can drive the ADC whilst the second flashes an LED so that I know something is happening…
Timer2 has a nice feature where you can set it to send a Trigger Event at each Update Event (UEV). Here I set the UEV to be when the counter spills over the period