Hello. I'm using stm32f103c8t6 with at24c256 and i have some trouble.
There is code:
#include <Wire.h>
#define EEPROM_ADDR 0x50 // I2C Buss address of 24LC256 256K EEPROM
String settings = "1,2,25,1";
//id,type,temp,valve
String wifi = "longwifiloginmorethan11";
String password = "longpasswordmorethan11";
byte PageData[30]; // array that will hold test data for a page
byte PageRead[30];
void setup()
{
delay(500);
byte settingsb[sizeof(settings)];
settings.getBytes(settingsb, sizeof(settings));
byte wifib[sizeof(wifi)];
wifi.getBytes(wifib, sizeof(wifi));
byte passwordb[sizeof(password)];
password.getBytes(passwordb, sizeof(password));
Wire.begin(); // join I2C bus (address optional for master)
Serial.begin(9600);
Serial.println("Start");
delay(500);
Serial.println("");
Serial.println("Writing settings:");
Serial.print(settings);
i2c_eeprom_write_page(EEPROM_ADDR, 1, settingsb, sizeof(settings)); // 28 bytes/page is max
delay(500);
Serial.println("");
Serial.println("Writing wifi:");
Serial.print(wifi);
i2c_eeprom_write_page(EEPROM_ADDR, 50, wifib, sizeof(wifi)); // 28 bytes/page is max
delay(500);
Serial.println("");
Serial.println("Writing password:");
Serial.print(password);
i2c_eeprom_write_page(EEPROM_ADDR, 100, passwordb, sizeof(password)); // 28 bytes/page is max
delay(500);
}
void loop()
{
Serial.println(" ");
Serial.println("Reading settings:");
i2c_eeprom_read_buffer( EEPROM_ADDR, 1, PageRead, sizeof(settings));
for (int i = 0; i < sizeof(settings); i++) {
Serial.write(PageRead[i]); // display the array read
}
delay(500);
Serial.println(" ");
Serial.println("Reading wifi:");
i2c_eeprom_read_buffer( EEPROM_ADDR, 50, PageRead, sizeof(wifi));
for (int i = 0; i < sizeof(wifi); i++) {
Serial.write(PageRead[i]); // display the array read
}
delay(500);
Serial.println(" ");
Serial.println("Reading password:");
i2c_eeprom_read_buffer( EEPROM_ADDR, 100, PageRead, sizeof(password));
for (int i = 0; i < sizeof(password); i++) {
Serial.write(PageRead[i]); // display the array read
}
delay(500);
}
void i2c_eeprom_write_byte( int deviceaddress, unsigned int eeaddress, byte data )
{
int rdata = data;
Wire.beginTransmission(deviceaddress);
Wire.write((int)(eeaddress >> 8)); // Address High Byte
Wire.write((int)(eeaddress & 0xFF)); // Address Low Byte
Wire.write(rdata);
Wire.endTransmission();
}
// Address is a page address, 6-bit (63). More and end will wrap around
// But data can be maximum of 28 bytes, because the Wire library has a buffer of 32 bytes
void i2c_eeprom_write_page
( int deviceaddress, unsigned int eeaddresspage, byte* data, byte length )
{
Wire.beginTransmission(deviceaddress);
Wire.write((int)(eeaddresspage >> 8)); // Address High Byte
Wire.write((int)(eeaddresspage & 0xFF)); // Address Low Byte
byte c;
for ( c = 0; c < length; c++)
Wire.write(data[c]);
Wire.endTransmission();
delay(10); // need some delay
}
byte i2c_eeprom_read_byte( int deviceaddress, unsigned int eeaddress )
{
byte rdata = 0xFF;
Wire.beginTransmission(deviceaddress);
Wire.write((int)(eeaddress >> 8)); // Address High Byte
Wire.write((int)(eeaddress & 0xFF)); // Address Low Byte
Wire.endTransmission();
Wire.requestFrom(deviceaddress, 1);
if (Wire.available()) rdata = Wire.read();
return rdata;
}
// should not read more than 28 bytes at a time!
void i2c_eeprom_read_buffer( int deviceaddress, unsigned int eeaddress, byte *buffer, int length )
{
Wire.beginTransmission(deviceaddress);
Wire.write((int)(eeaddress >> 8)); // Address High Byte
Wire.write((int)(eeaddress & 0xFF)); // Address Low Byte
Wire.endTransmission();
Wire.requestFrom(deviceaddress, length);
//int c = 0;
for ( int c = 0; c < length; c++ )
if (Wire.available()) buffer[c] = Wire.read();
}
And serial result is in attachments.
I cant reand more than 11 chars
Sorry for my english, i'm newbie with c lang.