Subversion Repositories Code-Repo

Rev

Rev 276 | Details | Compare with Previous | 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
237 Kevin 27
    } else { // Maximum time_us of 209712
206 Kevin 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
276 Kevin 35
//    IPC5SET = 0x0000000D;   // Set priority level = 3, sub-priority level = 1
36
    IPC5SET = 0x00000011;   // Set priority level = 4, sub-priority level = 1
199 Kevin 37
    IFS0CLR = 0x00100000;   // Clear timer interrupt flag
38
    IEC0SET = 0x00100000;   // Enable timer interrupt
39
 
40
    INTEnableInterrupts();
41
}
42
 
43
void TIMER5_Start(void) {
44
    T5CONSET = 0x8000; // Start timer
45
}
46
 
47
void TIMER5_Stop(void) {
48
    T5CONCLR = 0x8000; // Stop timer
49
}
50
 
51
void __ISR(_TIMER_5_VECTOR, ipl4) __TIMER_5_Interrupt_Handler(void) {
52
    // Call the saved callback function
201 Kevin 53
    (*timer_data_ptr->callback_function)();
206 Kevin 54
 
199 Kevin 55
    IFS0CLR = 0x00100000; // Clear the timer interrupt flag
206 Kevin 56
}