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: 1203

Responder esta

Respostas a este tópico

  Isso significa de zero a quantas posições ?

Explique melhor sua pergunta.  

"isso" significa qual EEPROM?

RV mineirin

 A 24LC516

 Pergunta1

"Isso significa de zero a quantas posições ?"

Resposta 1

"24LC516";

Como parece que vc tem dificuldade de escrever vou tentar explicar o que eu entendi das suas parca pergunta:

Tudo é uma questão de matemática e lógica.

Se voce:

Gravar 1 byte em uma EPROM 24LC516, como ela tem 516K bit ou 64 K bytes, ela pode gravar de 0 até 65.535 bytes.

Do endereço 0 até endereço 65.535.  1 byte por endereço.

Gravar 1 int (2 bytes) em uma EPROM 24LC516, como ela tem 516K bit ou 64 K bytes, ela pode gravar de 0 até 32767 ints e sobra 1 byte no final.

Do endereço 0 até endereço 65.534.  2 bytes por endereço.

Gravar 1 long  ou um float (4 bytes) em uma EPROM 24LC516, como ela tem 516K bit ou 64 K bytes, ela pode gravar de 0 até 16.383 long/floats e sobram 3 byte no final.

Do endereço 0 até endereço 65.532.  4 bytes por endereço.

O conhecimento do "mecanismo" e logica de memoria é um pré-requisito para quem quer entrar no mundo da programação, seja em qualquer tipo de plataforma.

RV mineirin

 Grato.

RSS

© 2024   Criado por Marcelo Rodrigues.   Ativado por

Badges  |  Relatar um incidente  |  Termos de serviço