0,0 → 1,100 |
#include "maindefs.h" |
#include "pin_interrupts.h" |
#include "pwm.h" |
#include "msg_queues.h" |
#include <delays.h> |
|
static unsigned char port_b_prev_state; |
|
void intx_init() { |
TRISAbits.TRISA5 = 1; |
|
TRISCbits.TRISC2 = 0; |
LATCbits.LATC2 = 0; |
|
RPINR1 = 16; // Bind INT1 interrupt to RP2 |
|
INTCON3bits.INT1IE = 1; // Enable interrupt for INT1 |
INTCON2bits.INTEDG1 = 0; // Trigger on falling edge |
} |
|
void int1_interrupt_handler() { |
LATCbits.LATC2 = 1; |
Delay10KTCYx(100); |
LATCbits.LATC2 = 0; |
} |
|
void port_b_int_init() { |
port_b_prev_state = 0x0F; |
|
// Turn on internal pull-up for all port B pins |
INTCON2bits.RBPU = 0;; |
|
// Set pins as inputs |
TRISBbits.TRISB4 = 1; |
TRISBbits.TRISB5 = 1; |
TRISBbits.TRISB6 = 1; |
TRISBbits.TRISB7 = 1; |
|
LATBbits.LATB4 = 0; |
LATBbits.LATB5 = 0; |
LATBbits.LATB6 = 0; |
LATBbits.LATB7 = 0; |
|
// Enable Port B interrupt |
INTCONbits.RBIE = 1; |
} |
|
void port_b_int_interrupt_handler() { |
// Pull the new pin values |
unsigned char new_state = (PORTB & 0xF0) >> 4; |
|
// Query which pin input value changed and send value to main() |
if ((new_state ^ port_b_prev_state) & 0x01) { |
if (port_b_prev_state & 0x01) { |
// Pin transitioned LOW -> HIGH (button released) |
DBG_PRINT_PORTB_INT("Port B4 LOW->HIGH\r\n"); |
MQ_sendmsg_ToMainFromHigh(0, MSGTYPE_PORTB_4_UP, (void *) 0); |
} else { |
// Pin transitioned HIGH -> LOW (button pressed) |
DBG_PRINT_PORTB_INT("Port B4 HIGH->LOW\r\n"); |
MQ_sendmsg_ToMainFromHigh(0, MSGTYPE_PORTB_4_DOWN, (void *) 0); |
} |
} |
if ((new_state ^ port_b_prev_state) & 0x02) { |
if (port_b_prev_state & 0x02) { |
// Pin transitioned LOW -> HIGH (button released) |
DBG_PRINT_PORTB_INT("Port B5 LOW->HIGH\r\n"); |
MQ_sendmsg_ToMainFromHigh(0, MSGTYPE_PORTB_5_UP, (void *) 0); |
} else { |
// Pin transitioned HIGH -> LOW (button pressed) |
DBG_PRINT_PORTB_INT("Port B5 HIGH->LOW\r\n"); |
MQ_sendmsg_ToMainFromHigh(0, MSGTYPE_PORTB_5_DOWN, (void *) 0); |
} |
} |
if ((new_state ^ port_b_prev_state) & 0x04) { |
if (port_b_prev_state & 0x04) { |
// Pin transitioned LOW -> HIGH (button released) |
DBG_PRINT_PORTB_INT("Port B6 LOW->HIGH\r\n"); |
MQ_sendmsg_ToMainFromHigh(0, MSGTYPE_PORTB_6_UP, (void *) 0); |
} else { |
// Pin transitioned HIGH -> LOW (button pressed) |
DBG_PRINT_PORTB_INT("Port B6 HIGH->LOW\r\n"); |
MQ_sendmsg_ToMainFromHigh(0, MSGTYPE_PORTB_6_DOWN, (void *) 0); |
} |
} |
if ((new_state ^ port_b_prev_state) & 0x08) { |
if (port_b_prev_state & 0x08) { |
// Pin transitioned LOW -> HIGH (button released) |
DBG_PRINT_PORTB_INT("Port B7 LOW->HIGH\r\n"); |
MQ_sendmsg_ToMainFromHigh(0, MSGTYPE_PORTB_7_UP, (void *) 0); |
} else { |
// Pin transitioned HIGH -> LOW (button pressed) |
DBG_PRINT_PORTB_INT("Port B7 HIGH->LOW\r\n"); |
MQ_sendmsg_ToMainFromHigh(0, MSGTYPE_PORTB_7_DOWN, (void *) 0); |
} |
} |
|
// Save the new state of pins |
port_b_prev_state = new_state; |
} |