summaryrefslogtreecommitdiff
path: root/n/asserv/src/counter.c
diff options
context:
space:
mode:
authorschodet2005-04-27 18:37:57 +0000
committerschodet2005-04-27 18:37:57 +0000
commitf0ccf37dce1373d0e3ca70a68772de98ed67d995 (patch)
tree5a15b67f1486043eb57bc06511596f2d70c95a41 /n/asserv/src/counter.c
parent62c6ae5cd97f7fa85709eba90a01a7cddd6685a5 (diff)
Modif des compteurs.
Modif de Taz.
Diffstat (limited to 'n/asserv/src/counter.c')
-rw-r--r--n/asserv/src/counter.c79
1 files changed, 52 insertions, 27 deletions
diff --git a/n/asserv/src/counter.c b/n/asserv/src/counter.c
index 774a9c8..9f8a5d5 100644
--- a/n/asserv/src/counter.c
+++ b/n/asserv/src/counter.c
@@ -45,10 +45,6 @@ static int16_t counter_left_diff, counter_right_diff;
static inline void
counter_init (void);
-/** Poll counters, should be called as often as possible. */
-static inline void
-counter_poll (void);
-
/** Update overall counter values and compute diffs. */
static inline void
counter_update (void);
@@ -75,45 +71,44 @@ counter_init (void)
0, 0, 0, 0, 0, 1, 1, 1);
/* Begin with safe values. */
counter_restart ();
+ /* Interrupts for direction. */
+ EICRB = 0x05;
+ EIFR = _BV (4) | _BV (5);
+ EIMSK = _BV (4) | _BV (5);
}
-/** Poll counters, should be called as often as possible. */
-static inline void
-counter_poll (void)
+/** Left direction change. */
+SIGNAL (SIG_INTERRUPT4)
{
uint8_t c;
- /* Read left counter. */
c = TCNT2;
-#if COUNTER_REVERSE_LEFT == 0
if (PINE & _BV (4))
-#else
- if (!(PINE & _BV (4)))
-#endif
{
- PORTD &= ~0x40;
- counter_left_frw += c - counter_left_old;
+ counter_left_rev += c - counter_left_old;
+ PORTD |= 0x40;
}
else
{
- PORTD |= 0x40;
- counter_left_rev += c - counter_left_old;
+ counter_left_frw += c - counter_left_old;
+ PORTD &= ~0x40;
}
counter_left_old = c;
- /* Read right counter. */
+}
+
+/** Right direction change. */
+SIGNAL (SIG_INTERRUPT5)
+{
+ uint8_t c;
c = TCNT3L;
-#if COUNTER_REVERSE_RIGHT == 0
if (PINE & _BV (5))
-#else
- if (!(PINE & _BV (5)))
-#endif
{
- PORTD &= ~0x20;
- counter_right_frw += c - counter_right_old;
+ counter_right_rev += c - counter_right_old;
+ PORTD |= 0x20;
}
else
{
- PORTD |= 0x20;
- counter_right_rev += c - counter_right_old;
+ counter_right_frw += c - counter_right_old;
+ PORTD &= ~0x20;
}
counter_right_old = c;
}
@@ -122,16 +117,46 @@ counter_poll (void)
static inline void
counter_update (void)
{
+ uint8_t c;
+ /* Disable ints. */
+ EIMSK &= ~(_BV (4) | _BV (5));
+ /* Read left counter. */
+ c = TCNT2;
+ if (PINE & _BV (4))
+ counter_left_frw += c - counter_left_old;
+ else
+ counter_left_rev += c - counter_left_old;
+ counter_left_old = c;
+ /* Read right counter. */
+ c = TCNT3L;
+ if (PINE & _BV (5))
+ counter_right_frw += c - counter_right_old;
+ else
+ counter_right_rev += c - counter_right_old;
+ counter_right_old = c;
+ /* Update counter values and diffs. */
+#if COUNTER_REVERSE_LEFT == 0
counter_left_diff = counter_left_frw;
- counter_left_frw = 0;
counter_left_diff -= counter_left_rev;
+#else
+ counter_left_diff = counter_left_rev;
+ counter_left_diff -= counter_left_frw;
+#endif
+ counter_left_frw = 0;
counter_left_rev = 0;
counter_left += counter_left_diff;
+#if COUNTER_REVERSE_RIGHT == 0
counter_right_diff = counter_right_frw;
- counter_right_frw = 0;
counter_right_diff -= counter_right_rev;
+#else
+ counter_right_diff = counter_right_rev;
+ counter_right_diff -= counter_right_frw;
+#endif
+ counter_right_frw = 0;
counter_right_rev = 0;
counter_right += counter_right_diff;
+ /* Enable ints. */
+ EIMSK |= _BV (4) | _BV (5);
}
/** Restart counting. */