| 119 |
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 = 2; // Bind INT1 interrupt to RP2
|
|
|
16 |
|
|
|
17 |
INTCON2bits.INTEDG1 = 0; // Trigger on falling edge
|
|
|
18 |
}
|
|
|
19 |
|
|
|
20 |
void int1_interrupt_handler() {
|
|
|
21 |
LATCbits.LATC2 = 1;
|
|
|
22 |
Delay10TCYx(255);
|
|
|
23 |
LATCbits.LATC2 = 0;
|
|
|
24 |
MQ_sendmsg_ToMainFromLow(0, MSGTYPE_INT1, (void *) 0);
|
|
|
25 |
}
|
|
|
26 |
|
|
|
27 |
void port_b_int_init() {
|
|
|
28 |
port_b_prev_state = 0x0F;
|
|
|
29 |
|
|
|
30 |
INTCON2bits.RBPU = 0;
|
|
|
31 |
|
|
|
32 |
// Set pins as inputs
|
|
|
33 |
TRISBbits.TRISB4 = 1;
|
|
|
34 |
TRISBbits.TRISB5 = 1;
|
|
|
35 |
TRISBbits.TRISB6 = 1;
|
|
|
36 |
TRISBbits.TRISB7 = 1;
|
|
|
37 |
|
|
|
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;
|
|
|
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) {
|
|
|
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 {
|
|
|
60 |
// Pin transitioned LOW -> HIGH (button released)
|
|
|
61 |
DBG_PRINT_PORTB_INT("Port B4 LOW->HIGH\r\n");
|
|
|
62 |
MQ_sendmsg_ToMainFromLow(0, MSGTYPE_PORTB_4_UP, (void *) 0);
|
|
|
63 |
}
|
|
|
64 |
}
|
|
|
65 |
if ((new_state ^ port_b_prev_state) & 0x02) {
|
|
|
66 |
if (port_b_prev_state & 0x02) {
|
|
|
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 {
|
|
|
71 |
// Pin transitioned LOW -> HIGH (button released)
|
|
|
72 |
DBG_PRINT_PORTB_INT("Port B5 LOW->HIGH\r\n");
|
|
|
73 |
MQ_sendmsg_ToMainFromLow(0, MSGTYPE_PORTB_5_UP, (void *) 0);
|
|
|
74 |
}
|
|
|
75 |
}
|
|
|
76 |
if ((new_state ^ port_b_prev_state) & 0x04) {
|
|
|
77 |
if (port_b_prev_state & 0x04) {
|
|
|
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 {
|
|
|
82 |
// Pin transitioned LOW -> HIGH (button released)
|
|
|
83 |
DBG_PRINT_PORTB_INT("Port B6 LOW->HIGH\r\n");
|
|
|
84 |
MQ_sendmsg_ToMainFromLow(0, MSGTYPE_PORTB_6_UP, (void *) 0);
|
|
|
85 |
}
|
|
|
86 |
}
|
|
|
87 |
if ((new_state ^ port_b_prev_state) & 0x08) {
|
|
|
88 |
if (port_b_prev_state & 0x08) {
|
|
|
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 {
|
|
|
93 |
// Pin transitioned LOW -> HIGH (button released)
|
|
|
94 |
DBG_PRINT_PORTB_INT("Port B7 LOW->HIGH\r\n");
|
|
|
95 |
MQ_sendmsg_ToMainFromLow(0, MSGTYPE_PORTB_7_UP, (void *) 0);
|
|
|
96 |
}
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
// Save the new state of pins
|
|
|
100 |
port_b_prev_state = new_state;
|
|
|
101 |
}
|