@@ -35,15 +35,24 @@ void common_hal_rotaryio_incrementalencoder_construct(rotaryio_incrementalencode
35
35
claim_pin (pin_a );
36
36
claim_pin (pin_b );
37
37
38
+ // This configuration counts on all edges of the quadrature signal: Channel 0
39
+ // counts on rising and falling edges of channel A, with the direction set by the
40
+ // polarity of channel B. Channel 1 does likewise, counting edges of channel B according
41
+ // to the polarity of channel A. A little pencil work suffices to show that this
42
+ // counts correctly on all 8 correct quadrature state transitions.
43
+ //
44
+ // These routines also implicitly configure the weak internal pull-ups, as expected
45
+ // in CircuitPython.
46
+
38
47
// Prepare configuration for the PCNT unit
39
48
pcnt_config_t pcnt_config_channel_0 = {
40
49
// Set PCNT input signal and control GPIOs
41
50
.pulse_gpio_num = pin_a -> number ,
42
51
.ctrl_gpio_num = pin_b -> number ,
43
52
.channel = PCNT_CHANNEL_0 ,
44
53
// What to do on the positive / negative edge of pulse input?
45
- .pos_mode = PCNT_COUNT_DEC , // Count up on the positive edge
46
- .neg_mode = PCNT_COUNT_INC , // Keep the counter value on the negative edge
54
+ .pos_mode = PCNT_COUNT_DEC , // Count down on the positive edge
55
+ .neg_mode = PCNT_COUNT_INC , // Count up on negative edge
47
56
// What to do when control input is low or high?
48
57
.lctrl_mode = PCNT_MODE_REVERSE , // Reverse counting direction if low
49
58
.hctrl_mode = PCNT_MODE_KEEP , // Keep the primary counter mode if high
@@ -61,8 +70,8 @@ void common_hal_rotaryio_incrementalencoder_construct(rotaryio_incrementalencode
61
70
.ctrl_gpio_num = pin_a -> number ,
62
71
.channel = PCNT_CHANNEL_1 ,
63
72
// What to do on the positive / negative edge of pulse input?
64
- .pos_mode = PCNT_COUNT_DEC , // Count up on the positive edge
65
- .neg_mode = PCNT_COUNT_INC , // Keep the counter value on the negative edge
73
+ .pos_mode = PCNT_COUNT_DEC , // Count down on the positive edge
74
+ .neg_mode = PCNT_COUNT_INC , // Count up on negative edge
66
75
// What to do when control input is low or high?
67
76
.lctrl_mode = PCNT_MODE_KEEP , // Keep the primary counter mode if low
68
77
.hctrl_mode = PCNT_MODE_REVERSE , // Reverse counting direction if high
0 commit comments