-
Notifications
You must be signed in to change notification settings - Fork 0
/
hal_timer1.c
157 lines (133 loc) · 4.26 KB
/
hal_timer1.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
/*
* File: timer1.c
* Author: Kareem Taha
*
* Created on November 3, 2022, 9:03 AM
*/
/* Section : Includes */
#include "timer1.h"
/* Section : Macros Definition */
/* Section : Macros Functions Declaration */
/* Section : Data Types Declaration */
/* Section : Function Declaration */
static inline void Timer1_mode_select(const timer1_t *_timer);
#if TIMER1_INTERRUPT_FEATURE_ENABLE == Interrupt_Feature_Enable
static void(*TIMER1_InterruptHandler)(void) = NULL;
#endif
static uint16 timer1_preload_value = 0;
/**
* @brief : initialize timer1
* @param _timer
* @return return 1 if initialization made successfully or 0.
*/
STD_ReturnType timer1_init(const timer1_t *_timer){
STD_ReturnType ret = E_OK;
if(_timer == NULL) ret = E_NOT_OK;
else{
/*disable timer1*/
TIMER1_DISABLE_MODULE();
/*Prescaler config*/
T1CONbits.T1CKPS = (_timer->timer1_prescaler);
/*timer0 mode select*/
Timer1_mode_select(_timer);
/* preload value */
TMR1H = (_timer->timer1_preload_value) >> 8;
TMR1L = (uint8)(_timer->timer1_preload_value);
timer1_preload_value = (_timer->timer1_preload_value);
/*________ Interrupt ______*/
#if TIMER1_INTERRUPT_FEATURE_ENABLE == Interrupt_Feature_Enable
TIMER1_Interrupt_Enable();
TIMER1_Interrupt_Flag_Clear();
TIMER1_InterruptHandler = (_timer->TIMER1_InterruptHandler);
#if Interrupt_Priority_Levels_Enable == Interrupt_Feature_Enable
Interrupt_PriorityLevelsEnable();
if(_timer->priority == INTERRUPT_High_Priority){
Interrupt_GlobalInterruptHighEnable();
TIMER1_Interrupt_Priority_High();
}
else{
Interrupt_GlobalInterruptLowEnable();
TIMER1_Interrupt_Priority_Low();
}
#else
/*Enable Global Interrupt */
Interrupt_GlobalInterruptEnable();
/*Enable Peripherals Interrupt */
Interrupt_PeripheralInterruptEnable();
#endif
#endif
/*enable timer1*/
TIMER1_ENABLE_MODULE();
}
return ret;
}
/**
* @brief : De initialize timer1
* @param _timer
* @return return 1 if initialization made successfully or 0.
*/
STD_ReturnType timer1_Deinit(const timer1_t *_timer){
STD_ReturnType ret = E_OK;
if(_timer == NULL) ret = E_NOT_OK;
else{
/* disable timer1*/
TIMER1_DISABLE_MODULE();
/*disable interrupt*/
#if TIMER1_INTERRUPT_FEATURE_ENABLE == Interrupt_Feature_Enable
TIMER1_Interrupt_Disable();
#endif
}
return ret;
}
/**
* @brief : write value at counter of timer1
* @param _timer & value
* @return return 1 if initialization made successfully or 0.
*/
STD_ReturnType timer1_write_value(const timer1_t *_timer, uint16 _value){
STD_ReturnType ret = E_OK;
if(_timer == NULL) ret = E_NOT_OK;
else{
TMR1H = (_value) >> 8;
TMR1L = (uint8)(_value);
}
return ret;
}
/**
* @brief : read value from counter of timer1
* @param _timer & pointer of value
* @return return 1 if initialization made successfully or 0.
*/
STD_ReturnType timer1_read_value(const timer1_t *_timer, uint16 *_value){
STD_ReturnType ret = E_OK;
uint8 tmr1_low = 0, tmr1_high = 0;
if(_timer == NULL || _value == NULL) ret = E_NOT_OK;
else{
tmr1_low = TMR1L;
tmr1_high = TMR1H;
(*_value) = (uint16)(tmr1_high << 8) + (tmr1_low);
}
return ret;
}
/*_____Function of interrupt____*/
void TIMER1_ISR(void){
TIMER1_Interrupt_Flag_Clear();
TMR1H = (timer1_preload_value) >> 8;
TMR1L = (uint8)(timer1_preload_value);
if(TIMER1_InterruptHandler){
TIMER1_InterruptHandler();
}
}
/*____timer mode select & edge____*/
static inline void Timer1_mode_select(const timer1_t *_timer){
if((_timer->timer1_mode) == TIMER1_SOURCE_TIMER_MOD){
TIMER1_SOURCE_TIMER_MODE();
}else{
TIMER1_SOURCE_COUNTER_MODE();
if((_timer->timer1_counter_mode) == TIMER1_SYNC_COUNTER_MOD){
TIMER1_SYNC_COUNTER_MODE();
}else{
TIMER1_ASYNC_COUNTER_MODE();
}
}
}