View previous topic :: View next topic |
Author |
Message |
pilar
Joined: 30 Jan 2008 Posts: 197
|
why I can not write correctly int16 data in the eeprom ? |
Posted: Tue Jan 11, 2011 5:29 pm |
|
|
Hi, what is the problem with this code, I can not write correctly data in the eeprom int16,
The data written in the eeprom must be 0x1781 but write the number 0xF506!!!, what is the problem?
Code: | #include <18F452.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP,NOBROWNOUT
#use delay(clock=20MHz)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)// RS232 Estándar
#include <string.h>
#include <internal_eeprom.c>
#include <STDLIB.H>
#include <MATH.H>
char Buffer_LSB[5];
int16 Pag_LSB;
void main(){
Buffer_LSB[0] = "1";
Buffer_LSB[1] = "7";
Buffer_LSB[2] = "8";
Buffer_LSB[3] = "1";
Pag_LSB = atol(Buffer_LSB);
write_int16_eeprom(0,Pag_LSB);
while (TRUE);
} |
|
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Jan 11, 2011 6:01 pm |
|
|
If you want to convert a hex string, you have to tell atol() that's it's a
hex string. This means you have to put "0x" in front of it.
You need to increase the size of your array by 2, and do that.
Also, atol() expects a string. A string has a 0 byte (0x00) at the end, in
the last byte. Your code doesn't do that. It's working by luck.
Also, even though double quotes works, the preferred method is to use
single quotes around individual text characters, such as '0', 'x', '1', '7', etc. |
|
![](templates/subSilver/images/spacer.gif) |
MikeP
Joined: 07 Sep 2003 Posts: 49
|
|
Posted: Tue Jan 11, 2011 6:14 pm |
|
|
Try the following.
Code: |
#include <18F452.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP,NOBROWNOUT
#use delay(clock=20MHz)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)// RS232 Estándar
#include <string.h>
#include <internal_eeprom.c>
#include <STDLIB.H>
#include <MATH.H>
char Buffer_LSB[5];
int16 Pag_LSB;
void main(){
Buffer_LSB[0] = '1';
Buffer_LSB[1] = '7';
Buffer_LSB[2] = '8';
Buffer_LSB[3] = '1';
Buffer_LSB[4] = '\0';
Pag_LSB = atol(Buffer_LSB);
write_int16_eeprom(0,Pag_LSB);
while (TRUE);
}
|
|
|
![](templates/subSilver/images/spacer.gif) |
pilar
Joined: 30 Jan 2008 Posts: 197
|
|
Posted: Tue Jan 11, 2011 7:12 pm |
|
|
Sorry, I modified my code but the problem is the same!!!
The data written in the eeprom must be 0x1781 but write the number 0xF506!!!, what is the problem?
Code: | #include <18F452.h>
.
.
.
char Buffer_LSB[5] = "1781";
int16 Pag_LSB;
void main(){
Pag_LSB = atol(Buffer_LSB);
write_int16_eeprom(0,Pag_LSB);
while (TRUE);
} |
|
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Jan 11, 2011 7:18 pm |
|
|
Can you read ? |
|
![](templates/subSilver/images/spacer.gif) |
collink
Joined: 08 Jan 2010 Posts: 137 Location: Michigan
|
|
Posted: Wed Jan 12, 2011 6:54 am |
|
|
Like PCMProgrammer has already told you: Add 0x to the string you are passing to atol. That is:
Code: |
char Buffer_LSB[7] = "0x1781";
|
If you are going to go to the effort to post on a forum then you should be willing to actually read and properly understand the replies too.
Now, I'm going to get technical so put your thinking cap on:
You also have another problem. 1781 in decimal is 0x06F5 in hex. Do you see your other problem? The byte order is reverse of what you want! write_int16_eeprom is putting the bytes in eeprom backwards of how you want them. I don't have the perfectly up to date version of CCS so maybe that function is part of CCS and maybe not. If you wrote that function then you'll have to put the bytes in EEPROM backwards of how you are currently doing it if you want the bytes to come out as 0x1781 instead of 0x8117.
pilar wrote: | Sorry, I modified my code but the problem is the same!!!
The data written in the eeprom must be 0x1781 but write the number 0xF506!!!, what is the problem?
Code: | #include <18F452.h>
.
.
.
char Buffer_LSB[5] = "1781";
int16 Pag_LSB;
void main(){
Pag_LSB = atol(Buffer_LSB);
write_int16_eeprom(0,Pag_LSB);
while (TRUE);
} |
|
|
|
![](templates/subSilver/images/spacer.gif) |
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Wed Jan 12, 2011 6:58 am |
|
|
Start by fixing these two simple points:
1) 1781 will be interpreted as a decimal number. If you want it to be seen as hex you must let the compiler know.
2) Atol() expects a null terminated string. Where is your null? _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
![](templates/subSilver/images/spacer.gif) |
pilar
Joined: 30 Jan 2008 Posts: 197
|
|
Posted: Wed Jan 12, 2011 9:04 am |
|
|
Thanks for your patience...., I've solved the problem!!! |
|
![](templates/subSilver/images/spacer.gif) |
|