0% found this document useful (0 votes)
149 views3 pages

Tiva Programa Con Teclado

This document describes code for initializing and reading input from a 4x4 keypad matrix connected to a microcontroller. It initializes the GPIO pins for the keypad rows and columns as inputs and outputs. A keypad_getkey() function scans the keypad matrix and returns the pressed key value by enabling one row at a time and reading the column pins. It uses delays to allow signals to settle. LEDs are also initialized on other GPIO pins and can be toggled on/off.

Uploaded by

RonyVargas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
149 views3 pages

Tiva Programa Con Teclado

This document describes code for initializing and reading input from a 4x4 keypad matrix connected to a microcontroller. It initializes the GPIO pins for the keypad rows and columns as inputs and outputs. A keypad_getkey() function scans the keypad matrix and returns the pressed key value by enabling one row at a time and reading the column pins. It uses delays to allow signals to settle. LEDs are also initialized on other GPIO pins and can be toggled on/off.

Uploaded by

RonyVargas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

/* 4x4 matrix keypad

*
* 4x4 Keypad Detection
*
**/

#include <stdint.h>
#include "inc/tm4c123gh6pm.h"

#define RED_LED 0x02 //


#define BLUE_LED 0x04
#define GREEN_LED 0x08

#define KEYPAD_ROW GPIOE


#define KEYPAD_COL GPIOC

#define GPIO_PORTF_LED (*((volatile int32_t *)0x40025038))

void keypad_init(void);
unsigned char keypad_kbhit(void);
unsigned char keypad_getkey(void);
void gpio_led_init(void);
void gpio_toggle_led(uint8_t led);
void delayMs(int n);
void delayUs(int n);

int main(void)
{
unsigned char key;

keypad_init();

{
// LCD_command(0x80); /* LCD cursor location */

key = keypad_getkey(); /* read the keypad */


if (key != 0)
{ /* if a key is pressed */
// LCD_data(key); /* display the key label */
}
else
// LCD_data(' ');

delayMs(20); /* wait for a while */


}

/* this function initializes the ports connected to the keypad */


void keypad_init(void)
{
SYSCTL_RCGC2_R |= 0x04; /* enable clock to GPIOC (COLUMN) */
SYSCTL_RCGC2_R |= 0x10; /* enable clock to GPIOE (ROW) */

GPIO_PORTE_DIR_R |= 0x0F; /* set row pins 3-0 as output */


GPIO_PORTE_DEN_R |= 0x0F; /* set row pins 3-0 as digital pins */
GPIO_PORTE_ODR_R |= 0x0F; /* set row pins 3-0 as open drain */

GPIO_PORTC_DIR_R &= ~0xF0; /* set column pin 7-4 as input */


GPIO_PORTC_DEN_R |= 0xF0; /* set column pin 7-4 as digital pins */
GPIO_PORTC_PUR_R |= 0xF0; /* enable pull-ups for pin 7-4 */
}

void gpio_led_init(void)
{
/* enable clock to GPIO PORTF at clock gating control register */
SYSCTL_RCGC2_R |= 0x00000020;
/* enable the GPIO pins for the LED (PF3, 2, 1) as output */
GPIO_PORTF_DIR_R = 0x0E;
/* enable the GPIO pins for digital function */
GPIO_PORTF_DEN_R = 0x0E;
}

/* This is a non-blocking function. */


/* If a key is pressed, it returns 1. Otherwise, it returns a 0 (not ASCII 0).*/
unsigned char keypad_getkey(void)
{
const unsigned char keymap[4][4] = {
{ '1', '2', '3', 'A'},
{ '4', '5', '6', 'B'},
{ '7', '8', '9', 'C'},
{ '*', '0', '#', 'D'},
};

int row, col;

/* check to see any key pressed first */


GPIO_PORTE_DATA_R = 0; /* enable all rows */
col = GPIO_PORTC_DATA_R & 0xF0; /* read all columns */
if (col == 0xF0) return 0; /* no key pressed */

/* If a key is pressed, it gets here to find out which key. */


/* Although it is written as an infinite loop, it will take one of the breaks
or return in one pass.*/
while (1)
{
row = 0;
GPIO_PORTE_DATA_R = 0x0E; /* enable row 0 */
delayUs(30); /* wait for signal to settle */
col = GPIO_PORTC_DATA_R & 0xF0;
if (col != 0xF0) break;

row = 1;
GPIO_PORTE_DATA_R = 0x0D; /* enable row 1 */
delayUs(2); /* wait for signal to settle */
col = GPIO_PORTC_DATA_R & 0xF0;
if (col != 0xF0) break;

row = 2;
GPIO_PORTE_DATA_R = 0x0B; /* enable row 2 */
delayUs(2); /* wait for signal to settle */
col = GPIO_PORTC_DATA_R & 0xF0;
if (col != 0xF0) break;

row = 3;
GPIO_PORTE_DATA_R = 0x07; /* enable row 3 */
delayUs(2); /* wait for signal to settle */
col = GPIO_PORTC_DATA_R & 0xF0;
if (col != 0xF0) break;

return 0; /* if no key is pressed */


}

/* gets here when one of the rows has key pressed */


if (col == 0xE0) return keymap[row][0]; /* key in column 0 */
if (col == 0xD0) return keymap[row][1]; /* key in column 1 */
if (col == 0xB0) return keymap[row][2]; /* key in column 2 */
if (col == 0x70) return keymap[row][3]; /* key in column 3 */
return 0; /* just to be safe */ /* a key is pressed */
}

void gpio_toggle_led(uint8_t led)


{
GPIO_PORTF_LED = (uint32_t)led;
}

unsigned char keypad_kbhit(void)


{
int col;

/* check to see any key pressed */


GPIO_PORTE_DATA_R = 0; /* enable all rows */
col = GPIO_PORTC_DATA_R & 0xF0; /* read all columns */
if (col == 0xF0)
return 0; /* no key pressed */
else
return 1; /* a key is pressed */
}

/* delay n milliseconds (16 MHz CPU clock) */


void delayMs(int n)
{
int i, j;

for(i = 0 ; i < n; i++)


for(j = 0; j < 3180; j++) {} /* do nothing for 1 ms */
}

void delayUs(int n)
{
int i, j;
for(i = 0 ; i < n; i++)
for(j = 0; j < 3; j++)
{} /* do nothing for 1 us */
}

You might also like