-
Notifications
You must be signed in to change notification settings - Fork 0
/
RealTimeClock.c
503 lines (444 loc) · 11.4 KB
/
RealTimeClock.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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
#define HIGH 1
#define LOW 0
//LCD Setup
sbit LCD_RS at RE1_bit;
sbit LCD_EN at RE0_bit;
sbit LCD_D4 at RB4_bit;
sbit LCD_D5 at RB5_bit;
sbit LCD_D6 at RB6_bit;
sbit LCD_D7 at RB7_bit;
sbit LCD_RS_Direction at TRISE1_bit;
sbit LCD_EN_Direction at TRISE0_bit;
sbit LCD_D4_Direction at TRISB4_bit;
sbit LCD_D5_Direction at TRISB5_bit;
sbit LCD_D6_Direction at TRISB6_bit;
sbit LCD_D7_Direction at TRISB7_bit;
/* Types */
typedef enum {
CLOCK_DISPLAY = 0,
ADJUSTING_ALARM = 1
} ModeClock;
typedef enum {
OFF = 0,
ON = 1
} Status;
typedef struct{
unsigned char hour;
unsigned char minute;
unsigned char second;
} Time;
typedef struct{
unsigned char day_week;
unsigned char day_month;
unsigned char month;
unsigned int year;
} Calendar;
/* Variables */
Status ALARM_STATUS = OFF;
ModeClock DISPLAY_MODE = CLOCK_DISPLAY;
Time clock;
Time alarm;
Calendar date;
char txt[7];
const unsigned short days_of_month[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
char* months[12] = {"Jan", "Fev", "Mar", "Abr", "Mai", "Jun", "Jul", "Ago", "Set", "Out", "Nov", "Dez"};
const char* days_of_week[7] = {"Domingo", "Segunda", "Terca ", "Quarta ", "Quinta ", "Sexta ", "Sabado "};
char can_triggers_alarm;
unsigned int last_month_day;
unsigned interrupt_counter;
/* Prototypes */
void loop();
void setup();
void set_clock();
void display_time();
void increment_time();
void set_date();
void display_date();
void set_alarm();
void display_alarm();
void check_alarm();
void triggers_alarm();
void display_alarm_icon();
void read_temperature();
void timer0_init();
char format_number(unsigned char number);
char format_day(unsigned char dayweek);
unsigned short get_last_day();
int is_leap_year();
int get_day_of_week(Calendar date);
void delay(unsigned milliseconds);
int vandermonde(int read_value);
/* Program */
void setup(){
//Required settings
INTCON2 = 0; //Turn on pull up resistor
ADCON1 = 15; //Digital ports
CMCON = 7; //Turn off comparers
//Setup base on PIC18F4520 (UniFtec)
TRISA = 48; //RA4 & RA5 as INPUT
TRISB = 15; //RB0 ..RB3
TRISC = 0; //output
TRISD = 0; //output
TRISE = 0; //output
//Init alls ports off
PORTA = LOW; //safe
PORTB = LOW; //safe
PORTC = LOW; //safe
PORTD = LOW; //safe
PORTE = LOW; //safe
//Init LCD
lcd_init();
lcd_cmd(_LCD_CLEAR);
lcd_cmd(_LCD_CURSOR_OFF);
//Init ADC
adc_init();
//Init others variables
clock.hour = 12;
clock.minute = 00;
clock.second = 00;
alarm.hour = 06;
alarm.minute = 00;
alarm.second = 00;
date.day_month = 30;
date.month = 8;
date.year = 2017;
date.day_week = get_day_of_week(date);
can_triggers_alarm = 0;
interrupt_counter = 0;
//Setup interrupt
T0CON = 136; //turn off all TMR0 controls (16bits)
INTCON = 0; //disable interruptions
timer0_init(); //initialize TMR0
TMR0IE_bit = 1; //enable TMR0 (start counter)
GIE_bit = 1; //enable global interruptions
}
void loop(){
if(RB0_bit == LOW){
char time_commit = clock.second + 1;
switch (DISPLAY_MODE) {
case ADJUSTING_ALARM:
while (RB0_bit == LOW){
if(clock.second == time_commit){
DISPLAY_MODE = CLOCK_DISPLAY;
}
}
if(clock.second < time_commit)
{
ALARM_STATUS = !ALARM_STATUS;
can_triggers_alarm = 0;
RC0_bit = LOW;//turn off led0
RC1_bit = LOW;//turn off led1
RC2_bit = LOW;//turn off led2
RC3_bit = LOW;//turn off led3
}
break;
default:
while(RB0_bit == LOW) delay(1);
DISPLAY_MODE = ADJUSTING_ALARM;
lcd_cmd(_LCD_CLEAR);
}
}
if(DISPLAY_MODE == CLOCK_DISPLAY){
set_date();
set_clock();
read_temperature();
display_alarm_icon();
display_date();
display_time();
}else{
set_alarm();
display_alarm();
}
check_alarm();
}
void main() {
setup();
while(1) loop();
}
void interrupt(){
GIE_bit = 0;
if (TMR0IF_bit){
TMR0IF_bit=0;
timer0_init();
interrupt_counter++; //for each TMR0 interrupt
if (interrupt_counter == 100){ //counter == {time} ~> 1sec elapsed
increment_time();
interrupt_counter = 0;
}
}
GIE_bit = 1;
}
/* Functions :: Clock */
void display_time(){
//hours
lcd_out(2, 9, format_number(clock.hour));
//separator
if(interrupt_counter > 40) lcd_chr_cp(':'); else lcd_chr_cp(' ');
//minutes
lcd_out_cp(format_number(clock.minute));
//separator
if(interrupt_counter > 40) lcd_chr_cp(':'); else lcd_chr_cp(' ');
//seconds
lcd_out_cp(format_number(clock.second));
}
void set_clock(){
//Hour
if(RB1_bit == LOW){
delay(100);
if(RB1_bit == LOW){
if(clock.hour < 23)
clock.hour++;
else
clock.hour = 0;
}
}
//Minute
if(RB2_bit == LOW){
delay(100);
if(RB2_bit == LOW){
if(clock.minute < 59)
clock.minute++;
else
clock.minute = 0;
}
}
}
void increment_time(){
//seconds
if(clock.second < 59)
clock.second++;
else{
clock.second = 0;
//minutes
if(clock.minute < 59)
clock.minute++;
else{
clock.minute = 0;
//hours
if(clock.hour < 23)
clock.hour++;
else{
clock.hour = 0;
//days
if(date.day_month < get_last_day()){
date.day_month++;
if(date.day_week < 6)
date.day_week++;
else
date.day_week = 0;
}else{
date.day_month = 1;
//months
if(date.month < 12)
date.month++;
else{
date.month = 1;
//years
if(date.year < 2100)
date.year++;
else
date.year = 2017;
}
}
}
}
}
}
/* Functions :: Date */
void set_date(){
//Day
if(RB3_bit == LOW){
delay(100);
if(RB3_bit == LOW){
if(date.day_month < get_last_day())
date.day_month++;
else
date.day_month = 1;
date.day_week = get_day_of_week(date);
}
}
//Month
if(RA4_bit == HIGH){
delay(100);
if(RA4_bit == HIGH){
if(date.month < 11){
date.month++;
while (date.day_month > get_last_day())
date.day_month--;
}else
date.month = 0;
date.day_week = get_day_of_week(date);
}
}
//Year
if(RA5_bit == HIGH){
delay(100);
if(RA5_bit == HIGH){
if(date.year < 2100){
date.year++;
if(!is_leap_year() && date.day_month > get_last_day())
date.day_month--;
}else
date.year = 2017;
date.day_week = get_day_of_week(date);
}
}
}
void display_date(){
//Day of Week
lcd_out(2, 1, format_day(date.day_week));
//Day of Month
lcd_out(1, 1, format_number(date.day_month));
//separator
lcd_chr_cp(47);
//Month
lcd_out_cp(ltrim(months[date.month]));
//separator
lcd_chr_cp(47);
//Year
inttostr(date.year, txt);
lcd_out_cp(ltrim(txt));
}
/* Functions :: Alarm */
void set_alarm(){
//Hour
if(RB1_bit == LOW){
delay(100);
if(RB1_bit == LOW){
if(alarm.hour < 23)
alarm.hour++;
else
alarm.hour = 0;
}
}
//Minute
if(RB2_bit == LOW){
delay(100);
if(RB2_bit == LOW){
if(alarm.minute < 59)
alarm.minute++;
else
alarm.minute = 0;
}
}
}
void display_alarm(){
lcd_out(1, 1, "Alarme");
//icon
display_alarm_icon();
//hours
lcd_out(2, 9, format_number(alarm.hour));
//separator
if(interrupt_counter > 40) lcd_chr_cp(':'); else lcd_chr_cp(' ');
//minutes
lcd_out_cp(format_number(alarm.minute));
//separator
if(interrupt_counter > 40) lcd_chr_cp(':'); else lcd_chr_cp(' ');
//seconds
lcd_out_cp(format_number(alarm.second));
}
void check_alarm(){
if(!can_triggers_alarm)
can_triggers_alarm = (clock.hour == alarm.hour && clock.minute == alarm.minute);
if(can_triggers_alarm && ALARM_STATUS == ON)
triggers_alarm();
}
void triggers_alarm(){
if(clock.second % 2 == 0){
RC0_bit = HIGH;//turn ON led0
RC1_bit = HIGH;//turn ON led1
RC2_bit = HIGH;//turn ON led2
RC3_bit = HIGH;//turn ON led3
}else{
RC0_bit = LOW;//turn OFF led0
RC1_bit = LOW;//turn OFF led1
RC2_bit = LOW;//turn OFF led2
RC3_bit = LOW;//turn OFF led3
}
}
void display_alarm_icon(){
if(ALARM_STATUS == ON){
const char alarm_icon[] = {0,27,14,17,21,17,14,0};
char i;
lcd_cmd(72);
for (i = 0; i<=7; i++) lcd_chr_cp(alarm_icon[i]);
lcd_cmd(_LCD_RETURN_HOME);
lcd_chr(1, 16, 1);
}else
lcd_chr(1, 16, ' ');
}
/* Functions :: Temperature*/
void read_temperature(){
char i, temperature;
const char temp_icon[] = {28,20,28,0,7,8,8,7};
if(interrupt_counter == 0){
int raw_value = adc_read(1);
int converted_value = vandermonde(raw_value);
temperature = converted_value % 100;
}
lcd_out(1, 13, format_number(temperature));
//icon
lcd_cmd(64);
for (i = 0; i<=7; i++) lcd_chr_cp(temp_icon[i]);
lcd_cmd(_LCD_RETURN_HOME);
lcd_chr(1, 15, 0);
}
/* Functions :: Helpers */
void timer0_init(){
//Initialize TMR0 with values
//20 Mhz (freq PIC) / 4 (prescaler) = 5Mhz
//5Mhz / 65536 = 76,29.... ~> 5Mhz/ (65536 - X) = [time] (ps: choosed time = 100)
//X = 15536 (DEC) => {HEX} 3CB0 => {HEX} bit_H[3C] bit_L[B0] => {DEC} bit_H[60] bit_L[176]
TMR0H = 60; //0x3C
TMR0L = 176; //0xB0
}
char format_number(unsigned char number){
bytetostr(number, txt);
if(number < 10){
txt[1] = '0';
}
return ltrim(txt);
}
char format_day(unsigned char dayweek){
static char day_txt[8];
const char *day_name = days_of_week[dayweek];
char i;
for(i=0; day_txt[i] = day_name[i]; i++);
return day_txt;
}
unsigned short get_last_day(){
last_month_day = days_of_month[date.month];
if(date.month == 1 && is_leap_year())
last_month_day +=1;
return last_month_day;
}
int is_leap_year(){
if(date.year % 4 == 0){
if(date.year % 100 == 0)
return (date.year % 400 == 0);
return 1;
}
return 0;
}
/* Sakamoto's method */
int get_day_of_week(Calendar date){//1 <= m <= 12, y > 1752 (in the U.K.)
char m = date.month+1;
char d = date.day_month;
int y = date.year;
static int t[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
y -= m < 3;
return (y + y/4 - y/100 + y/400 + t[m-1] + d) % 7;
}
void delay(unsigned int milliseconds){
while(milliseconds > 0){
delay_ms(1);
milliseconds--;
}
}
int vandermonde(int read_value){
int yield_value, converted_value;
yield_value = read_value / 42;
converted_value = read_value - yield_value;
converted_value = converted_value / 2;
return converted_value;
}