Subversion Repositories Code-Repo

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
199 Kevin 1
#include "defines.h"
2
#include "TIMER5.h"
3
 
206 Kevin 4
static TIMER5_DATA *timer_data_ptr;
199 Kevin 5
 
231 Kevin 6
void TIMER5_Init(TIMER5_DATA *data, void (*callback)(void), uint32_t time_us) {
201 Kevin 7
    if (data != NULL) // if ptr is null, use existing data
8
        timer_data_ptr = data;
206 Kevin 9
 
201 Kevin 10
    timer_data_ptr->callback_function = callback;
11
 
206 Kevin 12
    INTDisableInterrupts();
199 Kevin 13
 
206 Kevin 14
    T5CON = 0x0;
15
 
16
    // PR5 is 16 bits wide, so we need to determine what pre-scaler to use
231 Kevin 17
    uint16_t time;
206 Kevin 18
    if (time_us < 13107) {
19
        time = 5 * time_us;
20
        T5CONSET = 0x0040;         // Prescaler at 1:16
21
    } else if (time_us < 26214) {
22
        time = 2.5 * time_us;
23
        T5CONSET = 0x0050;         // Prescaler at 1:32
24
    } else if (time_us < 52428) {
25
        time = 1.25 * time_us;
26
        T5CONSET = 0x0060;         // Prescaler at 1:64
27
    } else { // Minimum time_us of 209712
28
        time = 0.3125 * time_us;
29
        T5CONSET = 0x0070;         // Prescaler at 1:256
30
    }
31
 
199 Kevin 32
    Nop();
33
    TMR5 = 0x0;             // Clear timer register
34
    PR5 = time;             // Load period register
234 Kevin 35
    IPC5SET = 0x0000000D;   // Set priority level = 3, sub-priority level = 1
199 Kevin 36
    IFS0CLR = 0x00100000;   // Clear timer interrupt flag
37
    IEC0SET = 0x00100000;   // Enable timer interrupt
38
 
39
    INTEnableInterrupts();
40
}
41
 
42
void TIMER5_Start(void) {
43
    T5CONSET = 0x8000; // Start timer
44
}
45
 
46
void TIMER5_Stop(void) {
47
    T5CONCLR = 0x8000; // Stop timer
48
}
49
 
50
void __ISR(_TIMER_5_VECTOR, ipl4) __TIMER_5_Interrupt_Handler(void) {
51
    // Call the saved callback function
201 Kevin 52
    (*timer_data_ptr->callback_function)();
206 Kevin 53
 
199 Kevin 54
    IFS0CLR = 0x00100000; // Clear the timer interrupt flag
206 Kevin 55
}