Subversion Repositories Code-Repo

Rev

Rev 107 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
107 Kevin 1
/* The processor calls these handlers when an interrupt is triggered */
2
#include "maindefs.h"
3
#include "msg_queues.h"
4
#include <timers.h>
5
#include "timers.h"
111 Kevin 6
#include "pwm.h"
107 Kevin 7
 
8
void timers_init() {
9
    /*--------------------Timer Delay Formulas-------------------- */
10
    /* InitTMR0 = 256 - ( Delay * Frequency ) / ( 4* Prescaler)    */
11
    /* Delay = (256 - InitTMR0 * Prescaler) / (Frequency / 4)      */
12
    /* ----------------------------------------------------------- */
111 Kevin 13
 
14
#ifdef _BASE_STATION
15
    OpenTimer0(TIMER_INT_ON & T0_16BIT & T0_SOURCE_INT & T0_PS_1_32);
16
    T0CONbits.TMR0ON = 0;   // Timer 0 initially off
17
#endif
107 Kevin 18
 
111 Kevin 19
//    // Set timer 1 to overflow every two seconds
107 Kevin 20
//    OpenTimer1(TIMER_INT_ON & T1_16BIT_RW &
21
//                T1_SOURCE_PINOSC & T1_PS_1_1 &
22
//                T1_OSC1EN_ON & T1_SYNC_EXT_OFF,
23
//                TIMER_GATE_OFF & TIMER_GATE_INT_OFF);
111 Kevin 24
 
25
#ifdef _REMOTE
26
    // Open timer 2 for ECCP1 (PWM)
27
    OpenTimer2(TIMER_INT_OFF & T2_PS_1_4 & T2_POST_1_1);
107 Kevin 28
 
111 Kevin 29
    // Open timer 3 for PWM IR signaling
30
    OpenTimer3(TIMER_INT_ON & T3_16BIT_RW & T3_SOURCE_FOSC_4 & 
31
            T3_OSC1EN_OFF & T3_PS_1_1 & T3_SYNC_EXT_OFF, TIMER_GATE_OFF);
32
    T3CONbits.TMR3ON = 0;   // Timer 3 initially off
33
#endif
107 Kevin 34
}
35
 
36
// Interrupt handler for timer 0
37
void timer0_interrupt_handler() {
111 Kevin 38
    MQ_sendmsg_ToMainFromLow(0, MSGTYPE_TIMER0, (void *) 0);
39
}
107 Kevin 40
 
111 Kevin 41
void timer0_enable() {
42
    T0CONbits.TMR0ON = 1;
107 Kevin 43
}
44
 
111 Kevin 45
void timer0_disable() {
46
    T0CONbits.TMR0ON = 0;
47
}
48
 
107 Kevin 49
// Interrupt handler for timer 1
50
void timer1_interrupt_handler() {
51
    // Set timer to overflow every 10ms
52
    WriteTimer1(62259);
111 Kevin 53
}
107 Kevin 54
 
111 Kevin 55
void timer3_interrupt_handler() {
56
    if (!pwm_on) {
57
        // Turn on PWM
58
        pwm_start();
59
        pwm_on = 1;
60
        WriteTimer3(0xE500);    // Send 38kHz pulses for 600us
61
    } else {
62
        // Turn off PWM
63
        pwm_stop();
64
        pwm_on = 0;
65
        WriteTimer3(0xE500);    // Send low for 0.6ms
66
//        WriteTimer3(0xD000);    // Send low for 1ms
67
//        WriteTimer3(0xA000);    // Send low for 2ms
68
//        WriteTimer3(0x1000);    // Send low for 5ms
69
    }
70
}
71
 
72
void timer3_enable() {
73
    // Enable timer and start PWM
74
    T3CONbits.TMR3ON = 1;
75
    pwm_start();
76
    pwm_on = 1;
77
}
78
 
79
void timer3_disable() {
80
    // Disable timer and stop PWM
81
    T3CONbits.TMR3ON = 0;
82
    pwm_stop();
83
    pwm_on = 0;
84
}