| 199 |
Kevin |
1 |
#include <xc.h>
|
|
|
2 |
#include <plib.h>
|
|
|
3 |
#include "defines.h"
|
|
|
4 |
#include "TIMER5.h"
|
|
|
5 |
|
|
|
6 |
static void (*callback_function)(void);
|
|
|
7 |
|
|
|
8 |
void TIMER5_Init(void (*callback)(void), unsigned int time_us) {
|
|
|
9 |
callback_function = callback;
|
|
|
10 |
int time = 5 * time_us;
|
|
|
11 |
|
|
|
12 |
INTDisableInterrupts();
|
|
|
13 |
|
|
|
14 |
T5CON = 0x0040; // Prescaler at 1:16, clock from peripheral clock
|
|
|
15 |
Nop();
|
|
|
16 |
TMR5 = 0x0; // Clear timer register
|
|
|
17 |
PR5 = time; // Load period register
|
|
|
18 |
IPC5SET = 0x00000011; // Set priority level = 4, sub-priority level = 1
|
|
|
19 |
IFS0CLR = 0x00100000; // Clear timer interrupt flag
|
|
|
20 |
IEC0SET = 0x00100000; // Enable timer interrupt
|
|
|
21 |
|
|
|
22 |
INTEnableInterrupts();
|
|
|
23 |
}
|
|
|
24 |
|
|
|
25 |
void TIMER5_Start(void) {
|
|
|
26 |
T5CONSET = 0x8000; // Start timer
|
|
|
27 |
}
|
|
|
28 |
|
|
|
29 |
void TIMER5_Stop(void) {
|
|
|
30 |
T5CONCLR = 0x8000; // Stop timer
|
|
|
31 |
}
|
|
|
32 |
|
|
|
33 |
void __ISR(_TIMER_5_VECTOR, ipl4) __TIMER_5_Interrupt_Handler(void) {
|
|
|
34 |
// Call the saved callback function
|
|
|
35 |
(*callback_function)();
|
|
|
36 |
|
|
|
37 |
IFS0CLR = 0x00100000; // Clear the timer interrupt flag
|
|
|
38 |
}
|