Classe para Ler Gravar Enviar Receber Dados de uma EEPROM I2C

as famosas 24LC256, 512, ...

 

Testado e 100% funcionando

 

Duvidas só comentar!

 

#Digo:)

 


#include <wire.h>

const int theDeviceAddress = 0x50; // endereco fisico da memoria setado A0, A1, A2 ao GND

void setup(void) {
  Serial.begin(9600);
  Wire.begin();

  WireEepromWriteByte(0x50, 1, 192);        //escreve no endereco 1 o valor 192
  WireEepromWriteByte(0x50, 2, 168);        //escreve no endereco 2 o valor 168
  WireEepromWriteByte(0x50, 3, 0);            //escreve no endereco 3 o valor 0
  WireEepromWriteByte(0x50, 4, 14);         //escreve no endereco 4 o valor 14

    Serial.println(WireEepromReadByte(0x50, 1)); //le byte no endereco 1
  Serial.println(WireEepromReadByte(0x50, 2)); //le byte no endereco 2
  Serial.println(WireEepromReadByte(0x50, 3)); //le byte no endereco 3
  Serial.println(WireEepromReadByte(0x50, 4)); //le byte no endereco 4
}
void loop()
{
}
void WireEepromRead(int theDeviceAddress, unsigned int theMemoryAddress, int theByteCount, byte* theByteArray) {
  for (int theByteIndex = 0; theByteIndex < theByteCount; theByteIndex++) {
    Wire.beginTransmission(theDeviceAddress);
    Wire.write((byte)((theMemoryAddress + theByteIndex) >> 8));
    Wire.write((byte)((theMemoryAddress + theByteIndex) >> 0));
    Wire.endTransmission();
    delay(5);
    Wire.requestFrom(theDeviceAddress, sizeof(byte));
    theByteArray[theByteIndex] = Wire.read();
  }
}
byte WireEepromReadByte(int theDeviceAddress, unsigned int theMemoryAddress) {
  byte theByteArray[sizeof(byte)];
  WireEepromRead(theDeviceAddress, theMemoryAddress, sizeof(byte), theByteArray);
  return (byte)(((theByteArray[0] 0)));
}
int WireEepromReadInt(int theDeviceAddress, unsigned int theMemoryAddress) {
  byte theByteArray[sizeof(int)];
  WireEepromRead(theDeviceAddress, theMemoryAddress, sizeof(int), theByteArray);
  return (int)(((theByteArray[0] 8)) | (int)((theByteArray[1] 0)));
}
void WireEepromWrite(int theDeviceAddress, unsigned int theMemoryAddress, int theByteCount, byte* theByteArray) {
  for (int theByteIndex = 0; theByteIndex < theByteCount; theByteIndex++) {
    Wire.beginTransmission(theDeviceAddress);
    Wire.write((byte)((theMemoryAddress + theByteIndex) >> 8));
    Wire.write((byte)((theMemoryAddress + theByteIndex) >> 0));
    Wire.write(theByteArray[theByteIndex]);
    Wire.endTransmission();
    delay(5);
  }
}
void WireEepromWriteByte(int theDeviceAddress, unsigned int theMemoryAddress, byte theByte) {
  byte theByteArray[sizeof(byte)] = {
    (byte)(theByte >> 0) };
  WireEepromWrite(theDeviceAddress, theMemoryAddress, sizeof(byte), theByteArray);
}
void WireEepromWriteInt(int theDeviceAddress, unsigned int theMemoryAddress, int theInt) {
  byte theByteArray[sizeof(int)] = {
    (byte)(theInt >> 8), (byte)(theInt >> 0) };
  WireEepromWrite(theDeviceAddress, theMemoryAddress, sizeof(int), theByteArray);
}

Exibições: 982

Responder esta

Respostas a este tópico

como é a conexao dos pinos WP, SCL e SDA no ARDUINO UNO?

RSS

© 2021   Criado por Marcelo Rodrigues.   Ativado por

Badges  |  Relatar um incidente  |  Termos de serviço