[SOLVED] HardTimerAsEncoder.ino example

Generic boards that are not Maple or Maple mini clones, and don't contain the additional USB reset hardware
Post Reply
Jimbo13
Posts: 19
Joined: Sun Jan 01, 2017 5:10 pm
Location: Essex UK

[SOLVED] HardTimerAsEncoder.ino example

Post by Jimbo13 » Tue Nov 28, 2017 9:25 pm

I cant get the HardTimerAsEncoder.ino to work on my BluePill STM32F103, any suggestions please.

https://github.com/rogerclarkmelbourne/ ... ncoder.ino
With my scope I determined that Physical Pin 14 and 15 were the simulator outputs.
I have connected Physical Pin14 to 12, 15 to 13.

Code: Select all

 *TIMER2 inputs -> Digital Pins used to simulate.
* D2 -> D4
* D3 -> D5 
On my scope I can see Pins 12 and 13 simulate the encoder output but on terminal display I get:
0 counts
direction 0
Full Revs: 0
So it appears that the counter bit isn't working.

Regards
Jim

victor_pv
Posts: 1866
Joined: Mon Apr 27, 2015 12:12 pm

Re: HardTimerAsEncoder.ino example

Post by victor_pv » Tue Nov 28, 2017 10:11 pm

I believe the bluepill doesn't have the pins numbered, only shows the port names (PA1, PA2, PB0, etc)
Those D2, D4 names are from the maple. Pin numbers in the bluepill and the maple/maple mini do not match. Not sure what you are referring to with Phisical Pin 14 and 15, since I believe no version of the bluepill uses only numbers for the pins.
If you are using the DIP package pin numbers, that's confusing, would be better to use PA0, PA1 etc for the rest of us to understand what you are connecting where.
As for the encoder timer function, it needs to use inputs (channels) 1 and 2 from the same timer to work. So the input pins need to be Ch1 and Ch2 of the same timer device.

User avatar
zoomx
Posts: 567
Joined: Mon Apr 27, 2015 2:28 pm
Location: Mt.Etna, Italy

Re: HardTimerAsEncoder.ino example

Post by zoomx » Wed Nov 29, 2017 7:49 am

On a BluePill the connections should be

PA_10 -> PB_5
PB_3 -> PB_4

as you can see here
Image

Jimbo13
Posts: 19
Joined: Sun Jan 01, 2017 5:10 pm
Location: Essex UK

Re: HardTimerAsEncoder.ino example

Post by Jimbo13 » Wed Nov 29, 2017 9:36 am

I am using the pinout as below.
Image
I looked at the Maple pinout but could not correlate the pin D2-D5 so I ran the prog, assuming the pin allocation would sort itself out. I then looked for the simulator output which appeared on Physical Pin (those in light Turquoise on diagram) 14 and 15. I then remmed out the timer setup part of the programme and wagled what would be input pins D2 D3 up and down and using scope I found they were physical pins 12 and 13.
So I connected Physical Pin14 to 12, 15 to 13.

I just tried using PA10 instead of D2 as input in the programme.
PB3 instead of D3 as input
PB5 instead of D4 as output
PB4 instead of D5 as output
I got simulator output on PB5 but not on PB4
I connected PB5 to PA10 then PB3 but got:
1016 counts
direction 0
Full Revs: 4294967295
1016 counts
direction 0
Full Revs: 4294967295
1016 counts
direction 0
Full Revs: 4294967295

I am a bit confused but will investigate the pins later in more detail.

Jim

victor_pv
Posts: 1866
Joined: Mon Apr 27, 2015 12:12 pm

Re: HardTimerAsEncoder.ino example

Post by victor_pv » Wed Nov 29, 2017 4:09 pm

I don't know where the numbers in that diagram come from, but in generic boards the pins to number relation is straight PA0-15, then PB0-15 and so, except not all pins are available in all MCUs. The numbers are not skipped, instead the next consecutive number is used for the next consecutive exiting pin. They are in an enum:
https://github.com/rogerclarkmelbourne/ ... rd/board.h

Since none of them is assigned a value, the compiler will start from 0 for PA0.

To avoid confusion, always use PA0, PA1 etc in your sketches, since 0 in a maple mini corresponds to a different pin than a bluepill, and that may correspond to something else in another board.

About your pin selection, both inputs need to be Ch1 and Ch2 of the same timer.
PA10 and PB3 are different timers, and on top PA10 is Channel 3 of that particular timer.
Correct me if I'm wrong, but according to the reference manual, you need to use inputs 1 and 2.

In the diagram, PA8 and PA9 are Ch1 and 2 of the same timer, try those 2.

Jimbo13
Posts: 19
Joined: Sun Jan 01, 2017 5:10 pm
Location: Essex UK

Re: HardTimerAsEncoder.ino example

Post by Jimbo13 » Wed Nov 29, 2017 10:25 pm

OK victor, that worked.
I also got timer 1 3 and 4 working using:
HardwareTimer timer(1); PA8-9
HardwareTimer timer(3); PA6-7
HardwareTimer timer(4); PB6-7

And learnt a lot in the process.

Thanks

Jim

Jimbo13
Posts: 19
Joined: Sun Jan 01, 2017 5:10 pm
Location: Essex UK

Re: [SOLVED] HardTimerAsEncoder.ino example

Post by Jimbo13 » Fri Dec 01, 2017 5:24 pm

A follow up with some information which may be of interest.
I required a rotary encoder to make a draw wire transducer with a 1 mm resolution. I have a 20 mm dia pulley attached to my rotary encoder, so PI*D = 62.8 mm. So I purchased a Bourns EM14 rotary encoder with 64 PPR which makes each pulse approx 1 mm (corrected by a scale factor).
Referring to the ST "RM0008 Reference manual" ( Page391) in quadrature encoder mode the counter counts up on each transition of the two inputs. That means that the counter actually counts 4 * 64 = 128 PPR, which would have been good in hindsight as I could have used a cheaper 16 PPR encoder.
QuadCounter2.png
QuadCounter2.png (22.15 KiB) Viewed 296 times
So now I need reduce the count value by a factor of four (also because in another application I don't want the 16 bit counter to overflow). In the HardTimerAsEncoder.ino example, the line:
timer.setPrescaleFactor(1);
can be changed to:
timer.setPrescaleFactor(5); //5 because in HardwareTimer.cpp: timer_set_prescaler(this->dev, (uint16)(factor - 1)); make prescaler factor = required +1
to achieve this.
I also had to do a level conversion between the 5 v encoder and the 3.3 v of the STM32 because it wouldnt work at 3.3 v.
http://www.hobbytronics.co.uk/mosfet-vo ... -converter

Jim

victor_pv
Posts: 1866
Joined: Mon Apr 27, 2015 12:12 pm

Re: [SOLVED] HardTimerAsEncoder.ino example

Post by victor_pv » Sat Dec 02, 2017 2:38 pm

You should use timer.setPrescaleFactor(4) to obtain a division by 4.

The reason to substract 1 is because the prescaler settings start with 0:
0 -> divide by 1
1 -> divide by 2
2 -> divide by 3
3 -> divide by 4

It's the way the prescaler register works, documented in the reference manual if you want to find more.
If you use timer.setPrescaleFactor(5) your scale will be off.

Post Reply