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

Storing a variable to program memory during run-time

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



Joined: 01 Oct 2003
Posts: 172
Location: Punta Gorda, Florida USA

View user's profile Send private message Send e-mail

Storing a variable to program memory during run-time
PostPosted: Thu Nov 29, 2007 12:58 pm     Reply with quote

I have been having issues with some calibration constants that are stored in internal eeprom, ocassionally getting corrupt, so I though of a secondary method where I could also store the same variable in a high location of program flash rom memory, where I could retreive it in case of eeprom corruption, this of course is just a backup in case the value of the variable that is stored in eeprom gets corrupted. Because I never have tried the CCS routines; write_program_memory() and read_program_memory, I wrote a simple test routine (below) to take a float variable store it in a rom location and then pass the value to another float variable. So far, this does not seem to work as both variables actually get blown away, so I guess that my method is quite flawed. Was wondering if anyone has implemented something similar that works?

*************************************************************

#include <18F6720.h>
#device ICD=TRUE
#device adc=10

#fuses HS,PUT,BROWNOUT,WDT128,NOLVP,NOWRT,DEBUG,STVREN // for ICD
#use delay(clock=10000000,RESTART_WDT)


#define ROMCALOC 0x186A0
#org ROMCALOC,0x186C0 {} // put this here to reserve the loc

void init()
{
setup_psp(PSP_DISABLED);
setup_adc_ports(AN0|VSS_VDD);
setup_adc(ADC_CLOCK_INTERNAL);
setup_wdt(WDT_OFF);
setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
setup_timer_4(T4_DISABLED,0,1);
disable_interrupts(INT_TIMER0);
disable_interrupts(INT_TIMER1);
disable_interrupts(INT_EXT);
disable_interrupts(INT_EXT1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
setup_low_volt_detect(FALSE);
setup_oscillator(False);
disable_interrupts(GLOBAL); }


void main()
{
float SpanFactor = 0.0023842; // var to be written to memory
float TestFloatVar =0; // var to read from memory

init();

write_program_memory(ROMCALOC, SpanFactor, sizeof(SpanFactor));
delay_us(1);
read_program_memory(ROMCALOC, TestFloatVar, sizeof(TestFloatVar));
delay_us(1);




do { restart_wdt(); } while(1);
}
SET



Joined: 15 Nov 2005
Posts: 161
Location: Glasgow, UK

View user's profile Send private message Send e-mail Visit poster's website

PostPosted: Thu Nov 29, 2007 2:45 pm     Reply with quote

Code:
#define ROMCALOC 0x186A0
#org ROMCALOC,0x186C0 {} // put this here to reserve the loc


Shouldnt these be the same address? Doubt thats the issue though you should be way above your code..
Ttelmah
Guest







PostPosted: Thu Nov 29, 2007 3:06 pm     Reply with quote

You need to give the read and write routines, the _addresses_ of the variables. For an _array_, the name of a variable, is it's address, but for a normal variable it isn't. You are writing to an address defined by the bytes making up the value '0.0023842', treated as an integer, and reading from a random address,defined by what just happens to be in 'TestFloatVar'...
Use the '&' function, to get the variable addresses, and it should all start working.

Best Wishes
cbarberis



Joined: 01 Oct 2003
Posts: 172
Location: Punta Gorda, Florida USA

View user's profile Send private message Send e-mail

PostPosted: Thu Nov 29, 2007 3:26 pm     Reply with quote

Embarassed Embarassed

I should have known better; to either use a pointer or the address of such variables. Also I meant to use #org ROMCALOC, 0x186A2 as a write to program memory sets aside 8 consecutive bytes for a write.

Thank you for your help Ttelmah!!

It now works exactly as I wanted it, I am wondering if I really need to have the #org statement, supposedly with it, the compiler will not place anything on that location??
Ttelmah
Guest







PostPosted: Thu Nov 29, 2007 3:45 pm     Reply with quote

As a completely different way of doing it, just declare a #ROM value at the location. If you declare it as a float, you can even initialise the values in the declaration. This both puts the declared values at this point, and reserves the memory, at the same time.

Best Wishes
cbarberis



Joined: 01 Oct 2003
Posts: 172
Location: Punta Gorda, Florida USA

View user's profile Send private message Send e-mail

PostPosted: Thu Nov 29, 2007 5:06 pm     Reply with quote

using something like: #ROM float ROMCALOC = {0xCDE93276} // to store 0.00273
This does not seem to work, actually I get an error during compilation under the float declaration "Expression must evaluate to a constant" If I recall when using the #ROM it defaults to a 16 bit constant on the PCH parts and a byte for the PCM parts, however you can declare a byte for the PCH part as a #ROM int8 address {const value}

Did I missinterpret your previous sugestion?
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