Question: C Programming (Tiva Interace) I implemented the following code to display keys of a 4x4 matrix keypad when pressed. I need help to modify it
C Programming (Tiva Interace)
I implemented the following code to display keys of a 4x4 matrix keypad when pressed. I need help to modify it to support key detection. When user pesses a key, return the ASCII code to the main code. But if the same key is held, then the function will return 0x00 unless user releases key or presses another key.
Initial Code shown below (GPIO Configuration not shown):
int main(void)
{
PEZOBJ_LCD lcd;
uint16_t i = 0;
char ch;
Setup_GPIO();
lcd = ezLCD_Create();
ezLCD_Connect_DataPort(lcd, GPIOD, PIN_3_0);
ezLCD_Connect_ENPin(lcd, GPIOE, PIN1);
ezLCD_Connect_RSPin(lcd, GPIOE, PIN2);
ezLCD_Connect_RWPin(lcd, GPIOE, PIN3);
ezLCD_Start(lcd);
ezLCD_ClearDisplay(lcd);
ezLCD_Position(lcd, 1, 0);
ezLCD_PrintString(lcd, "HELLO");
while(1){
ch = ReadKeyPad();
if (ch == '*') i *= 100;
if (ch == '#') {
i = 0;
ezLCD_ClearDisplay(lcd);
}
if (ch >='0' && ch <= '9') i = i*10 + ch - '0';
sprintf(str, "%c ",ch );
ezLCD_Position(lcd, 0, 0);
ezLCD_PrintString(lcd, str);
timer_waitMillis(100);
}
}
//--------------------------------------------------------------
char KeyPad[4][4]={ //Character array for keypad outputs depending on button pressed.
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}};
uint32_t *R1 = (uint32_t *) ((char *) GPIOB + ((_PIN0) << 2)); //Initialization for rows on the Matrix keypad.
uint32_t *R2 = (uint32_t *) ((char *) GPIOB + ((_PIN1) << 2));
uint32_t *R3 = (uint32_t *) ((char *) GPIOE + ((_PIN4) << 2));
uint32_t *R4 = (uint32_t *) ((char *) GPIOE + ((_PIN5) << 2));
uint32_t *C1 = (uint32_t *) ((char *) GPIOB + ((_PIN4) << 2)); //Initialization for columns on the Matrix keypad.
uint32_t *C2 = (uint32_t *) ((char *) GPIOA + ((_PIN5) << 2));
uint32_t *C3 = (uint32_t *) ((char *) GPIOA + ((_PIN6) << 2));
uint32_t *C4 = (uint32_t *) ((char *) GPIOA + ((_PIN7) << 2));
char ReadKeyPad()
{
int row;
int col;
char key = 0x00;
for (row = 1; row < 5; row++) {
switch (row) { //Switch and case used for the 4 rows.
case 1: //First case is row 1 combined with 4 columns.
*R1 = 0x00; //Row 1 set to logic low. (For Open Drain)
*R2 = 0xFF;
*R3 = 0xFF;
*R4 = 0xFF;
timer_waitMillis(10); //Time delay added from time pattern is written to when it is displayed.
if((*C1 & _PIN4) == 0) key = KeyPad [0][0];
if((*C2 & _PIN5) == 0) key = KeyPad [0][1];
if((*C3 & _PIN6) == 0) key = KeyPad [0][2];
if((*C4 & _PIN7) == 0) key = KeyPad [0][3];
break;
case 2: //Second case is row 2 combined with 4 columns.
*R1 = 0xFF;
*R2 = 0x00; //Row 2 set to logic low. (For Open Drain)
*R3 = 0xFF;
*R4 = 0xFF;
timer_waitMillis(10); //Time delay added from time pattern is written to when it is displayed.
if((*C1 & _PIN4) == 0) key = KeyPad [1][0];
if((*C2 & _PIN5) == 0) key = KeyPad [1][1];
if((*C3 & _PIN6) == 0) key = KeyPad [1][2];
if((*C4 & _PIN7) == 0) key = KeyPad [1][3];
break;
case 3: //Third case is row 3 combined with 4 columns.
*R1 = 0xFF;
*R2 = 0xFF;
*R3 = 0x00; //Row 3 set to logic low. (For Open Drain)
*R4 = 0xFF;
timer_waitMillis(10); //Time delay added from time pattern is written to when it is displayed.
if((*C1 & _PIN4) == 0) key = KeyPad [2][0];
if((*C2 & _PIN5) == 0) key = KeyPad [2][1];
if((*C3 & _PIN6) == 0) key = KeyPad [2][2];
if((*C4 & _PIN7) == 0) key = KeyPad [2][3];
break;
case 4: //Fourth case is row 4 combined with 4 columns.
*R1 = 0xFF;
*R2 = 0xFF;
*R3 = 0xFF;
*R4 = 0x00; //Row 4 set to logic low. (For Open Drain)
timer_waitMillis(10); //Time delay added from time pattern is written to when it is displayed.
if((*C1 & _PIN4) == 0) key = KeyPad [3][0];
if((*C2 & _PIN5) == 0) key = KeyPad [3][1];
if((*C3 & _PIN6) == 0) key = KeyPad [3][2];
if((*C4 & _PIN7) == 0) key = KeyPad [3][3];
break;
}
}
return key;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
