Electronics > Projects, Designs, and Technical Stuff
Help with STM32duino timers and frequency measurement
timdf911:
Hi guys
1st post but long term lurker !
I've been trying to update a design I originally did 7 years ago which used a Teensy processor and was written entirely in C using the Atmel environmanr.
I've recently discovered the delights of an STM32F401re and have started to port the code using stm32duino environment - but I've run into a road block and despite lots of googling I've made little progress due to being overwhelmed with all the registers associated with the timers and what I suspect is limited timer functionality in the stm32duino environment.
Here's what I need to do - basically a one shot measurement of a frequency between 1 and 30MHz to a resolution of 10kHz.
Here's how I think it should be done
1. Use timer 2 with the signal using ETR input with possibly the prescaler if it can't handle 30MHz directly
2. Set the timer2 counter to zero
3. Open the gate for 1mS using a second timer (say timer3) then read the timer 2 to see how many times it's been incremented. The resulting count should be between 1000 and 30000
To do the above seems to need a bunch of registers setting, I've had some limited success using some snippets of code from Andrewbcn GPSDO project, but frankly my attempt is a mess and I'd be ashamed to post it !
I'm hoping someone can give me a few pointers on key registers and settings which will link the two timers together and operate as above.
I've tried reading the data and using CubeMx but managed to get myself wrapped round the axle there aswell.
Regards Tim
coromonadalix:
https://www.engineersgarage.com/how-to-use-an-stm32-microcontroller-to-measure-external-signal-frequencies/
https://deepbluembedded.com/stm32-counter-mode-example-frequency-counter-timer-in-counter-mode/
https://microcontrollerslab.com/stm32-blue-pill-timer-input-capture-mode-frequency-measurement/
??
timdf911:
Thanks for those links, however during my extensive googling I had already come across them, but maybe I need to give them a second look / chance.
I was deterred from following them as some used CubeMX and / or did not explicitly use the ETR input for maximum frequency operation.
Maybe I need to practice figuring out how to use CubeMX more efficiently and how to embed the code it generates into mine.
Regards Tim
paulca:
CubeMX will guide you in what way you 'can' configure all the timers, making it quick to try things.
There is nothing preventing you from creating the basic project in a rapid throw away fashion using CubMX generated code and then put your own further register based code into an isolated C file. You can re-run the code generation when ever you want if you do it right.
I did this when I wanted to use a single timer to do two different things based on a sequence of interrupt cascades. CubeMX would set the timer up for me in the first mode, I then take over using the registers (and a few HAL functions and macros) to reconfigure and restart the timer in PWM DMA mode or basic counter delay timer.
timdf911:
Thanks for the ideas -funnily enough the timers will need to be reconfigured depending on what's happening with the device.
However given that my code size is already 150kB for all the other stuff going on before I add this frequency measurement part and the other code which goes round it (stepper motor control) I suspect it might be easier to integrate the CubeMX code into my code base rather than the other way around.
I had tried with CubeMX to generate the timer code but came a bit unstuck with exactly how to interconnect the timers such that the gating timer could gate the signal into the main counter for counting and what modes thy needed to be in etc - if that makes sense.
Basically how to configure using a two timer approach to count the incoming cycles over a fixed period of time.
Tomorrows another day and I'll have another crack at CubeMX by first getting the gating timer going at 1mS and checking with a scope it's operating as intended.
So much to learn and so few brain cells left :-)
Regards Tim
Navigation
[0] Message Index
[#] Next page
Go to full version