esp-2014-15 / tp2g6 / Exercise 2 / timer_config.c @ 83
History | View | Annotate | Download (3.97 KB)
1 |
|
---|---|
2 |
/**
|
3 |
* *****************************************************************************
|
4 |
* @file timers.c
|
5 |
* @author Jorge
|
6 |
* @version 1.0
|
7 |
* @date Oct 2014
|
8 |
* @brief This file contains drivers for Timer2
|
9 |
*******************************************************************************
|
10 |
* @copy
|
11 |
*
|
12 |
* THIS FILE CONTAINS A BASIC FUNCTION TO HANDLE TIMER2 IN PIC32MX795F512H
|
13 |
* TO BE USED FOR THE COURSE OF EMBEDDED SYSTEMS PROGRAMMING.
|
14 |
*
|
15 |
* UNIVERSIDADE DE AVEIRO
|
16 |
*/
|
17 |
|
18 |
#include <stdio.h> |
19 |
#include <stdlib.h> |
20 |
#include <stdint.h> |
21 |
#include <xc.h> |
22 |
#include "timer_config.h" |
23 |
|
24 |
|
25 |
int timer_enable(int timer) |
26 |
{ |
27 |
if(timer < 2 || timer > 5) |
28 |
return -1; |
29 |
|
30 |
switch (timer){
|
31 |
case 2: |
32 |
T2CONbits.ON = 1;
|
33 |
break;
|
34 |
case 3: |
35 |
T3CONbits.ON = 1;
|
36 |
break;
|
37 |
case 4: |
38 |
T4CONbits.ON = 1;
|
39 |
break;
|
40 |
case 5: |
41 |
T5CONbits.ON = 1;
|
42 |
break;
|
43 |
} |
44 |
return 0; |
45 |
} |
46 |
|
47 |
|
48 |
|
49 |
int timer_disable(int timer) |
50 |
{ |
51 |
if(timer < 2 || timer > 5) |
52 |
return -1; |
53 |
|
54 |
switch (timer){
|
55 |
case 2: |
56 |
T2CONbits.ON = 0;
|
57 |
break;
|
58 |
case 3: |
59 |
T3CONbits.ON = 0;
|
60 |
break;
|
61 |
case 4: |
62 |
T4CONbits.ON = 0;
|
63 |
break;
|
64 |
case 5: |
65 |
T5CONbits.ON = 0;
|
66 |
break;
|
67 |
} |
68 |
return 0; |
69 |
} |
70 |
|
71 |
int set_timer_freq(int timer,unsigned int freq) |
72 |
{ |
73 |
const unsigned int prescaler_values[] = {1, 2, 4, 8, 16, 32, 64, 256}; |
74 |
|
75 |
if(freq > PBCLK || freq < MIN_FREQ) // frequency of timer out of bounds |
76 |
return -1; |
77 |
if(timer < 2 || timer > 5) |
78 |
return -1; |
79 |
|
80 |
unsigned int pr = MAX_PR_VALUE + 1; |
81 |
unsigned int i = 0; |
82 |
while(pr > MAX_PR_VALUE)
|
83 |
{ |
84 |
pr = (PBCLK / (prescaler_values[i] * freq)) - 1;
|
85 |
i++; |
86 |
} |
87 |
|
88 |
i-=1;
|
89 |
switch (timer){
|
90 |
case 2: |
91 |
T2CONbits.TCKPS = i; |
92 |
PR2 = pr; |
93 |
TMR2 = 0;
|
94 |
break;
|
95 |
case 3: |
96 |
T3CONbits.TCKPS = i; |
97 |
PR3 = pr; |
98 |
TMR3 = 0;
|
99 |
break;
|
100 |
case 4: |
101 |
T4CONbits.TCKPS = i; |
102 |
PR4 = pr; |
103 |
TMR4 = 0;
|
104 |
break;
|
105 |
case 5: |
106 |
T5CONbits.TCKPS = i; |
107 |
PR5 = pr; |
108 |
TMR5 = 0;
|
109 |
break;
|
110 |
} |
111 |
|
112 |
return 0; |
113 |
} |
114 |
|
115 |
|
116 |
int read_timer_IntFlag(int timer) |
117 |
{ |
118 |
if(timer < 2 || timer > 5) |
119 |
return -1; |
120 |
|
121 |
switch (timer){
|
122 |
case 2: |
123 |
return IFS0bits.T2IF;
|
124 |
case 3: |
125 |
return IFS0bits.T3IF;
|
126 |
case 4: |
127 |
return IFS0bits.T4IF;
|
128 |
case 5: |
129 |
return IFS0bits.T5IF;
|
130 |
} |
131 |
|
132 |
return 0; |
133 |
} |
134 |
|
135 |
|
136 |
int reset_timer_IntFlag(int timer) |
137 |
{ |
138 |
if(timer < 2 || timer > 5) |
139 |
return -1; |
140 |
|
141 |
switch (timer){
|
142 |
case 2: |
143 |
IFS0bits.T2IF = 0;
|
144 |
break;
|
145 |
case 3: |
146 |
IFS0bits.T3IF = 0;
|
147 |
break;
|
148 |
case 4: |
149 |
IFS0bits.T4IF = 0;
|
150 |
break;
|
151 |
case 5: |
152 |
IFS0bits.T5IF = 0;
|
153 |
break;
|
154 |
} |
155 |
return 0; |
156 |
} |
157 |
|
158 |
int set_timer_Int(int timer, int enable, int priority) |
159 |
{ |
160 |
if(priority < 0 || priority > 7 || enable < 0 || enable > 1 || timer < 1 || timer > 5) |
161 |
return -1; |
162 |
|
163 |
switch (timer){
|
164 |
case 2: |
165 |
IFS0bits.T2IF = 0;
|
166 |
IPC2bits.T2IP = priority; |
167 |
IEC0bits.T2IE = enable; |
168 |
break;
|
169 |
case 3: |
170 |
IFS0bits.T3IF = 0;
|
171 |
IPC3bits.T3IP = priority; |
172 |
IEC0bits.T3IE = enable; |
173 |
break;
|
174 |
case 4: |
175 |
IFS0bits.T4IF = 0;
|
176 |
IPC4bits.T4IP = priority; |
177 |
IEC0bits.T4IE = enable; |
178 |
break;
|
179 |
case 5: |
180 |
IFS0bits.T5IF = 0;
|
181 |
IPC5bits.T5IP = priority; |
182 |
IEC0bits.T5IE = enable; |
183 |
break;
|
184 |
} |
185 |
|
186 |
return 0; |
187 |
} |
188 |
|
189 |
int set_ALL_Int(int enable){ |
190 |
|
191 |
if(enable < 0 || enable > 1) |
192 |
return -1; |
193 |
|
194 |
INTEnableSystemMultiVectoredInt(); |
195 |
if(enable){
|
196 |
EnableInterrupts(); |
197 |
}else
|
198 |
DisableInterrupts(); |
199 |
|
200 |
return 0; |
201 |
} |
202 |
|
203 |
|