Part Number: MSP-EXP430FR2355
Other Parts Discussed in Thread: MSP430FR2355,
Hi,
I am trying to interface a 4x4 matrix keypad with the MSP430FR2355 keypad. I have the 4 columns set as inputs on P3.0-P3.3 and the 4 rows set as outputs on P3.4-P3.7. I have the inputs connected to internal pull-up resistors, so their default value should be 1 and I also set the outputs to be default high. To find the key pressed, I am going through one by one and setting a row low and checking to see if any of the buttons were pressed on that column (if a button was pressed, that input pin should read 0). However, P3IN isn't reading the correct values and always shows P3.0 (column 0) to be equal to zero. I commented that pin out to see if the other buttons were working, but P3IN doesn't register the right buttons being pressed. I've posted my code below.
#include <msp430.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <math.h>
#include "msp430fr2355.h"
#define COL0 (P3IN & BIT0)
#define COL1 (P3IN & BIT1)
#define COL2 (P3IN & BIT2)
#define COL3 (P3IN & BIT3)
#define PRESS 0
//Functions
char getKey(void);
void delay_ms(unsigned int ms);
int main(void){
WDTCTL = WDTPW | WDTHOLD; //Stop watch dog timer
char key[] = " ";
while(1){
key[0] = getKey();
}
}
char getKey(void){
P3SEL0 &= 0x00;
P3DIR = 0xF0;
P3OUT &= 0x00;
P3REN |= 0x0F; //Enable resistor
P3OUT |= 0xFF; //Set resistor to pull-up
delay_ms(100);
//Scan Row 0
P3OUT &= (~BIT4); //set P3.4 low
if(COL0 == PRESS){
//delay_ms(50);
return 0x31; //1
}
if(COL1 == PRESS){
//delay_ms(50);
return 0x32; //2
}
if(COL2 == PRESS){
//delay_ms(50);
return 0x33; //3
}
if(COL3 != PRESS){
//delay_ms(50);
return 0x41; //A
}
P3OUT |= (BIT4);
//Scan Row 1
P3OUT &= (~BIT5); //set P3.5 low
if(COL0 == PRESS){
//delay_ms(50);
return 0x34; //4
}
if(COL1 == PRESS){
//delay_ms(50);
return 0x35; //5
}
if(COL2 == PRESS){
//delay_ms(50);
return 0x36; //6
}
if(COL3 == PRESS){
//delay_ms(50);
return 0x42; //B
}
P3OUT |= (BIT5);
//Scan Row 2
P3OUT &= (~BIT6); //set P3.6 low
if(COL0 == PRESS){
//delay_ms(50);
return 0x37; //7
}
if(COL1 == PRESS){
//delay_ms(50);
return 0x38; //8
}
if(COL2 == PRESS){
//delay_ms(50);
return 0x39; //9
}
if(COL3 == PRESS){
//delay_ms(50);
return 0x43; //C
}
P3OUT |= (BIT6);
//Scan Row 3
P3OUT &= (~BIT7); //set P3.7 low
if(COL0 == PRESS){
//delay_ms(50);
return 0x2A; //star
}
if(COL1 == PRESS){
//delay_ms(50);
return 0x30; //0
}
if(COL2 == PRESS){
//delay_ms(50);
return 0x23; //#
}
if(COL3 == PRESS){
//delay_ms(50);
return 0x44; //D
}
P3OUT |= (BIT7);
return 0x00; //NULL
}
void delay_ms(unsigned int ms){
while(ms){
__delay_cycles(1000); //1000 for 1MHz (default clock setting)
ms--;
}
}