|
|
View previous topic :: View next topic |
Author |
Message |
x!nDy Guest
|
reading flash memory one byte at a time. |
Posted: Tue Jul 29, 2008 8:47 pm |
|
|
Hi everyone, i am saving data in flash memory and these data are variable in length and are separated by a delimiter (*). So what i want to do is try to read flash one bit at a time and look for the (*). If i found an * then i would print the word. But i cant seem to get it to work. Here is my code.
Code: |
FlashLoopRead=FlashStartAddress; //FlashLoopRead receives the last address in flash holding a certain value.
FlashStartAddress = FlashReadAddress; // FlashStartAddress receives the first address in flash holding a certain value
disable_interrupts(GLOBAL);
ii=0;
do
{
read_program_memory(FlashStartAddress, temp1, 1); // temp1 is char
msg[ii] = temp1; // msg[ii] is an array with a length of 80
if (msg[ii]=='*')
{
*(msg[ii])='\0';
FlashStartAddress = FlashStartAddress + ii;
fprintf(PC,"\n%s\n",msg);
ii=0;
justOnce=True; // justOnce is a boolean
}
if (!justOnce)
ii++;
justOnce=false;
}while(FlashStartAddress<=FlashLoopRead);
|
So, if the contents of my flash memory is HELLO*WORLD!*, it would print HELLO
WORLD!
please help.. thanks in advance.. |
|
|
Indy31 Guest
|
|
Posted: Wed Jul 30, 2008 3:37 am |
|
|
Bug 1: read_program_memory() should be passed an address for storing the data read from flash. change: Code: | read_program_memory(FlashStartAddress, temp1, 1); // temp1 is char | to: Code: | read_program_memory(FlashStartAddress, &temp1, 1); // temp1 is char |
Bug 2:
should have been:
Code: | *(&msg[ii])='\0';
or
msg[ii]='\0'; |
General note 1: indirect addressing on the PIC is very inefficient and requires a lot of instructions. You can save memory and increase performance by minimizing array accesses. instead of: it is more efficient to write:
General note 2: Using 'justOnce' variables make your code difficult to understand. Instead of an if-if combination your code is faster and easier to understand if you use an if-else construction.
Regarding all above, here is my suggested code: Code: | ii=0;
do
{
read_program_memory(FlashStartAddress, &temp1, 1); // temp1 is char
if (temp1=='*')
{
msg[ii]='\0';
FlashStartAddress = FlashStartAddress + ii;
fprintf(PC,"\n%s\n",msg);
ii=0;
}
else
{
msg[ii] = temp1; // msg[ii] is an array with a length of 80
ii++;
}
}while(FlashStartAddress<=FlashLoopRead); |
|
|
|
x!nDy Guest
|
|
Posted: Wed Jul 30, 2008 9:09 pm |
|
|
hi.. thanks for the reply and explanation.. i really appreciate it.. however, when i try to use your code and try to look at the value of temp1, all it prints is 0.. can you tell me how this happened? here is my code:
Code: |
ii=0;
do
{
read_program_memory(FlashStartAddress, &temp1, 1); // temp1 is char
fprintf(PC,"temp1 = %c\n",temp1); // prints value of temp1
if (temp1=='*')
{
msg[ii]='\0';
FlashStartAddress = FlashStartAddress + ii;
fprintf(PC,"\n%s\n",msg);
ii=0;
}
else
{
msg[ii] = temp1; // msg[ii] is an array with a length of 80
ii++;
}
}while(FlashStartAddress<=FlashLoopRead);
|
|
|
|
RLScott
Joined: 10 Jul 2007 Posts: 465
|
|
Posted: Thu Jul 31, 2008 10:08 am |
|
|
x!nDy wrote: | hi.. thanks for the reply and explanation.. i really appreciate it.. however, when i try to use your code and try to look at the value of temp1, all it prints is 0.. can you tell me how this happened? here is my code:
Code: |
ii=0;
do
{
read_program_memory(FlashStartAddress, &temp1, 1); // temp1 is char
fprintf(PC,"temp1 = %c\n",temp1); // prints value of temp1
if (temp1=='*')
{
msg[ii]='\0';
FlashStartAddress = FlashStartAddress + ii;
fprintf(PC,"\n%s\n",msg);
ii=0;
}
else
{
msg[ii] = temp1; // msg[ii] is an array with a length of 80
ii++;
}
}while(FlashStartAddress<=FlashLoopRead);
|
|
You are not advancing FlashStartAddress when each character is read, so it seems that read_program_memory() will just read the same first byte over and over, and you will never get to the '*'. The msg[ii] = temp1 will eventually overflow the bounds of the msg[80] and trash some other memory and the program will crash. This will happen even if you did everything else right if the '*' is somehow absent. What did you see as a result of printout of temp1? _________________ Robert Scott
Real-Time Specialties
Embedded Systems Consulting |
|
|
x!ndy Guest
|
|
Posted: Thu Jul 31, 2008 8:23 pm |
|
|
you are right.. it was really stupid of me.. it works fine now.. thanks.. |
|
|
|
|
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
|