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.

interfacing of tiva c series microcontroller with keypad

i have got this code for interfacing of 2*2 keypad with tiva c but it is not working..........actually i wanted to see the output on putty using UART ...........

#include <stdint.h>

#include <stdbool.h>

#include "inc/hw_types.h"

#include "inc/hw_memmap.h"

#include "driverlib/sysctl.h"
#include "driverlib/pin_map.h"
#include "driverlib/gpio.h"
#include "driverlib/uart.h"



uint8_t k;

//function to detect which key is pressed
uint8_t key_detect()

{

GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_2 | GPIO_PIN_3, 0x08);

if(GPIOPinRead(GPIO_PORTE_BASE, GPIO_PIN_2)==0x00) return(0);

else if(GPIOPinRead(GPIO_PORTE_BASE, GPIO_PIN_3)==0x00) return(1);



GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_2 | GPIO_PIN_3, 0x04);

if(GPIOPinRead(GPIO_PORTE_BASE, GPIO_PIN_2)==0x00) return(2);

else if(GPIOPinRead(GPIO_PORTE_BASE, GPIO_PIN_3)==0x00) return(3);

if(GPIOPinRead(GPIO_PORTE_BASE, GPIO_PIN_3)!=0x00 && GPIOPinRead(GPIO_PORTE_BASE, GPIO_PIN_2)!=0x00)
	return(6);
}



void key_function(uint8_t key)

{

switch(key)

{

case 0: SysCtlDelay(13333334);
UARTCharPut(UART0_BASE, '0');break;

case 1: SysCtlDelay(13333334);
UARTCharPut(UART0_BASE, '1');break;

case 2: SysCtlDelay(13333334);
UARTCharPut(UART0_BASE, '2');break;

case 3: SysCtlDelay(13333334);
UARTCharPut(UART0_BASE, '3');break;

//prints 6 when no key is pressed
case 6: SysCtlDelay(13333334);
UARTCharPut(UART0_BASE, '6');break;



}

}

int main(void)

{

// Tiva C LaunchPad System Initialisation //

SysCtlClockSet(SYSCTL_SYSDIV_5 | SYSCTL_USE_PLL| SYSCTL_XTAL_16MHZ|SYSCTL_OSC_MAIN);


//ENABLING PORTS

SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD);

SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE);

SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);


//DEFINING THE PINS


//pins of PORT F are connected to the LEDs
GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3);


//pins of PORT D are connected to rows 1,2 of the keypad
GPIOPinTypeGPIOOutput(GPIO_PORTD_BASE, GPIO_PIN_2 | GPIO_PIN_3);

//pins of PORT E are connected to column 1,2 of the keypad
GPIOPinTypeGPIOOutput(GPIO_PORTE_BASE, GPIO_PIN_2 | GPIO_PIN_3);


// End of Tiva C LaunchPad System Initialisation //



// Wait 1 Millisecond after LCD Power Up //

SysCtlDelay(13333);


//making the rows 1,2 zero connected to PORTD
GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_2 | GPIO_PIN_3, 0x00);

SysCtlDelay(13333334);

//configuring UART
SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
GPIOPinConfigure(GPIO_PA0_U0RX);
GPIOPinConfigure(GPIO_PA1_U0TX);

//defining the UART pins at PORTA 0,1
GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 |GPIO_PIN_1);

UARTConfigSetExpClk(UART0_BASE, SysCtlClockGet(),115200,(UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE |UART_CONFIG_PAR_NONE));



GPIOPinWrite(GPIO_PORTE_BASE, GPIO_PIN_2 | GPIO_PIN_3, 0x0c);


//To check whether the write operation has be done properly or not,if done properly blue LED glows

if(GPIOPinRead(GPIO_PORTE_BASE, GPIO_PIN_3)==0x08)
{
      //blue LED glows
	 GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1| GPIO_PIN_2| GPIO_PIN_3, 0x04);
}
if(GPIOPinRead(GPIO_PORTE_BASE, GPIO_PIN_3)==0x00)
{
           
     //green LED glows
	 GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1| GPIO_PIN_2| GPIO_PIN_3, 0x08);
}

SysCtlDelay(133333349);

// Loop for detecting key press and key function //
while(1)

{

//Reading the value of column 1 of keypad connected at PORTE
if(GPIOPinRead(GPIO_PORTE_BASE, GPIO_PIN_2)==0x00)

{
   //glowing of LED
   GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1| GPIO_PIN_2| GPIO_PIN_3, 0x08);

   SysCtlDelay(13333334);

   k=key_detect();

   key_function(k);

}

else if(GPIOPinRead(GPIO_PORTE_BASE, GPIO_PIN_3)==0x00)

{

  GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1| GPIO_PIN_2| GPIO_PIN_3, 0x02);

  SysCtlDelay(13333334);

 
  k=key_detect();

key_function(k);

}

//if no key is pressed
else if(GPIOPinRead(GPIO_PORTE_BASE, GPIO_PIN_3)!=0x00 && GPIOPinRead(GPIO_PORTE_BASE, GPIO_PIN_2)!=0x00)
{
	SysCtlDelay(13333334);
	 k=key_detect();
	 key_function(k);
}

}

// End of loop for detecting key press and key function //

}


when i run this code blue LED is blinking showing that thePinWrite operation has been done correctly ,but on putty it is only diaplying 66666.....showing that no key is pressed even when i am pressing the key................

  • Hello Dhananjay,

    With the API GPIOPinTypeGPIOOutput(GPIO_PORTE_BASE, GPIO_PIN_2 | GPIO_PIN_3) you would be driving the column pin as 0. The 1 being driven on the row will result in pin driver contention (an electrical issue).

    The column pins must be in input mode,

    Regards
    Amit
  • Hello Amit Ashara,

    the logic is ...i make the columns as 1 and the rows are zero ..........when the key is pressed then the column corresponding to the key press would become zero and already rows are 0 .now i make one of the rows 0 and others 1 ,to check whether the column corresponding to the zero row has been pressed or not.....if not then i make the next row 0 and all other rows 1.....if that comes out to be true then i can find the key pressed ...........

    for this purpose i need to make the columns pins in output mode else i can write to it to make it 1..........

    and in the code presented the PORTE where the column pins are connected are in output mode performs both the operations read and write........i say this because the lines 137 to 141 works and the blue LED glows showing the read and write operations has been performed
  • Hello Dhananjay,

    I don't think this is how keyscan works. Keyscan requires that the either the column or row are made 0. On the other side the row or column is made as an input with Pull Up. If there is a key pressed the read will produce a 0 instead of a 1 due to a pull up. Knowing which column/row was 0 and which row/column has been read 0 the position of the key is determined.

    en.wikipedia.org/.../Computer_keyboard

    Regards
    Amit
  • thank u very much Amit Ashara..........that was a useful piece of information............