How does timer encoder mode work?

The official pyboard running MicroPython.
This is the reference design and main target board for MicroPython.
You can buy one at the store.
Target audience: Users with a pyboard.
Jim.S
Posts: 84
Joined: Fri Dec 18, 2015 7:36 pm

How does timer encoder mode work?

Post by Jim.S » Sun Apr 03, 2016 12:00 pm

I don't understand how timer encoder mode works (i.e setting the channel mode to Timer.Enc_A, Enc_B or Enc_AB)

I'm assuming that when it is in this mode, the timer hardware will take care of the pulse counting, counting up or down depending on the phase of the A and B signals from the quadrature encoder, but I'm now not sure of my assumption. I have written a simple program that generates quadrature signals on pins X1 and X2, I connect these to the alternate function pins on timer 2, i.e X4 and X3 and use the configuration shown below, but I can not get the timer to count. Ive tried different combinations of modes on the timer channels but I'm now clutching at straws. Has anyone a simple working example?

Code: Select all

    
    timer = pyb.Timer(2)
    timer_ch1=timer.channel(1,pyb.Timer.ENC_A)
    timer_ch2=timer.channel(2,pyb.Timer.ENC_B)
    
    pin_x4=pyb.Pin('X4')
    pin_x4.init(pyb.Pin.IN,af=pyb.Pin.AF1_TIM2)
    pin_x3=pyb.Pin('X3')
    pin_x3.init(pyb.Pin.IN,af=pyb.Pin.AF1_TIM2)
    

User avatar
pythoncoder
Posts: 4151
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: How does timer encoder mode work?

Post by pythoncoder » Sun Apr 03, 2016 3:38 pm

Peter Hinch

Jim.S
Posts: 84
Joined: Fri Dec 18, 2015 7:36 pm

Re: How does timer encoder mode work?

Post by Jim.S » Sun Apr 03, 2016 5:17 pm

Nearly. So my assumption about the operation is correct :) but I can not get encoder3.py example to count :( see [1] below
I'm running the latest firmware on a PYBLite i.e.
MicroPython v1.6-340-ga0cb4ed on 2016-04-02; PYBLITEv1.0 with STM32F411RE
I'm confident that I have X1 connected to X3 and X2 connected to X4 because I have run a simple test from the command line see [2] below

Any ideas on where to look next? Has anything changed in the timer configuration since the example was written?



[1]

Code: Select all

Writing X4 0 X3 0
Counter = 0
Writing X4 0 X3 1
Counter = 0
Writing X4 1 X3 1
Counter = 0
Writing X4 1 X3 0
Counter = 0
Writing X4 0 X3 0
Counter = 0
Writing X4 0 X3 1
Counter = 0
Writing X4 1 X3 1
Counter = 0
Writing X4 1 X3 0
Counter = 0
Writing X4 0 X3 0
Counter = 0
Writing X4 0 X3 1
Counter = 0
[2]

Code: Select all

>>> x1=pyb.Pin('X1', pyb.Pin.IN)
>>> x2=pyb.Pin('X2', pyb.Pin.IN)
>>> x3=pyb.Pin('X3',pyb.Pin.OUT_PP)
>>> x4=pyb.Pin('X4',pyb.Pin.OUT_PP)
>>> x1.value()
0
>>> x2.value()
0
>>> x3.high()
>>> x1.value()
1
>>> x4.high()
>>> x2.value()
1
>>> x4.low()
>>> x4.low()
>>> x2.value()
0

User avatar
dhylands
Posts: 3351
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: How does timer encoder mode work?

Post by dhylands » Sun Apr 03, 2016 7:13 pm

Hmm.

I just tried it on my regular pyboard and it works fine:

Code: Select all

>>> import encoder3
Writing X4 0 X3 0
Counter = 0
Writing X4 0 X3 1
Counter = 0
Writing X4 1 X3 1
Counter = 1
Writing X4 1 X3 0
Counter = 1
Writing X4 0 X3 0
Counter = 2
Writing X4 0 X3 1
Counter = 2
Writing X4 1 X3 1
Counter = 3
Writing X4 1 X3 0
Counter = 3
Writing X4 0 X3 0
Counter = 4
Writing X4 0 X3 1
Counter = 4
Writing X4 1 X3 1
Counter = 5
Writing X4 1 X3 0
Counter = 5
Writing X4 0 X3 0
Counter = 6
Writing X4 1 X3 0
Counter = 6
Writing X4 1 X3 1
Counter = 5
Writing X4 0 X3 1
Counter = 5
Writing X4 0 X3 0
Counter = 4
Writing X4 1 X3 0
Counter = 4
Writing X4 1 X3 1
Counter = 3
Writing X4 0 X3 1
Counter = 3
Writing X4 0 X3 0
Counter = 2
Writing X4 1 X3 0
Counter = 2
Writing X4 1 X3 1
Counter = 1
Writing X4 0 X3 1
Counter = 1
Writing X4 0 X3 0
Counter = 0
Writing X4 1 X3 0
Counter = 0
Writing X4 1 X3 1
Counter = 100000
Writing X4 0 X3 1
Counter = 100000
Writing X4 0 X3 0
Counter = 99999
Writing X4 1 X3 0
Counter = 99999
Writing X4 1 X3 1
Counter = 99998
Writing X4 0 X3 1
Counter = 99998
Writing X4 0 X3 0
Counter = 99997
Writing X4 1 X3 0
Counter = 99997
Writing X4 1 X3 1
Counter = 99996
Writing X4 0 X3 1
Counter = 99996
Writing X4 0 X3 0
Counter = 99995
Writing X4 0 X3 1
Counter = 99995
Writing X4 1 X3 1
Counter = 99996
Writing X4 1 X3 0
Counter = 99996
Writing X4 0 X3 0
Counter = 99997
Writing X4 0 X3 1
Counter = 99997
Writing X4 1 X3 1
Counter = 99998
Writing X4 1 X3 0
Counter = 99998
Writing X4 0 X3 0
Counter = 99999
Writing X4 0 X3 1
Counter = 99999
Writing X4 1 X3 1
Counter = 100000
Writing X4 1 X3 0
Counter = 100000
Writing X4 0 X3 0
Counter = 0
so I'm going to guess it has something to do with the pyblite. My pyblite still hasn't arrived, so I have nothing to test with.

User avatar
dhylands
Posts: 3351
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: How does timer encoder mode work?

Post by dhylands » Sun Apr 03, 2016 7:26 pm

The pyboard has this in the pins.csv file:

Code: Select all

X1,PA0
X2,PA1
X3,PA2
X4,PA3
where the pyblite has:

Code: Select all

X1,PA2
X2,PA3
X3,PA0
X4,PA1
What this means is that on the pyboard, TIM2_CH1 is on pin X1, where on the pyblite, TIM2_CH1 is on X3.

So to make encoder3.py work on the lite, you'll need to swap X1 and X3 everywhere thoughout the code, and also swap X2 and X4.

Jim.S
Posts: 84
Joined: Fri Dec 18, 2015 7:36 pm

Re: How does timer encoder mode work?[solved]

Post by Jim.S » Sun Apr 03, 2016 8:45 pm

Yes, I can confirm that swapping X1<->X3 and X2<->X4 solves the problem and timer 2 counts up and down perfectly. Thanks

Code: Select all

PYB: sync filesystems
PYB: soft reboot
Writing X2 0 X1 0
Counter = 0
Writing X2 0 X1 1
Counter = 0
Writing X2 1 X1 1
Counter = 1
Writing X2 1 X1 0
Counter = 1
Writing X2 0 X1 0
Counter = 2
Writing X2 0 X1 1
Counter = 2
Writing X2 1 X1 1
Counter = 3
Writing X2 1 X1 0
Counter = 3
Writing X2 0 X1 0
Counter = 4
Writing X2 0 X1 1
Counter = 4
Writing X2 1 X1 1
Counter = 5
Writing X2 1 X1 0
Counter = 5
Writing X2 0 X1 0
Counter = 6
Writing X2 1 X1 0
Counter = 6
Writing X2 1 X1 1
Counter = 5
Writing X2 0 X1 1
Counter = 5
Writing X2 0 X1 0
Counter = 4
Writing X2 1 X1 0
Counter = 4
Writing X2 1 X1 1
Counter = 3
Writing X2 0 X1 1
Counter = 3
Writing X2 0 X1 0
Counter = 2
Writing X2 1 X1 0
Counter = 2
Writing X2 1 X1 1
Counter = 1
Writing X2 0 X1 1
Counter = 1
Writing X2 0 X1 0
Counter = 0
Writing X2 1 X1 0
Counter = 0
Writing X2 1 X1 1
Counter = 100000
Writing X2 0 X1 1
Counter = 100000
Writing X2 0 X1 0
Counter = 99999
Writing X2 1 X1 0
Counter = 99999
Writing X2 1 X1 1
Counter = 99998
Writing X2 0 X1 1
Counter = 99998
Writing X2 0 X1 0
Counter = 99997
Writing X2 1 X1 0
Counter = 99997
Writing X2 1 X1 1
Counter = 99996
Writing X2 0 X1 1
Counter = 99996
Writing X2 0 X1 0
Counter = 99995
Writing X2 0 X1 1
Counter = 99995
Writing X2 1 X1 1
Counter = 99996
Writing X2 1 X1 0
Counter = 99996
Writing X2 0 X1 0
Counter = 99997
Writing X2 0 X1 1
Counter = 99997
Writing X2 1 X1 1
Counter = 99998
Writing X2 1 X1 0
Counter = 99998
Writing X2 0 X1 0
Counter = 99999
Writing X2 0 X1 1
Counter = 99999
Writing X2 1 X1 1
Counter = 100000
Writing X2 1 X1 0
Counter = 100000
Writing X2 0 X1 0
Counter = 0

cadomanis
Posts: 1
Joined: Wed Jun 28, 2017 8:47 pm

Re: How does timer encoder mode work?

Post by cadomanis » Wed Jun 28, 2017 8:49 pm

I know this is an older post but this is the only resource I have found for using the encoder mode of the timers. Anyone know if it is possible to use this code/technique on an ESP8266?

Thanks, Chuck

User avatar
deshipu
Posts: 1361
Joined: Thu May 28, 2015 5:54 pm

Re: How does timer encoder mode work?

Post by deshipu » Fri Jun 30, 2017 8:53 am

ESP8266 doesn't support encoders in hardarwe in its timers.

User avatar
pythoncoder
Posts: 4151
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: How does timer encoder mode work?

Post by pythoncoder » Sat Jul 01, 2017 5:14 am

You could look at this https://github.com/peterhinch/micropyth ... encoder.py. This was written for the Pyboard before encoder mode was supported in MicroPython. It will need to be changed to reflect the ESP8266 interrupt mechanism and to replace pyb with machine.
Peter Hinch

evidlo
Posts: 5
Joined: Tue Jul 04, 2017 5:47 pm

Re: How does timer encoder mode work?

Post by evidlo » Tue Jul 04, 2017 5:52 pm

I modified the code as pythoncoder said and tested successfully with a quadrature encoder.

You can find the code here https://github.com/Evidlo/periscope/blo ... encoder.py

Use it like so:

Code: Select all

from machine import Pin
from encoder import Encoder

pin_x = Pin(4, Pin.IN)
pin_y = Pin(5, Pin.IN)
enc = Encoder(pin_x, pin_y, False 1)
print(enc.position)
Last edited by evidlo on Sat Jul 08, 2017 10:09 pm, edited 1 time in total.

Post Reply