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