This repository has been archived by the owner on Jun 3, 2020. It is now read-only.
/
Main.c
391 lines (318 loc) · 8.95 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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/*
* File: Main_Program.c
* Author: Natawat "Minin0la" Hebnak
*
* Created on 27 July, 2016, 9:31 AM
*/
#include <xc.h>
#include "lcd.h"
#include "delays.h"
#include "keypad.h"
unsigned char hour, minute, second, hourA, minuteA, hourB, minuteB, hourC, minuteC;
int AlarmAOff, AlarmBOff, AlarmC2Off, AlarmCOff;
char int_2_char (unsigned char int1)
{
char char1;
switch (int1)
{
case 0 : char1 = '0' ; break;
case 1 : char1 = '1' ; break;
case 2 : char1 = '2' ; break;
case 3 : char1 = '3' ; break;
case 4 : char1 = '4' ; break;
case 5 : char1 = '5' ; break;
case 6 : char1 = '6' ; break;
case 7 : char1 = '7' ; break;
case 8 : char1 = '8' ; break;
case 9 : char1 = '9' ; break;
default : char1 = '?' ;
}
return(char1);
}
unsigned char char_2_int (char char1)
{
unsigned char int1;
switch (char1)
{
case '0' : int1 = 0 ; break;
case '1' : int1 = 1 ; break;
case '2' : int1 = 2 ; break;
case '3' : int1 = 3 ; break;
case '4' : int1 = 4 ; break;
case '5' : int1 = 5 ; break;
case '6' : int1 = 6 ; break;
case '7' : int1 = 7 ; break;
case '8' : int1 = 8 ; break;
case '9' : int1 = 9 ; break;
default : int1 = 0 ;
}
return(int1);
}
void interrupt ISR_Timer0_Int()
{
unsigned char hour10, hour1, minute10, minute1, second10, second1;
if (INTCONbits.TMR0IF == 1)
{
//TMR0H = 0x48;
//TMR0L = 0xE5;
second = second + 1;
if ( second > 59 )
{
second = 0;
minute = minute + 1;
if ( minute > 59 )
{
minute = 0;
hour = hour + 1;
if ( hour > 23 )
hour = 0;
}
}
hour10 = hour / 10;
hour1 = hour - hour10 * 10;
minute10 = minute / 10;
minute1 = minute - minute10 * 10;
second10 = second / 10;
second1 = second - second10 * 10;
lcd_write_cmd(0x80);
lcd_write_data('T');
lcd_write_data('i');
lcd_write_data('m');
lcd_write_data('e');
lcd_write_data(' ');
lcd_write_data('=');
lcd_write_data(' ');
lcd_write_data(int_2_char (hour10));
lcd_write_data(int_2_char (hour1));
lcd_write_data(':');
lcd_write_data(int_2_char (minute10));
lcd_write_data(int_2_char (minute1));
lcd_write_data(':');
lcd_write_data(int_2_char (second10));
lcd_write_data(int_2_char (second1));
INTCONbits.TMR0IF = 0;
}
}
void SetupTime() //Program to get current Time
{
unsigned char msgindex, outchar, ctemp;
unsigned char hour10, hour1, minute10, minute1, second10, second1;
char Message[ ] = "Set time hhmmss: ";
lcd_write_cmd(0x80);
for (msgindex = 0; msgindex < 20; msgindex++)
{
outchar = Message[msgindex];
lcd_write_data(outchar);
}
lcd_write_cmd(0xC0); // Move cursor to line 2 position 1
ctemp=getkey(); // waits and get an ascii key number when pressed
lcd_write_data(ctemp); //display on LCD
hour10 = char_2_int (ctemp);
ctemp=getkey();
lcd_write_data(ctemp);
hour1 = char_2_int (ctemp);
ctemp=getkey();
lcd_write_data(ctemp);
minute10 = char_2_int (ctemp);
ctemp=getkey();
lcd_write_data(ctemp);
minute1 = char_2_int (ctemp);
ctemp=getkey();
lcd_write_data(ctemp);
second10 = char_2_int (ctemp);
ctemp=getkey();
lcd_write_data(ctemp);
second1 = char_2_int (ctemp);
hour = hour10 * 10 + hour1;
minute = minute10 * 10 + minute1;
second = second10 * 10 + second1;
delay_ms(500);
lcd_write_cmd(0x01);
}
void SetupAlarmTimeA() //Program to get Alarm Time for Subject A
{
unsigned char msgindex, outchar, ctemp;
unsigned char hour10, hour1, minute10, minute1;
char Message[ ] = "Subject A Time hhmm:";
PORTA = 0b00000010; //On LED RA1
lcd_write_cmd(0x80);
for (msgindex = 0; msgindex < 20; msgindex++)
{
outchar = Message[msgindex];
lcd_write_data(outchar);
}
lcd_write_cmd(0xC0); // Move cursor to line 2 position 1
ctemp=getkey(); // waits and get an ascii key number when pressed
lcd_write_data(ctemp); //display on LCD
hour10 = char_2_int (ctemp);
ctemp=getkey();
lcd_write_data(ctemp);
hour1 = char_2_int (ctemp);
ctemp=getkey();
lcd_write_data(ctemp);
minute10 = char_2_int (ctemp);
ctemp=getkey();
lcd_write_data(ctemp);
minute1 = char_2_int (ctemp);
hourA = hour10 * 10 + hour1;
minuteA = minute10 * 10 + minute1;
delay_ms(500);
lcd_write_cmd(0x01);
}
void SetupAlarmTimeB() //Program to get Alarm Time for Subject B
{
unsigned char msgindex, outchar, ctemp;
unsigned char hour10, hour1, minute10, minute1;
char Message[ ] = "Subject B Time hhmm:";
PORTA = 0b00000100; //On LED RA2
lcd_write_cmd(0x80);
for (msgindex = 0; msgindex < 20; msgindex++)
{
outchar = Message[msgindex];
lcd_write_data(outchar);
}
lcd_write_cmd(0xC0); // Move cursor to line 2 position 1
ctemp=getkey(); // waits and get an ascii key number when pressed
lcd_write_data(ctemp); //display on LCD
hour10 = char_2_int (ctemp);
ctemp=getkey();
lcd_write_data(ctemp);
hour1 = char_2_int (ctemp);
ctemp=getkey();
lcd_write_data(ctemp);
minute10 = char_2_int (ctemp);
ctemp=getkey();
lcd_write_data(ctemp);
minute1 = char_2_int (ctemp);
hourB = hour10 * 10 + hour1;
minuteB = minute10 * 10 + minute1;
delay_ms(500);
lcd_write_cmd(0x01);
}
void SetupAlarmTimeC() //Program to get Alarm Time for Subject C
{
unsigned char msgindex, outchar, ctemp;
unsigned char hour10, hour1, minute10, minute1;
char Message[ ] = "Subject C Time hhmm:";
PORTA = 0b00001000; //On LED RA3
lcd_write_cmd(0x80);
for (msgindex = 0; msgindex < 20; msgindex++)
{
outchar = Message[msgindex];
lcd_write_data(outchar);
}
lcd_write_cmd(0xC0); // Move cursor to line 2 position 1
ctemp=getkey(); // waits and get an ascii key number when pressed
lcd_write_data(ctemp); //display on LCD
hour10 = char_2_int (ctemp);
ctemp=getkey();
lcd_write_data(ctemp);
hour1 = char_2_int (ctemp);
ctemp=getkey();
lcd_write_data(ctemp);
minute10 = char_2_int (ctemp);
ctemp=getkey();
lcd_write_data(ctemp);
minute1 = char_2_int (ctemp);
hourC = hour10 * 10 + hour1;
minuteC = minute10 * 10 + minute1;
delay_ms(500);
lcd_write_cmd(0x01);
}
void SetupTimerInterruptRegisters()
{
RCONbits.IPEN =1; // Bit7 Interrupt Priority Enable Bit
// 1 Enable priority levels on interrupts
// 0 Disable priority levels on interrupts
INTCONbits.GIEH =1; // Bit7 Global Interrupt Enable bit
// When IPEN = 1
// 1 Enable all high priority interrupts
// 0 Disable all high priority interrupts
T0CON = 0b00000111; // bit7:0 Stop Timer0
// bit6:0 Timer0 as 16 bit timer
// bit5:0 Clock source is internal
// bit4:0 Increment on lo to hi transition on TOCKI pin
// bit3:0 Prescaler output is assigned to Timer0
// bit2-bit0:111 1:256 prescaler
INTCON2 = 0b10000100; // bit7 :PORTB Pull-Up Enable bit
// 1 All PORTB pull-ups are disabled
// bit2 :TMR0 Overflow Int Priority Bit
// 1 High Priority
TMR0H = 0x48; // Initialising TMR0H
TMR0L = 0xE5; // Initialising TMR0L for 1 second interrupt
T0CONbits.TMR0ON = 1; // Turn on timer
INTCONbits.TMR0IE = 1; // bit5 TMR0 Overflow Int Enable bit
// 0 Disable the TMR0 overflow int
INTCONbits.TMR0IF = 0; // bit2 TMR0 Overflow Int Flag bit
// 0 TMR0 register did not overflow
}
void Startup()
{
unsigned char msgindex, outchar;
char Startup[ ] = "System Check ";
lcd_write_cmd(0x80);
for (msgindex = 0; msgindex < 20; msgindex++)
{
outchar = Startup[msgindex];
lcd_write_data(outchar);
}
PORTA = 0b00001111;
delay_ms(1000);
PORTA = 0b00000000;
char StartupDone[ ] = "System Check Done ";
lcd_write_cmd(0x80);
for (msgindex = 0; msgindex < 20; msgindex++)
{
outchar = StartupDone[msgindex];
lcd_write_data(outchar);
}
AlarmAOff = 0;
AlarmBOff = 0;
AlarmC2Off = 0;
AlarmCOff = 0;
}
void main(void) //------------ Main Program ---------------------------------------------------------------
{
ADCON1 = 0x0F;
CMCON = 0x07;
lcd_init();
TRISA = 0b00110000; //Set PORTAs as outputs and inputs
version();//Show version number
Startup();//Startup
SetupTime(); //Get Time
SetupAlarmTimeA(); //Get Alarm Time for Subject A
SetupAlarmTimeB(); //Get Alarm Time for Subject B
SetupAlarmTimeC(); //Get Alarm Time for Subject C
SetupTimerInterruptRegisters();
while(1)
{
if(hour==hourA && minute==minuteA && AlarmAOff == 0)
{
//On Buzzer RA0 and LED RA1
PORTAbits.RA0 = 1;
PORTAbits.RA1 = 1;
while (PORTAbits.RA5 == 1); //when mute is pressed
AlarmAOff = 1;
}
else if(hour==hourB && minute==minuteB && AlarmBOff == 0)
{
//On Buzzer RA0 and LED RA2
PORTAbits.RA0 = 1;
PORTAbits.RA2 = 1;
while (PORTAbits.RA5 == 1); //when mute is pressed
AlarmBOff = 1;
}
else if(hour==hourC && minute==minuteC && AlarmC2Off == 0)
{
//On Buzzer RA0 and LED RA3
PORTAbits.RA0 = 1;
PORTAbits.RA3 = 1;
while (PORTAbits.RA5 == 1); //when mute is pressed
AlarmC2Off = 1;
}
else
{
PORTA = 0b00000000; //Off Buzzer and All LEDS at start
}
}
}