Sharing input capture callbacks

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.
Post Reply
oli
Posts: 17
Joined: Wed Aug 05, 2015 9:13 am

Sharing input capture callbacks

Post by oli » Tue Feb 09, 2016 8:39 pm

hi,

today i wrote a little script to monitor 5 little fans by reading a tacho-signal. this signal is a kind of a square-wave signal with around 350Hz. For reading this signal i am using the input capture mode of the timers.

Because i am using the pins X1-X4 i can use the same timer with different channels which works great. but as far as i knows i have to write the same callback for each channel. is it possible to share one callback and get the channel which triggered the interrupt by the timer object or somehow else?

another question came across my trials: how to use a channel which is labeled like CH1N?

while i was looking for the right timer, i tried first the timer 8 on Y1. i couldn't get the capture mode to work. is there a kind of limitation on this timer?

thanks a lot in advance for your answers!

best regards,
oli

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

Re: Sharing input capture callbacks

Post by dhylands » Tue Feb 09, 2016 10:07 pm

Currently, the timer callback only passes in the timer object and not the timer channel.

You could create small wrapper routines and do something like this:

Code: Select all

def chan1_cb(tim):
    chan_cb(1)

def chan2_cb(tim):
    chan_cb(2)
    
def chan3_cb(tim):
    chan_cb(3)
    
def chan4_cb(tim):
    chan_cb(4)
    
def chan_cb(channel_num):
    print('channel', channel_num)
    # put other common code here

I don't see any reason why input capture shouldn't work on Y1. Do you have your code which was failing?

CH1N is an inverted version of CH1. These are output-only

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

Re: Sharing input capture callbacks

Post by dhylands » Tue Feb 09, 2016 10:15 pm

If you want per-channel specific data, you could also do something like this (written off the top of my head - not actually tested)

Code: Select all

import pyb

class Channel:
    def __init__(self, pin_name, timer, channel):
        self.channel = channel
        timer_channel = timer.channel(channel, pyb.Timer.IC, pin=pyb.Pin(pin_name), callback=self.callback)
        
    def callback(self, tim):
        print('Channel =', self.channel)
        
timer = pyb.Timer(2, prescaler=83, period=0x0fffffff)
ch1 = Channel('X1', timer, 1)
ch2 = Channel('X2', timer, 2)

oli
Posts: 17
Joined: Wed Aug 05, 2015 9:13 am

Re: Sharing input capture callbacks

Post by oli » Thu Feb 11, 2016 12:32 pm

Thank you! That help me. Now it looks better :)
Timer 8 is working also, no clue what went wrong, when i first tested it...

Post Reply