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

Initializing structures in CCS?

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
gpsmikey



Joined: 16 Nov 2010
Posts: 588
Location: Kirkland, WA

View user's profile Send private message

Initializing structures in CCS?
PostPosted: Wed Dec 08, 2010 8:39 pm     Reply with quote

In another post ( http://www.ccsinfo.com/forum/viewtopic.php?t=44178 )
I had asked about storing config data in eeprom and one example
Ttelmah had given was a structure of initial values as follows:

Code:
struct config_data {
   int8 data_ok=0xAA;
   int16 wash_time=200;
   int16 adc_zero=40;
   int16 adc_full=984;
   int16 output_4mA=166;
   int16 output_20mA=900;
}


However, I am not familiar with being able to declare a structures with
initial values like that (and the compiler does not like it either). I am
familiar with the construct where you declare the structure as in


Code:
struct config_data {
   int8 data;
   int16 wash_time;
   etc;
}


then being able to declare

Code:

static struct config_data my_config = {0xA5, 30, etc};  // load defaults


Is there a handier way along the lines originally shown ? Searching the
compiler (version 4) manual didn't provide any insight into other ways of
declaring/initializing a structure. I was also familiar with the:


Code:
static struct config_data {
   int8 data;
   int16 wash_time;
   etc;
} my_config = {0xA5, 30, etc};



Is there another way that I have not found documented ? I know CCS
doesn't always follow the standards with their enhancements, but I have
been unable to find any details.

thanks for looking !
(PIC18F14K22, compiler 4.114)
mikey
_________________
mikey
-- you can't have too many gadgets or too much disk space !
old engineering saying: 1+1 = 3 for sufficiently large values of 1 or small values of 3
bkamen



Joined: 07 Jan 2004
Posts: 1615
Location: Central Illinois, USA

View user's profile Send private message

PostPosted: Wed Dec 08, 2010 10:47 pm     Reply with quote

I've used both methods B and C in your examples.

Method A indeed causes a compiler error.


I've also declared array's of structures and used method B to loop through the array with array[x] = const_struct and that works too.

-Ben
_________________
Dazed and confused? I don't think so. Just "plain lost" will do. :D
bkamen



Joined: 07 Jan 2004
Posts: 1615
Location: Central Illinois, USA

View user's profile Send private message

PostPosted: Wed Dec 08, 2010 10:49 pm     Reply with quote

Oh, and in terms of saving a structure to memory like flash or EEPROM,

I typically roll it in and out with a loop that's zero to < sizeof(struct) and then make a pointer to the structure that's +x to save the array.

-Ben
_________________
Dazed and confused? I don't think so. Just "plain lost" will do. :D
gpsmikey



Joined: 16 Nov 2010
Posts: 588
Location: Kirkland, WA

View user's profile Send private message

PostPosted: Wed Dec 08, 2010 11:54 pm     Reply with quote

Thanks Ben - this is what I used to initialize the structure - I didn't bother with a template name since this structure only is used once. I used Ttelmah's block move routines to copy in and out of eeprom (at the start of main, if the first byte of eeprom doesn't match the check byte, the initialized sizeof(structure) is copied to eeprom. Seems to work fine on the test hardware


Code:
   // set up default calibration factors - temps are in deg C * 10
static struct {
   int8  chk1;       // checkbyte in eeprom 0xA5 if settings have been stored.
   int16 Tset_AMB;   // ambient air set point - deg C x 10
   int16 Tset_HTR;   // set point for heater plate - deg C x 10
   int16 Thyst;      // hysterisis to use for on/off - deg C x 10
   } settings = { CHK_BYTE, // check byte
                  20,       //  2.0 degrees for ambient threshold to turn on
                  250,      // 25.0 degrees as heater target temp
                  30 };     //  3.0 deg hyst


Then, up near the start of main(), I have the following that checks for the data in eeprom.

Code:
if (read_eeprom(0) != CHK_BYTE ) // settings not stored in eeprom ?
   {
   EEPROM_PUT(&settings,sizeof(settings),0); // copy defaults to eeprom if needed
   lcd_putc("\fWrite Defaults \n    ====> EEPROM");
   delay_ms(5000);      // time to read message.
   }
EEPROM_GET(&settings,sizeof(settings),0); // read settings into settings stuct


mikey
_________________
mikey
-- you can't have too many gadgets or too much disk space !
old engineering saying: 1+1 = 3 for sufficiently large values of 1 or small values of 3
alexandre



Joined: 10 Dec 2010
Posts: 7

View user's profile Send private message

read_eeprom & write_eeprom with PIC18F87J11
PostPosted: Fri Dec 10, 2010 3:16 am     Reply with quote

Hello every body,

I just started programming PIC18F, I'm using CCS 4.049

The built-in read_eeprom and write_eeprom functions are not recognised by my compiler.
The error message is:
"*** Error 12 "main.c" Line 74(16,17): Undefined identifier -- write_eeprom"

some one would help me please.

I want to save some information in the data memory of the PIC18F87J11. According to its datasheet this PIC has 3930 Bytes of data memory.

Does there any way (other than read_eeprom() and write_eeprom() built-in functions) to manage the Data Memory of PIC18F87J11?

Thank you very much for your help Rolling Eyes
Ttelmah



Joined: 11 Mar 2010
Posts: 19499

View user's profile Send private message

PostPosted: Fri Dec 10, 2010 5:07 am     Reply with quote

Don't ask the same question in multiple threads. Have answered in your separate thread.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion 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