MAX7300

i have a MAX7300 (Datasheet) and it uses I2c protocol to communicate with arduino, using the wire library i'm able to write to the MAX but i cannot find a way to read it.

Here's the code i'm using.

#include <Wire.h>
byte inFromGPIO = 0;
byte data = 0;
int ledPin = 13;
void setup(){
   pinMode (ledPin, OUTPUT);
   ledPin = LOW;
   Wire.begin();   //Initialize Wire library
   Serial.begin(9600);    //initialize serial port for testing
   Wire.beginTransmission (0x40);  //max7300 address
   Wire.send (0x04);   //configuration port
   Wire.send (0x01);    // set shutdown mode off
   Wire.endTransmission();   // close comm
   
   Wire.beginTransmission (0x40);   //max7300 address
   Wire.send (0x0F);   //Port Configuration for P31, P30, P29, P28 
   Wire.send (0x03);   //set to schmitt trigger in with pullup r
   Wire.endTransmission();  // close comm
}
void loop (){
  readI2C (0x3C,2);   //read 2 bits from port 28 (pin 21)
  
}
byte MAX7300_Address = 0x40;    // MAX7300 A0=0 A1=0 A2=0 A3=0
void sendI2C(byte command, byte data) //write function
{
  Wire.beginTransmission(MAX7300_Address);
  Wire.send(command);
  Wire.send(data);
  Wire.endTransmission();
}

void readI2C (byte port, byte numbytes)
 {
   byte MAX7300_Address = 0x40;  // MAX7300 A0=0 A1=0 A2=0 A3=0
   Wire.beginTransmission(MAX7300_Address);
   Wire.send(port); //max7300 needs to be written before read.
   Wire.send(0x00); // write 0
   Wire.endTransmission(); // close comm
   Wire.beginTransmission(MAX7300_Address);
   Wire.requestFrom(port, numbytes); //read port
       if (Wire.available()) {  //if data received
       data = Wire.receive(); //write to int
     }
     
   Wire.endTransmission(); //close comm
   
   Serial.println (data); //print to serial for testing
  
  if (data == 0) {
    ledPin = LOW;
   }
  if (data != 0) {
    ledPin = HIGH;
   }
   else{
   }
   delay (250);
   
  }

I get +5v at pin 21 so the pullup R is working (i have a pushbutton to GND with a 220ohm just in case it's putting high out instead .) but i get no changes in the LED or serial monitor.

Ok i got it on my own again.
In the function

void readI2C (byte port, byte numbytes)
 {
   byte MAX7300_Address = 0x40;  // MAX7300 A0=0 A1=0 A2=0 A3=0
   Wire.beginTransmission(MAX7300_Address);
   Wire.send(port); //max7300 needs to be written before read.
   Wire.send(0x00); // write 0
   Wire.endTransmission(); // close comm
   Wire.beginTransmission(MAX7300_Address);
   Wire.requestFrom(port, numbytes); //read port
       if (Wire.available()) {  //if data received
       data = Wire.receive(); //write to int
     }

You don't have to call "Wire.beginTransmission()" again as it will send the 8th bit of the device address as a "write" instruction. after closing the first transmission (remember MAX7300 needs to be written before read) you just call "Wire.requestFrom (deviceAddress, numofbytes)" and it will start from the las address you wrote to. So this is how it looks now.

#include <Wire.h>
byte inFromGPIO = 0;
unsigned int data = 0;
byte value = 0;
int ledPin = 13;
void setup(){
   pinMode (ledPin, OUTPUT);
   ledPin = LOW;
   Wire.begin();   //Initialize Wire library
   Serial.begin(9600);    //initialize serial port for testing
   Wire.beginTransmission (0x40);  //max7300 address
   Wire.send (0x04);   //configuration port
   Wire.send (0x01);    // set shutdown mode off
   Wire.endTransmission();   // close comm
   
   Wire.beginTransmission (0x40);   //max7300 address
   Wire.send (0x0F);   //Port Configuration for P31, P30, P29, P28 
   Wire.send (0x03);   //set to schmitt trigger in with pullup r
   Wire.endTransmission();  // close comm
}
void loop (){
  readI2C (0x57,8);   //read 8 bits from port 23 to 30
  
}
byte MAX7300_Address = 0x40;    // MAX7300 A0=0 A1=0 A2=0 A3=0
void sendI2C(byte command, byte value) //write function
{
  Wire.beginTransmission(MAX7300_Address);
  Wire.send(command);
  Wire.send(value);
  Wire.endTransmission();
}

void readI2C (byte port, byte numbytes)
 {
   byte MAX7300_Address = 0x40;  // MAX7300 A0=0 A1=0 A2=0 A3=0
   Wire.beginTransmission(MAX7300_Address);
   Wire.send(port); //max7300 needs to be written before read.
   Wire.send(0x01); // write 1
   Wire.endTransmission(); // close comm
  // Wire.beginTransmission(MAX7300_Address);
   Wire.requestFrom(MAX7300_Address, numbytes); //read port
       if (Wire.available()) {  //if data received
       data = (Wire.receive); //write to int 
       
     }
     
   Wire.endTransmission(); //close comm
   
   Serial.println (data, BIN); //print to serial for testing
  
  if (data == 0) {
    ledPin = LOW;
   }
  if (data != 0) {
    ledPin = HIGH;
   }
   else{
   }
   delay (250);
   
  }

Now i get a binary number, how do i read just certain position and assign it to a variable?

like,
how do i assign bit #4 to variable?

Mmm. i got it too (cofee is good!) you can use the bitRead function like this:

Wire.requestFrom(MAX7300_Address, numbytes); //read port
       if (Wire.available()) {  //if data received
       dataRaw = Wire.receive (); //write to int 
       
       data = bitRead(dataRaw,3); //copy 3rd bit starting from LSB (rightmost) to the int.
       
     }

hello,

could you help me with my project??
I have arduino and Max7300AAX and I want to set for instance P24 as OUTPUT with high logic level.

my code:
Wire.beginTransmission(0x44); // adres is change from defaut 0x40
Wire.write (0x04);
Wire.write (0x01);
error = Wire.endTransmission();
Serial.println(error);

// Wire.beginTransmission(0x44);
Wire.write (0x0E); //
Wire.write (0x01);
error = Wire.endTransmission();
Serial.println(error);

// Wire.write (B01010101);
// error = Wire.endTransmission();
Serial.println(error);

delay(1000);

where is my mistake?? thanks a lot!

Please do not reply to topics that have been dead for 9 years. Start your own topic and post a link to the old one you found.

Always post complete code, using code tags.

Read the forum guide in the sticky post.

Do you know what "//" means? It means that line is not part of your code, it is a comment, and will not be executed.