-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSensor_Programs.cpp
More file actions
203 lines (164 loc) · 4.88 KB
/
Sensor_Programs.cpp
File metadata and controls
203 lines (164 loc) · 4.88 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#include "ADC.h"
#include "BIT_MATH.h"
#include "DIO_INTERFACE.h"
#include "STD_TYPES.h"
#include "Sensors_Interface.h"
#include "MOTOR.h"
#include <util/delay.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#define F_CPU 8000000UL
void Sensors_Init()
{
/* configure sensors' pins*/
DIO_u8SetPinDir(FlameSensor_Port,Flame_PIN,DIO_PIN_IN);
DIO_u8SetPinDir(RainSensor_Port,Rain_PIN,DIO_PIN_IN);
DIO_u8SetPinDir(WaterSensor_Port,Water_PIN,DIO_PIN_IN);
DIO_u8SetPinDir(Thermometer_Port,Thermometer_CLOCKPIN,DIO_PIN_IN);
DIO_u8SetPinDir(Thermometer_Port,Thermometer_DATAPIN,DIO_PIN_IN);
DIO_u8SetPinValue(WaterSensor_Port,Water_PIN,1);
ADC_INIT();
INT0_init();
}
u8 Check_Fire()
{
u8 state=0;
DIO_u8GetPinValue(FlameSensor_Port,Flame_PIN,&state);
return state;
}
u8 Check_Rain()
{
u8 state=0;
DIO_u8GetPinValue(RainSensor_Port,Rain_PIN,&state);
return state;
}
u8 Check_Water()
{
u8 state=0;
DIO_u8GetPinValue(WaterSensor_Port,Water_PIN,&state);
return state;
}
u32 GET_Soil_Moisture()
{
s32 Analog_Value=ADC_READ(3);
s32 Soil_Moisture_Value=(u32)(Analog_Value*5000)/1024;
Soil_Moisture_Value /=10;
return Soil_Moisture_Value;
}
void GET_Tempreature(u16*HUM,u16*TEMP)
{
u8 humHigh,humLow,tempHigh,tempLow;
I2C_START(); //send START condition/
I2C_write(0xB8); //send write address of AM2320/
I2C_STOP(); //send STOP condition/
//write function code to point to data register of sensor/
I2C_START(); //send START condition/
I2C_write(0xB8); //send write address of AM2320/
I2C_write(0x03); //send function code: read data reg/
I2C_write(0x00); //send start address of data reg/
I2C_write(0x04); //send number of registers to read/
I2C_STOP(); //send STOP condition/
_delay_ms(2); //wait 2ms/
//read & store humidity & temp bytes from sensor/
I2C_START(); //send START condition/
I2C_write(0xB9); //send read address of AM2320/
//------------------------------------------------------------------
I2C_read(); //read function code (0x30)/
I2C_read(); //read number of registers (4)/
//------------------------------------------------------------------
I2C_read(); //read humidity high byte/
humHigh = TWDR; //copy humidity high byte from TWDR/
I2C_read(); //read humidity low byte/
humLow = TWDR; //copy humidity low byte from TWDR/
//------------------------------------------------------------------
I2C_read(); //read temp high byte/
tempHigh = TWDR; //copy temp high byte from TWDR/
I2C_read(); //read temp low byte/
tempLow = TWDR; //copy temp low byte from TWDR/
//------------------------------------------------------------------
I2C_read(); //read low byte CRC/
I2C_read_NACK(); //read high byte CRC & send NACK pulse/
//------------------------------------------------------------------
I2C_STOP(); //send STOP condition/
//compute 16-bit humidity & temp values//
*HUM = ((humHigh << 8) | humLow)/10; //compute 16-bit humidity/
*TEMP = ((tempHigh << 8) | tempLow)/10; //compute 16-bit temp/
_delay_ms(2000);
}
ISR(INT0_vect) {
// Handle the interrupt triggered by the flame sensor
// For example, you can toggle an LED connected to PB0
FIRE_SYS();
}
// Function to initialize external interrupt INT0
void INT0_init(void) {
// Configure PD2 (INT0) as input
DDRD &= ~(1 << PD2);
// Enable pull-up resistor on PD2
PORTD |= (1 << PD2);
// Enable external interrupt on INT0 with rising edge
EICRA |= (1 << ISC01) | (1 << ISC00);
// Enable INT0 interrupt
EIMSK |= (1 << INT0);
// Enable global interrupts
sei();
}
void FIRE_SYS()
{
while (1)
{
_delay_ms(250);
if (GET_BIT(PIND,2)==0)
{
FIRE_OFF();
break;
}
else
{
FIRE_ON();
}
}
}
void FIRE_ON()
{
BUZZER_alarm();
LED_alarm();
//FIRE_MOTOR_ON();
_delay_ms(250);
}
void FIRE_OFF()
{
BUZZER_alarm_off();
//FIRE_MOTOR_OFF();
LED_alarm_off();
}
void BUZZER_alarm()
{
SET_BIT(BUZZER_DDR, BUZZER_PIN);
TOGGLE_BIT(BUZZER_PORT,BUZZER_PIN);
}
void BUZZER_alarm_off()
{
CLEAR_BIT(BUZZER_PORT,BUZZER_PIN);
}
void LED_alarm()
{
SET_BIT(LED_DDR, LED_PIN);
TOGGLE_BIT(LED_PORT,LED_PIN);
}
void LED_alarm_off()
{
CLEAR_BIT(LED_PORT,LED_PIN);
}
void FIRE_MOTOR_ON()
{
SET_BIT(MOTOR_DDR, NEG_PIN);
SET_BIT(MOTOR_DDR, POS_PIN);
SET_BIT(MOTOR_PORT,POS_PIN);
CLEAR_BIT(MOTOR_PORT,NEG_PIN);
}
void FIRE_MOTOR_OFF()
{
CLEAR_BIT(MOTOR_PORT,NEG_PIN);
CLEAR_BIT(MOTOR_PORT,POS_PIN);
}