Using timer(s) to capture multiple inputs #1002
Replies: 12 comments
-
Posted at 2018-10-25 by @gfwilliams Great! You're doing this with a Pico? On the F4, certain peripherals are wired to certain pins. Usually you have the option of having them in one of two places - it's controlled by something called the 'Alternate Function register'. The datasheet for the F4 tends to have a big table of what can go where, which I've digitised for Espruino here: https://github.com/espruino/Espruino/blob/master/boards/pins/stm32f401_af.csv You could look at the reference manual (or probably googling for some examples is easier) but it's not that nice. Your best bet is just to look at the pinout docs for the Pico (or WiFi board, if you use that): https://www.espruino.com/Pico#pinout Hover the mouse over the |
Beta Was this translation helpful? Give feedback.
-
Posted at 2018-10-25 by jugglingcats That's helpful Gordon thanks. Yes I am using the Pico. So you think if I pick, say B6 and B7 (TIM4, CH1 and CH2 respectively), I should be able to do the same trick as you did with TIM1, just by looking up the relevant register addresses/offsets? |
Beta Was this translation helpful? Give feedback.
-
Posted at 2018-10-25 by jugglingcats I got it all working - thanks! Here is my code in case it's useful to anyone...
|
Beta Was this translation helpful? Give feedback.
-
Posted at 2018-10-25 by jugglingcats Nearly done... |
Beta Was this translation helpful? Give feedback.
-
Posted at 2018-10-25 by jugglingcats And the finished product :) |
Beta Was this translation helpful? Give feedback.
-
Posted at 2018-10-26 by @allObjects Great for tutorial... can you write up a little bit more about the application... and what you sense? Is the published code the final one? Then this has to go onto the key forum pages... (--> @gordon) |
Beta Was this translation helpful? Give feedback.
-
Posted at 2018-10-26 by @gfwilliams That's fantastic - thanks for posting it up! So you're doing it with 3 different timers in the end? I guess you might have to - if the pulses aren't all aligned you'll need a separate counter to count the time each one is high. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2018-10-26 by jugglingcats It just seemed to be easier to use 3 timers in the end because the register offsets are all the same, so I could write some generic code to initialise and read them etc. I've put all the code on Github... |
Beta Was this translation helpful? Give feedback.
-
Posted at 2018-10-26 by jugglingcats I'd now like to go further and handle step and direction (with direction being high/low signal on another gpio). Having trouble understanding how to translate the example given below into Espruino equivalent! https://stackoverflow.com/questions/32947972/stm32-how-to-make-pulse-count-up-down-with-timer |
Beta Was this translation helpful? Give feedback.
-
Posted at 2018-10-26 by @gfwilliams Yeah, that code is using ST's API rather than poking registers (mostly!). You'll need to look at the ST reference manual, check out the registers, and what stuff like It might help to cheat and look at what ST's API does itself: https://github.com/espruino/Espruino/blob/master/targetlibs/stm32f4/lib/stm32f4xx_tim.c#L302 Note that ST tend to change their API quite a lot and I got fed up chasing them a few years ago, so Espruino's libraries are old. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2018-10-28 by jugglingcats After a lot of messing about and reading the spec, I don't think it's possible to have the counter DIR bit change according to a simple high/low on another GPIO as required. The encoder mode of the timer is designed for a quadrature encoder. I ended up with a little inline C to switch this bit according to GPIO input edge change like this:
|
Beta Was this translation helpful? Give feedback.
-
Posted at 2018-10-29 by @allObjects ...the world is hybrid! Congrats to your great solution! |
Beta Was this translation helpful? Give feedback.
-
Posted at 2018-10-25 by jugglingcats
Hi, I read this page with interest: http://www.espruino.com/STM32+Peripherals.
I followed the "Counting edges" section and got it working - very happy.
I'd like to capture 3 inputs using this method. Is this possible using more channels on TIM1? Am not clear how pin A8 gets associated with TIM1 CH1 and how I'd do this for other pins. Any pointers would be appreciated.
EDIT: TL;DR
Completed project write up here: https://github.com/jugglingcats/pulse-counter
Thanks,
Alfie.
Beta Was this translation helpful? Give feedback.
All reactions