Subversion Repositories Code-Repo

Rev

Rev 119 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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