Memory (Part 5)

Part 1234567

The code sample from the previous post shows how to read and write bytes on the EEPROM chips. Just fine !  But what about reading and writing other types of data such as short and long integers or floating point values ? This post contains an example of use of the PlainSPI_EEPROM library which reads and writes all standard datatypes available from the Arduino’s environment .

The principle of operation of the read write test is as follows :

  • Initialize the EEPROM
  • Write sequential data of various types on the EEPROM chip
  • Read back and print these data

Initializing the EEPROM is plain simple and requires a single line of code within the setup() function:.

void setup(void) 
{
	/* Initialize serial communication */
	Serial.begin(115200);
	Serial.print("Initialize EEPROM: ");
	Serial.println();
	/* Initialize EEPROM */
	MEM.InitializeSPI_EEPROM(CS_PORT_EEPROM, CS_PIN_EEPROM, SPI_EEPROM_25LC1024);
}

One thing that you should always keep in mind is that the data types differ in size, ranging from 8 to 32 bits -hence the names – as per the following table:

types size in bits size in bytes
uint8_t, char 8 1
uint16_t, int16_t 16 2
uint32_t, int32_t, float 32 4

Since a byte is 8 bits, the correlation between the two last columns is not a surprise!

Also, keep in mind that the datatype double is not reported because it is the same as the float datatype  in Arduino’s environment. For sake of simplicity, the written values are not contiguous but placed at fixed interval of 4 bytes.  Next picture illustrates the first bytes of the EEPROM and the way the various data types are recorded :

EEPROM

Here is the testing function :

void WriteReadMixedData(void)
{
	Serial.print("Write data ...");
	Serial.println();
	uint32_t pages = MEM.Pages();
	uint16_t pageSize = MEM.PageSize();
	uint32_t data = 0;
	MEM.ActualAddress(0);
	for (uint32_t address = 0; address < (pageSize * pages); address += 4) {
		data &= 0xFF;
		switch(data % 6) {
		case 0: MEM.WriteUInt8(data); break;
		case 1: MEM.WriteInt16(data); break;
		case 2: MEM.WriteUInt16(data); break;
		case 3: MEM.WriteInt32(data); break;
		case 4: MEM.WriteUInt32(data); break;
		case 5: MEM.WriteFlt32(float(data)); break;
		}
		data += 1;
	}	
	Serial.print("Read data ...");
	Serial.println();
	MEM.ActualAddress(0);
	data = 0;
	for (uint32_t address = 0; address < (pageSize * pages); address += 4) {
		data &= 0xFF;
		Serial.print("Page: ");
		Serial.print((address / pageSize));
		Serial.print(" address: ");
		Serial.print((address % pageSize));
		Serial.print(" read: ");
		switch(data % 6) {
		case 0: Serial.print(MEM.ReadUInt8()); break;
		case 1: Serial.print(MEM.ReadInt16()); break;
		case 2: Serial.print(MEM.ReadUInt16()); break;
		case 3: Serial.print(MEM.ReadInt32()); break;
		case 4: Serial.print(MEM.ReadUInt32()); break;
		case 5: Serial.print(MEM.ReadFlt32(), 1); break;
		}
		Serial.println();
		data += 1;
	}
}

The test function is called within the loop() function :

void loop(void) 
{
	WriteReadMixedData();
	while(1); /* Run once */
}

In the next post we will describe various ways of recording and retrieving strings of characters.

 Next post on same subject

 

Leave a Reply

You must be logged in to post a comment.