casemod
September 13, 2014, 2:32pm
1
Hi folks, can someone spot the mistake on my code?
#include <Wire.h>
#define DEVICEADDRESS 0x57
uint8_t myvar1 = 0b00011000;
uint8_t myvar2 = 0b11100111;
int temp = 0;
int temp1 = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Wire.begin();
}
void loop() {
// put your main code here, to run repeatedly:
EepromWrite(0x05, myvar1);
delay(10);
EepromWrite(0x10, myvar2);
temp = EepromRead(0x05);
delay(50);
temp1 = EepromRead(0x10);
Serial.println(temp);
Serial.println(temp1);
delay(2000);
}
// ----------------------------------------------------------------
void EepromWrite(uint16_t theMemoryAddress, uint8_t u8Byte)
{
Wire.beginTransmission(DEVICEADDRESS);
Wire.write( (theMemoryAddress >> 8) & 0xFF );
Wire.write( (theMemoryAddress >> 0) & 0xFF );
Wire.write(u8Byte);
Wire.endTransmission();
delay(5);
}
// ----------------------------------------------------------------
uint8_t EepromRead(uint16_t theMemoryAddress)
{
uint8_t u8retVal = 0;
Wire.beginTransmission(DEVICEADDRESS);
Wire.write( (theMemoryAddress >> 8) & 0xFF );
Wire.write( (theMemoryAddress >> 0) & 0xFF );
Wire.endTransmission();
delay(5);
Wire.requestFrom(DEVICEADDRESS, 1);
u8retVal = Wire.read();
return u8retVal ;
}
This is my output:
11100111
11100111
Seems as the value written is always "myvar2" or I am always retrieving the last value.
Could you check the return value of the Wire.endtransmission?
It shows if the call really does work.
void EepromWrite(uint16_t theMemoryAddress, uint8_t u8Byte)
{
Wire.beginTransmission(DEVICEADDRESS);
Wire.write( (theMemoryAddress >> 8) & 0xFF );
Wire.write( (theMemoryAddress >> 0) & 0xFF );
Wire.write(u8Byte);
int x = Wire.endTransmission();
Serial.println(x);
delay(5);
}
// ----------------------------------------------------------------
uint8_t EepromRead(uint16_t theMemoryAddress)
{
uint8_t u8retVal = 0;
Wire.beginTransmission(DEVICEADDRESS);
Wire.write( (theMemoryAddress >> 8) & 0xFF );
Wire.write( (theMemoryAddress >> 0) & 0xFF );
int x = Wire.endTransmission();
Serial.println(x);
delay(5);
Wire.requestFrom(DEVICEADDRESS, 1);
u8retVal = Wire.read();
return u8retVal ;
}
loop() might be missing one delay() // neverthought I would say that
void loop() {
// put your main code here, to run repeatedly:
EepromWrite(0x05, myvar1);
delay(10);
EepromWrite(0x10, myvar2);
delay(10): // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ??
temp = EepromRead(0x05);
delay(50);
temp1 = EepromRead(0x10);
Serial.println(temp);
Serial.println(temp1);
delay(2000);
}
You need a delay between the write and the read.
EepromWrite(0x10, myvar2);
temp = EepromRead(0x05);
same idea, but the eepromWrite has already a 5 millis delay()
What EEPROM are you using, can you post a link to the datasheet?
casemod
September 13, 2014, 2:56pm
5
robtillaart:
same idea, but the eepromWrite has already a 5 millis delay()
What EEPROM are you using, can you post a link to the datasheet?
Im using this FRAM:
I placed the delays just in case someone wanted to try the code on a actual EEPROM
do you have pull up resistors connected?
casemod
September 13, 2014, 3:32pm
8
robtillaart:
do you have pull up resistors connected?
Yes, 100% Software issue.
did you see in the datasheet that this particular FRAM you use has only an 8 bit memory address, not a 16 bit ??
4Kbit = 256 bytes ==> 8 bits address..
the problem might be that you write both values to address 0x00 ..
can you try this code instead of the EEPROMwite/read?
void FRAMWrite(uint8_t theMemoryAddress, uint8_t u8Byte)
{
Wire.beginTransmission(DEVICEADDRESS);
Wire.write( (theMemoryAddress);
Wire.write(u8Byte);
Wire.endTransmission();
}
// ----------------------------------------------------------------
uint8_t FRAMRead(uint16_t theMemoryAddress)
{
Wire.beginTransmission(DEVICEADDRESS);
Wire.write( (theMemoryAddress);
int x = Wire.endTransmission();
Wire.requestFrom(DEVICEADDRESS, 1);
return Wire.read();
}
4Kbit = 256 bytes ==> 8 bits address..
There is a 9th address bit in bit 1 of the 8 bit slave address byte (i.e. bit 0 of the usual 7 bit address passed to the wire library). That allows selection of the bottom or top 2Kbits.
casemod
September 13, 2014, 3:51pm
11
robtillaart:
did you see in the datasheet that this particular FRAM you use has only an 8 bit memory address, not a 16 bit ??
4Kbit = 256 bytes ==> 8 bits address..
Now, how could i not see that
Hackscribble:
4Kbit = 256 bytes ==> 8 bits address..
There is a 9th address bit in bit 1 of the 8 bit slave address byte (i.e. bit 0 of the usual 7 bit address passed to the wire library). That allows selection of the bottom or top 2Kbits.
Ok, so I write 0x56 for half and 0x57 for the other half?
Ok, so I write 0x56 for half and 0x57 for the other half?
That's right - you have pins A1 and A2 on the FRAM pulled high?
casemod
September 13, 2014, 4:02pm
13
Confirmed.
Thanks to everyone for helping "digest" the information.
I can confirm the device is working with a single address byte and that by selecting the correct address for A0 (on the code) I can access the top or bottom 2K locations
Hackscribble:
Ok, so I write 0x56 for half and 0x57 for the other half?
That's right - you have pins A1 and A2 on the FRAM pulled high?
Yes, there are physical pull ups from the previous EEPROM pulling the pins high
Ok, so I write 0x56 for half and 0x57 for the other half?
so that would make
void FRAMWrite(uint8_t theMemoryAddress, uint8_t u8Byte)
{
uint8_t addr = DEVICEADDRESS;
if (theMemoryAddress > 127) addr++;
Wire.beginTransmission(addr);
Wire.write( (theMemoryAddress);
Wire.write(u8Byte);
Wire.endTransmission();
}
// ----------------------------------------------------------------
uint8_t FRAMRead(uint16_t theMemoryAddress)
{
uint8_t addr = DEVICEADDRESS;
if (theMemoryAddress > 127) addr++;
Wire.beginTransmission(addr);
Wire.write( (theMemoryAddress);
int x = Wire.endTransmission();
Wire.requestFrom(DEVICEADDRESS, 1);
return Wire.read();
}