Subversion Repositories Code-Repo

Rev

Rev 201 | 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 void (*callback_function)(void);

void TIMER5_Init(void (*callback)(void), unsigned int time_us) {
    callback_function = callback;
    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
    (*callback_function)();
    
    IFS0CLR = 0x00100000; // Clear the timer interrupt flag
}