Go Down

Topic: Which library to use for SDA1 and SCL1 (second I2C) Arduino Due (Read 16406 times) previous topic - next topic

ard_newbie


void writeEEPROM(int deviceaddress, unsigned int eeaddress, byte data )

fnb111

void writeEEPROM(int deviceaddress, unsigned int eeaddress, byte data )
Can somebody please explain what ard_newbie is trying to say/suggest?

MorganS

"The problem is in the code you didn't post."

fnb111

Please correct me if I'm wrong. As I understand it: http://artsites.ucsc.edu/ems/music/equipment/computers/bits_bytes/bits_bytes.html

the largest number that can be saved by the above code is 255?

fnb111

I tracked down another program that writes/ saves a string. The problem is that when I Serial.print I only receive 'DATA READ'

Code: [Select]

/*eeprom_LongStrings
   http://www.hobbytronics.co.uk/eeprom-page-write
   DUE + one 24LC256
*/

#include <Wire.h>      //use SCL & SDA
extern TwoWire Wire1;  //use SCL1 & SDA1
#define disk1 0x50     //Address of 24LC256 eeprom chip
#define WRITE_CNT 5

unsigned char rdata[32];

void setup(void)
{
  Serial.begin(9600);
  Wire.begin();
  Wire1.begin();        // join i2c bus
  unsigned int i;
  // define large string of data to be written
  char str_data[] = {"Hello-1234567890-and-abcdefghijklmnopqrstuvwxyz-Goodbye\n"};

  // Work out length of data
  char str_len = 0;
  do {
    str_len++;
  } while (str_data[str_len]);

  // Write out data several times consecutively starting at address 0
  for (i = 0; i < WRITE_CNT; i++) writeEEPROM(disk1, i * str_len, str_data);

  // read back the data 28 bytes at a time
  // reading data doesn't suffer from the page boundary rules
  Serial.println("DATA READ");
  for (i = 0; i < 10; i++) {
    readEEPROM(disk1, (i * 28), rdata, 28);
    Serial.write(rdata, 28);
  }

}

void loop() {
}

void writeEEPROM(int deviceaddress, unsigned int eeaddress, char* data)
{
  // Uses Page Write for 24LC256
  // Allows for 64 byte page boundary
  // Splits string into max 16 byte writes
  unsigned char i = 0, counter = 0;
  unsigned int  address;
  unsigned int  page_space;
  unsigned int  page = 0;
  unsigned int  num_writes;
  unsigned int  data_len = 0;
  unsigned char first_write_size;
  unsigned char last_write_size;
  unsigned char write_size;

  // Calculate length of data
  do {
    data_len++;
  } while (data[data_len]);

  // Calculate space available in first page
  page_space = int(((eeaddress / 64) + 1) * 64) - eeaddress;

  // Calculate first write size
  if (page_space > 16) {
    first_write_size = page_space - ((page_space / 16) * 16);
    if (first_write_size == 0) first_write_size = 16;
  }
  else
    first_write_size = page_space;

  // calculate size of last write
  if (data_len > first_write_size)
    last_write_size = (data_len - first_write_size) % 16;

  // Calculate how many writes we need
  if (data_len > first_write_size)
    num_writes = ((data_len - first_write_size) / 16) + 2;
  else
    num_writes = 1;

  i = 0;
  address = eeaddress;
  for (page = 0; page < num_writes; page++)
  {
    if (page == 0) write_size = first_write_size;
    else if (page == (num_writes - 1)) write_size = last_write_size;
    else write_size = 16;

    Wire1.beginTransmission(deviceaddress);
    Wire1.write((int)((address) >> 8));   // MSB
    Wire1.write((int)((address) & 0xFF)); // LSB
    counter = 0;
    do {
      Wire1.write((byte) data[i]);
      i++;
      counter++;
    } while ((data[i]) && (counter < write_size));
    Wire1.endTransmission();
    address += write_size; // Increment address for next write

    delay(6);  // needs 5ms for page write
  }
}

void readEEPROM(int deviceaddress, unsigned int eeaddress,
                unsigned char* data, unsigned int num_chars)
{
  unsigned char i = 0;
  Wire1.beginTransmission(deviceaddress);
  Wire1.write((int)(eeaddress >> 8));   // MSB
  Wire1.write((int)(eeaddress & 0xFF)); // LSB
  Wire1.endTransmission();

  Wire1.requestFrom(deviceaddress, num_chars);

  while (Wire1.available()) data[i++] = Wire1.read();

}



I have changed all the wire calls to wire1 but are not sure about the
Code: [Select]
Serial.write(rdata, 28);

Thanks for the help

MorganS

rdata is an array. Serial.write(array, length) will write (not print) the requested number of bytes from that array location.
"The problem is in the code you didn't post."

fnb111

This line say readEEPROM from disk1

Code: [Select]

// read back the data 28 bytes at a time
  // reading data doesn't suffer from the page boundary rules
  Serial.println("DATA READ");
  for (i = 0; i < 10; i++) {
    readEEPROM(disk1, (i * 28), rdata, 28);
    Serial.write(rdata, 28);


1) how do I Serial.print this?
2) what is the function of 'Serial.write(rdata, 28);' Where does it write too? {if it writes to the serial port why can't I see it?}

Thanks in advance

MorganS

If you are on a Due then Serial refers to the programming port. I use that port very rarely. I use the native port and SerialUSB.

Your data is a string. write() is the correct method to use.
"The problem is in the code you didn't post."

fnb111

This is what I get when I try to print:


fnb111

I managed to track down the fault. The delay(6) is to short. I changed it to delay(10) and Serial.print gave me this:


MorganS

The data is empty. You get 10 zeroes printed because the first item in the rdata[] array is zero and you used Serial.print(*rdata); (*rdata is equivalent to rdata[0])

So is the eeprom empty or did you fail to read? Try to put some more debugging in the write function, to check it is doing what you expect and the eeprom is responding.
"The problem is in the code you didn't post."

guegi

A byte has 8 bits and it can only represent a value between 0 and 255

Go Up