This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Problem Interfacing keypad with msp430f5529 lp

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

  • Hi Ram,

    Could you provide a diagram or some words as to how the keypad is connected to the system and also how the LEDs correspond to each pin and the key on the keypad?

    Also you have 4 keys you are wanted to map out but only 2 interrupts, are you trying to have each key press trigger two interrupts?

    Regards,
    Akash Patel
  • Hi Akash,

    First, sorry for the delay.

    The keypad is just like a 4x4 matrix keypad but in my case it is 2x2. the two columns of the keypad are connected to pins 1.5 & 1.6 and the rows are connected to 3.0 & 3.1. the LEDs are connected to 6.0, 6.1, 6.3 & 6.4. Each key has a corresponding LED which should be toggled when the corresponding key is pressed.

    Pin 1.5 & 1.6 are interrupt enabled and are used to find out the column corresponding to each key press.

    Regards,

    ~Ram

  • Hi Ram,

    I just reviewed your logic and it seems to be correct. The only thing I could see being an issue is if the rising edge interrupt is going to occur due to row 1, it may occur after line 42 is executed. For that reason, it may make sense to put some delay between line 39 and line 40. That way you have time for that interrupt to fire and you don't get row = 2 if you wanted row = 1.

    I hope that resolves the issue. If not, are you noticing any patterns with your incorrect key recognition?

    Also, here is an app note that talks about the program flow when doing something like this: www.ti.com/.../slaa139.pdf

    Regards,
    Akash Patel
  • Hi Ram,

    It's been a couple weeks since we've last heard from you so I will be closing the thread. If you're still having issues, please let me know and I can reopen the thread, otherwise, please hit "Verify Answer" in my above post if that solved your issue.

    Regards,
    Akash Patel

**Attention** This is a public forum