Subversion Repositories Code-Repo

Rev

Rev 113 | Details | Compare with Previous | 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
    MQ_sendmsg_ToMainFromLow(0, MSGTYPE_INT1, (void *) 0);
21
}
22
 
23
void port_b_int_init() {
24
    port_b_prev_state = 0x0F;
25
 
26
    INTCON2bits.RBPU = 0;
27
 
28
    // Set pins as inputs
29
    TRISBbits.TRISB4 = 1;
30
    TRISBbits.TRISB5 = 1;
31
    TRISBbits.TRISB6 = 1;
32
    TRISBbits.TRISB7 = 1;
33
 
34
    // Turn on internal voltage pull-up?
35
    PORTBbits.RB4 = 1;
36
    PORTBbits.RB5 = 1;
37
    PORTBbits.RB6 = 1;
38
    PORTBbits.RB7 = 1;
39
    LATBbits.LATB4 = 1;
40
    LATBbits.LATB5 = 1;
41
    LATBbits.LATB6 = 1;
42
    LATBbits.LATB7 = 1;
43
}
44
 
45
void port_b_int_interrupt_handler() {
46
    // Pull the new pin values
47
    unsigned char new_state = (PORTB & 0xF0) >> 4;
48
 
49
    // Query which pin input value changed and send value to main()
50
    if ((new_state ^ port_b_prev_state) & 0x01) {
51
        if (port_b_prev_state & 0x01) {
52
            // Pin transitioned HIGH -> LOW (button pressed)
53
            DBG_PRINT_PORTB_INT("Port B4 HIGH->LOW\r\n");
54
            MQ_sendmsg_ToMainFromLow(0, MSGTYPE_PORTB_4_DOWN, (void *) 0);
55
        } else {
56
            // Pin transitioned LOW -> HIGH (button released)
57
            DBG_PRINT_PORTB_INT("Port B4 LOW->HIGH\r\n");
58
            MQ_sendmsg_ToMainFromLow(0, MSGTYPE_PORTB_4_UP, (void *) 0);
59
        }
60
    }
61
    if ((new_state ^ port_b_prev_state) & 0x02) {
62
        if (port_b_prev_state & 0x02) {
63
            // Pin transitioned HIGH -> LOW (button pressed)
64
            DBG_PRINT_PORTB_INT("Port B5 HIGH->LOW\r\n");
65
            MQ_sendmsg_ToMainFromLow(0, MSGTYPE_PORTB_5_DOWN, (void *) 0);
66
        } else {
67
            // Pin transitioned LOW -> HIGH (button released)
68
            DBG_PRINT_PORTB_INT("Port B5 LOW->HIGH\r\n");
69
            MQ_sendmsg_ToMainFromLow(0, MSGTYPE_PORTB_5_UP, (void *) 0);
70
        }
71
    }
72
    if ((new_state ^ port_b_prev_state) & 0x04) {
73
        if (port_b_prev_state & 0x04) {
74
            // Pin transitioned HIGH -> LOW (button pressed)
75
            DBG_PRINT_PORTB_INT("Port B6 HIGH->LOW\r\n");
76
            MQ_sendmsg_ToMainFromLow(0, MSGTYPE_PORTB_6_DOWN, (void *) 0);
77
        } else {
78
            // Pin transitioned LOW -> HIGH (button released)
79
            DBG_PRINT_PORTB_INT("Port B6 LOW->HIGH\r\n");
80
            MQ_sendmsg_ToMainFromLow(0, MSGTYPE_PORTB_6_UP, (void *) 0);
81
        }
82
    }
83
    if ((new_state ^ port_b_prev_state) & 0x08) {
84
        if (port_b_prev_state & 0x08) {
85
            // Pin transitioned HIGH -> LOW (button pressed)
86
            DBG_PRINT_PORTB_INT("Port B7 HIGH->LOW\r\n");
87
            MQ_sendmsg_ToMainFromLow(0, MSGTYPE_PORTB_7_DOWN, (void *) 0);
88
        } else {
89
            // Pin transitioned LOW -> HIGH (button released)
90
            DBG_PRINT_PORTB_INT("Port B7 LOW->HIGH\r\n");
91
            MQ_sendmsg_ToMainFromLow(0, MSGTYPE_PORTB_7_UP, (void *) 0);
92
        }
93
    }
94
 
95
    // Save the new state of pins
96
    port_b_prev_state = new_state;
97
}