author Keith Packard Fri, 9 May 2014 07:05:39 +0000 (00:05 -0700) committer Keith Packard Fri, 9 May 2014 07:05:39 +0000 (00:05 -0700)
Set the timer to 200Hz for a 5ms debounce interval. Then, simply look
for transitions ending in both bits in the encoder being off, which
indicates the the encoder is resting in a detent. If bit '2' is
turning off, the encoder was rotated clockwise, otherwise the encoder
was rotated counter clockwise.

This is a lot more reliable, although still not perfect.

Signed-off-by: Keith Packard <keithp@keithp.com>

index d07488d08d4f9307b661a0f690f9447dab4fec73..66a77dfaa997b22824805cbbdc4bda9c63b5618d 100644 (file)
#include <ao_event.h>

-
-#define BIT(a,b)       ((a) | ((b) << 1))
-#define STATE(old_a, old_b, new_a, new_b)      (((BIT(old_a, old_b) << 2) | BIT(new_a, new_b)))

#define port(q)        AO_QUADRATURE_ ## q ## _PORT
#define bita(q) AO_QUADRATURE_ ## q ## _A
@@ -54,51 +50,29 @@ _ao_quadrature_queue(uint8_t q, int8_t step)
}

-static const int8_t    step[16] = {
-       [STATE(0,0,0,0)] = 0,
-       [STATE(0,0,0,1)] = -1,
-       [STATE(0,0,1,0)] = 1,
-       [STATE(0,0,1,1)] = 0,
-       [STATE(0,1,0,0)] = 1,
-       [STATE(0,1,1,0)] = 0,
-       [STATE(0,1,1,1)] = -1,
-       [STATE(1,0,0,0)] = -1,
-       [STATE(1,0,0,1)] = 0,
-       [STATE(1,0,1,0)] = 0,
-       [STATE(1,0,1,1)] = 1,
-       [STATE(1,1,0,0)] = 0,
-       [STATE(1,1,0,1)] = 1,
-       [STATE(1,1,1,0)] = -1,
-       [STATE(1,1,1,1)] = 0
-};

static void
-       uint8_t v;
-
-       v = ao_quadrature_state[q] & 3;
-       value = value & 3;

-       if (v == value)
-               return;
-
-       ao_quadrature_state[q] = (v << 2) | value;
-
-       if (value == 0) {
+       if (old != new && new == 0) {
+               if (old & 2)
-               else if (ao_quadrature_raw[q] == -4)
+               else if (old & 1)
}
}

static void
{
+#endif
+#endif
}

int32_t
@@ -120,6 +94,8 @@ static void
{
uint8_t q;
+       int32_t c;
+       uint8_t s;

ao_cmd_decimal();
q = ao_cmd_lex_i;
ao_cmd_status = ao_cmd_syntax_error;
return;
}
-       printf ("count %d state %x raw %d\n",
+
+       c = -10000;
+       s = 0;
+       while (ao_quadrature_count[q] != 10) {
+               if (ao_quadrature_count[q] != c ||
+                       printf ("count %3d state %2x\n", c, s);
+                       flush();
+               }
+       }
#if 0
for (;;) {
int32_t c;
@@ -88,7 +88,11 @@ void stm_tim6_isr(void)
#define TIMER_23467_SCALER 1
#endif

-#define TIMER_10kHz    ((AO_PCLK1 * TIMER_23467_SCALER) / 10000)
+#ifndef FAST_TIMER_FREQ
+#define FAST_TIMER_FREQ        10000
+#endif
+
+#define TIMER_FAST     ((AO_PCLK1 * TIMER_23467_SCALER) / FAST_TIMER_FREQ)

void
ao_fast_timer_init(void)
@@ -100,7 +104,7 @@ ao_fast_timer_init(void)
/* Turn on timer 6 */
stm_rcc.apb1enr |= (1 << STM_RCC_APB1ENR_TIM6EN);

-               stm_tim6.psc = TIMER_10kHz;
+               stm_tim6.psc = TIMER_FAST;
stm_tim6.arr = 9;
stm_tim6.cnt = 0;

index 62f221a1da8979402697e6da3dfbebe94db05bbe..609e549416ba1c2b048865e28f1ff86f91309cfd 100644 (file)
@@ -72,6 +72,8 @@
#define PACKET_HAS_SLAVE       0
#define PACKET_HAS_MASTER      0

+#define FAST_TIMER_FREQ                200     /* 5ms for debouncing */
+
/*
* Radio is a cc1120 connected via SPI
*/