Project

General

Profile

Revision ec9e417d

IDec9e417da9689f8ed4cf566110abcaf8f462f876

Added by Thomas Mullins about 12 years ago

Added second range sensor to scout_avr

Not yet tested.

View differences:

scout_avr/src/range.cpp
17 17
// so that we can test on a 328
18 18
#if defined(__AVR_ATmega128RFA1__)
19 19
#  define read_INT0 (PIND & _BV(PD0))
20
#  define read_INT1 (PIND & _BV(PD1))
20 21
#elif defined(__AVR_ATmega328P__) || defined(__AVR_ATmega328__)
21 22
#  define read_INT0 (PIND & _BV(PD2))
23
#  define read_INT1 (PIND & _BV(PD3))
22 24
#else
23
#  error "Please define read_INT0 for this device"
25
#  error "Please define read_INTx for this device"
24 26
#endif
25 27

  
28
struct range_t {
29
  unsigned int start; // timer value on rising edge
30
  unsigned int value; // last measured range
31
} range[2];
32

  
26 33
unsigned int range;
27 34

  
28
ISR(INT0_vect)
35
static void on_edge(int which)
29 36
{
37
  unsigned int time = TCNT1;
30 38
  if (read_INT0)
31 39
  {
32
    // reset timer
33
    TCNT1 = 0;
40
    range[which].start = time;
34 41
  }
35 42
  else
36 43
  {
37
    range = TCNT1;
44
    // if timer overflowed since start, this arithmetic should still work out
45
    range[which].value = time - range[which].start;
38 46
  }
39 47
}
40 48

  
49
ISR(INT0_vect)
50
{
51
  on_edge(0);
52
}
53

  
54
ISR(INT1_vect)
55
{
56
  on_edge(1);
57
}
58

  
41 59
void range_init()
42 60
{
43
  // ISC0 = 1, edge triggered
44
  EICRA |= _BV(ISC00);
45
  // enable INT0
46
  EIMSK |= _BV(INT0);
61
  // ISCx = 1, edge triggered
62
  EICRA |= _BV(ISC10) | _BV(ISC00);
63
  // enable INT0 and INT1
64
  EIMSK |= _BV(INT1) | _BV(INT0);
47 65
  
48 66
  // CS1 = 2, 1/8 prescaler
49 67
  TCCR1B = _BV(CS11);
......
51 69
  range = -1;
52 70
}
53 71

  
54
int range_get()
72
unsigned int range_get(int which)
55 73
{
56
  int ret;
57
  cli();
58
  ret = range;
59
  sei();
60
  return ret;
74
  unsigned int ret;
75
  if (0 <= which && which <= 1)
76
  {
77
    cli();
78
    ret = range[which].value;
79
    sei();
80
    return ret;
81
  }
82
  else return 0xFFFF;
61 83
}

Also available in: Unified diff