Subversion Repositories Code-Repo

Rev

Rev 223 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
202 Kevin 1
#include <xc.h>
2
#include "main.h"
3
#include "TIMER.h"
4
 
223 Kevin 5
void (*timer_1_callback)(void);
6
void (*timer_2_callback)(void);
222 Kevin 7
 
223 Kevin 8
void TIMER_1_Init(void (*callback)(void)) {
9
    timer_1_callback = callback;
202 Kevin 10
    T1CONbits.TMR1CS = 2;   // Clock source is ext oscillator
11
    T1CONbits.T1CKPS = 0;   // Prescale of 1:1
12
    T1CONbits.T1OSCEN = 1;  // Dedicated oscillator circuit enabled
13
    T1CONbits.nT1SYNC = 1;  // Async external clock input
14
    T1CONbits.TMR1ON = 0;   // Timer stopped
15
    T1GCONbits.TMR1GE = 0;  // Gate disabled
16
    TMR1 = 0x8000;
17
 
18
    PIE1bits.TMR1IE = 1;    // Timer 1 overflow interrupt
19
}
20
 
21
void TIMER_1_Start(void) {
22
    T1CONbits.TMR1ON = 1;   // Start timer
23
}
24
 
25
void TIMER_1_Stop(void) {
26
    T1CONbits.TMR1ON = 0;   // Stop timer
27
}
28
 
29
void TIMER_1_Interrupt_Handler(void) {
223 Kevin 30
    timer_1_callback();
31
    TMR1 = 0x8000;
202 Kevin 32
}
222 Kevin 33
 
223 Kevin 34
void TIMER_2_Init(void (*callback)(void)) {
35
    timer_2_callback = callback;
36
    T2CONbits.T2OUTPS = 0;  // 1:1 Postscaler
37
    T2CONbits.TMR2ON = 0;   // Timer stopped
38
    T2CONbits.T2CKPS = 0;   // 1:1 Perscaler
222 Kevin 39
 
223 Kevin 40
    PIE1bits.TMR2IE = 1;    // Timer 2 overflow interrupt
222 Kevin 41
}
42
 
43
void TIMER_2_Start(void) {
44
    T2CONbits.TMR2ON = 1;   // Start timer
45
}
46
 
47
void TIMER_2_Stop(void) {
48
    T2CONbits.TMR2ON = 0;   // Stop timer
49
}
50
 
51
void TIMER_2_Interrupt_Handler(void) {
223 Kevin 52
    timer_2_callback();
222 Kevin 53
}