main.c


1
//------------------------------------------------------------------------ Initialisierung -----------------------------------------------------------------------------------------------
2
//#include "pic16f877"
3
//#include <delay.h>
4
#include <htc.h>
5
#include <stdlib.h>
6
#include <stdio.h>
7
#include <string.h>
8
#define _XTAL_FREQ 32768  //Quarz Oszillator RTC
9
#define FOSC 32768ul
10
11
//LCD Segments
12
//Digit1
13
#define D1A    SEG0COM0
14
#define D1B   SEG1COM0
15
#define D1C   SEG2COM0
16
#define D1D   SEG3COM0
17
#define D1E   SEG4COM0
18
#define D1F   SEG5COM0
19
#define D1G   SEG6COM0
20
#define D1DOT  SEG7COM0
21
22
//Digit2
23
#define D2A     SEG41COM0
24
#define D2B     SEG16COM0
25
#define D2C     SEG17COM0
26
#define D2D     SEG18COM0
27
#define D2E     SEG19COM0
28
#define D2F     SEG20COM0
29
#define D2G     SEG21COM0
30
#define D2DOT   SEG22COM0
31
32
//Digit3
33
#define D3A     SEG23COM0
34
#define D3B     SEG24COM0
35
#define D3C     SEG25COM0
36
#define D3D     SEG26COM0
37
#define D3E     SEG27COM0
38
#define D3F     SEG28COM0
39
#define D3G     SEG29COM0
40
#define D3DOT   SEG30COM0
41
42
//Digit4
43
#define D4A     SEG31COM0
44
#define D4B     SEG32COM0
45
#define D4C     SEG33COM0
46
#define D4D     SEG34COM0
47
#define D4E     SEG35COM0
48
#define D4F     SEG36COM0
49
#define D4G     SEG37COM0
50
#define D4DOT   SEG38COM0
51
52
#define COL    SEG8COM0
53
54
//Buttons
55
#define SW_mode   RC5
56
#define SW_temp    RC6
57
#define SW_down    RC4
58
#define SW_up    RB5
59
60
//LED's
61
#define red     RC1
62
#define green     RC0
63
#define blue     RD0
64
65
//Prototyping
66
67
//Time
68
unsigned char number=0;
69
unsigned char hour_high=0;
70
unsigned char hour_low=0;
71
unsigned char min_high=0;
72
unsigned char min_low=0;
73
unsigned char sec=0;
74
bit toggle =0;
75
unsigned char modify_digits=0;
76
unsigned char limit_digit2=0; 
77
78
unsigned char set_time=0;
79
unsigned char temp_set=0;
80
81
void interrupt time(void);
82
void init(void);
83
void put_display(unsigned char digit1, unsigned char digit2, unsigned char digit3, unsigned char digit4);
84
void time_calculation(void);
85
void clear_all(void);
86
void read_temp(void);
87
void set_digits(void);
88
89
unsigned char button_up(void);
90
unsigned char up_flag=0;
91
unsigned char status_up=0;
92
unsigned char button_down(void);
93
unsigned char down_flag=0;
94
unsigned char status_down=0;
95
unsigned char button_mode(void);
96
unsigned char mode_flag=0;
97
unsigned char status_mode=0;
98
unsigned char mode_pressed=0;
99
100
int adc_value =0;
101
int temp_h=0;
102
int temp_l=0;
103
float temp=0;
104
105
//-------------------------------------------------------------------------------- Main --------------------------------------------------------------------------------------------------
106
107
main (void)
108
{
109
110
111
init();
112
put_display(0, 0, 0, 0);
113
114
  do
115
  {
116
  //SLEEP();
117
  }while(1);
118
}
119
120
121
//--------------------------------------------------------------------------Interrupts --------------------------------------------------------------------------------------------------
122
123
//--------------------------------------------------------------------------Timer1 Interrupt --------------------------------------------------------------------------------------------------
124
void interrupt time (void)  
125
{
126
if(TMR1IF == 1)
127
{
128
  GIE=0;
129
  TMR1IE = 0;
130
  
131
  TMR1IF = 0;
132
  TMR1H=224;
133
  TMR1L=0;
134
  
135
  sec++;
136
  COL = !COL;
137
  
138
    while(SW_temp == 1)
139
    {  
140
        GIE=1;
141
        TMR1IE = 1;  
142
      read_temp();
143
    }
144
  
145
    if(SW_mode == 1)  //Enter Clock SETUP
146
    {
147
    set_digits();
148
    }
149
  GIE=1;
150
  TMR1IE = 1;  
151
  PEIE=1;
152
  time_calculation();
153
  put_display(hour_high, hour_low, min_high, min_low);  
154
}
155
156
157
}
158
159
//-------------------------------------------------------------------------- Funktionen --------------------------------------------------------------------------------------------------
160
161
void init (void)
162
163
{
164
//LCD initialisation
165
  
166
LCDSE0 = 0b11111111;  //  SE<7:0>
167
LCDSE1 = 0b10000001;  //  SE<15:8>
168
LCDSE2 = 0b11111111;  //  SE<23:16>
169
LCDSE3 = 0b11111111;  //  SE<31:24>
170
LCDSE4 = 0b01111111;  //  SE<39:32>
171
LCDSE5 = 0b00000010;  //  SE<41:40>
172
173
LCDCON = 0b10001000;
174
LCDPS = 0b10110000;
175
LCDDATA0 = 0;
176
LCDDATA1 = 0;
177
LCDDATA2 = 0;
178
LCDDATA3 = 0;
179
LCDDATA12 = 0;
180
LCDDATA13 = 0;
181
LCDDATA14 = 0;
182
COL = 0;
183
184
//BUTTONS::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
185
TRISC6=1;
186
TRISC5=1;
187
TRISC4=1;
188
TRISB5=1;
189
190
//LED's::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
191
TRISC1=0;
192
TRISC0=0;
193
TRISD0=0;
194
red = green = blue =0;
195
196
// Timer1 Registers: Prescaler=1:1; TMR1 Preset=57344; Freq=1.00Hz; Period=1.00 s
197
T1CKPS1 = 0;  // bits 5-4  Prescaler Rate Select bits
198
T1CKPS0 = 0;  // bit 4
199
200
T1OSCEN = 0;  // bit 3 Timer1 Oscillator Enable Control: bit 1=on
201
T1SYNC  = 1;  // bit 2 Timer1 External Clock Input Synchronization Control bit:1=Do not synchronize external clock input
202
TMR1CS  = 0;  // bit 1 Timer1 Clock Source Select bit:0=Internal clock (FOSC/4) / 1 = External clock from pin T1CKI (on the rising edge)
203
204
//T1GE =1; //Gate enable
205
206
TMR1H = 0;         // preset for timer1 MSB register
207
TMR1L = 0;         // preset for timer1 LSB register
208
209
TMR1IF=0;
210
211
GIE = 1;
212
PEIE = 1;
213
TMR1IE = 1;
214
215
TMR1H = 224;         // preset for timer1 MSB register
216
TMR1L = 0;         // preset for timer1 LSB register
217
TMR1ON  = 1;  // bit 0 enables timer
218
219
//ADC Temperature::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
220
TRISA1=1;        //AN1 = Input
221
TRISA2=1;        //AN2 = Input
222
ANSEL = 0b00000110;    //AN1 and AN2 = Analog
223
ADCON0 = 0b10101000;      //ADFM=1.RJ,Vref-=VSS,Vref+=PIN,AN2 select010, GODONE=0,ADON=A/D operrating statusbit
224
ADCON1 = 0b01000000;      //Conversation time between 1.6us and 6.4us = FOSC/2 =(1/32768) = ~30us*2 =60us
225
ADRESH=0;
226
ADRESL=0;
227
228
}
229
230
231
//TIME CALCULATION:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
232
void time_calculation()
233
{
234
235
if(sec > 59)
236
{
237
sec = 0;
238
min_low++;
239
}
240
241
if(min_low > 9)
242
{
243
min_low=0;
244
min_high++;
245
}
246
247
if(min_high > 5)
248
{
249
min_low=0;
250
hour_low++;
251
}
252
if(min_low > 9)
253
{
254
min_low=0;
255
min_high++;
256
}
257
258
if( (hour_high >= 2) && (hour_low >= 4) )
259
{
260
hour_high=0;
261
hour_low=0;
262
min_high=0;
263
min_low=0;
264
sec=0;
265
}
266
267
268
}
269
270
//READ_TEMPERATURE:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
271
void read_temp(void)
272
{
273
green =1;
274
ADON=1;          //enable ADC module
275
GO_DONE=1;        //Start conversation
276
while(GO_DONE ==1);    //Wait for complete conversation
277
temp_h = ADRESH;
278
temp_l = ADRESL;
279
280
adc_value = ((temp_h<<8) | temp_l);
281
temp = ((adc_value*0.0024414)/0.023) ;
282
ADON=0;
283
temp_h = temp/10;
284
temp_l = temp-(temp_h*10);
285
clear_all();
286
put_display(temp_h, temp_l, '°','C' );
287
temp_h = temp_l =0;
288
ADON=0;
289
green=0;
290
}
291
292
//SET_TIME::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
293
void set_digits(void)
294
{
295
put_display('°', '°', '°', '°');
296
COL=1;
297
blue=1;
298
hour_high = hour_low = min_high = min_low = 0;
299
while(SW_mode ==1);
300
301
do                              //DIGIT1
302
{
303
D1DOT=1;
304
  if(button_up()==1)
305
  {
306
    if((hour_high>=0) && (hour_high <2))
307
      {
308
      hour_high++;
309
      put_display(hour_high, '°', '°', '°');
310
      }
311
  }
312
  if(button_down())
313
  {
314
    if((hour_high>0) && (hour_high <3))
315
      {
316
      hour_high--;
317
      put_display(hour_high, '°', '°', '°');
318
      }
319
   }
320
}while(button_mode()==0);
321
if(hour_high == '°'){hour_high=0;}
322
put_display(hour_high, '°', '°', '°');
323
D1DOT=0;
324
325
do                              //DIGIT2
326
{
327
D2DOT=1;
328
if(hour_high >=2)
329
{
330
limit_digit2 = 3;
331
}
332
else
333
{
334
limit_digit2 = 9;
335
}
336
337
  if(button_up()==1)
338
  {
339
    if((hour_low>=0) && (hour_low < limit_digit2))        //VARIABLE FOR <24:00
340
      {
341
      hour_low++;
342
      put_display(hour_high, hour_low, '°', '°');
343
      }
344
  }
345
  if(button_down())
346
  {
347
    if((hour_low>0) && (hour_low < limit_digit2+1))
348
      {
349
      hour_low--;
350
      put_display(hour_high, hour_low, '°', '°');
351
      }
352
   }
353
}while(button_mode()==0);
354
if(hour_low == '°'){hour_low=0;}
355
put_display(hour_high, hour_low, '°', '°');
356
D2DOT=0;
357
358
do                              //DIGIT3
359
{
360
D3DOT=1;
361
  if(button_up()==1)
362
  {
363
    if((min_high>=0) && (min_high <5))
364
      {
365
      min_high++;
366
      put_display(hour_high, hour_low, min_high, '°');
367
      }
368
  }
369
  if(button_down())
370
  {
371
    if((min_high>0) && (min_high <6))
372
      {
373
      min_high--;
374
      put_display(hour_high, hour_low, min_high, '°');
375
      }
376
   }
377
}while(button_mode()==0);
378
if(min_high == '°'){min_high=0;}
379
put_display(hour_high, hour_low, min_high, '°');
380
D3DOT=0;
381
382
do                              //DIGIT4
383
{
384
  if(button_up()==1)
385
  {
386
    if((min_low>=0) && (min_low <9))
387
      {
388
      min_low++;
389
      put_display(hour_high, hour_low, min_high, min_low);
390
      }
391
  }
392
  if(button_down())
393
  {
394
    if((min_low>0) && (min_low <10))
395
      {
396
      min_low--;
397
      put_display(hour_high, hour_low, min_high, min_low);
398
      }
399
   }
400
}while(button_mode()==0);
401
if(min_high == '°'){min_high=0;}
402
put_display(hour_high, hour_low, min_high, min_low);
403
404
blue=0;
405
sec=0;
406
}
407
408
//DISPLAY:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
409
410
void put_display(unsigned char digit1, unsigned char digit2, unsigned char digit3, unsigned char digit4)
411
{
412
413
D1A = D1B = D1C = D1D = D1E = D1F = D1G = 0; //LÖSCHEN
414
switch(digit1)
415
  {  
416
    
417
    case 0:
418
      D1A = D1B = D1C = D1D = D1E = D1F = 1;
419
      break;    
420
    case 1:
421
      D1B = D1C = 1;          // these pixal defination are in the lcdmap.h you may need to chage them according to you lcd pins and pixal allignment
422
      break;
423
    case 2:
424
      D1A = D1B = D1G = D1E = D1D = 1;
425
      break;
426
    case 3:
427
      D1A = D1B = D1G = D1C = D1D = 1;
428
      break;
429
    case 4:
430
      D1F = D1G = D1B = D1C = 1;
431
      break;
432
    case 5:
433
      D1A = D1F = D1G = D1C = D1D = 1;
434
      break;
435
    case 6:
436
      D1A = D1F = D1E = D1D = D1C = D1G = 1;
437
      break;
438
    case 7:
439
      D1A = D1B = D1C = 1;
440
      break;
441
    case 8:
442
      D1A = D1B = D1C = D1D = D1E = D1F = D1G = 1;
443
      break;
444
    case 9:
445
      D1A = D1B = D1C = D1D = D1F = D1G = 1;
446
      break;
447
    case '°':
448
      D1A = D1B = D1F = D1G = 1;
449
      break;
450
    case 'C':
451
      D1A = D1D = D1E = D1F = 1;
452
      break;
453
    }
454
455
456
D2A = D2B = D2C = D2D = D2E = D2F = D2G = 0; //LÖSCHEN
457
switch(digit2)
458
  {
459
    case 0:
460
      D2A = D2B = D2C = D2D = D2E = D2F = 1;
461
      break;  
462
    case 1:
463
      D2B = D2C = 1;          // these pixal defination are in the lcdmap.h you may need to chage them according to you lcd pins and pixal allignment
464
      break;
465
    case 2:
466
      D2A = D2B = D2G = D2E = D2D = 1;
467
      break;
468
    case 3:
469
      D2A = D2B = D2G = D2C = D2D = 1;
470
      break;
471
    case 4:
472
      D2F = D2G = D2B = D2C = 1;
473
      break;
474
    case 5:
475
      D2A = D2F = D2G = D2C = D2D = 1;
476
      break;
477
    case 6:
478
      D2A = D2F = D2E = D2D = D2C = D2G = 1;
479
      break;
480
    case 7:
481
      D2A = D2B = D2C = 1;
482
      break;
483
    case 8:
484
      D2A = D2B = D2C = D2D = D2E = D2F = D2G = 1;
485
      break;
486
    case 9:
487
      D2A = D2B = D2C = D2D = D2F = D2G = 1;
488
      break;
489
    case '°':
490
      D2A = D2B = D2F = D2G = 1;
491
      break;
492
    case 'C':
493
      D2A = D2D = D2E = D2F = 1;
494
      break;
495
    }
496
497
D3A = D3B = D3C = D3D = D3E = D3F = D3G = 0; //LÖSCHEN
498
switch(digit3)
499
  {
500
    case 0:
501
      D3A = D3B = D3C = D3D = D3E = D3F = 1;
502
      break;  
503
    case 1:
504
      D3B = D3C = 1;          // these pixal defination are in the lcdmap.h you may need to chage them according to you lcd pins and pixal allignment
505
      break;
506
    case 2:
507
      D3A = D3B = D3G = D3E = D3D = 1;
508
      break;
509
    case 3:
510
      D3A = D3B = D3G = D3C = D3D = 1;
511
      break;
512
    case 4:
513
      D3F = D3G = D3B = D3C = 1;
514
      break;
515
    case 5:
516
      D3A = D3F = D3G = D3C = D3D = 1;
517
      break;
518
    case 6:
519
      D3A = D3F = D3E = D3D = D3C = D3G = 1;
520
      break;
521
    case 7:
522
      D3A = D3B = D3C = 1;
523
      break;
524
    case 8:
525
      D3A = D3B = D3C = D3D = D3E = D3F = D3G = 1;
526
      break;
527
    case 9:
528
      D3A = D3B = D3C = D3D = D3F = D3G = 1;
529
      break;
530
    case '°':
531
      D3A = D3B = D3F = D3G = 1;
532
      break;
533
    case 'C':
534
      D3A = D3D = D3E = D3F = 1;
535
      break;
536
    }
537
D4A = D4B = D4C = D4D = D4E = D4F = D4G = 0; //LÖSCHEN
538
switch(digit4)
539
  {
540
    case 0:
541
      D4A = D4B = D4C = D4D = D4E = D4F = 1;
542
      break;  
543
    case 1:
544
      D4B = D4C = 1;          // these pixal defination are in the lcdmap.h you may need to chage them according to you lcd pins and pixal allignment
545
      break;
546
    case 2:
547
      D4A = D4B = D4G = D4E = D4D = 1;
548
      break;
549
    case 3:
550
      D4A = D4B = D4G = D4C = D4D = 1;
551
      break;
552
    case 4:
553
      D4F = D4G = D4B = D4C = 1;
554
      break;
555
    case 5:
556
      D4A = D4F = D4G = D4C = D4D = 1;
557
      break;
558
    case 6:
559
      D4A = D4F = D4E = D4D = D4C = D4G = 1;
560
      break;
561
    case 7:
562
      D4A = D4B = D4C = 1;
563
      break;
564
    case 8:
565
      D4A = D4B = D4C = D4D = D4E = D4F = D4G = 1;
566
      break;
567
    case 9:
568
      D4A = D4B = D4C = D4D = D4F = D4G = 1;
569
      break;
570
    case '°':
571
      D4A = D4B = D4F = D4G = 1;
572
      break;
573
    case 'C':
574
      D4A = D4D = D4E = D4F = 1;
575
      break;
576
    }
577
578
}
579
580
void clear_all(void)
581
{
582
LCDDATA0 = 0;
583
LCDDATA1 = 0;
584
LCDDATA2 = 0;
585
LCDDATA3 = 0;
586
LCDDATA12 = 0;
587
LCDDATA13 = 0;
588
LCDDATA14 = 0;
589
}
590
591
unsigned char button_up(void)
592
{
593
if(SW_up == 1)          
594
  {
595
  up_flag=1;
596
  }
597
status_up=0;
598
if((SW_up==0) && (up_flag==1))
599
  {
600
  up_flag =0;
601
      status_up=1;
602
  }
603
return (status_up);
604
}
605
606
unsigned char button_down(void)
607
{
608
if(SW_down == 1)          
609
  {
610
  down_flag=1;
611
  }
612
status_down=0;
613
if((SW_down==0) && (down_flag==1))
614
  {
615
  down_flag =0;
616
      status_down=1;
617
  }
618
return (status_down);
619
}
620
621
unsigned char button_mode(void)
622
{
623
if(SW_mode == 1)          
624
  {
625
  mode_flag=1;
626
  }
627
status_mode=0;
628
if((SW_mode==0) && (mode_flag==1))
629
  {
630
  mode_flag =0;
631
      status_mode=1;
632
  }
633
return (status_mode);
634
}
635
636
637
//EXAMPLE::::::::::::::::::::::::::::::::::::::::::::::::::::::
638
//GIE=0;    //  LED's überprüfen
639
//
640
//if(SW_mode==1)
641
//{
642
//green=1;
643
//}
644
//else{green=0;}
645
//if(SW_down==1)
646
//{
647
//red=1;
648
//}else{red=0;}
649
//if(SW_up==1)
650
//{
651
//blue=1;
652
//}else{blue=0;}