-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgas_Sensor.c
115 lines (87 loc) · 2.26 KB
/
gas_Sensor.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#define F_CPU 9600000 // Define software reference clock for delay
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#define LED_SENSOR1 PB2
#define ON 1
#define OFF 0
#define ADC_SENSOR1 0
#define ADC_SENSOR2 1
#define BIT_L(port,pin) port &= ~(1<<pin)
#define BIT_H(port,pin) port |= (1<<pin)
#define PAUSE _delay_ms(10)
#define SOUND PB0
#define NO_SOUND BIT_L(PORTB,SOUND);PAUSE
#define PLAY_SOUND if(ENABLE_SOUND!=OFF){BIT_H(PORTB,SOUND);PAUSE;}NO_SOUND
#define OFF_LEDS BIT_L(PORTB,LED_SENSOR1);
#define ACTIONS BlinkLeds();PlaySound()
volatile int ENABLE_SOUND=OFF;
volatile int POWER=0;
volatile int CURRENT_ADC=0;
volatile int s=0;
volatile int pwm_dcy_led;
void BlinkLeds()
{
BIT_H(PORTB,LED_SENSOR1);
PAUSE;
PAUSE;
OFF_LEDS;
}
void PlaySound()
{
PLAY_SOUND;
}
void SetMUX(int nMUX)
{
if(nMUX==0)
{ADMUX = 0b00000010; /*(ADC2)*/}
else
{ADMUX = 0b00000011;/*(ADC3)*/ }
}
ISR(PCINT0_vect)
{
if(s>0)
{
ENABLE_SOUND^=(1<<0x01);s=0;
PlaySound();
BIT_H(PORTB,SOUND);
PAUSE;
NO_SOUND;
return;
}
s++;
}
void SystemInit(void)
{
DDRB |= ( 1 << LED_SENSOR1 ) | /*( 1 << LED_SENSOR2 ) | */ ( 1 << SOUND );
ADCSRA |= (1<<ADEN)| // Analog - Digital enable bit
(1<<ADPS1)| // set prescalerto 8(clock/8)
(1<<ADPS0); // set prescaler to 8(clock/8)
PCMSK |= (1<<PCINT1); // pin change mask: listen to portb bit 2
GIMSK |= (1<<PCIE); // enable PCINT interrupt
MCUCR = 0b00000010; //(1<<ISC00);
sei(); // enable all interrupts
}
int main (void)
{
SystemInit();
int CURRENT_ADC=ADC_SENSOR1;
for (;;)
{
SetMUX(CURRENT_ADC);
ADCSRA |= (1<<ADEN); // Analog Digital enable bit
ADCSRA |= (1<<ADSC); // Discarte first conversion
while(ADCSRA & (1<<ADSC)) ; // wait until conversion is done
ADCSRA |=(1<<ADSC); // start single conversion
while(ADCSRA &(1<<ADSC)) // wait until conversion is done
ADCSRA &= ~(1<<ADEN); // shutdown the ADC
PAUSE;
if(ADCH > 1){ACTIONS;}
if(CURRENT_ADC==ADC_SENSOR1){
CURRENT_ADC=ADC_SENSOR2;
}else{
CURRENT_ADC=ADC_SENSOR1;
}
}
return 0;
}