// EE_SV.C

//

// Illustrates how to save a quantity to and fetch a quantity from

// EEPROM.

//

// Saves a float and a struct TM to EEPROM and then fetches them. 

// dummy = 0 statements are used to set break points to verify

// operation.

//

// Note that a byte pointer which points to the beginning of the

// quantity is passed to each function.

//

// copyright, Peter H. Anderson, Baltimore, MD, Jan, '01

 

#include <16f877.h>

 

#device *=16 ICD=TRUE

 

#use delay (clock=4000000)

 

void save_to_eeprom(byte adr, byte *p_dat, byte num_bytes);

void read_from_eeprom(byte adr, byte *p_dat, byte num_bytes);

 

 

struct TM

{

      byte hr;

      byte mi;

      byte se;

};

 

void main(void)

{

   float float_1 = 1.2e-12, float_2;

   struct TM t1, t2;

   byte *ptr, dummy;

 

   t1.hr = 12;    t1.mi = 45;       t1.se = 33;

 

   // save the float beginning at EEPROM location 0x00

   ptr = (byte *) &float_1;   // ptr points to first byte of float_1

   save_to_eeprom(0x00, ptr, sizeof(float));

     

   // save the struct TM beginning at EEPROM loc 0x10

   ptr = (byte *) &t1;

   save_to_eeprom(0x10, ptr, sizeof(struct TM));      // save t1

 

 

   // now read the float and struct TM back

   ptr = (byte *) &float_2;

   read_from_eeprom(0x00, ptr, sizeof(float));

 

   dummy = 0;           // used to set a breakpoint

 

   ptr = (byte *) &t2;

   read_from_eeprom(0x10, ptr, sizeof(struct TM));

 

   dummy = 0;     // used to set a breakpoint to verify program works

}

 

void save_to_eeprom(byte adr, byte *p_dat, byte num_bytes)

{

   byte n;

 

   for (n=0; n<num_bytes; n++)

   {

         WRITE_EEPROM(adr, *p_dat);

           DELAY_MS(8);

         ++adr;

         ++p_dat;

   }

}

 

void read_from_eeprom(byte adr, byte *p_dat, byte num_bytes)

{

   byte n;

 

   for (n=0; n<num_bytes; n++)

   {

         *p_dat = READ_EEPROM(adr);

         ++adr;

         ++p_dat;

   }

}