Subversion Repositories Code-Repo

Rev

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