|
|
View previous topic :: View next topic |
Author |
Message |
Blob
Joined: 02 Jan 2006 Posts: 75 Location: Neeroeteren, Limburg, Belgium
|
writing - reading flash only when power stays on. 18f2520 |
Posted: Mon Apr 21, 2008 4:29 am |
|
|
hello,
I am building an application in which I have a module containing a pic18f2520.
I have my PC softwarepackage nearly ready, with which I send some data to the mudule with the pic. then the mocule will be attached to the app. and the pic will make the app work according to the data stored in it with the PC.
I made a test program for PC to test my USB interface and the communication with the pic.
As you can see, i need non volatile memory to do this. so i want to use my pic's flash memory.
If I am right, the code to write - read flash is read_program_memory and write_program_memory.
With my test program i can write and read data from the flash memory with these functions if the power stays on.
But when I power down and up again, I only read FFFF on the locations i have been writing to.
beneath is my code.
Code: |
#include <18F2520.h>
#device ADC=10
#include <stdlib.h>
#fuses HS, NOWDT, PUT, BORV27, NOLVP, NOSTVREN, NODEBUG, PROTECT //, EBTR
#org 190, 8190 {}
#use fast_io(A)
#use fast_io(B)
#use fast_io(C)
#use delay (clock=20000000)
#use rs232 (baud=115200, xmit=PIN_C6, rcv=PIN_C7)
#define LED PIN_C3 // LED
int int_a, int_b, rs_pnt;
char rs_in[12];
char ch_read[4];
boolean bl_ok;
#INT_RDA
void serial_isr();
#separate
void blink_fast();
#separate
void blink_slow();
#INT_RDA
void serial_isr()
{
int16 adr, val;
char tmp[6];
rs_in[rs_pnt] = getc();
if ( rs_in[rs_pnt] == ';' )
{
bl_ok = !bl_ok;
if ( rs_in[(rs_pnt+1)%12] == '$') // write
{
tmp[0] = rs_in[(rs_pnt+2)%12];
tmp[1] = rs_in[(rs_pnt+3)%12];
tmp[2] = rs_in[(rs_pnt+4)%12];
tmp[3] = rs_in[(rs_pnt+5)%12];
tmp[4] = rs_in[(rs_pnt+6)%12];
tmp[5] = '\0';
adr = atol(tmp);
tmp[0] = rs_in[(rs_pnt+7)%12];
tmp[1] = rs_in[(rs_pnt+8)%12];
tmp[2] = rs_in[(rs_pnt+9)%12];
tmp[3] = rs_in[(rs_pnt+10)%12];
tmp[4] = rs_in[(rs_pnt+11)%12];
tmp[5] = '\0';
val = atol(tmp);
write_program_memory(adr, val,4);
printf("owkay!");
}
else
if ( rs_in[(rs_pnt+1)%12] == '£') // read
{
tmp[0] = rs_in[(rs_pnt+2)%12];
tmp[1] = rs_in[(rs_pnt+3)%12];
tmp[2] = rs_in[(rs_pnt+4)%12];
tmp[3] = rs_in[(rs_pnt+5)%12];
tmp[4] = rs_in[(rs_pnt+6)%12];
tmp[5] = '\0';
adr = atol(tmp);
read_program_memory(adr,val,4);
printf("%Lx",val);
}
}
rs_pnt = (rs_pnt+1)%12;
}
#separate
void blink_fast()
{
output_high(LED);
delay_ms(50);
output_low(LED);
delay_ms(50);
}
#separate
void blink_slow()
{
output_high(LED);
delay_ms(250);
output_low(LED);
delay_ms(250);
}
#separate
void main(void)
{
int_a = 0;
int_b = 0;
bl_ok = 0;
rs_pnt = 0;
set_tris_c(129);
SETUP_ADC_PORTS(NO_ANALOGS);
output_low(LED);
enable_interrupts(GLOBAL);
enable_interrupts(INT_RDA);
while(1)
{
if(bl_ok)
blink_fast();
else
blink_slow();
}
}
|
Thanks in advance,
Blob |
|
|
Ttelmah Guest
|
|
Posted: Mon Apr 21, 2008 4:41 am |
|
|
Use the read_eeprom, and write_eeprom ability instead, and store the data in the EEPROM mmory, rather than the flash.
The 'write life' of the flash memory, is typically only 1/100th that of the EEPROM, and the main code has to stop completely, while it is written. The EEPROM takes as long to write,but can be accessed while the program keeps running.
Don't perform the write in your serial ISR. Set a flag, and do it in the main.
The main reason it doesn't work, is that the second value the calls, needs to be the _address_in RAM of the value to write, not the value itself. so '&val', not 'val'.
Best Wishes |
|
|
Blob
Joined: 02 Jan 2006 Posts: 75 Location: Neeroeteren, Limburg, Belgium
|
|
Posted: Mon Apr 21, 2008 5:19 am |
|
|
hello Ttelmah,
thanks for your quick reply.
It works now using the pointers instead, (it is in the ccs manual ... :-s)
I did consider before to use the eeprom, but it is not enough for my application. the 2520 has only 256 bytes eeprom.
So now i can move on to my second problem.
The datasheet tells me that i should first erase a block before writing in block.
Now on this pic the flash_write_size and flash_erase_size are the same, so erase will automatically be performed when i write.
The question is: should i still write in blocks of 32 bytes?
thanks,
Blob |
|
|
|
|
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
|