View previous topic :: View next topic |
Author |
Message |
maria100
Joined: 01 Feb 2012 Posts: 63
|
Storing 16 bit variable in EEPROM atol problem |
Posted: Wed Mar 07, 2012 5:24 pm |
|
|
Hello. I'm trying to store a 16 bit variable in the internal eeprom of 18f25k22, CCS PCH C Compiler, Version 4.124.
My code:
Code: |
int16 myvar=256; //decimal variable
int16 LL=0;
LL=atol(myvar); //convert to hex, result should be 0x100
write_int16_eeprom(0,LL);
|
but when I'm reading the EEPROM with the PIC programmer, I'm getting
00 00 (not FF FF ) instead of 0x100...same goes with other myvar 16 bit values. What I do wrong?
If I do:
Code: |
myvar=0x100;
write_int16_eeprom(0,myvar);
|
is working properly, so the problem must be with the atol command. Please help. |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
atol() |
Posted: Wed Mar 07, 2012 6:04 pm |
|
|
Code: |
int16 myvar=256; //decimal variable
int16 LL=0;
LL=atol(myvar); //convert to hex, result should be 0x100
write_int16_eeprom(0,LL);
|
Your code is doing EXACTLY what your telling it to do.
Your myvar is an int16.
The function atol() converts ASCII characters!!
Mike |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Mar 07, 2012 6:12 pm |
|
|
Download the CCS manual. Put it on your Windows desktop:
http://www.ccsinfo.com/downloads/ccs_c_manual.pdf
Look up atol(). See what it expects for a parameter. Give it exactly
what it asks for.
Do this for all functions that you use. Then many of your problems
will be solved. |
|
|
maria100
Joined: 01 Feb 2012 Posts: 63
|
|
Posted: Thu Mar 08, 2012 5:45 am |
|
|
I have done some various tests...the problem is that write_int16_eeprom command cannot read the value from a variable that i just set in the previous instruction.
if i do:
int16 variable=120;
write_int16_eeprom(0,variable); //is working
if i do:
int16 variable=0x78;
write_int16_eeprom(0,variable); //is working
if i do:
value=get_timer1();
//do some math
VBRGH1=((16000000/value)/4)-1; // SPBRG16
fprintf(MYSTREAM,"BRG16: %Ld HZ", VBRGH1);
//is printing the proper value as should ( 357)
write_int16_eeprom(0,VBRGH1);
//I GET 00 00 in the eeprom at that location........ |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9221 Location: Greensville,Ontario
|
|
Posted: Thu Mar 08, 2012 7:19 am |
|
|
hmm...
write_int16_eeprom(0,VBRGH1);
I don't have that function in either of my PCM or PCWHD versions.
It 'looks' like it only accepts an 8 bit variable though. What happens when you try 254,255,256 as VBRGH1 ?
If it does fail..then contact CCS support, sounds like a 'bug'.
hmmm.....another idea.
You should read the actual data in the PIC using MPLAB though! Assuming you have a 'read_int16_eeprom(..)' function,the write could be OK and the read faulty !! |
|
|
maria100
Joined: 01 Feb 2012 Posts: 63
|
|
Posted: Thu Mar 08, 2012 7:53 am |
|
|
YES..i think it is a BUG..i had just tried the simple "write_eeprom" command and these one works just fine with my variable.....the only problem with it is that it support 8 bit only...so i`m getting half of my value..instead of 0x165 , i get only the "65"...dont know what to say...
(I`m reading the values from eeprom with my PIC KIT2). |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19496
|
|
Posted: Thu Mar 08, 2012 8:00 am |
|
|
PCM programmers comment is dead right here. 'atol', expects an _ASCII_ value before it'll work. However you do not have ASCII involved at all.
If you do want to involve ASCII:
Code: |
char mytextvar[]="256";
int16 LL;
LL=atol(mytextvar);
write_int16_eeprom(0,LL);
|
Here I've started with the _ASCII_ representation of '256', and this is what atol expects, so it'll work.
Best Wishes |
|
|
maria100
Joined: 01 Feb 2012 Posts: 63
|
|
Posted: Thu Mar 08, 2012 8:07 am |
|
|
the variable is from getting the timer1 value...so is not ASCII...i simple want to write to the eeprom that value..i think i have mistaken when i used the atol in the first place...i have no ASCII.. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19496
|
|
Posted: Thu Mar 08, 2012 8:22 am |
|
|
maria100 wrote: | the variable is from getting the timer1 value...so is not ASCII...i simple want to write to the eeprom that value..i think i have mistaken when i used the atol in the first place...i have no ASCII.. |
OK. So just write the value to eeprom, without atol.
I don't know 'where' your int16 writer comes from, but using the standard code, assuming 'LL' has the 16bit value, you need:
Code: |
write_eeprom(0,make8(LL,0)); //write LSB to EEPROM
write_eeprom(1,make8(LL,1)); //write second byte to EEPROM
|
So you just write each byte in turn to the EEPROM.
Best Wishes |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19496
|
|
Posted: Thu Mar 08, 2012 8:44 am |
|
|
As a separate comment, seeing this is coming from a timer, I hope you are not writing too often....
The EEPROM, is _not_ designed for values that change really frequently. Though not as bad as the main program memory, if (for instance), you write to the EEPROM every second, on most chips, it'll potentially kill the EEPROM in little over a day.
The 'solution', is to either use something like a battery backed RAM, or only actually write to EEPROM, when the power goes 'off'.
Best Wishes |
|
|
maria100
Joined: 01 Feb 2012 Posts: 63
|
|
Posted: Thu Mar 08, 2012 8:54 am |
|
|
no no. I use the timer interrupt as a external edge trigger. So i write the eeprom only on the PIC startup..no overkill issue. Unfortunately i have tried your code but it does not work. Is very correct i can see that, but does not work. It does not write anything. I think it is a problem with that int16 variable or something. I just cannot understand... |
|
|
gpsmikey
Joined: 16 Nov 2010 Posts: 588 Location: Kirkland, WA
|
|
Posted: Thu Mar 08, 2012 10:43 am |
|
|
Where did you get your write_int16_eeprom() ? I don't see it in my manual either (which is about a year old). _________________ 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 |
|
|
maria100
Joined: 01 Feb 2012 Posts: 63
|
|
Posted: Thu Mar 08, 2012 12:02 pm |
|
|
From the internal_eeprom.c library. |
|
|
|