Rev 231 | Blame | Last modification | View Log | Download | RSS feed
#include "defines.h"#include "TIMER4.h"static TIMER4_DATA *timer_data_ptr;void TIMER4_Init(TIMER4_DATA *data, void (*callback)(void), uint32_t time_us) {if (data != NULL) // if ptr is null, use existing datatimer_data_ptr = data;timer_data_ptr->callback_function = callback;INTDisableInterrupts();T4CON = 0x0;// PR4 is 16 bits wide, so we need to determine what pre-scaler to useuint16_t time;if (time_us < 13107) {time = 5 * time_us;T4CONSET = 0x0040; // Prescaler at 1:16} else if (time_us < 26214) {time = 2.5 * time_us;T4CONSET = 0x0050; // Prescaler at 1:32} else if (time_us < 52428) {time = 1.25 * time_us;T4CONSET = 0x0060; // Prescaler at 1:64} else {time = 0.3125 * time_us; // Minimum time_us of 209712T4CONSET = 0x0070; // Prescaler at 1:256}Nop();TMR4 = 0x0; // Clear timer registerPR4 = time; // Load period registerIPC4SET = 0x0000000D; // Set priority level = 3, sub-priority level = 1IFS0CLR = 0x00010000; // Clear timer interrupt flagIEC0SET = 0x00010000; // Enable timer interruptINTEnableInterrupts();}void TIMER4_Start(void) {T4CONSET = 0x8000; // Start timer}void TIMER4_Stop(void) {T4CONCLR = 0x8000; // Stop timer}void __ISR(_TIMER_4_VECTOR, ipl4) __TIMER_4_Interrupt_Handler(void) {// Call the saved callback function(*timer_data_ptr->callback_function)();IFS0CLR = 0x00010000; // Clear the timer interrupt flag}