1 | //==========================================================
|
2 | //================== Timer Code ===================
|
3 | //==========================================================
|
4 |
|
5 | #include "timer.h"
|
6 |
|
7 | volatile RTC RTC_Value;
|
8 |
|
9 | volatile union{ //System Ticks
|
10 | struct{
|
11 | unsigned tick_10ms:1;
|
12 | unsigned tick_100ms:1;
|
13 | unsigned tick_sec:1;
|
14 | unsigned none:5;
|
15 | } Bit;
|
16 | unsigned char Byte;
|
17 | } RTC_Ticks;
|
18 |
|
19 |
|
20 | void RTC_INIT(void){
|
21 | RTC_Value.counter_10ms = 0;
|
22 | RTC_Value.counter_100ms = 0;
|
23 | RTC_Value.counter_sec = 0;
|
24 | RTC_Value.counter_min = 0;
|
25 | RTC_Value.counter_h = 0;
|
26 | RTC_Value.counter_day = 0;
|
27 | RTC_Ticks.Bit.tick_10ms = 0;
|
28 | RTC_Ticks.Bit.tick_100ms = 0;
|
29 | RTC_Ticks.Bit.tick_sec = 0;
|
30 | RTC_Value.boot_delay = 0;
|
31 | }
|
32 | void RTC_INT(void){
|
33 | RTC_Ticks.Bit.tick_10ms = 1; //10ms ticker
|
34 | RTC_Value.counter_10ms++; //10ms count
|
35 |
|
36 | if(RTC_Value.counter_10ms >= 10){
|
37 | RTC_Ticks.Bit.tick_100ms = 1; //100ms tick
|
38 | RTC_Value.counter_100ms++; //100ms count
|
39 | RTC_Value.counter_10ms = 0; //reset 10ms count
|
40 | }
|
41 | if(RTC_Value.counter_100ms >= 10){
|
42 | RTC_Ticks.Bit.tick_sec = 1; //1 sec tick
|
43 | RTC_Value.counter_sec++; //1sec count
|
44 | RTC_Value.counter_100ms = 0; //reset 100ms count
|
45 | }
|
46 | if(RTC_Value.counter_sec >= 60){
|
47 | RTC_Value.counter_min++; //1min count
|
48 | RTC_Value.counter_sec = 0; //reset sec count
|
49 | }
|
50 | if(RTC_Value.counter_min >= 60){
|
51 | RTC_Value.counter_h++; //1h count
|
52 | RTC_Value.counter_min = 0; //reset min count
|
53 | }
|
54 | if(RTC_Value.counter_h >= 24){
|
55 | RTC_Value.counter_day++; //1day count
|
56 | RTC_Value.counter_h = 0; //reset h count
|
57 | }
|
58 | if(RTC_Value.counter_day >= 365){
|
59 | RTC_Value.counter_day = 0; //reset day count
|
60 | }
|
61 | }
|
62 | void TIMER0_INIT(void){
|
63 | OpenTimer0( TIMER_INT_OFF & T0_8BIT & T0_SOURCE_EXT & T0_EDGE_FALL & T0_PS_1_1);
|
64 | WriteTimer0(0); //Timmer 1 load (50mal pro sec)
|
65 | }
|
66 | void TIMER1_INIT(void){
|
67 | OpenTimer1( TIMER_INT_ON & T1_16BIT_RW & T1_SOURCE_FOSC_4 & T1_PS_1_4 & T1_OSC1EN_OFF & T1_SYNC_EXT_OFF, TIMER_GATE_OFF & TIMER_GATE_INT_OFF);
|
68 | WriteTimer1(25536); //Timmer 1 load (10ms)
|
69 | PIR1bits.TMR1IF = 0; //clear interrupt flag
|
70 | IPR1bits.TMR1IP = 0; //low Prio
|
71 | PIE1bits.TMR1IE = 1; //Timer 1 Interupt enabel
|
72 | }
|
73 | void TIMER2_INIT(void){
|
74 | OpenTimer2( TIMER_INT_OFF & T2_PS_1_1 & T2_POST_1_16);
|
75 | }
|
76 | void TIMER4_INIT(void){
|
77 | OpenTimer4(TIMER_INT_ON & T4_PS_1_16 & T4_POST_1_4);
|
78 | WriteTimer4(0); //Timer 4 Clear
|
79 | PR4 = 250; //Timer 4 set compare level
|
80 | PIR5bits.TMR4IF = 0; //clear interrupt flag
|
81 | PIE5bits.TMR4IE = 1; //Timer 4 Interupt Enabel
|
82 | IPR5bits.TMR4IP = 1; //Timer 4 Compare Interrupt prio High
|
83 | }
|
84 | void TIMER6_INIT(void){
|
85 | OpenTimer6(TIMER_INT_OFF & T6_PS_1_1 & T6_POST_1_1);
|
86 | }
|