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 support@ccsinfo.com

why I can not write correctly int16 data in the eeprom ?

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



Joined: 30 Jan 2008
Posts: 197

View user's profile Send private message

why I can not write correctly int16 data in the eeprom ?
PostPosted: Tue Jan 11, 2011 5:29 pm     Reply with quote

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);
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Jan 11, 2011 6:01 pm     Reply with quote

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.
MikeP



Joined: 07 Sep 2003
Posts: 49

View user's profile Send private message

PostPosted: Tue Jan 11, 2011 6:14 pm     Reply with quote

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);
}

pilar



Joined: 30 Jan 2008
Posts: 197

View user's profile Send private message

PostPosted: Tue Jan 11, 2011 7:12 pm     Reply with quote

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);
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Jan 11, 2011 7:18 pm     Reply with quote

Can you read ?
collink



Joined: 08 Jan 2010
Posts: 137
Location: Michigan

View user's profile Send private message Visit poster's website

PostPosted: Wed Jan 12, 2011 6:54 am     Reply with quote

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);
}
SherpaDoug



Joined: 07 Sep 2003
Posts: 1640
Location: Cape Cod Mass USA

View user's profile Send private message

PostPosted: Wed Jan 12, 2011 6:58 am     Reply with quote

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.
pilar



Joined: 30 Jan 2008
Posts: 197

View user's profile Send private message

PostPosted: Wed Jan 12, 2011 9:04 am     Reply with quote

Thanks for your patience...., I've solved the problem!!!
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