3,20 → 3,34 |
#include "defines.h" |
#include "TIMER5.h" |
|
static TIMER_DATA *timer_data_ptr; |
static TIMER5_DATA *timer_data_ptr; |
|
void TIMER5_Init(TIMER_DATA *data, void (*callback)(void), unsigned int time_us) { |
void TIMER5_Init(TIMER5_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(); |
|
INTDisableInterrupts(); |
|
T5CON = 0x0040; // Prescaler at 1:16, clock from peripheral clock |
T5CON = 0x0; |
|
// PR5 is 16 bits wide, so we need to determine what pre-scaler to use |
int time; |
if (time_us < 13107) { |
time = 5 * time_us; |
T5CONSET = 0x0040; // Prescaler at 1:16 |
} else if (time_us < 26214) { |
time = 2.5 * time_us; |
T5CONSET = 0x0050; // Prescaler at 1:32 |
} else if (time_us < 52428) { |
time = 1.25 * time_us; |
T5CONSET = 0x0060; // Prescaler at 1:64 |
} else { // Minimum time_us of 209712 |
time = 0.3125 * time_us; |
T5CONSET = 0x0070; // Prescaler at 1:256 |
} |
|
Nop(); |
TMR5 = 0x0; // Clear timer register |
PR5 = time; // Load period register |
38,6 → 52,6 |
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 |
} |
} |