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.

KEY PAD INTERFACING

Other Parts Discussed in Thread: PCF8575

Hello,

am interfacing keypad to lm4f232 with i/o expander and am new to this interfacing. have tried with the below code but its not working can any one help out

//*****************************************************************************/
// Project : Split Revolving Display
// Board Name : SRDC
// Description : This Test Routine is used to test the Inputs and Outputs of SRDC board & Daughter Board.
// Inputs IP0 to IP15 is mapped to Output pins of PCF8575.

//

//
//*****************************************************************************/

#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "inc/hw_i2c.h"
#include "driverlib/i2c.h"
#include "driverlib/sysctl.h"
#include "driverlib/gpio.h"
#include "driverlib/rom.h"
#include "grlib/grlib.h"
#include "inc/hw_ints.h"
#include "driverlib/i2c.h"
#include "driverlib/pin_map.h"
#include "driverlib/UART.h"

//
// Set the address for slave module. This is a 7-bit address sent in the
// following format:
// [0:1:0:0:0:0:0:RS]
//
// A zero in the "RS" position of the first byte means that the master
// transmits (sends) data to the selected slave, and a one in this position
// means that the master receives data from the slave.
//
//*****************************************************************************
#define SLAVE_ADDRESS2 0x19
#define SLAVE_ADDRESS1 0x20
void
UARTSend(const unsigned char *pucBuffer, unsigned long ulCount);
unsigned long Hour = 0x00, Min = 0x00, Sec = 0x00, temp1,Temp2;
unsigned short iVal, Temp;
unsigned char Time[6], Byte1,Byte2;
unsigned int Temp3=0,Temp4;
void
column1(int);
void
column2(int);
void
column3(int);
void
column4(int);


void
KeypadIntHandler(void)
{

UARTSend((unsigned char *)"key pressed is 3", 18);
GPIOPinIntClear(GPIO_PORTJ_BASE, GPIO_PIN_2);
GPIOPinIntDisable(GPIO_PORTJ_BASE, GPIO_PIN_2);

/************** clearing o/p pins **********************/

ROM_I2CMasterSlaveAddrSet(I2C0_MASTER_BASE, SLAVE_ADDRESS1, false);

Temp2 = ROM_I2CMasterErr(I2C0_MASTER_BASE);

ROM_I2CMasterControl(I2C0_MASTER_BASE, I2C_MASTER_CMD_SINGLE_SEND);

Temp2 = ROM_I2CMasterErr(I2C0_MASTER_BASE);

ROM_I2CMasterDataPut(I2C0_MASTER_BASE, 0x01);

while(ROM_I2CMasterBusy(I2C0_MASTER_BASE))
{
}
ROM_I2CMasterDataPut(I2C0_MASTER_BASE, 0x00);
// I2CMasterControl(I2C0_MASTER_BASE, I2C_MASTER_CMD_BURST_SEND_FINISH);

// Wait for the first write/transaction to complete
while(ROM_I2CMasterBusy(I2C0_MASTER_BASE))
{
}
/*********************reading input pins************************/
ROM_I2CMasterSlaveAddrSet(I2C0_MASTER_BASE, SLAVE_ADDRESS1, false);

Temp2 = ROM_I2CMasterErr(I2C0_MASTER_BASE);

ROM_I2CMasterControl(I2C0_MASTER_BASE, I2C_MASTER_CMD_SINGLE_RECEIVE);

Temp2 = ROM_I2CMasterErr(I2C0_MASTER_BASE);



Temp3=ROM_I2CMasterDataGet(I2C0_MASTER_BASE);

while(ROM_I2CMasterBusy(I2C0_MASTER_BASE))
{
}
Temp4=Temp3;

/***********************scanning columns******************************/

if((Temp3 & 0x00001000) == 0)
{

column4(Temp4);
}
else if ((Temp3 & 0x00000100) == 0)
{
column3(Temp4);
}
else if ((Temp3 & 0x00000010) == 0)
{
column2(Temp4);
}
else
{
column1(Temp4);
}



}

void column1(int Temp5)
{
if((Temp5 & 0x00010000) == 0)
{
UARTSend((unsigned char *)"key pressed is one", 18);
}
else if ((Temp5 & 0x00100000) == 0)
{
UARTSend((unsigned char *)"key pressed is 5", 18);
}
else if ((Temp5 & 0x01000000) == 0)
{
UARTSend((unsigned char *)"key pressed is 9", 18);
}
else
{
UARTSend((unsigned char *)"key pressed is c", 18);
}
}


void column2(int Temp5)
{
if((Temp5 & 0x00010000) == 0)
{
UARTSend((unsigned char *)"key pressed is 2", 18);
}
else if ((Temp5 & 0x00100000) == 0)
{
UARTSend((unsigned char *)"key pressed is 6", 18);
}
else if ((Temp5 & 0x01000000) == 0)
{
UARTSend((unsigned char *)"key pressed is 10", 18);
}
else
{
UARTSend((unsigned char *)"key pressed is D", 18);
}
}

void column3(int Temp5)
{
if((Temp5 & 0x00010000) == 0)
{
UARTSend((unsigned char *)"key pressed is 3", 18);
}
else if ((Temp5 & 0x00100000) == 0)
{
UARTSend((unsigned char *)"key pressed is 7", 18);
}
else if ((Temp5 & 0x01000000) == 0)
{
UARTSend((unsigned char *)"key pressed is A", 18);
}
else
{
UARTSend((unsigned char *)"key pressed is E", 18);
}
}

void column4(int Temp5)
{
if((Temp5 & 0x00010000) == 0)
{
UARTSend((unsigned char *)"key pressed is 4", 18);
}
else if ((Temp5 & 0x00100000) == 0)
{
UARTSend((unsigned char *)"key pressed is 8", 18);
}
else if ((Temp5 & 0x01000000) == 0)
{
UARTSend((unsigned char *)"key pressed is B", 18);
}
else
{
UARTSend((unsigned char *)"key pressed is F", 18);
}
}

void
UARTSend(const unsigned char *pucBuffer, unsigned long ulCount)
{
//
// Loop while there are more characters to send.
//
while(ulCount--)
{
//
// Write the next character to the UART.

ROM_UARTCharPutNonBlocking(UART7_BASE, *pucBuffer++);
}
}


int
main(void)
{

unsigned long i,j;


//
// Set the clocking to run directly from the external crystal/oscillator.
// TODO: The SYSCTL_XTAL_ value must be changed to match the value of the
// crystal on your board.
//
ROM_SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN |
SYSCTL_XTAL_8MHZ);

/*******************************sumanth************************************/
ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOJ);
ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE);
ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C0);
ROM_GPIOPinConfigure(GPIO_PB2_I2C0SCL);
ROM_GPIOPinConfigure(GPIO_PB3_I2C0SDA);

ROM_GPIOPinTypeI2C(GPIO_PORTB_BASE, GPIO_PIN_3);
ROM_GPIOPinTypePWM(GPIO_PORTB_BASE, GPIO_PIN_2);
ROM_I2CMasterInitExpClk(I2C0_MASTER_BASE, SysCtlClockGet(), false);
/***********************************sumanth******************************/
ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_UART7);

ROM_GPIOPinConfigure(GPIO_PE1_U7TX);
ROM_GPIOPinConfigure(GPIO_PE0_U7RX);
ROM_GPIOPinTypeUART(GPIO_PORTE_BASE, GPIO_PIN_0 | GPIO_PIN_1);

ROM_UARTConfigSetExpClk(UART7_BASE, ROM_SysCtlClockGet(), 115200,UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE |
UART_CONFIG_PAR_NONE);


ROM_IntEnable(INT_UART7);
ROM_UARTIntEnable(UART7_BASE, UART_INT_RX | UART_INT_RT | UART_INT_TX);
/*Interrupt enable for GPIO PORTJ PIN*/

ROM_IntMasterEnable();
GPIOPinTypeGPIOInput(GPIO_PORTJ_BASE, GPIO_PIN_2);
GPIOIntTypeSet(GPIO_PORTJ_BASE, GPIO_PIN_2,GPIO_FALLING_EDGE);
GPIOPinIntEnable(GPIO_PORTJ_BASE, GPIO_PIN_2);


/* Configure the Port pins as OUTPUT by writing 0x00 to Configuration register */


ROM_I2CMasterSlaveAddrSet(I2C0_MASTER_BASE, SLAVE_ADDRESS1, false);

Temp2 = ROM_I2CMasterErr(I2C0_MASTER_BASE);

ROM_I2CMasterControl(I2C0_MASTER_BASE, I2C_MASTER_CMD_SINGLE_SEND);

Temp2 = ROM_I2CMasterErr(I2C0_MASTER_BASE);

ROM_I2CMasterDataPut(I2C0_MASTER_BASE, 0x03);
while(ROM_I2CMasterBusy(I2C0_MASTER_BASE))
{
}

ROM_I2CMasterDataPut(I2C0_MASTER_BASE, 0x0F);
while(ROM_I2CMasterBusy(I2C0_MASTER_BASE))
{
}

ROM_I2CMasterSlaveAddrSet(I2C0_MASTER_BASE, SLAVE_ADDRESS1, false);

Temp2 = ROM_I2CMasterErr(I2C0_MASTER_BASE);

ROM_I2CMasterControl(I2C0_MASTER_BASE, I2C_MASTER_CMD_SINGLE_SEND);

Temp2 = ROM_I2CMasterErr(I2C0_MASTER_BASE);

ROM_I2CMasterDataPut(I2C0_MASTER_BASE, 0x01);
while(ROM_I2CMasterBusy(I2C0_MASTER_BASE))
{
}

ROM_I2CMasterDataPut(I2C0_MASTER_BASE, 0xF0);
while(ROM_I2CMasterBusy(I2C0_MASTER_BASE))
{
}


while(1)
{
//UARTSend((unsigned char *)"key pressed is 3", 18);
}
}

  • It's good - and appreciated - that you mentioned the use of I/O extender in your Ap.  While these extenders have appeal - they enforce another layer of complexity.  You've chosen an I2C version - and quick scan of your code reveals:

    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C0);
    ROM_GPIOPinConfigure(GPIO_PB2_I2C0SCL);
    ROM_GPIOPinConfigure(GPIO_PB3_I2C0SDA);
    ROM_GPIOPinTypeI2C(GPIO_PORTB_BASE, GPIO_PIN_3);

    Might you require a, "ROM_GPIOPinTypeI2C()" for the 2nd I2C pin - as well?  (but of course)  Beware - under M4 this is a special function - MCU manual and/or SW-DRL-UG details.

    Might you consider techniques/strategies you may employ - to ease the time/effort of all remote helpers?  Code supplied is littered w/UART calls - not central to this post.  Would it not be better to first "organize" your post - present the "essence" - rather than bombard this space?  Presentation - at both fine restaurant or this forum - indeed influences response.

    If yours is a 16 key device - I'd switch to MCU GPIO to perfect your software design.  (later - after your methods are "proven" - you may revert to I/O expander)  Much code for keypad scan/debounce and decode exists across the web - via GPIO. 

    KISS process almost always best/fastest - introducing complications early usually proves unwise...

     

  • Hi,

    thanks for your valuable advice.

    by going through the data sheet as the 2nd pin of it has a special function. i configured it as ROM_GPIOPinTypePWM(GPIO_PORTB_BASE, GPIO_PIN_2);. but still it is not working. 

    thanku 

    chethan kumar

  • Hi,

    Try this link - you will find out a .C routine for scanning a 4x4 keyboard - try it first on a GPIO as suggested by cb1- and after you understand the mechanics behind, you can implement on an expander.

    Also, from the same author, more useful examples. 

    Petrei

  • cb1_mobile said:
    Might you require a, "ROM_GPIOPinTypeI2C()" for the 2nd I2C pin - as well?

    Mon ami - ARMs are complex - detail-heavy devices - your attention to such (suggested) detail bit off mark...

    Function past missing is of type GPIOPinTypeI2CSCL() - and to my best knowledge - has not yet been promoted to ROM residence.  (i.e. don't call the ROM version - call the Flash version {thus do not employ the ROM_ prefix})

    Note that friend Petrei/this reporter - in agreement as to your (hoped for) use of I2C I/O extender - at this way early stage.  Again ("skipping record follows" - don't do this - someway/somehow harvest 8 GPIO - and connect these to keypad!)  Web loitered/over-flowing w/examples. (low-vision, attention-deficited, 3rd grader - while playing video game - could quickly find...) 

    Scan design, debounce, then decode of the keypad via far simpler - more direct GPIO - surely will keep you busy.  And - will provide a superb "roadmap" - should you later wish to cast off this "solution" - and add the complexity/cost of I2C GPIO Extender.  (note - this roadmap free from vendor's repeated admonition, "contact your local rep - all critical data "secret.")

    Odds of your quickly/properly/completely, "connecting all dots" - of both I2C "extender" and then scanning, debouncing, decoding a keypad - much resembles that of NASA chimp - with head injury - engaged in conversation - "nailing shuttle landing" (runway 15/33, Kennedy) - during strong crosswind... 

    KISS - discounted/rejected - too often yields predictable outcomes - few of them good...