Subversion Repositories Code-Repo

Rev

Rev 109 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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