//When a key is pressed the LEDS that represent the corresponding
//row and column should turn ON.
#include "register.h"
#include "functions.h"
#define ROWS 4
#define COLS 4
// Define keypad array globally
char keypad[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
// Function to read the keypad
char read_keypad()
{
uint8_t row, column;
for (row = 0; row < ROWS; row++)
{
*outf = (1 << row); // Activate one row at a time
for (column = 0; column < COLS; column++)
{
if ((*ink & (1 << column))) // Check each column in the active row
{
return keypad[row][column];
}
}
}
return '\0';
}
void setup()
{
*dirf = 0x0F; // Set first four pins of PORT F as o/p. KEYPAD row pins are connected to port f.
*dirk = 0x00; // Set PORT K as i/p port. KEYPAD column pins are connected to port k.
*dira = 0x0F; // Set first 4 pins of PORT A as o/p.
*dirc = 0x0F; // Set first 4 pins of PORT C as o/p.
char key;
uint8_t i, j;
while (1)
{
key = read_keypad();
if (key != '\0')
{
for (i = 0; i < ROWS; i++)
{
for (j = 0; j < COLS; j++)
{
if (keypad[i][j] == key) //check the keypad array to find which key is pressed
{
*outc = (1 << i);//LEDs representing rows are connected to PORTC
*outa = (1 << j);//LEDs representing columns are connected to PORTA
}
}
}
}
else
{
// No key pressed, turn off all LEDs
*outa = *outc = 0x00;
}
}
}