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

Save data in program memory - Pic 24f

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



Joined: 21 Dec 2011
Posts: 42

View user's profile Send private message

Save data in program memory - Pic 24f
PostPosted: Thu Sep 06, 2012 1:26 pm     Reply with quote

Hi,

I'm trying to save data in the space memory program of pic24f....but nothing.

1. Address for my data
Code:
#org 0x000200, 0x000201 {}

2. Write my data
Code:
int16 A;
   A = 100;
   write_program_memory(0x000200, &A, 2);

3. Read my data
Code:
int16 B;
    B=0;
    read_program_memory(0x000200,B,2);


>>> Why now 'A' is not equal 'B' ???
Thanks in advance is you have an idea, is you see a terrible error !
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Thu Sep 06, 2012 1:47 pm     Reply with quote

Code:
read_program_memory(0x000200,B,2);
Change to:
Code:
read_program_memory(0x000200,&B,2);
Ttelmah



Joined: 11 Mar 2010
Posts: 19549

View user's profile Send private message

PostPosted: Thu Sep 06, 2012 1:53 pm     Reply with quote

Key thing to understand on all these PIC's, is that _erase_ has to be done on an entire page.
The write function will erase memory, if you write to the first location of a page, _but not otherwise_. Means unless the page has already been cleared, what you write will be corrupted by what is already there.
To write a small area of memory to the PIC, you have to read the entire page into RAM, modify the bytes you want to change, and write the whole page back.
This is a hardware limitation of the chips, and not a code problem.
Start by reserving an entire page, starting on a page boundary, and then things can start to work.
The page size depends on your PIC, but is typically 2KB on PIC24's.

Best Wishes
joseph20480



Joined: 21 Dec 2011
Posts: 42

View user's profile Send private message

PostPosted: Thu Sep 06, 2012 2:07 pm     Reply with quote

Thanks for your reply !
I do the correction....

Now i try to see the real value with this line :
Code:
printf("A: %lu   -  B: %lu\n\r",A,B);

result : "A : 100 - B : 65535"

??
jeremiah



Joined: 20 Jul 2010
Posts: 1357

View user's profile Send private message

PostPosted: Thu Sep 06, 2012 7:09 pm     Reply with quote

You must write program memory in 4 byte chunks and the 4th byte must be 0x00. It has to do with how instructions are stored in program memory, but it affects data too.
joseph20480



Joined: 21 Dec 2011
Posts: 42

View user's profile Send private message

PostPosted: Fri Sep 07, 2012 6:56 am     Reply with quote

ok...
So i need to reserve more space ??

Anyone have a code ? and example ?
thanks in advance
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