Created
June 10, 2017 06:35
-
-
Save brianrumburg/4ce41449644a260849d76c6e77b242eb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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