Statistics
| Branch: | Revision:

root / scout_avr / src / cliffSensor.cpp @ 18db4dfa

History | View | Annotate | Download (1.64 KB)

1
/*
2
* cliffSensor.cpp
3
*
4
* Created: 2012-9-24 17:38:59
5
*  Author: Anson
6
*/
7

    
8
#include "cliffSensor.h"
9
#include <avr/io.h>
10
#include <avr/interrupt.h>
11
unsigned char flag_cliff0=0,flag_cliff1=0,flag_cliff2=0; // are these global variables???
12

    
13
void cliffSensor_init(void)
14
{
15
  sei();   //set the I-bit of SREG to 1, enabling global interrupters
16
        EIMSK |= 0b01000011; //enable INT0,INT1,INT6
17
        EICRA |= 0b00001111; 
18
        EICRB |= 0b00110000; //The rising edge of INTn triggers an interrupt request.
19
  DDRD &= ~(0b00000011);
20
  DDRE &= ~(0b01000000); // set PD0, PD1 and PE6 as input pins.
21
}
22

    
23
bool read_cliffSensor_front(void)
24
{
25
        if(flag_cliff0 || (PIND & (1<<PD0))) //either a rising edge or a high logic level will be determined as cliff
26
        {
27
                flag_cliff0 = 0; // reset the flag
28
                EIMSK |= (1<<INT0); // re-enable the interrupt
29
                return true;
30
        }
31
        return false;
32
}
33

    
34
bool read_cliffSensor_left(void)
35
{
36
        if(flag_cliff1 || (PIND & (1<<PD1)))
37
        {
38
                flag_cliff1 = 0;
39
                EIMSK |= (1<<INT1);
40
                return true;
41
        }
42
        return false;
43
}
44

    
45
bool read_cliffSensor_right(void)
46
{
47
        if(flag_cliff2 || (PINE & (1<<PE6)))
48
        {
49
                flag_cliff2 = 0;
50
                EIMSK |= (1<<INT6); // enable INT6
51
                return true;
52
        }
53
        return false;
54
}
55

    
56
unsigned char read_cliffSensor_all(void) // return a 3-digit binary number(left, front, right)
57
{
58
        unsigned char result=0;
59
        if(read_cliffSensor_left()){result |= 0b100;}
60
        if(read_cliffSensor_front()){result |= 0b010;}
61
        if(read_cliffSensor_right()){result |= 0b001;}
62
        return result;
63
}
64

    
65
ISR(INT0_vect)
66
{
67
        flag_cliff0 = 1;
68
        EIMSK &= ~(1<<INT0); // disable INT0
69
}
70

    
71
ISR(INT1_vect){
72
        flag_cliff1 = 1;
73
        EIMSK &= ~(1<<INT1); // disable INT1
74
}
75

    
76
ISR(INT6_vect){
77
        flag_cliff2 = 1;
78
        EIMSK &= ~(1<<INT6); // disable INT6
79
}