8000 Add accurate interrupt counter · Issue #3775 · micropython/micropython · GitHub
[go: up one dir, main page]

Skip to content
Add accurate interrupt counter #3775
Open
@laurivosandi

Description

@laurivosandi

The microcontrollers support counting rising/falling edges at rather fast pace (1-10kHz), eg to measure RPM of a motor with hall sensor or disc encoder. It would be nice to have the interrupt handling implemented in C code and export something easier to use into Python:

from machine import Counter, Pin
from time import sleep
counter = Counter(Pin(34))
while True:
    count, duration = counter.poll()
    print("%d RPM" % (count * 60 / duration))
    sleep(1)

In this pseudo-code instantiating a counter object sets up interrupt handlers and an integer for storing the count of events and another integer for storing current timestamp. When poll() function is executed, count and timestamp delta are returned and variables are reset.

The main benefit of such approach is accuracy and this probably hogs the Python runtime less as well especially at higher frequecies (1kHz and above)

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementFeature requests, new feature implementations

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0