MCP23008 Read inputs

After much help from the forum at using the outputs, im trying to use the inputs now.
Iv Verified the addressing as i can set outputs, but not sure how to read. the address is 0x26 High High Low

from the datasheet i think i need to
select iodir
set all to inputs (255)

i then do a wireRequestFrom
and serialoutput

i just recive 0,
if i pull up 0 using a 10k to 5v i get 0
if i put 0 to gnd i recive a 0
and if i leave the pull up in place and put 0 to gnd still recive 0

#include <Wire.h>
int io_data[2];

void setup() {
Wire.begin(); // initialise the wire library and hardware
Serial.begin(9600);
}

void loop() {
Wire.beginTransmission(0x26); //Chip address
Wire.send(0x00); //select the IODIR register
Wire.send(255); //set register value-all high, sets all pins as inputs on MCP23008
Wire.endTransmission();
Wire.requestFrom(0x26,2); //only two bytes
if(Wire.available())
{ io_data[0] =(Wire.receive()); io_data[1]=(Wire.receive());
Serial.println(io_data[0]);
Serial.println(io_data[1]);
}
else {Serial.println("ERROR: request from GP0 failed");} //FIXME - this should be removed
}

You need an 'if(Wire.available())' in front of each of the Wire.receive() statements.

better yet you need a loop that waits until there is data before you read it.
Like this:-
if (!Wire.available()) { } // do nothing until data arrives

Always post your code between the brackets you get from the hash icon in the reply box.

o.k you are a little wrong there, and I could give a little “rubbish” to mike , but I won't.

first set the DDR /IODIR .

Wire.beginTransmission(0x26);
Wire.send(0),Wire.send(255)
Wire.endTransmission();

Read DATA

Wire.beginTransmission(0x26);
Wire.send(9);
Wire.endTransmission();
Wire.requestFrom(0x26, 1);
tmp=Wire.receive();

why 2 bytes ?

if you want to read from it again, you have to repeat the read DATA loop , you just can`t read 2 bytes .

Well not sure why it is rubbish it is a snip from my Arduinocater code,
http://www.google.co.uk/url?sa=t&source=web&ct=res&cd=1&ved=0CAcQFjAA&url=http%3A%2F%2Fblog.makezine.com%2Farchive%2F2009%2F03%2Farduinocaster_shreds_in_midi.html&ei=-SnEStOoFMaZjAeK48g5&usg=AFQjCNFVW-4HzyFqUp3856KbdLgrXK4WEw&sig2=NTFbEW0V_n0c_agu0QsozQ
the full read routine is:-

void gpio_write(int address, int data, int reg) {
  //  Send output register address
  Wire.beginTransmission(address);
  Wire.send(reg);
  //  Connect to device and send two bytes
  Wire.send(0xff & data);  //  low byte
  Wire.send(data >> 8);    //  high byte
  Wire.endTransmission();
}
int gpio_read(int address) {
  int data = 0;
 //  Send input register address
  Wire.beginTransmission(address);
  Wire.send(I2CregisterInput);
  Wire.endTransmission();
  //  Connect to device and request two bytes
 // Wire.beginTransmission(address);
  Wire.requestFrom(address, 2);
 if (!Wire.available()) { } // do nothing until data arrives
    data = Wire.receive();

 if (!Wire.available()) { } // do nothing until data arrives
    data |= Wire.receive() << 8;
  Wire.endTransmission();
  return data;
}

This used a MCP23016 which is two 8 bit registers but the protocol is almost the same.

Thanks for the replys, Seems i missed out selecting the Gipo Register.
The reason for two bytes, id read that some chips always send two bytes so you have to request 2 and pick the one you want.

Il be using both examples.
As Mikes is blocking, i will be using it when im expecting data I.E setting the clock.
and the other non blocking so the program can run until user input I.E open the main menu.

The reason for two bytes, id read that some chips always send two bytes so you have to request 2 and pick the one you want.

No, normally it doesn't work like that .

If you send 3 bytes eg 9 , 4 , 2

You are writing to registers 9 with 4 and 10 with 2 , so you may be accidentally setting a registers.

Reading works the same way.

If you send 9 then read 2 bytes you are reading from registers 9 and 10.

Most of the time you don't need to use Wire.available because you know how many bytes will be placed on the bus and when.
I only use it if for example, when you are been sent a string over i2c, where the string length is unknown, Just ask for 254 bytes and keep reading the bus until you get the endTransmission from the other side.

This used a MCP23016 which is two 8 bit registers but the protocol is almost the same.

The MCP23016 is similar but not the same !!!!
You have to remember the change of registers and you are only writing and reading 1 byte and not 2.