Revision f1582e6c

View differences:

scout_avr/src/range.cpp
2 2
{
3 3
#include <avr/io.h>
4 4
#include <avr/interrupt.h>
5
#include <util/delay.h>
5 6
}
6 7
#include "range.h"
7 8

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

  
......
38 40
  if (int_high)
39 41
  {
40 42
    range[which].start = time;
43
    range[which].started = 1;
41 44
  }
42 45
  else
43 46
  {
44
    PORT_SONAR_TX &= ~ _BV(P_SONAR_TX);
45 47
    // if timer overflowed since start, this arithmetic should still work out
46 48
    range[which].value = time - range[which].start;
47 49
    range[which].done = 1;
......
81 83
  for (i = 0; i < 2; i++)
82 84
  {
83 85
    range[i].value = RANGE_ERR;
86
    range[i].started = 0;
84 87
    range[i].done = 0;
85 88
  }
86 89

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

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

Also available in: Unified diff