Revision cc9ca04e scout_avr/src/range.cpp

View differences:

scout_avr/src/range.cpp
1
#include "range.h"
2

  
3 1
extern "C"
4 2
{
5 3
#include <avr/io.h>
6 4
#include <avr/interrupt.h>
7 5
}
6
#include "range.h"
8 7

  
9 8
/* Ultrasonic Sensor:
10 9
 * -if RX pin is left open, it will continuously take readings
......
12 11
 * -PW will be high for a maximum of 37.5ms if no target is detected
13 12
 * 
14 13
 * 37.5ms * 8 MHz / 8 prescaler = 37500 max wait
14
 * 37.5ms * 16 MHz / 8 prescaler = problem
15 15
 */
16 16

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

  
28 17
struct range_t {
29 18
  unsigned int start; // timer value on rising edge
30 19
  unsigned int value; // last measured range
......
33 22
static void on_edge(int which)
34 23
{
35 24
  unsigned char int_high;
36
  // TODO centralize timer 5
37
  // TODO ensure this is in microseconds even for 16MHz
38 25
  unsigned int time = TCNT5;
39 26
  
40 27
  if (which)
41 28
  {
42
    int_high = read_INT1;
29
    int_high = PIN_SONAR_PWM & _BV(P_SONAR_PWM1);
43 30
  }
44 31
  else
45 32
  {
46
    int_high = read_INT0;
33
    int_high = PIN_SONAR_PWM & _BV(P_SONAR_PWM0);
47 34
  }
48 35
  
49 36
  if (int_high)
......
57 44
  }
58 45
}
59 46

  
60
ISR(INT0_vect)
47
ISR(INT3_vect)
61 48
{
62 49
  on_edge(0);
63 50
}
64 51

  
65
ISR(INT1_vect)
52
ISR(INT2_vect)
66 53
{
67 54
  on_edge(1);
68 55
}
......
70 57
void range_init()
71 58
{
72 59
  // ISCx = 1, edge triggered
73
  EICRA |= _BV(ISC10) | _BV(ISC00);
74
  // enable INT0 and INT1
75
  EIMSK |= _BV(INT1) | _BV(INT0);
60
  EICRA |= _BV(ISC20) | _BV(ISC30);
61
  // enable INT2 and INT3
62
  EIMSK |= _BV(INT2) | _BV(INT3);
76 63
  
77 64
  // CS1 = 2, 1/8 prescaler
78 65
  // if this is changed, remember to change recv_edge in bom.cpp!

Also available in: Unified diff