Rev 199 | Rev 206 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
#include <xc.h>
#include <plib.h>
#include "defines.h"
#include "TIMER5.h"
static TIMER_DATA *timer_data_ptr;
void TIMER5_Init(TIMER_DATA *data, void (*callback)(void), unsigned int time_us) {
if (data != NULL) // if ptr is null, use existing data
timer_data_ptr = data;
timer_data_ptr->callback_function = callback;
// Note: PR5 is 16-bit wide! (max time_us = 13107)
int time = 5 * time_us;
INTDisableInterrupts();
T5CON = 0x0040; // Prescaler at 1:16, clock from peripheral clock
Nop();
TMR5 = 0x0; // Clear timer register
PR5 = time; // Load period register
IPC5SET = 0x00000011; // Set priority level = 4, sub-priority level = 1
IFS0CLR = 0x00100000; // Clear timer interrupt flag
IEC0SET = 0x00100000; // Enable timer interrupt
INTEnableInterrupts();
}
void TIMER5_Start(void) {
T5CONSET = 0x8000; // Start timer
}
void TIMER5_Stop(void) {
T5CONCLR = 0x8000; // Stop timer
}
void __ISR(_TIMER_5_VECTOR, ipl4) __TIMER_5_Interrupt_Handler(void) {
// Call the saved callback function
(*timer_data_ptr->callback_function)();
IFS0CLR = 0x00100000; // Clear the timer interrupt flag
}