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.
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?
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.
}