ArduinoGeneral

i2C GPIO example code error

userHead Account cancelled 2014-04-17 07:31:05 1855 Views0 Replies
There are a couple errors in the example code for using the Arduino I2C to GPIO Module (SKU:DFR0013) to read a 4x4 keypad. The issue is that code sets the output pins sequentially HIGH and then reads the input pins to see if any are HIGH. However, the input pins appear to be pulled high by default (onboard pull up resistors). Therefore, if you call the function gpio_read(PCA9555) you will be returned 15 (b1111) regardless of whether any buttons are pressed or not.

A workaround is to set the output pins sequentially LOW and then read the input pins to see if any are LOW. The sample code can be used, but the function readdata needs to be updated: this code worked for me...

unsigned char readdata(void) //main read function
{
for (int i=0;i<4;i++) //for loop
{
int input=0xF0;
input ^= (1 << (i+4)); // toggle output LOW (outputs are 5th - 8th bits)
write_io (OUT_P0, input);
for (int j=0;j<4;j++)
{
unsigned int temp=0x0F; //binary 1111
temp ^= (1 << j);
if (gpio_read(PCA9555)==temp)
{ return outputchar(i,j);} // output the char
}
}
return 'E'; // if no button is pressed, return E
}