Timer Encoder mode

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
ST58
Posts: 16
Joined: Fri Mar 10, 2017 4:56 pm

Timer Encoder mode

Post by ST58 » Mon Nov 13, 2017 5:43 pm

I have a quadrature encoder connected on a PyBoard to X1 and X2. I am trying to set up Timer2 in encoder mode to use with the encoder. I am inputting with REPL - I have tried..
timer = pyb.Timer(2)
pin1 = pyb.Pin(pyb.Pin.board.X1,mode=pyb.Pin.AF_PP,af=pyb.Pin.AF1_TIM2)
pin2 = pyb.Pin(pyb.Pin.board.X2,mode=pyb.Pin.AF_PP,af=pyb.Pin.AF1_TIM2)
ch1 = timer.channel(1,pyb.Timer.ENC_AB)
ch2 = timer.channel(2,pyb.Timer.ENC_AB)
Then move the encoder to generate some pulses and then read the timer counter
timer.counter()
but response is always 0, I have also tried reading the timer channel capture value - but that's 0 as well
Docs say create the timer, define the two inputs as af timer using pin, set the timer channel modes to encoder, read value from timer counter.
I have also tried config of the channels as
ch1 = timer.channel(1,pyb.Timer.ENC_AB,pin=pyb.Pin(pyb.Pin.board.X1,mode=pyb.Pin.AF_PP,af=1))
but this doesn't work either.

Any one know what I am doing wrong?

User avatar
pythoncoder
Posts: 1964
Joined: Fri Jul 18, 2014 8:01 am

Re: Timer Encoder mode

Post by pythoncoder » Tue Nov 14, 2017 6:11 am

See this thread viewtopic.php?f=6&t=1735.
Peter Hinch

ST58
Posts: 16
Joined: Fri Mar 10, 2017 4:56 pm

Re: Timer Encoder mode

Post by ST58 » Tue Nov 14, 2017 8:04 am

Thank you Peter - initialising the gpio's correctly fixed the problem

Post Reply