Subversion Repositories Code-Repo

Rev

Rev 109 | Go to most recent revision | Details | 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
 
15
    RPINR1 = 16; // Bind INT1 interrupt to RP2
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;
23
    Delay10KTCYx(100);
24
    LATCbits.LATC2 = 0;
25
}
26
 
27
void port_b_int_init() {
28
    port_b_prev_state = 0x0F;
29
 
30
    // Turn on internal pull-up for all port B pins
31
    INTCON2bits.RBPU = 0;;
32
 
33
    // Set pins as inputs
34
    TRISBbits.TRISB4 = 1;
35
    TRISBbits.TRISB5 = 1;
36
    TRISBbits.TRISB6 = 1;
37
    TRISBbits.TRISB7 = 1;
38
 
39
    LATBbits.LATB4 = 0;
40
    LATBbits.LATB5 = 0;
41
    LATBbits.LATB6 = 0;
42
    LATBbits.LATB7 = 0;
43
 
44
    // Enable Port B interrupt
45
    INTCONbits.RBIE = 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 LOW -> HIGH (button released)
56
            DBG_PRINT_PORTB_INT("Port B4 LOW->HIGH\r\n");
57
            MQ_sendmsg_ToMainFromHigh(0, MSGTYPE_PORTB_4_UP, (void *) 0);
58
        } else {
59
            // Pin transitioned HIGH -> LOW (button pressed)
60
            DBG_PRINT_PORTB_INT("Port B4 HIGH->LOW\r\n");
61
            MQ_sendmsg_ToMainFromHigh(0, MSGTYPE_PORTB_4_DOWN, (void *) 0);
62
        }
63
    }
64
    if ((new_state ^ port_b_prev_state) & 0x02) {
65
        if (port_b_prev_state & 0x02) {
66
            // Pin transitioned LOW -> HIGH (button released)
67
            DBG_PRINT_PORTB_INT("Port B5 LOW->HIGH\r\n");
68
            MQ_sendmsg_ToMainFromHigh(0, MSGTYPE_PORTB_5_UP, (void *) 0);
69
        } else {
70
            // Pin transitioned HIGH -> LOW (button pressed)
71
            DBG_PRINT_PORTB_INT("Port B5 HIGH->LOW\r\n");
72
            MQ_sendmsg_ToMainFromHigh(0, MSGTYPE_PORTB_5_DOWN, (void *) 0);
73
        }
74
    }
75
    if ((new_state ^ port_b_prev_state) & 0x04) {
76
        if (port_b_prev_state & 0x04) {
77
            // Pin transitioned LOW -> HIGH (button released)
78
            DBG_PRINT_PORTB_INT("Port B6 LOW->HIGH\r\n");
79
            MQ_sendmsg_ToMainFromHigh(0, MSGTYPE_PORTB_6_UP, (void *) 0);
80
        } else {
81
            // Pin transitioned HIGH -> LOW (button pressed)
82
            DBG_PRINT_PORTB_INT("Port B6 HIGH->LOW\r\n");
83
            MQ_sendmsg_ToMainFromHigh(0, MSGTYPE_PORTB_6_DOWN, (void *) 0);
84
        }
85
    }
86
    if ((new_state ^ port_b_prev_state) & 0x08) {
87
        if (port_b_prev_state & 0x08) {
88
            // Pin transitioned LOW -> HIGH (button released)
89
            DBG_PRINT_PORTB_INT("Port B7 LOW->HIGH\r\n");
90
            MQ_sendmsg_ToMainFromHigh(0, MSGTYPE_PORTB_7_UP, (void *) 0);
91
        } else {
92
            // Pin transitioned HIGH -> LOW (button pressed)
93
            DBG_PRINT_PORTB_INT("Port B7 HIGH->LOW\r\n");
94
            MQ_sendmsg_ToMainFromHigh(0, MSGTYPE_PORTB_7_DOWN, (void *) 0);
95
        }
96
    }
97
 
98
    // Save the new state of pins
99
    port_b_prev_state = new_state;
100
}