-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtimer.c
More file actions
55 lines (45 loc) · 1.01 KB
/
timer.c
File metadata and controls
55 lines (45 loc) · 1.01 KB
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
/*
* Timer 2 function implementations.
*
* @author Matas Kairaitis
* @author Marko Lazic
* @version 2016-12-12
*/
#include <pic32mx.h>
#include "timer.h"
#include "pid.h"
unsigned int timeElapsed = 0;
unsigned int throttleStart = 0;
/*
* Timer 2 Interrupt Service Routine
*/
void user_isr(void) {
IFS(0) = 0;
timeElapsed += 5;
if ((PORTE & 2) == 2 && throttleStart == 0) {
// Start clounting pulse
throttleStart = timeElapsed;
} else if ((PORTE & 2) != 2 && throttleStart > 0) {
// Pulse is over, set throttle to input pulse
setThrottle(timeElapsed - throttleStart);
throttleStart = 0;
}
}
void timerBegin() {
T2CON = 0x0;
TMR2 = 0x0;
PR2 = 80 * 5;
T2CONSET = 0x8000;
volatile int* iec = 0xbf881060;
volatile int* ipc = 0xbf8810b0;
*iec = *iec | 0x100;
*ipc = *ipc | 0x1c;
*ipc = *ipc | 0x3;
}
unsigned int getTime() {
return timeElapsed;
}
void delayMicroseconds(unsigned int delay) {
unsigned int startTime = timeElapsed;
while (timeElapsed < startTime + delay);
}