Jump to content

Recommended Posts

I wish to store data in EEPROM of a 16F877A. The data is held as a 32 bit value which I periodically (every 24 hours) wish to copy into EEPROM. I've used the following snippet, but it only saves the least two significant bytes. At the risk of confusing, the code is actually only to save 3 bytes, as the most significant byte I realised would always be zero, to save EEPROM space.

Perhaps someone can suggest a better way of achieving this data transfer.

 

if (store_flag)
{
store_flag=0;
eeprom_write(store_day, day);
store_day++;
long_temp=ON_time_secs;
long_temp=long_temp>>16;
store_val=(unsigned char)long_temp;
eeprom_write(store_day, store_val);	   //save hi-byte
store_day++;

long_temp=ON_time_secs;
long_temp=long_temp>>8;
store_val=(unsigned char)long_temp;
eeprom_write(store_day, store_val);	//save mid-byte
store_day++;

long_temp=ON_time_secs;
store_val=(unsigned char)long_temp;
eeprom_write(store_day, store_val);	//save lo-byte
store_day++;
ON_time_secs=0;
}

Share this post


Link to post
Share on other sites

put your long into a union along with an array of 4 unsigned chars.

 

union {
 unsigned long temp;
 unsigned char arTemp[4];
} longTime;

longTime.temp=ON_time_secs;

eeprom_write(store_day, longTime.arTemp[2]);
eeprom_write(store_day+1, longTime.arTemp[1]);
eeprom_write(store_day,+2 longTime.arTemp[0]);

 

or something similar to this.

 

Cheers

 

Reynard

Share this post


Link to post
Share on other sites
put your long into a union along with an array of 4 unsigned chars.

 

union {
 unsigned long temp;
 unsigned char arTemp[4];
} longTime;

longTime.temp=ON_time_secs;

eeprom_write(store_day, longTime.arTemp[2]);
eeprom_write(store_day+1, longTime.arTemp[1]);
eeprom_write(store_day,+2 longTime.arTemp[0]);

 

or something similar to this.

 

Cheers

 

Reynard

 

Thanks for the quick response, although I feel rather silly! Of course a union is exactly what is needed, but not being a programmer I've never actually used one before.

Best regards.

Share this post


Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...