This thread has been locked.
If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.
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....??
Hi, at a glance a good basic course on C programming is the best cure so try extract tip and style from but also remember avoid non C programmer style:
vikas saxena said:0 1 2 3 4 5 < 6
uint8_t ui8Value[]={0x38, 0x0e, 0x06, 0x0c, 0x01, 0x80};
for(value=0;value<=5;value++)
--------------------------------^
Use < 6 or better sizeof() so you can add arbitrary number of parameter.
vikas saxena said:/* Enable Pulse Width (High Level) 1us */
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_7, 0x80);
First happen here and so on use same GPIO_PIN_x save error setting the mask
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_7, GPIO_PIN_7)
And here the final disease of this bad habit:
vikas saxena said: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);
/* otherwise */ return (xxx);
}
Your function return an undefined value, 0 or 0xff must be in place of xxx to detect when no key where pressed. Is possible from your description compiler set undefined return forever @ 0.
No multiple key detection is in place, scanning keyboard (linitd as your don't care) require some attention on driving pin, diode or resistor must be placed in series to avoid conflicting multiple key press. TIVA suffer a lot this problem and pin driver wear out in a very short time.
At last this select forever write something...
Key up key down not detected nor at almost no key pressed status...
vikas saxena said: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;
}
Roberto provides very sound guidance.
We note that you abandoned your past post-request:
e2e.ti.com/support/microcontrollers/tiva_arm/f/908/t/335722.aspx
even after extensive time/effort and detail was (timely) provided in your behalf.
Such is not especially motivating...
cb1_mobile said:even after extensive time/effort and detail was (timely) provided in your behalf.
Such is not especially motivating...
Don't worry about, sometimes they burn themselves, other time they go to politics like in my worst country... Or the wrong xtal capacitance calculus too ;)
I was trying migrating language I can suppose you also know, but there where no local support on ti site, I need learn too so "Hilfe, ich lehre nicht in meinem Fach."
http://www.ti.com/ww/de/prod_portal.html