Hi all,
I am working on a project where i have to interface a matrix keypad with 40 keys(5x8). I tried implementing the code with 4 push buttons, kind of like a 2x2 matrix and using 4 leds as output. This is the code I wrote
#include <msp430f5529.h> /* * main.c */ volatile int col = 0; volatile int row = 0; int main(void) { WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer P6SEL &= 0x00; //P6 initialization P6OUT = 0x00; P6DIR = 0x1F; P6OUT = 0x00; P3SEL &= 0x00; //P3 initialization P3OUT = 0x00; P3DIR |= 0x03; P3OUT &= 0x00; P1SEL &= 0x00; //p1 initialization P1OUT = 0x00; // initializing all outputs to low P1DIR &= 0x00; // P1 as input P1REN |= 0x60; // P1.5,6 Enable Pullup/Pulldown P1OUT |= 0x60; // P1.5,6 pullup P1IES |= 0x60; // P1.5,6 Hi/lo falling edge P1IFG &= 0x00; // P1.5,6 IFG cleared just in case P1IE |= 0x60; // P1.5,6 interrupt enabled __bis_SR_register(GIE); while(1) { if (col == 0); else { row = 1; P3OUT |= 0x01; if (row == 1) { row = 2; P3OUT |= 0x02; } } } return 0; } #pragma vector = PORT1_VECTOR __interrupt void Port1 (void) { switch(__even_in_range(P1IV,16)) { case 0:break; case 2:break; case 4:break; case 6:break; case 8:break; case 10:break; case 12: { if (row == 0) { col = 1; P1IES &= ~BIT5; //P1.5 IFG cleared P1IFG &= ~BIT5; // rising edge } else { P3OUT &= 0x00; int i; for(i=0; i<=200; i++) __delay_cycles(1000); P1IFG &= ~BIT5; // P1.5 IFG cleared P1IES |= BIT5; //Falling Edge if (row == 1) P6OUT ^= BIT0; if (row == 2) P6OUT ^= BIT1; col = 0; row = 0; } } break; case 14: { if (row == 0) { col = 2; P1IFG &= ~BIT6; // P1.6 IFG cleared P1IES &= ~BIT6; // rising edge } else { P3OUT &= 0x00; int j; for(j=0; j<=200; j++) __delay_cycles(1000); P1IFG &= ~BIT6; // P1.6 IFG cleared P1IES |= BIT6; // falling edge if (row == 1) P6OUT ^= BIT4; if (row == 2) P6OUT ^= BIT3; col = 0; row = 0; } } break; case 16:break; default: break; } }
Sometimes key presses are not registered properly. Pressing a particular key produces output for a different key. Is there a problem in my logic or the way i implemented it ?
any help is greatly appreciated.
Regards,
~Ram