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

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

   Boa noite

   Sabe dizer em uma 24LC512 quantas posições totais eu tenho disponíveis ?
    ex:  A EPROM do ATmega é de 1k e nos dá as posições de 0 a 255

  Quantas posições nos dá uma 24LC512?
  

  Outra coisa, o código acima não consegui compilar de forma alguma.
 Ele para nessa linha :
 return (int)(((theByteArray[0] 8)) | (int)((theByteArray[1] 0)));

 com a mensagem :  expected ')' before numeric constant

  Muito grato desde já

Olá, Cezar!

   Acho que, antes do 8 e do último zero à direita deve haver dois sinais de 'menor do que' ( dois antes de cada um destes números ), que não aparecem no código original porque foi postado na área de mensagem e não no anexo.

   É por esta razão que sempre pedimos para não postar código na área de mensagem.

   Vai compilar, mas o programa funcionar é outra história, porque eu estou "chutando" o que deveria ter antes do 8 e do último zero à direita.

Sucesso!

D. T. Ribeiro

 

  Vou tentar.

  Muito grato.

Bom dia:

Se voce está afirmando isto, então vc está errado:

  "  ex:  A EPROM do ATmega é de 1k e nos dá as posições de 0 a 255 "

1K = 1024 posições e não 255.  (1024 endereços de 1 byte).

 Sabe dizer em uma 24LC512 quantas posições totais eu tenho disponíveis ?

 Quantas posições nos dá uma 24LC512?

Seria 24LC512?

  24LC512   512K endereços de 1 bit.  ou 64 K byte

"  The Microchip Technology Inc. 24AA512/24LC512/
24FC512 (24XX512*) is a 64K x 8 (512 Kbit) "

Ref:  https://ww1.microchip.com/downloads/en/devicedoc/21754m.pdf

RV mineirin

 

o que sei é que só consigo dar um PUT e um GET em posições de 0 a 255

 Ou estou errado?

Olá, Cesar!

   Se o put() e o get() forem com inteiros de 32 bits( 4 bytes ), 1024 / 4 = 256 ( 0 a 255 ).

   Será que não é isto?

D. T. Ribeiro

exatamente isso.

Então minha dúvida é:   com uma 24LC512  para  o put() e o get() que serão inteiros de 32 bits( 4 bytes ) , quantas posições eu consigo ter ? De 0 a ...   ?

As funções PUT() e GET(), usam o espaço definido pelo tipo da variável usada, 

Se voce usar byte,  elas usam 1 byte, se usar int , usam 2 bytes, se usar long ou float, usam 4 bytes.

Isto na "família" arduino UNO/Mini/Mega) .

No ESPxxxx o tamanho do espaço das variáveis é diferente.

RV mineirin

Bom dia, qual o tamanho da variavel que vc está usando para o put/get?

Int, long, float etc....

RV mineirin

  Uso INT

int na "família" arduino UNO/Mini/Mega) tem o tamanho de 2 bytes,

então voce usando put/get consegue armazenar até 512 byres.

E na 24LC512, até 32 K bytes.

RV mineirin

RSS

© 2024   Criado por Marcelo Rodrigues.   Ativado por

Badges  |  Relatar um incidente  |  Termos de serviço