Subversion Repositories Code-Repo

Rev

Rev 201 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 201 Rev 206
Line 1... Line 1...
1
#include <xc.h>
1
#include <xc.h>
2
#include <plib.h>
2
#include <plib.h>
3
#include "defines.h"
3
#include "defines.h"
4
#include "TIMER5.h"
4
#include "TIMER5.h"
5
 
5
 
6
static TIMER_DATA *timer_data_ptr;
6
static TIMER5_DATA *timer_data_ptr;
7
 
7
 
8
void TIMER5_Init(TIMER_DATA *data, void (*callback)(void), unsigned int time_us) {
8
void TIMER5_Init(TIMER5_DATA *data, void (*callback)(void), unsigned int time_us) {
9
    if (data != NULL) // if ptr is null, use existing data
9
    if (data != NULL) // if ptr is null, use existing data
10
        timer_data_ptr = data;
10
        timer_data_ptr = data;
11
    
-
 
12
    timer_data_ptr->callback_function = callback;
-
 
13
 
11
 
14
    // Note: PR5 is 16-bit wide! (max time_us = 13107)
12
    timer_data_ptr->callback_function = callback;
15
    int time = 5 * time_us;
-
 
16
 
13
 
17
    INTDisableInterrupts();
14
    INTDisableInterrupts();
-
 
15
 
-
 
16
    T5CON = 0x0;
-
 
17
 
-
 
18
    // PR5 is 16 bits wide, so we need to determine what pre-scaler to use
18
    
19
    int time;
-
 
20
    if (time_us < 13107) {
-
 
21
        time = 5 * time_us;
19
    T5CON = 0x0040;         // Prescaler at 1:16, clock from peripheral clock
22
        T5CONSET = 0x0040;         // Prescaler at 1:16
-
 
23
    } else if (time_us < 26214) {
-
 
24
        time = 2.5 * time_us;
-
 
25
        T5CONSET = 0x0050;         // Prescaler at 1:32
-
 
26
    } else if (time_us < 52428) {
-
 
27
        time = 1.25 * time_us;
-
 
28
        T5CONSET = 0x0060;         // Prescaler at 1:64
-
 
29
    } else { // Minimum time_us of 209712
-
 
30
        time = 0.3125 * time_us;
-
 
31
        T5CONSET = 0x0070;         // Prescaler at 1:256
-
 
32
    }
-
 
33
 
20
    Nop();
34
    Nop();
21
    TMR5 = 0x0;             // Clear timer register
35
    TMR5 = 0x0;             // Clear timer register
22
    PR5 = time;             // Load period register
36
    PR5 = time;             // Load period register
23
    IPC5SET = 0x00000011;   // Set priority level = 4, sub-priority level = 1
37
    IPC5SET = 0x00000011;   // Set priority level = 4, sub-priority level = 1
24
    IFS0CLR = 0x00100000;   // Clear timer interrupt flag
38
    IFS0CLR = 0x00100000;   // Clear timer interrupt flag
Line 36... Line 50...
36
}
50
}
37
 
51
 
38
void __ISR(_TIMER_5_VECTOR, ipl4) __TIMER_5_Interrupt_Handler(void) {
52
void __ISR(_TIMER_5_VECTOR, ipl4) __TIMER_5_Interrupt_Handler(void) {
39
    // Call the saved callback function
53
    // Call the saved callback function
40
    (*timer_data_ptr->callback_function)();
54
    (*timer_data_ptr->callback_function)();
41
    
55
 
42
    IFS0CLR = 0x00100000; // Clear the timer interrupt flag
56
    IFS0CLR = 0x00100000; // Clear the timer interrupt flag
43
}
-
 
44
57
}
-
 
58