Saturday 19 November 2011

Read/Write Program and circuit with External EEPROM Memory Chip

I have created a program and circuit that allows for the reading and writing of memory onto the EEPROM chip. The program has 4 analog inputs, and if the switch at d8 is flipped to 5V, then the microchip will begin to read values from four analog inputs (A6, A5, A4, A3) and write them to the memory of the external EEPROM chip. If the switch at d9 is turned to 5V, then the microchip will existing data from the EEPROM memory chip.


Heres the code I wrote for the program.

/*
WRITE READ 4 ANALOG INPUTS
program waits in a loop for the switch on either d8 or d9 to be thrown.
if d8 is thrown, it goes into write mode writing the voltages at 4 analog inputs
if d9 is thrown, it goes into read mode, reading all the information stored on the eeprom chip (waits for d9 to be shut off)
*/
#include  "Wire.h"    // for External EEPROM CHIP
#define chip 0x50    // device address for our chip
byte d=0; // example variable to handle data going in and out of EERPROM
int endLoop=0; //to define the end of our write loop
void setup()
{
Serial.begin(9600); // for screen output
Wire.begin();   // wake up, EEPROM
}
void writeData(unsigned int add, byte data)
// writes a byte of data 'data' to the chip at I2C address 'device', in memory location 'add'
{
Wire.beginTransmission(chip);
Wire.send((int)(add >> 8));   // left-part of pointer address
Wire.send((int)(add & 0xFF)); // and the right
Wire.send(data);
Wire.endTransmission();
Serial.println(data, DEC);
delay(10);
}
byte readData(unsigned int add) // reads a byte of data from memory location 'add' in chip at I2C address 'device'
{
byte result;  // returned value
Wire.beginTransmission(chip); //  these three lines set the pointer position in the EEPROM
Wire.send((int)(add >> 8));   // left-part of pointer address
Wire.send((int)(add & 0xFF)); // and the right
Wire.endTransmission();
Wire.requestFrom(chip,1); // now get the byte of data...
result = Wire.receive();  return result; // and return it as a result of the function readData
}
void loop()  {
  
  int sensor0=0; // integer value between 0 and 1023 (10 bits) for sensor 1
  int sensor1=0; 
  int sensor2=0;
  int sensor3=0;  
  Serial.println("Waiting to write or read data....");
  while(digitalRead(8)==LOW&&digitalRead(9)==LOW){   // while loop reads pin 8 and will stay in loop until pin 8 recieves 5 volts
    digitalWrite(13, HIGH);  //writes pin 13 to 5 volts
    delay(1000);             //waits a second
    digitalWrite(13, LOW);   //writes pin 13 to 0 volts
    delay(1000);             //watis a seconds
  }
    
  if(digitalRead(8)==HIGH){           //Loop that writes data
    int a=0;
    Serial.println("Writing data...");
    while(digitalRead(8)==HIGH)
    {
      digitalWrite(13, HIGH);  //writes pin 13 to 5 volts
      sensor0=analogRead(A0);
      sensor1=analogRead(A1);
      sensor2=analogRead(A2);
      sensor3=analogRead(A3);
      byte first0  = lowByte(sensor0); //takes the lowest 8 bits from the 10 bit number 
      byte second0 = highByte(sensor0);//takes the takes the last 2 digits from the 10 bit number, and 6 zeros
      byte first1  = lowByte(sensor1); //takes the lowest 8 bits from the 10 bit number 
      byte second1 = highByte(sensor1);//takes the takes the last 2 digits from the 10 bit number, and 6 zeros
      byte first2  = lowByte(sensor2); //takes the lowest 8 bits from the 10 bit number 
      byte second2 = highByte(sensor2);//takes the takes the last 2 digits from the 10 bit number, and 6 zeros
      byte first3  = lowByte(sensor3); //takes the lowest 8 bits from the 10 bit number 
      byte second3 = highByte(sensor3);//takes the takes the last 2 digits from the 10 bit number, and 6 zeros
      writeData(a,first0);
      writeData(a+1,second0);
      writeData(a+2,first1);
      writeData(a+3,second1);
      writeData(a+4,first2);
      writeData(a+5,second2);
      writeData(a+6,first3);
      writeData(a+7,second3);
      delay(5000);
      a=a+8;
    }
    endLoop=a;  
}
  if(digitalRead(9)==HIGH){                ///loop that reads data
    digitalWrite(13,LOW);
    Serial.println("S0   S1   S2   S3");
    int b=0;
    while(b<endLoop)
    {
      byte low0 = readData(b);    //defines low0 as whats in the chips memory space b
      byte high0 = readData(b+1);
      byte low1 = readData(b+2);
      byte high1 = readData(b+3);
      byte low2 = readData(b+4);
      byte high2 = readData(b+5);
      byte low3 = readData(b+6);
      byte high3 = readData(b+7);
      int result0=high0<<8; // rotate left 8 bits the high value and store in result
      int result1=high1<<8;
      int result2=high2<<8;
      int result3=high3<<8;
      Serial.print(result0+low0,DEC);//prints first result 
      Serial.print("  ");
      Serial.print(result1+low1,DEC);//prints second result 
      Serial.print("  ");//puts a space between results
      Serial.print(result2+low2,DEC);//prints third result 
      Serial.print("  ");
      Serial.println(result3+low3,DEC); //prints the fourth result and carridge returns
      b=b+8;
  }
  while(digitalRead(9)==HIGH){}
 } 
}

No comments:

Post a Comment