Subversion Repositories Code-Repo

Rev

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

Rev 222 Rev 223
Line 1... Line 1...
1
#include <xc.h>
1
#include <xc.h>
2
#include "main.h"
2
#include "main.h"
3
#include "TIMER.h"
3
#include "TIMER.h"
4
 
4
 
5
char timer_2_pwm = 0;
5
void (*timer_1_callback)(void);
6
extern char LED_R_ON;
-
 
7
extern char LED_G_ON;
-
 
8
extern char LED_B_ON;
6
void (*timer_2_callback)(void);
9
 
7
 
10
void TIMER_1_Init(void) {
8
void TIMER_1_Init(void (*callback)(void)) {
-
 
9
    timer_1_callback = callback;
11
    T1CONbits.TMR1CS = 2;   // Clock source is ext oscillator
10
    T1CONbits.TMR1CS = 2;   // Clock source is ext oscillator
12
    T1CONbits.T1CKPS = 0;   // Prescale of 1:1
11
    T1CONbits.T1CKPS = 0;   // Prescale of 1:1
13
    T1CONbits.T1OSCEN = 1;  // Dedicated oscillator circuit enabled
12
    T1CONbits.T1OSCEN = 1;  // Dedicated oscillator circuit enabled
14
    T1CONbits.nT1SYNC = 1;  // Async external clock input
13
    T1CONbits.nT1SYNC = 1;  // Async external clock input
15
    T1CONbits.TMR1ON = 0;   // Timer stopped
14
    T1CONbits.TMR1ON = 0;   // Timer stopped
Line 26... Line 25...
26
void TIMER_1_Stop(void) {
25
void TIMER_1_Stop(void) {
27
    T1CONbits.TMR1ON = 0;   // Stop timer
26
    T1CONbits.TMR1ON = 0;   // Stop timer
28
}
27
}
29
 
28
 
30
void TIMER_1_Interrupt_Handler(void) {
29
void TIMER_1_Interrupt_Handler(void) {
-
 
30
    timer_1_callback();
31
    
31
    TMR1 = 0x8000;
32
}
32
}
33
 
33
 
34
void TIMER_2_Init(void) {
34
void TIMER_2_Init(void (*callback)(void)) {
-
 
35
    timer_2_callback = callback;
35
    T2CONbits.T2OUTPS = 0;    // 1:1 Postscaler
36
    T2CONbits.T2OUTPS = 0;  // 1:1 Postscaler
36
    T2CONbits.TMR2ON = 0;       // Timer stopped
37
    T2CONbits.TMR2ON = 0;   // Timer stopped
37
    T2CONbits.T2CKPS = 0;    // 1:1 Perscaler
38
    T2CONbits.T2CKPS = 0;   // 1:1 Perscaler
38
 
39
 
39
    PIE1bits.TMR2IE = 1;   // Timer 2 overflow interrupt
40
    PIE1bits.TMR2IE = 1;    // Timer 2 overflow interrupt
40
}
41
}
41
 
42
 
42
void TIMER_2_Start(void) {
43
void TIMER_2_Start(void) {
43
    T2CONbits.TMR2ON = 1;   // Start timer
44
    T2CONbits.TMR2ON = 1;   // Start timer
44
}
45
}
Line 46... Line 47...
46
void TIMER_2_Stop(void) {
47
void TIMER_2_Stop(void) {
47
    T2CONbits.TMR2ON = 0;   // Stop timer
48
    T2CONbits.TMR2ON = 0;   // Stop timer
48
}
49
}
49
 
50
 
50
void TIMER_2_Interrupt_Handler(void) {
51
void TIMER_2_Interrupt_Handler(void) {
51
    // Here we manually 'PWM' the LEDs
-
 
52
    // Note: this is terribly inefficient but we need to do this
-
 
53
    //  otherwise we will blow out the blue LED (max 10mA)
-
 
54
    if (timer_2_pwm == 0) {
-
 
55
        LED_R_LAT = (LED_R_ON) ? 0 : 1;
-
 
56
        LED_G_LAT = (LED_G_ON) ? 0 : 1;
-
 
57
        LED_B_LAT = (LED_B_ON) ? 0 : 1;
-
 
58
    } 
-
 
59
    if (timer_2_pwm == LED_R_MAX_BRIGHTNESS) {
-
 
60
        LED_R_LAT = 1;
-
 
61
    }
-
 
62
    if (timer_2_pwm == LED_G_MAX_BRIGHTNESS) {
-
 
63
        LED_G_LAT = 1;
-
 
64
    }
-
 
65
    if (timer_2_pwm == LED_B_MAX_BRIGHTNESS) {
-
 
66
        LED_B_LAT = 1;
-
 
67
    }
-
 
68
    timer_2_pwm++;
52
    timer_2_callback();
69
}
53
}
70
54