Subversion Repositories Code-Repo

Rev

Rev 111 | Blame | Compare with Previous | Last modification | View Log | RSS feed

/* The processor calls these handlers when an interrupt is triggered */
#include "maindefs.h"
#include "msg_queues.h"
#include <timers.h>
#include "timers.h"
#include "pwm.h"

void timers_init() {
    /*--------------------Timer Delay Formulas-------------------- */
    /* InitTMR0 = 256 - ( Delay * Frequency ) / ( 4* Prescaler)    */
    /* Delay = (256 - InitTMR0 * Prescaler) / (Frequency / 4)      */
    /* ----------------------------------------------------------- */
    
#ifdef _BASE_STATION
    OpenTimer0(TIMER_INT_ON & T0_16BIT & T0_SOURCE_INT & T0_PS_1_16);
    T0CONbits.TMR0ON = 0;   // Timer 0 initially off
#endif

//    // Set timer 1 to overflow every two seconds
//    OpenTimer1(TIMER_INT_ON & T1_16BIT_RW &
//                T1_SOURCE_PINOSC & T1_PS_1_1 &
//                T1_OSC1EN_ON & T1_SYNC_EXT_OFF,
//                TIMER_GATE_OFF & TIMER_GATE_INT_OFF);
    
#ifdef _REMOTE
    // Open timer 2 for ECCP1 (PWM)
    OpenTimer2(TIMER_INT_OFF & T2_PS_1_4 & T2_POST_1_1);

    // Open timer 3 for PWM IR signaling
    OpenTimer3(TIMER_INT_ON & T3_16BIT_RW & T3_SOURCE_FOSC_4 & 
            T3_OSC1EN_OFF & T3_PS_1_1 & T3_SYNC_EXT_OFF, TIMER_GATE_OFF);
    T3CONbits.TMR3ON = 0;   // Timer 3 initially off
#endif
}

// Interrupt handler for timer 0
void timer0_interrupt_handler() {
    MQ_sendmsg_ToMainFromLow(0, MSGTYPE_TIMER0, (void *) 0);
}

void timer0_enable() {
    T0CONbits.TMR0ON = 1;
}

void timer0_disable() {
    T0CONbits.TMR0ON = 0;
}

// Interrupt handler for timer 1
void timer1_interrupt_handler() {
    // Set timer to overflow every 10ms
    WriteTimer1(62259);
}

void timer3_interrupt_handler() {
    if (!pwm_on) {
        // Turn on PWM
        pwm_start();
        pwm_on = 1;
        WriteTimer3(0xE500);    // Send 38kHz pulses for 600us
    } else {
        // Turn off PWM
        pwm_stop();
        pwm_on = 0;
        WriteTimer3(0xE500);    // Send low for 0.6ms
//        WriteTimer3(0xD000);    // Send low for 1ms
//        WriteTimer3(0xA000);    // Send low for 2ms
//        WriteTimer3(0x1000);    // Send low for 5ms
    }
}

void timer3_enable() {
    // Enable timer and start PWM
    T3CONbits.TMR3ON = 1;
    pwm_start();
    pwm_on = 1;
}

void timer3_disable() {
    // Disable timer and stop PWM
    T3CONbits.TMR3ON = 0;
    pwm_stop();
    pwm_on = 0;
}