guys,
I need to read an external EEPROM 24C128. Also write...
My current code is below as a test... surely, it is faulty and that i need to fix.
#include <Wire.h>
#define DEBUG
#ifdef DEBUG
#define DEBUG_PRINT(x) Serial.print (x)
#define DEBUG_PRINTDEC(x) Serial.print (x, DEC)
#define DEBUG_PRINTHEX(x) Serial.print (x, HEX)
#define DEBUG_PRINTLN(x) Serial.println (x)
#else
#define DEBUG_PRINT(x)
#define DEBUG_PRINTDEC(x)
#define DEBUG_PRINTHEX(x)
#define DEBUG_PRINTLN(x)
#endif
#define eeprom1 0x50 //Address of 24LC256 eeprom chip
#define WRITE_CNT 5
#define rxBufSize 25
#define txBufSize 35
char dbgStrRS232_rx[rxBufSize];
char command[10]; // Arbitrary Value for command size
char data[15]; // ditto for data size
int byteCount;
unsigned char rdata[32];
unsigned int i;
void setup(void) {
Wire.begin(); // initialise the connection
Serial.begin(9600);
Serial.println(F("Key reader Powered ON"));
//char somedata[] = "1;15CU01;X254ZB;CXDFVGFRTESDFREWEXSDCVBGF;101018;301218;"; // data to write
//i2c_eeprom_write_page(eeprom1, 0, (byte *)somedata, sizeof(somedata)); // write to EEPROM
//delay(100); //add a small delay
//Serial.println("Memory written");
Serial.println(F("Key reader Ready"));
}
void loop(){
/*
int addr=0; //first address
byte b = i2c_eeprom_read_byte(eeprom1, 0); // access the first address from the memory
while (b!=0){
Serial.print((char)b); //print content to serial port
addr++; //increase address
b = i2c_eeprom_read_byte(eeprom1, addr); //access an address from the memory
}
Serial.println(" ");
*/
SerialParser();
if (strcmp(command, "0x50") == 0) {
memset(command, 0, sizeof(command));
DEBUG_PRINT("New KEY: ");
char key[30];
i2c_eeprom_read_buffer(eeprom1, 0, key, sizeof(key) );
Serial.println(key);
delay(2000);
}
DEBUG_PRINT("New KEY1: ");
char key[50];
i2c_eeprom_read_buffer(eeprom1, 0, key, sizeof(key) );
delay(100);
Serial.println(key);
delay(2000);
Serial.flush();
}
//Serial parser
void SerialParser(void) {
//
// One command per line. Eventually, Data may have multiple
// fields separated by ":"
// Command Format: "up to 5 Letter command, up to 10 letter data<\n>"
// No checking.
//
// count will be below Zero on a timeout.
// read up to X chars or until EOT - in this case "\n"
byteCount = -1;
byteCount = Serial.readBytesUntil('\n',dbgStrRS232_rx,rxBufSize);
delay(5);
if (byteCount > 0) {
strcpy(command,strtok(dbgStrRS232_rx,","));
strcpy(data,strtok(NULL,","));
}
memset(dbgStrRS232_rx, 0, sizeof(dbgStrRS232_rx)); // Clear contents of Buffer
Serial.flush();
}
void i2c_eeprom_write_byte( int deviceaddress, unsigned int eeaddress, byte data ) {
int rdata = data;
Wire.beginTransmission(deviceaddress);
Wire.write((int)(eeaddress >> 8)); // MSB
Wire.write((int)(eeaddress & 0xFF)); // LSB
Wire.write(rdata);
Wire.endTransmission();
}
// WARNING: address is a page address, 6-bit end will wrap around
// also, data can be maximum of about 30 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)); // MSB
Wire.write((int)(eeaddresspage & 0xFF)); // LSB
byte c;
for ( c = 0; c < length; c++)
Wire.write(data[c]);
Wire.endTransmission();
}
byte i2c_eeprom_read_byte( int deviceaddress, unsigned int eeaddress ) {
byte rdata = 0xFF;
Wire.beginTransmission(deviceaddress);
Wire.write((int)(eeaddress >> 8)); // MSB
Wire.write((int)(eeaddress & 0xFF)); // LSB
Wire.endTransmission();
Wire.requestFrom(deviceaddress,1);
if (Wire.available()) rdata = Wire.read();
return rdata;
}
// maybe let's not read more than 30 or 32 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)); // MSB
Wire.write((int)(eeaddress & 0xFF)); // LSB
Wire.endTransmission();
Wire.requestFrom(deviceaddress,length);
int c = 0;
for ( c = 0; c < length; c++ )
if (Wire.available()) buffer[c] = Wire.read();
}
Now, char somedata[] = "1;15CU01;X254ZB;CXDFVGFRTESDFREWEXSDCVBGF;101018;301218;" is my data that I have to write... But the amazing part is, each semicolon is a specific field and i can just put them not in a single shot, but in blocks... Like :
char a[] = "1";
char b[] = "15CU01";
and so on....
And these are fixed length values... thus I can also write in a small chunk (as i2 has a limited buffer of 30 bytes max). [My max data will be 25 chars.
so please help me to correct the code for block wise write and read too.
After reading, finally I will concat the whole thing to send via Serial as you can see... But read and write in block... how???
Mishu~