Subversion Repositories Code-Repo

Rev

Rev 115 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
113 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"
6
#include "pwm.h"
7
 
115 Kevin 8
unsigned char overflow_counter;
113 Kevin 9
 
10
void timers_init() {
11
    /*--------------------Timer Delay Formulas-------------------- */
12
    /* InitTMR0 = 256 - ( Delay * Frequency ) / ( 4* Prescaler)    */
13
    /* Delay = (256 - InitTMR0 * Prescaler) / (Frequency / 4)      */
14
    /* ----------------------------------------------------------- */
114 Kevin 15
 
16
    // Sleep timer
113 Kevin 17
    OpenTimer0(TIMER_INT_ON & T0_16BIT & T0_SOURCE_INT & T0_PS_1_256);
18
    T0CONbits.TMR0ON = 0;   // Timer 0 initially off
115 Kevin 19
 
20
#ifdef _BASE_STATION
117 Kevin 21
    OpenTimer1(TIMER_INT_ON & T1_16BIT_RW &
22
                T1_SOURCE_FOSC_4 & T1_PS_1_1 &
23
                T1_OSC1EN_OFF & T1_SYNC_EXT_OFF,
24
                TIMER_GATE_OFF & TIMER_GATE_INT_OFF);
25
    T1CONbits.TMR1ON = 0;   // Timer 1 initially off
26
    overflow_counter = 0;
115 Kevin 27
#endif
114 Kevin 28
#ifdef _REMOTE
29
    // Data polling timer
113 Kevin 30
    OpenTimer1(TIMER_INT_ON & T1_16BIT_RW &
114 Kevin 31
                T1_SOURCE_FOSC_4 & T1_PS_1_1 &
113 Kevin 32
                T1_OSC1EN_OFF & T1_SYNC_EXT_OFF,
33
                TIMER_GATE_OFF & TIMER_GATE_INT_OFF);
34
    T1CONbits.TMR1ON = 0;   // Timer 1 initially off
115 Kevin 35
    overflow_counter = 0;
113 Kevin 36
 
37
    // Open timer 2 for ECCP1 (PWM)
38
    OpenTimer2(TIMER_INT_OFF & T2_PS_1_4 & T2_POST_1_1);
39
 
40
    // Open timer 3 for PWM IR signaling
41
    OpenTimer3(TIMER_INT_ON & T3_16BIT_RW & T3_SOURCE_FOSC_4 & 
42
            T3_OSC1EN_OFF & T3_PS_1_1 & T3_SYNC_EXT_OFF, TIMER_GATE_OFF);
43
    T3CONbits.TMR3ON = 0;   // Timer 3 initially off
114 Kevin 44
#endif
113 Kevin 45
}
46
 
47
// Interrupt handler for timer 0
48
void timer0_interrupt_handler() {
49
    timer0_disable();   // Run once!
50
    MQ_sendmsg_ToMainFromLow(0, MSGTYPE_TIMER0, (void *) 0);
51
}
52
 
53
void timer0_enable() {
54
    WriteTimer0(0);
55
    T0CONbits.TMR0ON = 1;
56
}
57
 
58
void timer0_disable() {
59
    T0CONbits.TMR0ON = 0;
60
}
61
 
62
// Interrupt handler for timer 1
63
void timer1_interrupt_handler() {
114 Kevin 64
#ifdef _REMOTE
115 Kevin 65
    overflow_counter++;
66
    if (overflow_counter % 3 == 0) {
113 Kevin 67
        MQ_sendmsg_ToMainFromLow(0, MSGTYPE_TIMER1, (void *) 0);
68
    }
114 Kevin 69
#endif
117 Kevin 70
#ifdef _BASE_STATION
71
    MQ_sendmsg_ToMainFromLow(0, MSGTYPE_TIMER1, (void *) 0);
72
    timer1_disable();
73
#endif
113 Kevin 74
}
75
 
76
void timer1_enable() {
77
    WriteTimer1(0);
78
    T1CONbits.TMR1ON = 1;
79
}
80
 
81
void timer1_disable() {
82
    T1CONbits.TMR1ON = 0;
83
}
84
 
115 Kevin 85
// Interrupt handler for timer 2
86
void timer2_interrupt_handler() {
117 Kevin 87
    MQ_sendmsg_ToMainFromLow(0, MSGTYPE_TIMER2, (void *) 0);
115 Kevin 88
}
89
 
90
void timer2_enable() {
91
    WriteTimer2(0);
92
    T2CONbits.TMR2ON = 1;
93
}
94
 
95
void timer2_disable() {
96
    T2CONbits.TMR2ON = 0;
97
}
98
 
113 Kevin 99
void timer3_interrupt_handler() {
114 Kevin 100
#ifdef _REMOTE
101
    if (!PWM_IR_STATE) {
113 Kevin 102
        // Turn on PWM
103
        pwm_IR_start();
114 Kevin 104
        PWM_IR_STATE = 1;
113 Kevin 105
        WriteTimer3(0xEA00);    // Send 38kHz pulses for 600us
106
    } else {
107
        // Turn off PWM
108
        pwm_IR_stop();
114 Kevin 109
        PWM_IR_STATE = 0;
115 Kevin 110
//        WriteTimer3(0xEA00);    // Send low for 0.6ms
113 Kevin 111
//        WriteTimer3(0xD800);    // Send low for 1ms
112
//        WriteTimer3(0xB000);    // Send low for 2ms
115 Kevin 113
        WriteTimer3(0x1000);    // Send low for 6ms
113 Kevin 114
    }
114 Kevin 115
#endif
113 Kevin 116
}
117
 
118
void timer3_enable() {
119
    // Enable timer and start PWM
120
    T3CONbits.TMR3ON = 1;
121
    pwm_IR_start();
114 Kevin 122
    PWM_IR_STATE = 1;
113 Kevin 123
}
124
 
125
void timer3_disable() {
126
    // Disable timer and stop PWM
127
    T3CONbits.TMR3ON = 0;
128
    pwm_IR_stop();
114 Kevin 129
    PWM_IR_STATE = 0;
113 Kevin 130
}