/* Sample program to scan the matrix keypad and write the key code on the LEDs and Serial monitor. */ /* Tiva TM4C123 */ //const int LED[] = {PB_0, PB_1, PB_2, PB_3}; const int LED[] = {3, 4, 19, 38}; void setup() { // initialize Serial monitor Serial.begin(9600); Serial.println("Keypad:"); // initialize LEDs for display for (int i = 0; i < 4; i++) { pinMode(LED[i], OUTPUT); } // turn off all LEDs digitalGroupWrite(LED, 4, 0); // initialize pins connected to keypad keypad_init(); } void loop() { char key; key = keypad_getkey(); // read the keypad // if a key is pressed, send the key code to Serial monitor if(key != 0) { Serial.print(" key = "); Serial.println(key, DEC); } digitalGroupWrite(LED, 4, key); // and write it to the LEDs } /* * This function intialize the pins connected to the keypad. * Rows are used as excitation and columns are used as input. * The connections between the keypad and the pins are * row 0 23 * row 1 24 * row 2 25 * row 3 26 * col 0 11 * col 1 12 * col 2 13 * col 3 8 */ /* Tiva TM4C123 */ //const int ROW[] = {PD_0, PD_1, PD_2, PD_3}; const int ROW[] = {23, 24, 25, 26}; /* Tiva TM4C123 */ //const int COL[] = {PA_2, PA_3, PA_4, PA_5}; const int COL[] = {11, 12, 13, 8}; void keypad_init(void) { // all pins are initialized as input for (int i = 0; i < 4; i++) { pinMode(ROW[i], INPUT); pinMode(COL[i], INPUT); } } /* * This is a non-blocking function to read the keypad. * If a key is pressed, it returns a keycode. Otherwise, a zero * is returned. * Columns are used as input. There are pull-down resistors for the columns * on the EduBase board. When these pins are not active, they are low. * Rows are used as excitation. First all rows are driven high and the * input pins are read. If no keys are pressed, it will read a zero. * Otherwise, some key is pressed. * If any key is pressed, the program drives one row high at a time and * leave the rest of the rows inactive (float as input) then read the input pins. * Knowing which row is active and which column is active, the program * can decide which key is pressed. */ char keypad_getkey(void) { int i; int row, col; // check to see any key pressed first // enable all rows for (i = 0; i < 4; i++) pinMode(ROW[i], OUTPUT); digitalGroupWrite(ROW, 4, 0x0F); // read all columns col = digitalGroupRead(COL, 4); // disable all rows for (i = 0; i < 4; i++) pinMode(ROW[i], INPUT); if (col == 0) return 0; // no key pressed, return a zero // If a key is pressed, it comes here to find out which key is pressed. // One row is activated at a time and the inputs are read to see // which column is active. for (row = 0; row < 4; row++) { // disable all rows for (i = 0; i < 4; i++) pinMode(ROW[i], INPUT); pinMode(ROW[row], OUTPUT); // enable one row digitalWrite(ROW[row], HIGH); // turn the active row high delayMicroseconds(2); // wait for signal to settle col = digitalGroupRead(COL, 4); // read all columns if (col != 0) break; // if one of the input is low, some key is pressed. } // disable all rows for (i = 0; i < 4; i++) pinMode(ROW[i], INPUT); if (row == 4) return 0; // if we get here, no key is pressed // gets here when one of the rows has key pressed if (col == 1) return row * 4 + 1; // key in column 0 if (col == 2) return row * 4 + 2; // key in column 1 if (col == 4) return row * 4 + 3; // key in column 2 if (col == 8) return row * 4 + 4; // key in column 3 return 0; // just to be safe } // write to a group of output pins void digitalGroupWrite(const int* group, int length, int data) { for (length--; length >= 0; length--) { digitalWrite(group[length], data & (1 << length) ? HIGH : LOW); } } // read from a group of input pins int digitalGroupRead(const int* group, int length) { int data = 0%3b.html for (length--; length >= 0; length--) { data <<= 1; data |= digitalRead(group[length]) ? 1 : 0; } return data; }