Revision ac53b84f

View differences:

scout_avr/src/cliffSensor.cpp
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
#define _PINLEVEL(x) (PIND & (1<<x))
12
unsigned char flag_cliff0=0,flag_cliff1=0,flag_cliff2=0; // are these global variables???
13

  
14
void cliffSensor_init(void)
15
{
16
  sei();   //set the I-bit of SREG to 1, enabling global interrupters
17
	EIMSK |= 0b00000011;  //enable INT0,INT1,INT6
18
	EICRA |= 0b00001111; 
19
	EICRB |= 0b00110000;   //The rising edge of INTn triggers an interrupt request.
20
}
21

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

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

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

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

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

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

  
75
ISR(INT6_vect){
76
	flag_cliff2 = 1;
77
	EIMSK &= ~(1<<INT6); // disable INT6
78
}
scout_avr/src/cliffSensor.h
1
#ifndef _CLIFFSENSOR_H_
2
#define _CLIFFSENSOR_H_
3

  
4
#include <avr/io.h>
5
#include <avr/interrupt.h>
6

  
7
void cliffSensor_init(void);
8
bool read_cliffSensor_front(void);
9
bool read_cliffSensor_left(void);
10
bool read_cliffSensor_right(void);
11

  
12
/* The returned result is in the form of left_front_right; For example, if both left
13
   and front sensors detect a cliff, the returned value is 0b110 */
14
unsigned char read_cliffSensor_all(void);
15

  
16
#endif

Also available in: Unified diff