-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.c
297 lines (257 loc) · 8.75 KB
/
main.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
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#include "main.h"
#include "GSM.h"
//#define DEBUG
volatile const char WaterAlarmSMS[] = "ALARM - Der er opdaget vand! Med venlig hilsen, Din Vandalarm";
volatile const char UserSMS[] = "Du er nu registreret som bruger. Med venlig hilsen, Din Vandalarm";
char TimeOut;
char CurrentKeyPadCount = 0;
char i = 0; // Global temporary byte (variable)
void Interrupt() {
if (INTCON.INTF)
{
WaterAlarmFlag = 1;
INTCON = 0b10010000; // Reset Interrupt Flag and Re-enable Interrupt
return;
}
}
void Buzzer_On(void)
{
PORTC.B4 = 1;
}
void Buzzer_Off(void)
{
PORTC.B4 = 0;
}
void Buzzer_Beep(char count)
{
while (count > 0) {
PORTC.B4 = 1;
Delay_ms(100);
PORTC.B4 = 0;
Delay_ms(100);
count--;
}
}
void BeepAndBlink(char count)
{
while (count > 0) {
PORTC.B2 = 1;
PORTC.B4 = 1;
Delay_ms(100);
PORTC.B2 = 0;
PORTC.B4 = 0;
Delay_ms(100);
count--;
}
}
void LED_On(void)
{
PORTC.B2 = 1;
}
void LED_Off(void)
{
PORTC.B2 = 0;
}
void LED_Blink(char count)
{
while (count > 0) {
PORTC.B2 = 1;
Delay_ms(100);
PORTC.B2 = 0;
Delay_ms(100);
count--;
}
}
void KeyPadPulse(void)
{
PORTC.B7 = 1;
Delay_ms(1);
PORTC.B7 = 0;
Delay_ms(1);
}
void KeyPadCountTo(char countToVar)
{
if (CurrentKeyPadCount < countToVar) // Needed count is higher than current count counted to
{
while (CurrentKeyPadCount < countToVar)
{
KeyPadPulse();
CurrentKeyPadCount++;
}
return;
} else if (CurrentKeyPadCount > countToVar) { // Needed count is lower than current count counted to, therefor we have to roll over
while (CurrentKeyPadCount < 9) // Count to max of BCD Chip
{
KeyPadPulse();
CurrentKeyPadCount++;
}
/* Count 1 extra, so it roll overs (becomes 0) */
KeyPadPulse();
CurrentKeyPadCount = 0;
/* Count to the count needed (countToVar) */
while (CurrentKeyPadCount < countToVar)
{
KeyPadPulse();
CurrentKeyPadCount++;
}
return;
}
}
char GetKeyPad(void)
{
KeyPadCountTo(1);
Delay_ms(10);
if (PORTC.B3) return '1';
if (PORTC.B5) return '2';
if (PORTC.B1) return '3';
KeyPadCountTo(2);
Delay_ms(10);
if (PORTC.B3) return '4';
if (PORTC.B5) return '5';
if (PORTC.B1) return '6';
KeyPadCountTo(4);
Delay_ms(10);
if (PORTC.B3) return '7';
if (PORTC.B5) return '8';
if (PORTC.B1) return '9';
KeyPadCountTo(8);
Delay_ms(10);
if (PORTC.B3) return '*';
if (PORTC.B5) return '0';
if (PORTC.B1) return '#';
return 0;
}
void CancelAlarmOnClick(void)
{
/* Always be able to disable alarm by pressing #, even when GSM transmisson is in progress */
if (WaterAlarmFlag != 0) {
if (GetKeyPad() == '#') {
WaterAlarmFlag = 0; // Clear the Alarm Flag, so the LED isn't turned on after GSM transmission has finished
Buzzer_Off(); // Disable Alarm
}
}
}
void main(void)
{
char PressedKey;
OSCCON = 0b01110101; // Set the PIC to use the internal 8MHz clock
TRISA = 0b00000100; // Set RA2 as input, and the rest on PORTA to output
PORTA = 0; // Set all the PORTA outputs to LOW
ANSEL = 0b00000000; // Disable all analog inputs
ANSELH = 0b00000000; // ---------- || -----------
/* GSM Module */
TRISB = 0;
PORTB.B6 = 1; // Set RB1 to HIGH (GSM PWRKEY should be HIGH or toggled)
UART1_Init(9600); // Initialize UART module at 9600 bps
Delay_ms(100); // Wait for UART module to stabilize
TRISC = 0b00101010; // Set RC1, RC3, RC5 to input, and the rest on PORTC to output
PORTC = 0; // Set all the PORTC outputs to LOW
CM1CON0 = 0; // Disable all the comparators on PORTC
OPTION_REG = 0b10000000; // Disable PORTB Internal Pull-Ups, and set RA2 interrupt to Falling Edge
Delay_ms(1500); // Wait for powersupply to counter to stabalize
ReadNumberFromEEProm();
#ifdef DEBUG
UART1_Write_Text_Constant("Current mobile number:");
UART1_Write_Text(MobileNumber);
UART1_Write(13); //Carriage return (new line)
#endif
#ifdef DEBUG
UART1_Write_Text_Constant("Initializing GSM");
UART1_Write(13); //Carriage return (new line)
#endif
GSM_PowerOn();
GSM_Initialize(1);
GSM_PowerOff();
/* Finished initializing GSM - inform the user */
Buzzer_On();
LED_On();
Delay_ms(600);
Buzzer_Off();
LED_Off();
#ifdef DEBUG
UART1_Write_Text_Constant("Finished initializing GSM");
UART1_Write(13); //Carriage return (new line)
#endif
INTCON = 0b10010000; // Enable interrupt
while (1) { // Endless loop
if (WaterAlarmFlag != 0)
{
Buzzer_On(); // Buzzer on
if (CheckNumber()) // Send alarm SMS to the user, if the saved number is valid
SendSMS(WaterAlarmSMS);
/* If WaterAlarmFlag is still set (has not been cleared by pressing # when GSM tranmission is in progress), turn LED On and clear the flag */
if (WaterAlarmFlag != 0) {
LED_On(); // Turn on LED
WaterAlarmFlag = 0;
}
}
PressedKey = GetKeyPad();
if (PressedKey != 0) {
Buzzer_Beep(1);
while (GetKeyPad() != 0); // Wait for release
}
#ifdef DEBUG
if (PressedKey != 0)
UART1_Write(PressedKey);
#endif
if (PressedKey == '*')
{
#ifdef DEBUG
UART1_Write_Text_Constant("* is pressed");
UART1_Write_Text(WriteBuffer);
UART1_Write(13); //Carriage return (new line)
#endif
PressedKey = 0;
while (PressedKey == 0) {
PressedKey = GetKeyPad();
}
Buzzer_Beep(1);
while (GetKeyPad() != 0); // Wait for release
if (PressedKey == '#') {
Buzzer_Beep(1);
while (GetKeyPad() != 0); // Wait for release
INTCON.GIE = 0; // Disable global interrupt
BeepAndBlink(2);
#ifdef DEBUG
UART1_Write_Text_Constant("Setting new number...");
UART1_Write_Text(WriteBuffer);
UART1_Write(13); //Carriage return (new line)
#endif
SetNewNumber();
/* Finished getting number */
if (CheckNumber()) {
SaveNumberToEEProm(); // Save the number in the internal EEProm
/* Inform the user that the number is correct (OK) = One long beep */
Buzzer_On();
LED_On();
Delay_ms(600);
Buzzer_Off();
LED_Off();
/* Send SMS to the new user to indicate the number was correct */
SendSMS(UserSMS);
} else {
ReadNumberFromEEProm(); // Because the number in the RAM is incorrect, read the last saved number into RAM
/* Inform the user that the number is incorrect (WRONG) = Two long beeps */
Buzzer_On();
LED_On();
Delay_ms(600);
Buzzer_Off();
LED_Off();
Delay_ms(200);
Buzzer_On();
LED_On();
Delay_ms(600);
Buzzer_Off();
LED_Off();
}
WaterAlarmFlag = 0;
INTCON.GIE = 1; // Enable global interrupt
}
}
if (PressedKey == '#')
{
LED_Off(); // Turn off LED
Buzzer_Off(); // Buzzer off
}
}
}