Project

General

Profile

Statistics
| Revision:

root / trunk / code / projects / diagnostic_station / station / hardware_encoders.c @ 1214

History | View | Annotate | Download (894 Bytes)

1 1183 deffi
#include "hardware_encoders.h"
2
3
volatile int16_t count_l=0, count_r=0;
4
5
6
void encoders_init ()
7
{
8
  //enable pin change interrupts 4 and 5 (correspond to DIO2 and DIO3)
9
  //to trigger on both edges
10
  cli();
11
  EICRB = _BV(ISC40) | _BV(ISC50);
12
        EIMSK = _BV(INT4) | _BV(INT5);
13
  sei();
14
15
  //enable the 5V switching regulator to power the encoders
16
  DDRB |= _BV(PB4);
17
  PORTB &= ~_BV(PB4);
18
19
}
20
21
void encoders_reset (void)
22
{
23
  SYNC
24
  {
25
    count_l = 0;
26
    count_r = 0;
27
  }
28
}
29
30
ISR(INT4_vect)
31
{
32
  uint8_t temp = PINE;
33
34
  if(((temp&_BV(PE4))>>2)^(temp&_BV(PE2)))
35
    count_l--;
36
  else
37
    count_l++;
38
}
39
40
ISR(INT5_vect)
41
{
42
  uint8_t temp = PINE;
43
44
  if(((temp&_BV(PE5))>>2)^(temp&_BV(PE3)))
45
    count_r++;
46
  else
47
    count_r--;
48
}
49
50
51 1214 deffi
void encoders_read (int16_t *left, int16_t *right)
52 1183 deffi
{
53
  SYNC
54
  {
55 1214 deffi
        *left= count_l;
56
    *right = count_r;
57 1183 deffi
  }
58
}