|
| 1 | +.. currentmodule:: machine |
| 2 | +.. _machine.Counter: |
| 3 | + |
| 4 | +class Counter-- Pulse Counter |
| 5 | +============================= |
| 6 | + |
| 7 | +This class provides access to hardware-supported pulse counting. |
| 8 | + |
| 9 | +It is currently provided for ports: |
| 10 | + |
| 11 | + * :ref:`ESP32 <esp32_machine.Counter>` |
| 12 | + * :ref:`MIMXRT <mimxrt_machine.Counter>` |
| 13 | + |
| 14 | +Minimal example usage:: |
| 15 | + |
| 16 | + from machine import Pin, Counter |
| 17 | + |
| 18 | + counter = Counter(0, src=Pin(0, mode=Pin.IN)) # create Counter object and start to count input pulses |
| 19 | + counter.init(filter_ns=1000) # switch source filtering on |
| 20 | + value = counter.value(0) # get current Counter value, set counter to 0 |
| 21 | + counter.deinit() # turn off the Counter |
| 22 | + |
| 23 | + print(counter) # show the Counter object properties |
| 24 | + |
| 25 | +Constructor |
| 26 | +----------- |
| 27 | + |
| 28 | +.. class:: Counter(id, src=None, \*, direction=Counter.UP, filter_ns=0) |
| 29 | + |
| 30 | + - *id*. Values of *id* depend on a particular port and its hardware. |
| 31 | + Values 0, 1, etc. are commonly used to select hardware block #0, #1, etc. |
| 32 | + |
| 33 | + - *src*. The Counter pulses input pin, which is usually a |
| 34 | + :ref:`machine.Pin <machine.Pin>` object, but a port may allow other values, |
| 35 | + like integers or strings, which designate a Pin in the *machine.Pin* class. |
| 36 | + It may be omitted on ports that have a predefined pin for *id*-specified hardware block. |
| 37 | + |
| 38 | + - *direction* specifies the direction to count. Values for this include the constants |
| 39 | + ``Counter.UP`` (default value) and ``Counter.DOWN``. Ports may support additional values or |
| 40 | + objects, such as a :ref:`machine.Pin <machine.Pin>` object to control the direction externally. |
| 41 | + |
| 42 | + - *filter_ns* specifies a minimum period of time in nanoseconds that the source signal needs to |
| 43 | + be stable for a pulse to be counted. Implementations should use the longest filter supported |
| 44 | + by the hardware that is less than or equal to this value. The default is 0 – no filter. |
| 45 | + |
| 46 | +Methods |
| 47 | +------- |
| 48 | + |
| 49 | +.. method:: Counter.init(*, src, ...) |
| 50 | + |
| 51 | + Modify the settings of the Counter object. See the **Constructor** for details about the parameters. |
| 52 | + |
| 53 | +.. method:: Counter.deinit() |
| 54 | + |
| 55 | + Stops the Counter, disables interrupts and releases hardware resources used by the counter. |
| 56 | + A Soft Reset involve deinitializing all Encoder objects. |
| 57 | + |
| 58 | +.. method:: Counter.value([value]) |
| 59 | + |
| 60 | + Get, and optionally set, the counter value as a signed integer. Implementations should aim to do the get and set atomically. |
| 61 | + |
| 62 | +Constants |
| 63 | +--------- |
| 64 | + |
| 65 | +.. data:: Counter.UP |
| 66 | + Counter.DOWN |
| 67 | + |
| 68 | + Select the counter direction. |
0 commit comments