CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

big variables to EEPROM

 
Post new topic   Reply to topic    CCS Forum Index -> Code Library
View previous topic :: View next topic  
Author Message
TimothyCarter



Joined: 08 Aug 2016
Posts: 22

View user's profile Send private message

big variables to EEPROM
PostPosted: Thu Mar 31, 2022 9:51 am     Reply with quote

Should anyone find this useful, here are some helper functions for reading/writing 16 & 32 bit variables to EEPROM.

Note: as stated in the comments, variables should be stored in sequential bytes of EE.

Download link: https://gitlab.com/smilingtexan/ccs-picc-eeprom-large-var.git

Code:

/*
 * @file: eeprom_large.c
 * @author: T. Carter
 * @date: March 30, 2022
 *
 * This code is to provide methods to read and write 16-bit and 32-bit
 *   variables to the EEPROM of the PIC (it uses the built-in functions
 *   for read_eeprom and write_eeprom)
 *
 */

#ifndef _STDINT
   #include <stdint.h>         // need this library from the CCS drivers folder
#endif

// The CCS built-in function read_eeprom will read and return one byte
//    from the EEPROM memory. The following two functions are added
//    to return a 16-bit and 32-bit unsigned int respectively.
// The macro __EEADDRESS__ is defined in the processor header file as
//    either an 8-bit or 16-bit variable depending on EEPROM size.
// For the 16-bit and 32-bit methods of read & write; the addr variable
//    should be the EEPROM address of the LSB (least-significant byte)
//    Thus, the EEPROM addresses for the 16-bit and 32-bit variables
//    should be sequential or things can get really confusing

uint16_t read_eeprom16 (__EEADDRESS__ addr)
{
   uint8_t low, high;
   low = read_eeprom(addr++);       // read from eeprom at addr and then increment addr
   high = read_eeprom(addr);
   return(make16(high, low));
}

uint32_t read_eeprom32 (__EEADDRESS__ addr)
{
   uint8_t low, high;
   uint16_t wLow, wHigh; 
   low = read_eeprom(addr++);       // read from eeprom at addr and then increment addr
   high = read_eeprom(addr++);
   wLow = make16(high, low);
   low = read_eeprom(addr++);
   high = read_eeprom(addr);
   wHigh = make16(high, low);
   return(make32(wHigh, wLow));
}

// The following eeprom_write function only writes the data to EEPROM if
//    it is different from what is at that addr
void eeprom_write (__EEADDRESS__ addr, uint8_t data)
{
   uint8_t temp = read_eeprom(addr);
   if (data != temp)
   {
      write_eeprom(addr, data);
   }
}

// The following two functions are provided to write 16-bit and 32-bit
//    variables to the EEPROM; similar to the read_eeprom functions above
void write_eeprom16 (__EEADDRESS__ addr, uint16_t data)
{
   uint8_t smallData = make8(data, 0);
   eeprom_write(addr++, smallData);
   smallData = make8(data, 1);
   eeprom_write(addr, smallData);
}

void write_eeprom32 (__EEADDRESS__ addr, uint32_t data)
{
   uint8_t smallData = make8(data, 0);
   eeprom_write(addr++, smallData);
   smallData = make8(data, 1);
   eeprom_write(addr++, smallData);
   smallData = make8(data, 2);
   eeprom_write(addr++, smallData);
   smallData = make8(data, 3);
   eeprom_write(addr, smallData);
}

_________________
“Computer science education cannot make anybody an expert programmer any more than studying brushes and pigment can make somebody an expert painter.”
- Eric S. Raymond
Ttelmah



Joined: 11 Mar 2010
Posts: 19495

View user's profile Send private message

PostPosted: Mon Apr 04, 2022 6:30 am     Reply with quote

Just FYI, the compiler already has code to do this (and for several more
types as well). Look at 'internal_eeprom.c', which has routines to handle

int1, int16, int32, float, and for PCD, float48, float64 etc..
TimothyCarter



Joined: 08 Aug 2016
Posts: 22

View user's profile Send private message

I figured CCS had something ...
PostPosted: Tue Apr 12, 2022 3:20 pm     Reply with quote

Ttelmah wrote:
Just FYI, the compiler already has code to do this (and for several more
types as well). Look at 'internal_eeprom.c', which has routines to handle

int1, int16, int32, float, and for PCD, float48, float64 etc..


I figured they must have had something, but could not find it at the time I wrote this. (Originally written a couple of years ago.)
_________________
“Computer science education cannot make anybody an expert programmer any more than studying brushes and pigment can make somebody an expert painter.”
- Eric S. Raymond
Ttelmah



Joined: 11 Mar 2010
Posts: 19495

View user's profile Send private message

PostPosted: Mon Apr 25, 2022 1:14 am     Reply with quote

I think perhaps they need to do something like the 'readme', with a list
of the driver files and what they do.
The file goes back to 2012, so should have been with your compiler
'a couple of years ago'. Very Happy
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> Code Library All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group