View previous topic :: View next topic |
Author |
Message |
joseph20480
Joined: 21 Dec 2011 Posts: 42
|
Save data in program memory - Pic 24f |
Posted: Thu Sep 06, 2012 1:26 pm |
|
|
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
|
|
Posted: Thu Sep 06, 2012 1:47 pm |
|
|
Code: | read_program_memory(0x000200,B,2); | Change to: Code: | read_program_memory(0x000200,&B,2); |
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19549
|
|
Posted: Thu Sep 06, 2012 1:53 pm |
|
|
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
|
|
Posted: Thu Sep 06, 2012 2:07 pm |
|
|
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
|
|
Posted: Thu Sep 06, 2012 7:09 pm |
|
|
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
|
|
Posted: Fri Sep 07, 2012 6:56 am |
|
|
ok...
So i need to reserve more space ??
Anyone have a code ? and example ?
thanks in advance |
|
|
|