Helllo... Dear,
I am new to this forum. i am doing an project on arduino and external eeprom. i found a code online to read data from the eeprom... i can read valid data from the eeprom. But my problem is i dont need all data... i need first 3 bit of every 8 bit data.... i dont know actually how to do it.... i want them to store in a varriable.
Code:
/*
* Use the I2C bus with small EEPROMs
* 24C01, 20C02, 24C04, 24C08, 24C16
* Sketch: I2C_EEPROM_Small.pde
*
* Derived from sketch for 24C64 devices posted on
* http://www.arduino.cc/playground/Code/I2CEEPROM
* From hkhijhe Date: 01/10/2010
*
* This one by davekw7x
* March, 2011
*
* For a single device, connect as follows:
* EEPROM 4 (GND) to GND
* EEPROM 8 (Vcc) to Vcc (5 Volts)
* EEPROM 5 (SDA) to Arduino Analog Pin 4
* EEPROM 6 (SCL) to Arduino Analog Pin 5
* EEPROM 7 (WP) to GND
* EEPROM 1 (A0) to GND
* EEPROM 2 (A1) to GND
* EEPROM 3 (A2) to GND
*/
#include <Wire.h>
// The seven-bit device address for EEPROMs
// I'll define it here rather than hard-code it inside all of the
// functions.
//
const byte DEVADDR = 0x50;
void setup()
{
byte msg1[] = "Message 1."; // data to write
byte msg2[] = "Zaphod says yo";
byte msg3[] = "Tttthat's all, folks!";
byte msgf[16] = {
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
};
Wire.begin();
Serial.begin(115200);
}
void loop()
{
//
// Read the first page in EEPROM memory, a byte at a time
//
Serial.println("eeprom_read_byte, starting at 0");
for (int i = 0; i < 16; i++) {
byte b = eeprom_read_byte(DEVADDR, i);
Serial.print(b, HEX);
Serial.print(' ');
}
Serial.println();
Serial.println();
// Now dump 512 bytes
Serial.println("eeprom_dump(DEVADDR, 0, 512)");
eeprom_dump(DEVADDR, 0, 512);
Serial.println();
delay(10000);
}
/*void eeprom_write_byte(byte deviceaddress, int eeaddress, byte data)
{
// Three lsb of Device address byte are bits 8-10 of eeaddress
byte devaddr = deviceaddress | ((eeaddress >> 8) & 0x07);
byte addr = eeaddress;
Wire.beginTransmission(devaddr);
Wire.write(int(addr));
Wire.write(int(data));
Wire.endTransmission();
delay(10);
}*/
// Pages are blocks of 16 bytes, starting at 0x000.
// That is, pages start at 0x000, 0x010, 0x020, ...
// For a device "page write", the last byte must be
// on the same page as the first byte.
//
// No checking is done in this routine.
//
// TODO: Do some checking, or, better yet (maybe)
// make length an int and do repeated device
// page writes if necessary. (Then maybe rename to
// eeprom_write_pages or some such thing.)
//
/*void eeprom_write_page(byte deviceaddress, unsigned eeaddr,
const byte * data, byte length)
{
// Three lsb of Device address byte are bits 8-10 of eeaddress
byte devaddr = deviceaddress | ((eeaddr >> 8) & 0x07);
byte addr = eeaddr;
Wire.beginTransmission(devaddr);
Wire.write(int(addr));
for (int i = 0; i < length; i++) {
Wire.write(data[i]);
}
Wire.endTransmission();
delay(10);
}*/
// TODO: Change to integer data type and return -1 if can't
// read.
//
int eeprom_read_byte(byte deviceaddress, unsigned eeaddr)
{
byte rdata = -1;
// Three lsb of Device address byte are bits 8-10 of eeaddress
byte devaddr = deviceaddress | ((eeaddr >> 8) & 0x07);
byte addr = eeaddr;
Wire.beginTransmission(devaddr);
Wire.write(int(addr));
Wire.endTransmission();
Wire.requestFrom(int(devaddr), 1);
if (Wire.available()) {
rdata = Wire.read();
}
return rdata;
}
//
// Returns number of bytes read from device
//
// Due to buffer size in the Wire library, don't read more than 30 bytes
// at a time! No checking is done in this function.
//
// TODO: Change length to int and make it so that it does repeated
// EEPROM reads for length greater than 30.
int eeprom_read_buffer(byte deviceaddr, unsigned eeaddr,
byte * buffer, byte length)
{
// Three lsb of Device address byte are bits 8-10 of eeaddress
byte devaddr = deviceaddr | ((eeaddr >> 8) & 0x07);
byte addr = eeaddr;
Wire.beginTransmission(devaddr);
Wire.write(int(addr));
Wire.endTransmission();
Wire.requestFrom(devaddr, length);
int i;
for (i = 0; i < length && Wire.available(); i++) {
buffer[i] = Wire.read();
}
return i;
}
//
// The display is like hexdump -C. It will always
// begin and end on a 16-byte boundary.
//
void eeprom_dump(byte devaddr, unsigned addr, unsigned length)
{
// Start with the beginning of 16-bit page that contains the first byte
unsigned startaddr = addr & (~0x0f);
// stopaddr is address of next page after the last byte
unsigned stopaddr = (addr + length + 0x0f) & (~0x0f);
for (unsigned i = startaddr; i < stopaddr; i += 16) {
byte buffer[16]; // Hold a page of EEPROM
char outbuf[6]; //Room for three hex digits and ':' and ' ' and '\0'
sprintf(outbuf, "%03x: ", i);
Serial.print(outbuf);
eeprom_read_buffer(devaddr, i, buffer, 16);
for (int j = 0; j < 16; j++) {
if (j == 8) {
Serial.print(" ");
}
sprintf(outbuf, "%02x ", buffer[j]);
Serial.print(outbuf);
}
Serial.print(" |");
for (int j = 0; j < 16; j++) {
if (isprint(buffer[j])) {
Serial.print(buffer[j]);
}
else {
Serial.print('.');
}
}
Serial.println("|");
}
}
//I removed the write part..........
OUTPUT:
eeprom_read_byte, starting at 0
AA 6D A6 65 6F E1 64 A9 0 80 86 28 C7 14 14 63
eeprom_dump(DEVADDR, 0, 512)
000: aa 6d a6 65 6f e1 64 a9 00 80 86 28 c7 14 14 63 |.109.101111.100....40...99|
010: 0f 0c 32 06 8f 5d a6 84 b6 06 0a 0a 64 80 a4 96 |..50..93......100...|
020: 87 ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff |................|
030: 6e 55 3a 61 0c 02 10 5f 71 56 42 61 0c 01 10 68 |110855897...95113866697...104|
040: 8b 51 43 61 0c 81 10 68 00 00 00 00 00 00 00 00 |.816797...104........|
050: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
060: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
070: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
080: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
090: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
0a0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
0b0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
0c0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
0d0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
0e0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
0f0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
100: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
110: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
120: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
130: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
140: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
150: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
160: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
170: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
180: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
190: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
1a0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
1b0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
1c0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
1d0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
1e0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
1f0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
I attached a picture of which data i want to store in varriable