-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtimer.c
136 lines (133 loc) · 4.2 KB
/
timer.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
#include "DIO.h"
#include <stdio.h>
#include <math.h>
#include "MCAL_PIC18F.h"
#define 8bit_OV 0
#define sys_clk 1
#define TIMER0 0
Time SysTime; u8_t global_prescaler;
void MCAL_PIC_InitTimer0(u8_t mode,u16_t prescaler){
SysTime.ticks=1;SysTime.milliseconds=0; SysTime.seconds=0; SysTime.minutes=0; //defaults value assigned
TMR0H = 0; TMR0L = 0; //resetting
T0CONbits.TMR0ON=1; //enable
switch(mode){
case 8bit_OV:
T0CONbits.T08BIT=1; //8bit operation
T0CONbits.T0CS=0; //same processor clock
T0CONbits.T0SE=0; //MORE ACCURATE on low - to high trans
T0CONbits.PSA=0; //prescaler assgined
INTCON1bits.TMR0IF=0; //flag cleared before enabling intp
INTCON1bits.TMR0IE=1; //ENABLE INTERRUPT
MCAL_PIC_Timer0SetPS(prescaler);
break;
case sys_clk:
T0CONbits.T08BIT=1; //8bit operation
T0CONbits.T0CS=0; //same processor clock
T0CONbits.T0SE=0; //MORE ACCURATE on low - to high trans
T0CONbits.PSA=0; //prescaler assgined
INTCON1bits.TMR0IF=0; //flag cleared before enabling intp
INTCON1bits.TMR0IE=1; //ENABLE INTERRUPT
MCAL_PIC_Timer0SetPS(256); //based on 8 MHZ freq every 8 ticks generates 1 MS
MCAL_PIC_Timer0_GetTick();
MCAL_PIC_Timer0OVredirect(MCAL_PIC_Timer0SysClkHandler); //setting interrupt call back
MCAL_PIC_SysClkredirect(MCAL_PIC_SysClkDef); //default
break;
case default:
break;}
return;}
void MCAL_PIC_Timer0SetPS(u16_t prescaler){
global_prescaler=prescaler
switch(prescaler){
case 256:
T0CONbits.T0PS0=1;
T0CONbits.T0PS1=1;
T0CONbits.T0PS2=1;
break;
case 128:
T0CONbits.T0PS0=0;
T0CONbits.T0PS1=1;
T0CONbits.T0PS2=1;
break;
case 64:
T0CONbits.T0PS0=1;
T0CONbits.T0PS1=0;
T0CONbits.T0PS2=1;
break;
case 32:
T0CONbits.T0PS0=0;
T0CONbits.T0PS1=0;
T0CONbits.T0PS2=1;
break;
case 16:
T0CONbits.T0PS0=1;
T0CONbits.T0PS1=1;
T0CONbits.T0PS2=0;
break;
case 8:
T0CONbits.T0PS0=0;
T0CONbits.T0PS1=1;
T0CONbits.T0PS2=0;
break;
case 4:
T0CONbits.T0PS0=1;
T0CONbits.T0PS1=0;
T0CONbits.T0PS2=0;
break;
case 2:
T0CONbits.T0PS0=0;
T0CONbits.T0PS1=0;
T0CONbits.T0PS2=0;
break;
case default:
T0CONbits.T0PS0=1;
T0CONbits.T0PS1=1;
T0CONbits.T0PS2=1;
break;}
return;}
void MCAL_PIC_Timer0_GetTick(void){
u32_t freq=0 ;
if(get_bit(OSCCON,6)){freq+=1;}
if(get_bit(OSCCON,5)){freq+=3;}
if(get_bit(OSCCON,4)){freq+=5;}
switch(freq){
case 9:freq=8*1000000;SysTime.ticks=8; break;
case 4:freq=4*1000000;SysTime.ticks=4;break;
case 6:freq=2*1000000;SysTime.ticks=2;break;
case 1:freq=1*1000000;SysTime.ticks=1;break;
case 8:freq=500*1000;SysTime.ticks=0.5;break;
case 3:freq=250*1000;SysTime.ticks=0.25;break;
case 5:freq=125*1000;SysTime.ticks=0.125;break;
case 0:freq=31*1000;SysTime.ticks=0.031;break;
}
if (SysTime.ticks<1){ //error -0.064
(u16_t)global_prescaler=ceilf(((float)global_prescaler)*SysTime.ticks );
void MCAL_PIC_Timer0SetPS(global_prescaler);
SysTime.ticks=1; //corrseponds to 1 ms tick
}
if (SysTime.ticks>1){ //error -0.064
(u16_t)global_prescaler=ceilf(((float)global_prescaler)/SysTime.ticks );
void MCAL_PIC_Timer0SetPS(global_prescaler);
SysTime.ticks=1; //corrseponds to 1 ms tick
}
return;}
void MCAL_PIC_Timer0SysClkHandler(void){
if (SysTime.ticks==1){
if(SysTime.milliseconds==999){
if(SysTime.seconds==59){SysTime.minutes++;SysTime.seconds=0}
else {SysTime.seconds++;}
SysTime.milliseconds=0;}
else { SysTime.milliseconds++;}
MCAL_PIC_SysClkHandler();} //provides further functionality to the user
}
void MCAL_PIC_Timer0_SetCM(u8_t OVT0){
TMR0=OVT0 ;
return;}
Time MCAL_PIC_GetTime(void){return SysTime;}
void MCAL_PIC_ResetTime(void){SysTime.milliseconds=0;SysTime.seconds=0;SysTime.minutes=0;}
/*function to set call back for desired function to happen in every case of timer-related interrupts
OUTPUTS: NONE
INPUTS: Signal ID,pointer to the desired function
SIGNALS: OVERFLOW ,COMPARE MATCH
NOTES: */
void MCAL_PIC_SysClkredirect(void (*ptr) (void)){MCAL_PIC_SysClkHandler=ptr;}
void MCAL_PIC_SysClkDef(void){return;}