Subversion Repositories Code-Repo

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
113 Kevin 1
#include "maindefs.h"
2
#include "pin_interrupts.h"
3
#include "pwm.h"
4
#include "msg_queues.h"
5
#include <delays.h>
6
 
7
unsigned char port_b_prev_state;
8
 
9
void intx_init() {
10
    TRISAbits.TRISA5 = 1;
11
 
12
    TRISCbits.TRISC2 = 0;
13
 
14
    RPINR1 = 2; // Bind INT1 interrupt to RP2
15
 
16
    INTCON2bits.INTEDG1 = 0;    // Trigger on falling edge
17
}
18
 
19
void int1_interrupt_handler() {
20
    pwm_LED_start();
21
    Delay10TCYx(1);
22
    pwm_LED_stop();
23
    MQ_sendmsg_ToMainFromLow(0, MSGTYPE_INT1, (void *) 0);
24
}
25
 
26
void port_b_int_init() {
27
    port_b_prev_state = 0x0F;
28
 
29
    INTCON2bits.RBPU = 0;
30
 
31
    // Set pins as inputs
32
    TRISBbits.TRISB4 = 1;
33
    TRISBbits.TRISB5 = 1;
34
    TRISBbits.TRISB6 = 1;
35
    TRISBbits.TRISB7 = 1;
36
 
37
    // Turn on internal voltage pull-up?
38
    PORTBbits.RB4 = 1;
39
    PORTBbits.RB5 = 1;
40
    PORTBbits.RB6 = 1;
41
    PORTBbits.RB7 = 1;
42
    LATBbits.LATB4 = 1;
43
    LATBbits.LATB5 = 1;
44
    LATBbits.LATB6 = 1;
45
    LATBbits.LATB7 = 1;
46
}
47
 
48
void port_b_int_interrupt_handler() {
49
    // Pull the new pin values
50
    unsigned char new_state = (PORTB & 0xF0) >> 4;
51
 
52
    // Query which pin input value changed and send value to main()
53
    if ((new_state ^ port_b_prev_state) & 0x01) {
54
        if (port_b_prev_state & 0x01) {
55
            // Pin transitioned HIGH -> LOW (button pressed)
56
            DBG_PRINT_PORTB_INT("Port B4 HIGH->LOW\r\n");
57
            MQ_sendmsg_ToMainFromLow(0, MSGTYPE_PORTB_4_DOWN, (void *) 0);
58
        } else {
59
            // Pin transitioned LOW -> HIGH (button released)
60
            DBG_PRINT_PORTB_INT("Port B4 LOW->HIGH\r\n");
61
            MQ_sendmsg_ToMainFromLow(0, MSGTYPE_PORTB_4_UP, (void *) 0);
62
        }
63
    }
64
    if ((new_state ^ port_b_prev_state) & 0x02) {
65
        if (port_b_prev_state & 0x02) {
66
            // Pin transitioned HIGH -> LOW (button pressed)
67
            DBG_PRINT_PORTB_INT("Port B5 HIGH->LOW\r\n");
68
            MQ_sendmsg_ToMainFromLow(0, MSGTYPE_PORTB_5_DOWN, (void *) 0);
69
        } else {
70
            // Pin transitioned LOW -> HIGH (button released)
71
            DBG_PRINT_PORTB_INT("Port B5 LOW->HIGH\r\n");
72
            MQ_sendmsg_ToMainFromLow(0, MSGTYPE_PORTB_5_UP, (void *) 0);
73
        }
74
    }
75
    if ((new_state ^ port_b_prev_state) & 0x04) {
76
        if (port_b_prev_state & 0x04) {
77
            // Pin transitioned HIGH -> LOW (button pressed)
78
            DBG_PRINT_PORTB_INT("Port B6 HIGH->LOW\r\n");
79
            MQ_sendmsg_ToMainFromLow(0, MSGTYPE_PORTB_6_DOWN, (void *) 0);
80
        } else {
81
            // Pin transitioned LOW -> HIGH (button released)
82
            DBG_PRINT_PORTB_INT("Port B6 LOW->HIGH\r\n");
83
            MQ_sendmsg_ToMainFromLow(0, MSGTYPE_PORTB_6_UP, (void *) 0);
84
        }
85
    }
86
    if ((new_state ^ port_b_prev_state) & 0x08) {
87
        if (port_b_prev_state & 0x08) {
88
            // Pin transitioned HIGH -> LOW (button pressed)
89
            DBG_PRINT_PORTB_INT("Port B7 HIGH->LOW\r\n");
90
            MQ_sendmsg_ToMainFromLow(0, MSGTYPE_PORTB_7_DOWN, (void *) 0);
91
        } else {
92
            // Pin transitioned LOW -> HIGH (button released)
93
            DBG_PRINT_PORTB_INT("Port B7 LOW->HIGH\r\n");
94
            MQ_sendmsg_ToMainFromLow(0, MSGTYPE_PORTB_7_UP, (void *) 0);
95
        }
96
    }
97
 
98
    // Save the new state of pins
99
    port_b_prev_state = new_state;
100
}