Friday, 4 November 2011

EEPROM MEMORY STORAGE

I have been working on a program that will take data, and store it. Regularly when values are taken from a microchip, the data is stored into SRAM, which is cleared when the power is reset. For our balloon we want to be able to measure voltages from thermistors, pressure sensors and possibly other devices, and we don't want to lose this data when the power is shut off. So this data must be stored to either the EEPROM memory or the Atmega's flash memory. The difference between the two is that data going into EEPROM can only be stored byte by byte, and must be addressed where it is going, where data in flash memory can be stored in tables and arrays and are just stored "somewhere". After struggling with storing and retrieving data from flash memory, I decided to try using the EEPROM, even though we will likely have to use flash memory due to the lack of space in the Atmega's EEPROM.


***UPDATE*** The EEPROM memory will have to be used because only compiled information can be stored in the flash memory. Anything being declared after complile stages cannot be stored in the flash memory. We have placed and order for a seperate EEPROM memory chip that has 128 kB of memory, which will be more than enough for storing the flight data.


Below, is the code I successfully wrote for this process. I will be attempting to learn and figure out how to properly store and retrieve data into the flash memory.


/*
 Analog Reader
 Standby: Repeatedly blinks an LED
 Recieves voltage at input 8, turns off standby
 Repeatedly reads Voltages from analog pin 0, stores them in EEPROM memory.
 Once switch is turned, stops storing and goes into stanby 2 waiting for an input a pin9
 Once this is turned, it will read values either until it has read them all, or pin9 is turned off
*/
#include <EEPROM.h>
void setup() {              
  pinMode(8, INPUT);      //pin 8 is set to an input
  pinMode(13, OUTPUT);    //pin 12 is set to output
  pinMode(9,INPUT);       //pin 9 is set to an input
  Serial.begin(9600);     // baud rate for the chip is set to 9600
  }
void loop(){
  int sensor0 = 0;            //int to store value read from A0
 
  while(digitalRead(8)==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
  }

  digitalWrite(13,HIGH);
  int k=0;
  while(digitalRead(8)==HIGH){      //while loop stores data in the first 6 bits availiable in EEPROM just for testing purposes
    sensor0=analogRead(A0);         //sets sensor0 to the value read from A0
    byte first  = lowByte(sensor0); //takes the lowest 8 bits from the 10 bit number
    byte second = highByte(sensor0);//takes the takes the last 2 digits from the 10 bit number, and 6 zeros
    int first8bits = first;         //converts the first 8 bits to an int
    int last8bits = second;         //converts the last bits to an int
    EEPROM.write(k, first8bits);    //writes this integer to EEPROM memory in space k
    EEPROM.write((k+1), last8bits); //writes this integer to EEPROM memory in space K+1
    digitalWrite(13,LOW);           //blinks to show it has written
    delay(500);
    digitalWrite(13,HIGH);      
    delay(5000);              
    k=k+2;                          //sets counter variable k=k+2
  }
  while(digitalRead(8)==LOW&&digitalRead(9)==LOW){  ///standby 2. Waits for switch at input9 to be turned on.
    digitalWrite(13,LOW);
    delay(100);
    digitalWrite(13,HIGH);
    delay(100);
  }
  int j=0;                            //new counter variable
  while(j<=k&&digitalRead(9)==HIGH){  //loops until j is i greater than k or switch is turned off
   digitalWrite(13,LOW);              //blink to show what state it is in
   int test1 = EEPROM.read(j);        //sets temp1 variable equal to what is in EEPROM slot j
   int test2 = EEPROM.read(j+1);      //sets temp2 variable equal to what is in eeprom slot j+1
   Serial.println(test1);             //sends numbers to computer via serial
   Serial.println(test2);
   delay(500);
   digitalWrite(13,HIGH);
   j=j+2;                             //counter is increased by 2
  }
}

No comments:

Post a Comment