Project

General

Profile

Statistics
| Revision:

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

History | View | Annotate | Download (927 Bytes)

1
#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
void encoders_read (int16_t *values)
52
{
53
        usb_puts ("# Reading encoder values");
54
  SYNC
55
  {
56
        *values = count_l;
57
  *(values+1) = count_r;
58
  }
59
}
60