I want to scan 4x4 keypad using TM4C123g launchpad. I tried with pins D1,D2,A5 as output pins, and E1,E2,E3 pins as input pins (with pullups.)
when I write 0 to D1 and and 1 to D2,A5,
GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_1 |GPIO_PIN_2, 0x0D);
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_5, 0xFF);
I am gettin the correct output in the lcd.
When I use the other values, I am getting the wrong display.
GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_1 |GPIO_PIN_2, 0xFF);
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_5, 0x00);
I am not able to understand where is the problem. Can anyone help??
I am using the I2C memory, which uses A6,A7 pins, which are attached while running this.
my code:-
// project //
#include <stdint.h>
#include <stdbool.h>
#include "inc/tm4c123gh6pm.h"
//#include "inc/hw_i2c.h"
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "inc/hw_gpio.h"
//#include "driverlib/i2c.h"
#include "driverlib/sysctl.h"
#include "driverlib/gpio.h"
#include "driverlib/pin_map.h"
//----------------------------------------
// Prototypes
//----------------------------------------
//----------------------------------------
// Prototypes
//----------------------------------------
void hardware_init(void);
//void ledToggle(void);
void delay(void);
void LCD_configuration();
void CMD_write(int val);
void data_write(char value1);
void scan_key();
char a= '0';
int valid_ID_flag=0;
void main(void)
{
int i=0;
char password[3];
char correct_msg[16]="password valid ";
char incorrect_msg[16]="password invalid";
hardware_init();
delay();
LCD_configuration();
delay();
CMD_write(0x01);
delay();
//name_display(count);
int p=0;
for(p=0;p<3;p++)
{
scan_key();
//delay();
password[p]=a;
}
delay();
CMD_write(0x01);
if (password == "321")
{
CMD_write(0x01);
delay();
for(i=0;i<16; i++)
{
data_write(correct_msg[i]);
delay();
}
}
else
{
CMD_write(0x01);
delay();
for(i=0;i<16; i++)
{
data_write(incorrect_msg[i]);
}
}
delay();
delay();
delay();
delay();
while(1)
{
}
}
void scan_key()
{
int flag=0;
while(flag==0)
{
GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_1 |GPIO_PIN_2, 0xFF);
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_5, 0x00);
if(GPIOPinRead(GPIO_PORTE_BASE, GPIO_PIN_1) == 0 && valid_ID_flag==0)
{
delay();
if(GPIOPinRead(GPIO_PORTE_BASE, GPIO_PIN_1) == 0)
{
flag=1;
//count=count+1;
}
a='4';
data_write(a);
}
if(GPIOPinRead(GPIO_PORTE_BASE, GPIO_PIN_2) == 0 && valid_ID_flag==0)
{
delay();
while(GPIOPinRead(GPIO_PORTE_BASE, GPIO_PIN_2) == 0)
{
flag=1;
//count=count+1;
}
a='5';
data_write(a);
}
if(GPIOPinRead(GPIO_PORTE_BASE, GPIO_PIN_3) == 0 && valid_ID_flag==0)
{
delay();
while(GPIOPinRead(GPIO_PORTE_BASE, GPIO_PIN_3) == 0)
{
flag=1;
//count=count+1;
}
a=6;
data_write(54);
}
}
}
void CMD_write(int val)
{
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4,0x00);
GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_0| GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7,val);
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_4,0x10);
delay();
}
void data_write(char value1)
{
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4,0x04);
GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_0| GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7,value1);
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_4,0x10);
delay();
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_4,0x00);
}
// functions working with LCD
void LCD_configuration()
{
CMD_write(0x38);
CMD_write(0x0f);
CMD_write(0x06);
CMD_write(0x01);
// CMD_write(0x80);
}
//---------------------------------------------------------------------------
// hardware_init()
//
// inits GPIO pins for toggling the LED
//---------------------------------------------------------------------------
void hardware_init(void)
{
//int ui32Period_p;
//Set CPU Clock to 40MHz. 400MHz PLL/2 = 200 DIV 5 = 40MHz
SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ); //setup clock
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE);
GPIOPinTypeGPIOOutput(GPIO_PORTA_BASE,GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5);
GPIOPinTypeGPIOOutput(GPIO_PORTB_BASE,GPIO_PIN_0| GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7);
GPIOPinTypeGPIOOutput(GPIO_PORTD_BASE,GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3);
GPIOPinTypeGPIOInput(GPIO_PORTE_BASE,GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3);
}
//---------------------------------------------------------------------------
// delay()
//
// Creates a 500ms delay via TivaWare fxn
//---------------------------------------------------------------------------
void delay(void)
{
SysCtlDelay(670000); // creates ~500ms delay - TivaWare fxn
}