I am trying to develop a code for interfacing a 2x2 keypad with a 16x2 LCD using a Tiva C launchpad. The code will display a "1" on the screen if the first button is pressed, "2" when the second button is pressed and so on till the fourth button is pressed. The "Vo" pin on the LCD has been connected to the POT. The LCD is also working fine if I simply display any name on it.
Column1 Column2
________ _______
| Button 1 | Button 2 | Row1
________ _______
| Button 3 | Button 4 | Row2
________ _______
Here is the Program :-
/* PB0-PB7 (LaunchPad) - D0-D7 (LCD) */
/* Ground (LaunchPad) - RW (LCD) */
/* PA6 (LaunchPad) - RS (LCD) */
/* PA7 (LaunchPad) - EN (LCD) */
/* PD2 (LaunchPad) - Row1 */
/* PD3 (LaunchPad) - Row2 */
/* PE4 (LaunchPad) - Column1 */
/* PE5 (LaunchPad) - Column2 */
#include <stdint.h>
#include <stdbool.h>
#include "inc/hw_types.h"
#include "inc/hw_memmap.h"
#include "driverlib/sysctl.h"
#include "driverlib/gpio.h"
uint8_t k;
void LCDCommand(uint8_t c);
void LCDInitialise()
{
uint8_t value;
uint8_t ui8Value[]={0x38, 0x0e, 0x06, 0x0c, 0x01, 0x80};
for(value=0;value<=5;value++)
LCDCommand(ui8Value[value]);
}
void LCDCommand(uint8_t c)
{
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, c);
/* Register Select(RS) = 0 for 200ns */
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_6, 0x00);
SysCtlDelay(3);
/* Enable Pulse Width (High Level) 1us */
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_7, 0x80);
SysCtlDelay(13);
/* Register Select(RS) = 0, Enable(E) = 0 for 100ns */
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_6 | GPIO_PIN_7, 0x00);
SysCtlDelay(2);
/* Setting up RS and EN again */
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_6|GPIO_PIN_7, 0x40);
SysCtlDelay(2); /* 100ns */
/* 2ms delay */
SysCtlDelay(26667);
}
void LCDData(uint8_t d)
{
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, d);
/* Register Select(RS) = 1 for 200ns */
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_6, 0x40);
SysCtlDelay(3);
/* Enable Pulse Width (High Level) 1us */
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_7, 0x80);
SysCtlDelay(13);
/* Register Select(RS) = 1, Enable(E) = 0 for 100ns */
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_6 | GPIO_PIN_7, 0x40);
SysCtlDelay(2);
/* Setting up RS and EN again */
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_6|GPIO_PIN_7, 0x00);
SysCtlDelay(2); /* 100ns delay */
/* 2ms delay */
SysCtlDelay(26667);
}
uint8_t key_detect()
{
GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_2 | GPIO_PIN_3, 0x08);
if(GPIOPinRead(GPIO_PORTE_BASE, GPIO_PIN_4)==0x00) return(0);
else if(GPIOPinRead(GPIO_PORTE_BASE, GPIO_PIN_5)==0x00) return(1);
GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_2 | GPIO_PIN_3, 0x04);
if(GPIOPinRead(GPIO_PORTE_BASE, GPIO_PIN_4)==0x00) return(2);
else if(GPIOPinRead(GPIO_PORTE_BASE, GPIO_PIN_5)==0x00) return(3);
}
void key_function(uint8_t key)
{
switch(key)
{
case 0: LCDData(0x31); GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_2 | GPIO_PIN_3, 0x00); SysCtlDelay(13333334); break;
case 1: LCDData(0x32); GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_2 | GPIO_PIN_3, 0x00); SysCtlDelay(13333334); break;
case 2: LCDData(0x33); GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_2 | GPIO_PIN_3, 0x00); SysCtlDelay(13333334); break;
case 3: LCDData(0x34); GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_2 | GPIO_PIN_3, 0x00); SysCtlDelay(13333334); break;
case 100: LCDData(0x30); break;
}
}
int main(void)
{
// Tiva C LaunchPad System Initialisation //
SysCtlClockSet(SYSCTL_SYSDIV_5 | SYSCTL_USE_PLL| SYSCTL_XTAL_16MHZ|SYSCTL_OSC_MAIN);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3);
GPIOPinTypeGPIOOutput(GPIO_PORTA_BASE, GPIO_PIN_6 | GPIO_PIN_7);
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_2 | GPIO_PIN_3);
GPIOPinTypeGPIOInput(GPIO_PORTE_BASE, GPIO_PIN_4 | GPIO_PIN_5);
// End of Tiva C LaunchPad System Initialisation //
// Wait 1 Millisecond after LCD Power Up //
SysCtlDelay(13333);
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_6 | GPIO_PIN_7, 0x00);
// LCD Initialisation //
LCDInitialise();
// End of LCD Initialisation //
LCDData(0x30);
GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_2 | GPIO_PIN_3, 0x00);
SysCtlDelay(13333334);
// Loop for detecting key press and key function //
while(1)
{
//GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_2 | GPIO_PIN_3, 0x00);
if(GPIOPinRead(GPIO_PORTE_BASE, GPIO_PIN_4)==0x00)
{
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1| GPIO_PIN_2| GPIO_PIN_3, 0x02);
SysCtlDelay(13333334);
k=key_detect();
key_function(k);
}
else if(GPIOPinRead(GPIO_PORTE_BASE, GPIO_PIN_5)==0x00)
{
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1| GPIO_PIN_2| GPIO_PIN_3, 0x04);
SysCtlDelay(13333334);
k=key_detect();
key_function(k);
}
}
// End of loop for detecting key press and key function //
}
The problem with the code is that as soon as I download the code into the chip, it automatically starts displaying 1's on the screen. Both the first and second row on the LCD are filled up with 1's in a matter of time even if I don't press the first button. Also, 2 is displayed on the screen when I press the third button or when I press the second and third button simultaneously and as soon as I release the button(s), again "1" starts filling up the space . The same happens with 3. 3 is displayed only when I press the third and fourth button simultaneously. 4 is never displayed.
Any suggestions....??