Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#include <Wire.h>
#define DEV_ADDR 0x57
#define START_ADDR 0x045C
#define LENGTH 4
void setup()
{
Serial.begin(57600);
Wire.begin();
delay(1000);
Serial.println("Ready!");
}
void loop()
{
if(Serial.available() > 0)
{
char c = Serial.read();
switch(c)
{
case 'r':
i2c_read();
break;
case 'w':
i2c_write_one();
Serial.println("Done!");
break;
default:
Serial.println("Error!");
break;
}
}
}
void i2c_read()
{
Wire.beginTransmission(DEV_ADDR);
Wire.write((int)(START_ADDR >> 8)); // MSB
Wire.write((int)(START_ADDR & 0xFF)); // LSB
Wire.endTransmission();
Wire.requestFrom(DEV_ADDR,LENGTH);
while(Wire.available())
{
byte val = Wire.read();
if(val < 0x10)
Serial.print("0");
Serial.print(val, HEX);
Serial.print(" ");
}
Serial.println();
}
void i2c_write_one()
{
Wire.beginTransmission(DEV_ADDR);
Wire.write((int)(START_ADDR >> 8)); // MSB
Wire.write((int)(START_ADDR & 0xFF)); // LSB
Wire.write(0);
Wire.write(0);
Wire.write(0);
Wire.write(1);
Wire.endTransmission();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment