Subversion Repositories Code-Repo

Rev

Rev 107 | Rev 111 | 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
 
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
 
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
    // Enable Port B interrupt
49
    INTCONbits.RBIE = 1;
50
}
51
 
52
void port_b_int_interrupt_handler() {
53
    // Pull the new pin values
54
    unsigned char new_state = (PORTB & 0xF0) >> 4;
55
 
56
    // Query which pin input value changed and send value to main()
57
    if ((new_state ^ port_b_prev_state) & 0x01) {
58
        if (port_b_prev_state & 0x01) {
59
            // Pin transitioned LOW -> HIGH (button released)
60
            DBG_PRINT_PORTB_INT("Port B4 LOW->HIGH\r\n");
61
            MQ_sendmsg_ToMainFromHigh(0, MSGTYPE_PORTB_4_UP, (void *) 0);
62
        } else {
63
            // Pin transitioned HIGH -> LOW (button pressed)
64
            DBG_PRINT_PORTB_INT("Port B4 HIGH->LOW\r\n");
65
            MQ_sendmsg_ToMainFromHigh(0, MSGTYPE_PORTB_4_DOWN, (void *) 0);
66
        }
67
    }
68
    if ((new_state ^ port_b_prev_state) & 0x02) {
69
        if (port_b_prev_state & 0x02) {
70
            // Pin transitioned LOW -> HIGH (button released)
71
            DBG_PRINT_PORTB_INT("Port B5 LOW->HIGH\r\n");
72
            MQ_sendmsg_ToMainFromHigh(0, MSGTYPE_PORTB_5_UP, (void *) 0);
73
        } else {
74
            // Pin transitioned HIGH -> LOW (button pressed)
75
            DBG_PRINT_PORTB_INT("Port B5 HIGH->LOW\r\n");
76
            MQ_sendmsg_ToMainFromHigh(0, MSGTYPE_PORTB_5_DOWN, (void *) 0);
77
        }
78
    }
79
    if ((new_state ^ port_b_prev_state) & 0x04) {
80
        if (port_b_prev_state & 0x04) {
81
            // Pin transitioned LOW -> HIGH (button released)
82
            DBG_PRINT_PORTB_INT("Port B6 LOW->HIGH\r\n");
83
            MQ_sendmsg_ToMainFromHigh(0, MSGTYPE_PORTB_6_UP, (void *) 0);
84
        } else {
85
            // Pin transitioned HIGH -> LOW (button pressed)
86
            DBG_PRINT_PORTB_INT("Port B6 HIGH->LOW\r\n");
87
            MQ_sendmsg_ToMainFromHigh(0, MSGTYPE_PORTB_6_DOWN, (void *) 0);
88
        }
89
    }
90
    if ((new_state ^ port_b_prev_state) & 0x08) {
91
        if (port_b_prev_state & 0x08) {
92
            // Pin transitioned LOW -> HIGH (button released)
93
            DBG_PRINT_PORTB_INT("Port B7 LOW->HIGH\r\n");
94
            MQ_sendmsg_ToMainFromHigh(0, MSGTYPE_PORTB_7_UP, (void *) 0);
95
        } else {
96
            // Pin transitioned HIGH -> LOW (button pressed)
97
            DBG_PRINT_PORTB_INT("Port B7 HIGH->LOW\r\n");
98
            MQ_sendmsg_ToMainFromHigh(0, MSGTYPE_PORTB_7_DOWN, (void *) 0);
99
        }
100
    }
101
 
102
    // Save the new state of pins
103
    port_b_prev_state = new_state;
104
}