M24LC256 v1
|
This is an example of how to use the M24L256 class. It writes a big block of data to the EEPROM, and then slowly reads it out again one little block at a time.
/* Copyright (C) 2011 James Coliz, Jr. <maniacbug@ymail.com> This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License version 2 as published by the Free Software Foundation. */ #include <Wire.h> #include <M24LC256.h> // One EEPROM connected on address '0' M24LC256 eeprom(0); // Forward declaration of our sample data, just to move that stuff // to the end of the file extern unsigned char sample_data[]; extern unsigned int sample_data_len; // Where on the eeprom we will write the sample data const int eeprom_write_location = 0; void setup(void) { // Prepare everything Serial.begin(9600); Wire.begin(); eeprom.begin(); Serial.println("M24LC256/examples/readwrite/"); // Write a really large chunk to a certain location on the EEPROM, // and see how long it takes unsigned long start_time = millis(); eeprom.write(eeprom_write_location,sample_data,sample_data_len); unsigned long duration = millis() - start_time; Serial.print("Bytes written: "); Serial.println(sample_data_len); Serial.print("Time elapsed: "); Serial.println(duration); Serial.print("Bits per second: "); Serial.println(sample_data_len*8000L/duration); } // How many bytes to read every time through const int read_length = 32; // How long to wait after every reading const int chunk_delay = 500; // Current address to read from on the EEPROM uint16_t current_read_location = eeprom_write_location; // When to stop reading back from the EEPROM uint16_t end_read_location = eeprom_write_location + sample_data_len; // Buffer to pull data down into char buffer[read_length+1]; // Reads one chunk at a time real slowly and prints it to the Serial port // Stops when it's read everything. void loop(void) { // If there are no chunks left to read, we are done. Wait here. if (current_read_location >= end_read_location) while(1); // Read down a chunk from the eeprom uint16_t bytes_read = min(read_length,end_read_location-current_read_location); eeprom.read(current_read_location,buffer,bytes_read); // Insert a zero at the end, so we can print it as a string buffer[bytes_read] = 0; // Print out the buffer Serial.print(buffer); // Wait a bit delay(chunk_delay); // Recalculate where to read the next chunk current_read_location += bytes_read; } // This data is generated from a file named "sample_data" by doing: // xxd -i sample_data >> readwrite.pde unsigned char sample_data[] = { 0x20, 0x20, 0x20, 0x20, 0x20, 0x31, 0x2e, 0x20, 0x20, 0x53, 0x75, 0x6e, 0x20, 0x54, 0x7a, 0x75, 0x20, 0x73, 0x61, 0x69, 0x64, 0x3a, 0x20, 0x20, 0x49, 0x6e, 0x20, 0x74, 0x68, 0x65, 0x20, 0x6f, 0x70, 0x65, 0x72, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x20, 0x6f, 0x66, 0x20, 0x77, 0x61, 0x72, 0x2c, 0x20, 0x77, 0x68, 0x65, 0x72, 0x65, 0x20, 0x74, 0x68, 0x65, 0x72, 0x65, 0x20, 0x61, 0x72, 0x65, 0x0a, 0x69, 0x6e, 0x20, 0x74, 0x68, 0x65, 0x20, 0x66, 0x69, 0x65, 0x6c, 0x64, 0x20, 0x61, 0x20, 0x74, 0x68, 0x6f, 0x75, 0x73, 0x61, 0x6e, 0x64, 0x20, 0x73, 0x77, 0x69, 0x66, 0x74, 0x20, 0x63, 0x68, 0x61, 0x72, 0x69, 0x6f, 0x74, 0x73, 0x2c, 0x20, 0x61, 0x73, 0x20, 0x6d, 0x61, 0x6e, 0x79, 0x20, 0x68, 0x65, 0x61, 0x76, 0x79, 0x20, 0x63, 0x68, 0x61, 0x72, 0x69, 0x6f, 0x74, 0x73, 0x2c, 0x0a, 0x61, 0x6e, 0x64, 0x20, 0x61, 0x20, 0x68, 0x75, 0x6e, 0x64, 0x72, 0x65, 0x64, 0x20, 0x74, 0x68, 0x6f, 0x75, 0x73, 0x61, 0x6e, 0x64, 0x20, 0x6d, 0x61, 0x69, 0x6c, 0x2d, 0x63, 0x6c, 0x61, 0x64, 0x20, 0x73, 0x6f, 0x6c, 0x64, 0x69, 0x65, 0x72, 0x73, 0x2c, 0x0a, 0x74, 0x68, 0x65, 0x20, 0x65, 0x78, 0x70, 0x65, 0x6e, 0x64, 0x69, 0x74, 0x75, 0x72, 0x65, 0x20, 0x61, 0x74, 0x20, 0x68, 0x6f, 0x6d, 0x65, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x61, 0x74, 0x20, 0x74, 0x68, 0x65, 0x20, 0x66, 0x72, 0x6f, 0x6e, 0x74, 0x2c, 0x20, 0x69, 0x6e, 0x63, 0x6c, 0x75, 0x64, 0x69, 0x6e, 0x67, 0x20, 0x65, 0x6e, 0x74, 0x65, 0x72, 0x74, 0x61, 0x69, 0x6e, 0x6d, 0x65, 0x6e, 0x74, 0x0a, 0x6f, 0x66, 0x20, 0x67, 0x75, 0x65, 0x73, 0x74, 0x73, 0x2c, 0x20, 0x73, 0x6d, 0x61, 0x6c, 0x6c, 0x20, 0x69, 0x74, 0x65, 0x6d, 0x73, 0x20, 0x73, 0x75, 0x63, 0x68, 0x20, 0x61, 0x73, 0x20, 0x67, 0x6c, 0x75, 0x65, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x70, 0x61, 0x69, 0x6e, 0x74, 0x2c, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x73, 0x75, 0x6d, 0x73, 0x20, 0x73, 0x70, 0x65, 0x6e, 0x74, 0x20, 0x6f, 0x6e, 0x0a, 0x63, 0x68, 0x61, 0x72, 0x69, 0x6f, 0x74, 0x73, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x61, 0x72, 0x6d, 0x6f, 0x72, 0x2c, 0x20, 0x77, 0x69, 0x6c, 0x6c, 0x20, 0x72, 0x65, 0x61, 0x63, 0x68, 0x20, 0x74, 0x68, 0x65, 0x20, 0x74, 0x6f, 0x74, 0x61, 0x6c, 0x20, 0x6f, 0x66, 0x20, 0x61, 0x20, 0x74, 0x68, 0x6f, 0x75, 0x73, 0x61, 0x6e, 0x64, 0x20, 0x6f, 0x75, 0x6e, 0x63, 0x65, 0x73, 0x20, 0x6f, 0x66, 0x0a, 0x73, 0x69, 0x6c, 0x76, 0x65, 0x72, 0x20, 0x70, 0x65, 0x72, 0x20, 0x64, 0x61, 0x79, 0x2e, 0x20, 0x20, 0x53, 0x75, 0x63, 0x68, 0x20, 0x69, 0x73, 0x20, 0x74, 0x68, 0x65, 0x20, 0x63, 0x6f, 0x73, 0x74, 0x20, 0x6f, 0x66, 0x20, 0x72, 0x61, 0x69, 0x73, 0x69, 0x6e, 0x67, 0x20, 0x61, 0x6e, 0x20, 0x61, 0x72, 0x6d, 0x79, 0x20, 0x6f, 0x66, 0x20, 0x31, 0x30, 0x30, 0x2c, 0x30, 0x30, 0x30, 0x0a, 0x6d, 0x65, 0x6e, 0x2e, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x32, 0x2e, 0x20, 0x20, 0x57, 0x68, 0x65, 0x6e, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x65, 0x6e, 0x67, 0x61, 0x67, 0x65, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x63, 0x74, 0x75, 0x61, 0x6c, 0x20, 0x66, 0x69, 0x67, 0x68, 0x74, 0x69, 0x6e, 0x67, 0x2c, 0x20, 0x69, 0x66, 0x20, 0x76, 0x69, 0x63, 0x74, 0x6f, 0x72, 0x79, 0x20, 0x69, 0x73, 0x20, 0x6c, 0x6f, 0x6e, 0x67, 0x0a, 0x69, 0x6e, 0x20, 0x63, 0x6f, 0x6d, 0x69, 0x6e, 0x67, 0x2c, 0x20, 0x74, 0x68, 0x65, 0x6e, 0x20, 0x6d, 0x65, 0x6e, 0x27, 0x73, 0x20, 0x77, 0x65, 0x61, 0x70, 0x6f, 0x6e, 0x73, 0x20, 0x77, 0x69, 0x6c, 0x6c, 0x20, 0x67, 0x72, 0x6f, 0x77, 0x20, 0x64, 0x75, 0x6c, 0x6c, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x74, 0x68, 0x65, 0x69, 0x72, 0x20, 0x61, 0x72, 0x64, 0x6f, 0x72, 0x20, 0x77, 0x69, 0x6c, 0x6c, 0x0a, 0x62, 0x65, 0x20, 0x64, 0x61, 0x6d, 0x70, 0x65, 0x64, 0x2e, 0x20, 0x20, 0x49, 0x66, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x6c, 0x61, 0x79, 0x20, 0x73, 0x69, 0x65, 0x67, 0x65, 0x20, 0x74, 0x6f, 0x20, 0x61, 0x20, 0x74, 0x6f, 0x77, 0x6e, 0x2c, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x77, 0x69, 0x6c, 0x6c, 0x20, 0x65, 0x78, 0x68, 0x61, 0x75, 0x73, 0x74, 0x20, 0x79, 0x6f, 0x75, 0x72, 0x0a, 0x73, 0x74, 0x72, 0x65, 0x6e, 0x67, 0x74, 0x68, 0x2e, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x33, 0x2e, 0x20, 0x20, 0x41, 0x67, 0x61, 0x69, 0x6e, 0x2c, 0x20, 0x69, 0x66, 0x20, 0x74, 0x68, 0x65, 0x20, 0x63, 0x61, 0x6d, 0x70, 0x61, 0x69, 0x67, 0x6e, 0x20, 0x69, 0x73, 0x20, 0x70, 0x72, 0x6f, 0x74, 0x72, 0x61, 0x63, 0x74, 0x65, 0x64, 0x2c, 0x20, 0x74, 0x68, 0x65, 0x20, 0x72, 0x65, 0x73, 0x6f, 0x75, 0x72, 0x63, 0x65, 0x73, 0x20, 0x6f, 0x66, 0x0a, 0x74, 0x68, 0x65, 0x20, 0x53, 0x74, 0x61, 0x74, 0x65, 0x20, 0x77, 0x69, 0x6c, 0x6c, 0x20, 0x6e, 0x6f, 0x74, 0x20, 0x62, 0x65, 0x20, 0x65, 0x71, 0x75, 0x61, 0x6c, 0x20, 0x74, 0x6f, 0x20, 0x74, 0x68, 0x65, 0x20, 0x73, 0x74, 0x72, 0x61, 0x69, 0x6e, 0x2e, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x34, 0x2e, 0x20, 0x20, 0x4e, 0x6f, 0x77, 0x2c, 0x20, 0x77, 0x68, 0x65, 0x6e, 0x20, 0x79, 0x6f, 0x75, 0x72, 0x20, 0x77, 0x65, 0x61, 0x70, 0x6f, 0x6e, 0x73, 0x20, 0x61, 0x72, 0x65, 0x20, 0x64, 0x75, 0x6c, 0x6c, 0x65, 0x64, 0x2c, 0x20, 0x79, 0x6f, 0x75, 0x72, 0x20, 0x61, 0x72, 0x64, 0x6f, 0x72, 0x20, 0x64, 0x61, 0x6d, 0x70, 0x65, 0x64, 0x2c, 0x0a, 0x79, 0x6f, 0x75, 0x72, 0x20, 0x73, 0x74, 0x72, 0x65, 0x6e, 0x67, 0x74, 0x68, 0x20, 0x65, 0x78, 0x68, 0x61, 0x75, 0x73, 0x74, 0x65, 0x64, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x79, 0x6f, 0x75, 0x72, 0x20, 0x74, 0x72, 0x65, 0x61, 0x73, 0x75, 0x72, 0x65, 0x20, 0x73, 0x70, 0x65, 0x6e, 0x74, 0x2c, 0x20, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x63, 0x68, 0x69, 0x65, 0x66, 0x74, 0x61, 0x69, 0x6e, 0x73, 0x0a, 0x77, 0x69, 0x6c, 0x6c, 0x20, 0x73, 0x70, 0x72, 0x69, 0x6e, 0x67, 0x20, 0x75, 0x70, 0x20, 0x74, 0x6f, 0x20, 0x74, 0x61, 0x6b, 0x65, 0x20, 0x61, 0x64, 0x76, 0x61, 0x6e, 0x74, 0x61, 0x67, 0x65, 0x20, 0x6f, 0x66, 0x20, 0x79, 0x6f, 0x75, 0x72, 0x20, 0x65, 0x78, 0x74, 0x72, 0x65, 0x6d, 0x69, 0x74, 0x79, 0x2e, 0x20, 0x20, 0x54, 0x68, 0x65, 0x6e, 0x20, 0x6e, 0x6f, 0x20, 0x6d, 0x61, 0x6e, 0x2c, 0x0a, 0x68, 0x6f, 0x77, 0x65, 0x76, 0x65, 0x72, 0x20, 0x77, 0x69, 0x73, 0x65, 0x2c, 0x20, 0x77, 0x69, 0x6c, 0x6c, 0x20, 0x62, 0x65, 0x20, 0x61, 0x62, 0x6c, 0x65, 0x20, 0x74, 0x6f, 0x20, 0x61, 0x76, 0x65, 0x72, 0x74, 0x20, 0x74, 0x68, 0x65, 0x20, 0x63, 0x6f, 0x6e, 0x73, 0x65, 0x71, 0x75, 0x65, 0x6e, 0x63, 0x65, 0x73, 0x20, 0x74, 0x68, 0x61, 0x74, 0x20, 0x6d, 0x75, 0x73, 0x74, 0x0a, 0x65, 0x6e, 0x73, 0x75, 0x65, 0x2e, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x35, 0x2e, 0x20, 0x20, 0x54, 0x68, 0x75, 0x73, 0x2c, 0x20, 0x20, 0x74, 0x68, 0x6f, 0x75, 0x67, 0x68, 0x20, 0x77, 0x65, 0x20, 0x68, 0x61, 0x76, 0x65, 0x20, 0x68, 0x65, 0x61, 0x72, 0x64, 0x20, 0x6f, 0x66, 0x20, 0x73, 0x74, 0x75, 0x70, 0x69, 0x64, 0x20, 0x68, 0x61, 0x73, 0x74, 0x65, 0x20, 0x69, 0x6e, 0x20, 0x77, 0x61, 0x72, 0x2c, 0x0a, 0x63, 0x6c, 0x65, 0x76, 0x65, 0x72, 0x6e, 0x65, 0x73, 0x73, 0x20, 0x68, 0x61, 0x73, 0x20, 0x6e, 0x65, 0x76, 0x65, 0x72, 0x20, 0x62, 0x65, 0x65, 0x6e, 0x20, 0x73, 0x65, 0x65, 0x6e, 0x20, 0x61, 0x73, 0x73, 0x6f, 0x63, 0x69, 0x61, 0x74, 0x65, 0x64, 0x20, 0x77, 0x69, 0x74, 0x68, 0x20, 0x6c, 0x6f, 0x6e, 0x67, 0x20, 0x64, 0x65, 0x6c, 0x61, 0x79, 0x73, 0x2e, 0x0a }; unsigned int sample_data_len = 1133; // vim:ci:sw=2 sts=2 ft=cpp