Subversion Repositories Code-Repo

Rev

Details | Last modification | View Log | RSS feed

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