@@ -350,6 +350,98 @@ supported ADC resolutions:
350
350
- ``ADC.WIDTH_12BIT `` = 12
351
351
352
352
353
+ Counter (pin pulse/edge counting)
354
+ ---------------------------------
355
+
356
+ The ESP32 provides up to 8 pulse counter peripherals depending on the hardware,
357
+ with id 0..7. These can be configured to count rising and/or falling edges on
358
+ any input pin.
359
+
360
+ Use the :ref: `machine.Counter <machine.Counter >` class::
361
+
362
+ from machine import Pin, Counter
363
+
364
+ counter = Counter(0, pin=Pin(2), rising=Counter.INCREMENT) # create counter
365
+ counter.start() # start counter
366
+ count = counter.value() # read count, -32768..32767
367
+ counter.value(0) # reset counter
368
+ count = counter.value(0) # read and reset
369
+
370
+ ESP32 specific Counter reference:
371
+
372
+ .. class :: Counter(id, *, pin, rising, falling, ...)
373
+
374
+ The constructor and ``init `` method support an additional set of
375
+ keyword arguments over-and-above the basic
376
+ :ref: `machine.Counter <machine.Counter >` API:
377
+
378
+ - ``mode_pin ``: ESP32 pulse counters support monitoring a second pin and
379
+ altering the behaviour of the counter based on its level - set this
380
+ keyword to any input Pin
381
+ - ``mode_low ``: set to either ``Counter.HOLD `` or ``Counter.REVERSE `` to
382
+ either suspend counting or reverse the direction of the counter (i.e.,
383
+ ``Counter.INCREMENT `` behaves as ``Counter.DECREMENT `` and vice versa)
384
+ when ``mode_pin `` is low
385
+ - ``mode_high ``: as ``mode_low `` but for the behaviour when ``mode_pin ``
386
+ is high
387
+ - ``filter ``: set to a value 1..1023, in ticks of the 80MHz clock, to
388
+ enable the pulse width filter
389
+ - ``minimum ``: set to the minimum level of the counter value when
390
+ decrementing (-32768..0)
391
+ - ``maximum ``: set to the maximum level of the counter value when
392
+ decrementing (0..32767)
393
+ - ``threshold0 ``: sets the counter value for the
394
+ ``Counter.IRQ_THRESHOLD0 `` event (see ``irq `` method)
395
+ - ``threshold1 ``: sets the counter value for the
396
+ ``Counter.IRQ_THRESHOLD1 `` event (see ``irq `` method)
397
+ - ``channel ``: see description below
398
+
399
+ Each pulse counter unit supports two channels, 0 and 1. By default, the
400
+ constructor and ``init() `` method configure the 0 channel. Call the
401
+ ``init() `` method with ``channel=1 `` to configure the other channel. Both
402
+ channels update the same counter value, but are able to monitor different
403
+ pins with different edge behaviour. The ``pin ``, ``rising ``, ``falling ``,
404
+ ``mode_pin ``, ``mode_low `` and ``mode_high `` keywords are per-channel, all
405
+ other keyword arguments are per-unit and specifying them will override any
406
+ previous configuration.
407
+
408
+ The second channel can be used to configure 4X quadrature decoding with a
409
+ single counter unit::
410
+
411
+ pin_a = Pin(2, Pin.INPUT, pull=Pin.PULL_UP)
412
+ pin_b = Pin(3, Pin.INPUT, pull=Pin.PULL_UP)
413
+ rotary = Counter(0, pin=pin_a, falling=Counter.INCREMENT, rising=Counter.DECREMENT, mode_pin=pin_b, mode_low=Counter.REVERSE)
414
+ rotary.init(channel=1, pin=pin_b, falling=Counter.DECREMENT, rising=Counter.INCREMENT, mode_pin=pin_a, mode_low=Counter.REVERSE)
415
+ rotary.start()
416
+
417
+ .. method :: Counter.value([value])
418
+
419
+ Call this method with no arguments to return the current counter value or
420
+ pass the value 0 to reset the counter and return the value before reset.
421
+ ESP32 pulse counters do not support being set to any value other than 0.
422
+ Reset and return is not atomic and so it is possible for a pulse to be
423
+ missed.
424
+
425
+ .. method :: Counter.irq(trigger=Counter.IRQ_ZERO, handler=None)
426
+
427
+ ESP32 pulse counters support interrupts on these counter events:
428
+
429
+ - ``Counter.IRQ_ZERO ``: the counter has reset to zero
430
+ - ``Counter.IRQ_MINIMUM ``: the counter has hit the ``minimum `` value
431
+ - ``Counter.IRQ_MAXIMUM ``: the counter has hit the ``maximum `` value
432
+ - ``Counter.IRQ_THRESHOLD0 ``: the counter has hit the ``threshold0 `` value
433
+ - ``Counter.IRQ_THRESHOLD1 ``: the counter has hit the ``threshold1 `` value
434
+
435
+ ``trigger `` should be the desired events or'ed together and ``handler ``
436
+ should be a callable taking a single argument, which will be the Counter
437
+ object. Only one handler can be in place per-unit. Set ``handler `` to
438
+ ``None `` to disable the event interrupt (or call ``irq `` with no arguments).
439
+
440
+ .. Note ::
441
+ ESP32 pulse counters reset to *zero * when reaching the minimum or maximum
442
+ value. Thus the ``IRQ_ZERO `` event will also trigger when either of these
443
+ events occurs.
444
+
353
445
Software SPI bus
354
446
----------------
355
447
0 commit comments