Hello,
I'm looking into the encoder interface with the timers but I can't seem to find the correct interrupt configuration for over and underflow of the counter.
Has anyone here worked with the timers before and these interrupts?
Hello,
I'm looking into the encoder interface with the timers but I can't seem to find the correct interrupt configuration for over and underflow of the counter.
Has anyone here worked with the timers before and these interrupts?
I've successfully connected encoder using approach described in this article: http://we.easyelectronics.ru/STM32/stm32--podklyuchaem-enkoder.html Unfortunately it is in Russian and for ST peripheral library, although the idea should be clear from the code - encoder is connected to GPIO A0 and GPIO A1 (outer pins), central pin is connected to ground. The timer is configured so it increments value by 4 on each encoder step, so in order to obtain real value you'll need to shift value obtained from timer by 2 bits. Hope this helps. Also, if this will be necessary, I can translate the article mentioned above (with author permission, of course).
Once I'll reach home I'll try to provide more libmaple-friendly code (although libmaple seems still can't configure timer properly without stepping down to register level).
That would be brilliant.
I'm stuck at two things. The first is to use Maple friendly code (to create a class to be used by Maple) and at that I'm not quite sure where to adapt the register configuration in the timer.c/h.
The other problem I have is to configure under and overflow interrupts.
The way I thought about implementing an Encoder class would be one where you's set the Reload register with the PulsePerRevolution of the encoder and every time it underflows, a turns variable would be decremented and vice-versa.
One nifty little trick that could be used is, if the encoder is connected to a motor shaft but there is a gear in front, loading the prescaler with the gear ratio would ease the calculations a bit (and maybe the interrupt load).
Is this a good idea?
I was reading the manual on how to create a frequency/PWM generator with a STM32 and came across this encoder interface. If there is something that people using Arduino miss is an easy way to interface encoders. So it's something that shouldn't be missing here. :)
The example (incomplete) shown below:
...
static inline timer_gen_reg_map* timer_get_reg_map(timer_dev* dev) {
return (dev->regs).gen;
}
...
void setup() {
...
timer_init(TIMER2);
timer_cc_set_pol(TIMER2, 1, 1);
timer_cc_set_pol(TIMER2, 2, 1);
timer_get_reg_map(TIMER2)->CCMR1 |= TIMER_CCMR1_CC1S; //set both channels
timer_get_reg_map(TIMER2)->SMCR |= TIMER_SMCR_SMS_ENCODER1 | TIMER_SMCR_SMS_ENCODER2;
timer_set_reload(TIMER2, 256 * 4);
timer_resume(TIMER2);
...
}
...
void loop() {
...
uint16 val = timer_get_count(TIMER2) >> 2;
...
All details are there, I call this example "incomplete" only because it can't be directly copied into IDE and requires modifications before it can be used.
Some comments for the code above:
1. There are no interrupt handlers at all. They can be attached, I guess, but so far I see no need for that.
2. As you might notice, in order to work, this code requires two channels on the same timer. Configuring them in general case might be not very simple, so, perhaps it worth to assume fixed pin layout in the initial version.
3. There is another approach used in one of the Arduino encoder libs - use two external interrupts to handle transitions. Since most pins can serve as interrupt inputs, this might be more convenient for configuration in general case.
Thank you.
Do you know anything about the under overflow interrupts? I see that it generates an update event, but then there isn't much (apart from the DIR bit) that I can see to understand which of them ocurred. :\
Edit:
I missed your post about the interrupts.
The way I was thinking, the interrupts would serve to count the number of turns a given shaft had moved. The Counts in the timer would tell the angle of the shaft at a given moment and with that prescaler (in case of an integer gear ratio) we could shift all this to the outside of a gear (more of a gimmick than actual interesting function).
I'll have a look and set my hardware up to test this.
Thank you so much for your help.
You must log in to post.