Subversion Repositories Code-Repo

Rev

Rev 202 | Go to most recent revision | Blame | Last modification | View Log | RSS feed

#include <xc.h>
#include "main.h"
#include "TIMER.h"

char timer_2_pwm = 0;
extern char LED_R_ON;
extern char LED_G_ON;
extern char LED_B_ON;

void TIMER_1_Init(void) {
    T1CONbits.TMR1CS = 2;   // Clock source is ext oscillator
    T1CONbits.T1CKPS = 0;   // Prescale of 1:1
    T1CONbits.T1OSCEN = 1;  // Dedicated oscillator circuit enabled
    T1CONbits.nT1SYNC = 1;  // Async external clock input
    T1CONbits.TMR1ON = 0;   // Timer stopped
    T1GCONbits.TMR1GE = 0;  // Gate disabled
    TMR1 = 0x8000;

    PIE1bits.TMR1IE = 1;    // Timer 1 overflow interrupt
}

void TIMER_1_Start(void) {
    T1CONbits.TMR1ON = 1;   // Start timer
}

void TIMER_1_Stop(void) {
    T1CONbits.TMR1ON = 0;   // Stop timer
}

void TIMER_1_Interrupt_Handler(void) {
    
}

void TIMER_2_Init(void) {
    T2CONbits.T2OUTPS = 0;    // 1:1 Postscaler
    T2CONbits.TMR2ON = 0;       // Timer stopped
    T2CONbits.T2CKPS = 0;    // 1:1 Perscaler

    PIE1bits.TMR2IE = 1;   // Timer 2 overflow interrupt
}

void TIMER_2_Start(void) {
    T2CONbits.TMR2ON = 1;   // Start timer
}

void TIMER_2_Stop(void) {
    T2CONbits.TMR2ON = 0;   // Stop timer
}

void TIMER_2_Interrupt_Handler(void) {
    // Here we manually 'PWM' the LEDs
    // Note: this is terribly inefficient but we need to do this
    //  otherwise we will blow out the blue LED (max 10mA)
    if (timer_2_pwm == 0) {
        LED_R_LAT = (LED_R_ON) ? 0 : 1;
        LED_G_LAT = (LED_G_ON) ? 0 : 1;
        LED_B_LAT = (LED_B_ON) ? 0 : 1;
    } 
    if (timer_2_pwm == LED_R_MAX_BRIGHTNESS) {
        LED_R_LAT = 1;
    }
    if (timer_2_pwm == LED_G_MAX_BRIGHTNESS) {
        LED_G_LAT = 1;
    }
    if (timer_2_pwm == LED_B_MAX_BRIGHTNESS) {
        LED_B_LAT = 1;
    }
    timer_2_pwm++;
}