Revision 86b48573 scout_avr/src/range.cpp

View differences:

scout_avr/src/range.cpp
14 14
 * 37.5ms * 8 MHz / 8 prescaler = 37500 max wait
15 15
 * 37.5ms * 16 MHz / 8 prescaler = problem
16 16
 * 37.5ms * 16 MHz / 64 prescaler = 9375 max wait
17
 * 147us/in * 16 MHz / 64 prescaler = 1.44685039 ticks / mm
17 18
 */
18 19

  
19 20
struct range_t {
20 21
  unsigned int start; // timer value on rising edge
21 22
  unsigned int value; // last measured range
22
  char started;
23
  char done;
23
  char busy;
24 24
} volatile range[2];
25 25

  
26 26
static void on_edge(int which)
......
40 40
  if (int_high)
41 41
  {
42 42
    range[which].start = time;
43
    range[which].started = 1;
43
    range[which].busy = 1;
44 44
  }
45 45
  else
46 46
  {
47 47
    // if timer overflowed since start, this arithmetic should still work out
48 48
    range[which].value = time - range[which].start;
49
    range[which].done = 1;
49
    range[which].busy = 0;
50 50
  }
51 51
}
52 52

  
......
83 83
  for (i = 0; i < 2; i++)
84 84
  {
85 85
    range[i].value = RANGE_ERR;
86
    range[i].started = 0;
87
    range[i].done = 0;
86
    range[i].busy = 0;
88 87
  }
89 88

  
90 89
  // TODO ensure that one interrupt won't be delayed because of the other
91 90
  PORT_SONAR_TX |= _BV(P_SONAR_TX);
92
  _delay_ms(1);
91
  _delay_ms(40);
93 92
  PORT_SONAR_TX &= ~ _BV(P_SONAR_TX);
94 93

  
95 94
  for (i = 0; i < 2; i++)
96 95
  {
97
    if (range[i].started) {
98
      while (!range[i].done) {}
99
      values[i] = range[i].value;
100
    }
96
    while (range[i].busy) {}
97
    values[i] = range[i].value;
101 98
  }
102 99
}

Also available in: Unified diff