Project

General

Profile

Statistics
| Branch: | Revision:

root / scout_avr / src / range.cpp @ 1c3c96ce

History | View | Annotate | Download (991 Bytes)

1
#include "range.h"
2

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

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

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

    
26
unsigned int range;
27

    
28
ISR(INT0_vect)
29
{
30
  if (read_INT0)
31
  {
32
    // reset timer
33
    TCNT1 = 0;
34
  }
35
  else
36
  {
37
    range = TCNT1;
38
  }
39
}
40

    
41
void range_init()
42
{
43
  // ISC0 = 1, edge triggered
44
  EICRA |= _BV(ISC00);
45
  // enable INT0
46
  EIMSK |= _BV(INT0);
47
  
48
  // CS1 = 2, 1/8 prescaler
49
  TCCR1B = _BV(CS11);
50
  
51
  range = -1;
52
}
53

    
54
int range_get()
55
{
56
  int ret;
57
  cli();
58
  ret = range;
59
  sei();
60
  return ret;
61
}