|
|
View previous topic :: View next topic |
Author |
Message |
Supernova Guest
|
Memory not memorised?? |
Posted: Thu Apr 24, 2008 4:23 am |
|
|
Hello i've been trying to save some data in to the flash memory but every time I restart my PIC the info is gone. Does any one know how to make the info stay?
here's my code:
Code: | /-----------------------------------------------------------------
//Declaration de librairie
#include "test.h"
#include "Flex_LCD.h"
//Prototypes
//-----------------------------------------------------------------
//Déclaration de constante
#define ROM_ADDR 0x4000
//-----------------------------------------------------------------
//Déclaration de variables globales
//Interuptions
#INT_EEPROM
void Test_ecriture(void)
{
printf(lcd_putc,"\fEcriture effectué");
delay_ms(1000);
}
//-----------------------------------------------------------------
//- Programme Principale
//-----------------------------------------------------------------
void main(void)
{
//Déclaration des variables locaux
//-----------------------------------------------------------------
int8 i;
int8 write_data[5] = {1,2,3,4,5};
int8 read_data[5] = {0,0,0,0,0};
lcd_init();
printf(lcd_putc,"\fFlash write size = \n%u", getenv("FLASH_WRITE_SIZE"));
delay_ms(2000);
// Read flash memory.
read_program_memory(ROM_ADDR, read_data, sizeof(read_data));
// Display it, to show it's erased.
printf(lcd_putc,"\fBefore \n ");
for(i = 0; i < sizeof(read_data); i++)
printf("%X ", read_data[i]);
delay_ms(2000);
// Erase flash memory.
erase_program_eeprom(ROM_ADDR);
// Read flash memory.
read_program_memory(ROM_ADDR, read_data, sizeof(read_data));
// Display it, to show it's erased.
printf(lcd_putc,"\fFlash memory \n ");
for(i = 0; i < sizeof(read_data); i++)
printf("%X ", read_data[i]);
delay_ms(2000);
// Now write 8 bytes.
write_program_memory(ROM_ADDR, write_data, sizeof(write_data));
// Read them back.
read_program_memory(ROM_ADDR, read_data, sizeof(read_data));
// Display data read from Flash memory.
printf(lcd_putc,"\fAfter being written: \n");
for(i = 0; i < sizeof(read_data); i++)
printf(lcd_putc,"%X ", read_data[i]);
delay_ms(2000);
while(1);
}
//----------------------------------------------------------------- |
test.h
Code: | #include <18F452.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=115200,parity=N, xmit=PIN_C6, rcv=PIN_C7,bits=8,stream=sortie)
#use i2c(master,sda=PIN_C4,scl=PIN_C3,SLOW)
|
|
|
|
Matro Guest
|
|
Posted: Thu Apr 24, 2008 4:33 am |
|
|
Add the "NOWRT" in your fuses.
And as a test, try to replace :
Code: |
write_program_memory(ROM_ADDR, write_data, sizeof(write_data));
|
by :
Code: |
write_program_memory(ROM_ADDR, write_data, 8);
|
Because I'm not sure that sizeof() applied on a pointer will give the expected result.
Matro |
|
|
RLScott
Joined: 10 Jul 2007 Posts: 465
|
Re: Memory not memorised?? |
Posted: Thu Apr 24, 2008 5:10 am |
|
|
Supernova wrote: |
// Now write 8 bytes.
write_program_memory(ROM_ADDR, write_data, sizeof(write_data));
|
The comment says "Now write 8 bytes, but the array, write_data[] is declared with just 5 bytes. So sizeof(write_data) will equal 5.
Robert Scott
Real-Time Specialties |
|
|
Supernova Guest
|
|
Posted: Thu Apr 24, 2008 5:20 am |
|
|
Well thanks a lot for the answers, i've tryed both options you've proposed but still it doesn't write in the flash.
It writes it once while it runs, but as soon as I stop it and restart it, the datas are gone... :( |
|
|
Matro Guest
|
|
Posted: Thu Apr 24, 2008 5:27 am |
|
|
Did you posted the whole code or are there other pieces of code (like a bootloader)?
Matro |
|
|
Supernova Guest
|
|
Posted: Thu Apr 24, 2008 5:33 am |
|
|
This is all I have, a part from the flex_lcd.h witch is not used to save in the memory but just to print what i've read... |
|
|
Matro Guest
|
|
Posted: Thu Apr 24, 2008 6:25 am |
|
|
Could you add a print() just after the write in the flash that will display the WRERR bit?
Matro |
|
|
Supernova Guest
|
|
Posted: Thu Apr 24, 2008 6:42 am |
|
|
I get 10000100
Quote: | WRERR: FLASH Program/Data EE Error Flag bit
1 = A write operation is prematurely terminated
(any RESET during self-timed programming in normal operation)
0 = The write operation completed
Note: When a WRERR occurs, the EEPGD and CFGS bits are not cleared. This allows
tracing of the error condition.
|
And it is at 1 so what can I do? |
|
|
Matro Guest
|
|
Posted: Thu Apr 24, 2008 7:02 am |
|
|
I suppose that you display EECON1, so WRERR isn't 1. That's bit 3 so its value's 0.
The write cycle is correctly done.
If you have an ICD, you can execute your program and then connect to the device and read program memory to check that the write has been done.
Matro |
|
|
Supernova Guest
|
|
Posted: Thu Apr 24, 2008 8:33 am |
|
|
Problem solved.
I used the function write_program_eeprom instead of the other ones.
It works much better now.
Thanks anyway for the help. |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Thu Apr 24, 2008 9:06 am |
|
|
I tried your original code (without LCD functions) in the MPLAB simulator and it worked without problems.
What is your compiler version? |
|
|
Supernova Guest
|
|
Posted: Fri Apr 25, 2008 1:32 am |
|
|
it's ccs 3.2222 |
|
|
Ttelmah Guest
|
|
Posted: Fri Apr 25, 2008 7:37 am |
|
|
I'm guessing 3.222. The versions only ever reached 3.249, so 2222, is rather high!. If so, then this is the problem. The read and write program memory functions were only introduced at around the 3.200 level, and the early versions had some problems. There were some threads here about these at the time (about spring 2005).
Best Wishes |
|
|
|
|
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
|