Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

PIC32 memory allocation - "incrementation"

Status
Not open for further replies.

tboydva

Member
I'm not sure how exactly to put this question (which I guess has made it hard to search for a suitable answer). I've been programming PICs for a while and have always "output" any data through a UART to PC (where I collect). I'm working on a project where the data need to be stored for about 12 hours in internal memory, then sent via UART (XBee). I want to concatenate the new data with the historical data every 5 seconds. So, I was trying to remember the old days using C for strings and arrays (not too much is left in the noggin). Essentially, I think I want to create a memory block with a pointer (string *s). However, I don't know exactly how much data I'm going to collect. I have a decent idea, but I'd like to allocate enough to be safe. My compiler (mikroC) supports malloc, but I still don't know how best to allocate (I'm using a PIC32MX460x512L - so I could just allocate most of the data memory I guess). Is there a straightforward way to allocate a string variable of ~500kb?

Secondly, from my memory, if you wanted "increment" a file, you would "find" the EOF and start writing there, but that was opening actual files on the computer. If I have a string and I want to "add" the new data collected in the current 5 second interval, is it just best to do a strcat or is there a more elegant (or better) way?

Sorry for such a noob question, but I hadn't really even thought about how I'd do this, and I really can't find a clear example (but again, it could be the way I'm searching). Any advise would be greatly appreciated.
 
For a PIC where you have a fixed amount of memory to work with, I'd reserve a fixed block of memory for your buffer.

As for your "increment" scenario, the most efficient way is to keep track of the end of the string you're working with (store a pointer), then increment that each time you write to the string. strcat() is inefficient since it has to search the string for the null terminator, unless mikroC does it differently.
 
kpatz,

Was on travel and didn't get a chance to thank you. I think I will just allocate a large string as you suggest. I don't really know how to point to the address of the last character. Would you use strlen() to find it (or is it as inefficient as strcat())? TO be honest, I haven't done a lot with string in the mC realm (use C# for stuff like that). When I look at the PIC32 datasheet, I'm still a little confused how to allocate a fixed memory block. My compiler perhaps doesn't "dumb it down" for me. I can put all kinds of wild numbers in for the char[xxx] and the compiler is fine with it (and the memory allocation pie chart doesn't seem to change). Probably simple stuff for old hats, but I really don't know exactly how to calculate it!
Thanks for the advise.
Tom
 
I know that when I allocate a block of memory in MPLAB for my PIC32, it doesn't change the memory allocation until I write to the block.

Even just write one byte to the block of memory and you should see the allocation change to reflect the full block.

Code:
unsigned char block[31000];

main()
{
	block[0] = 0xff;
        //...............
	block[15000] = 0xff;
        //...............
	block[30999] = 0xff;
}

or if you like, you can fill up the whole block:

Code:
unsigned int x;

for(x=0; x!=31000; x++)
{
        block[x] = 0xff;
}



To write to the memory, just do what kpatz said and have a pointer which you increment whenever you write a new value to the block of memory. Just keep track of the pointer and once it reaches the amount of samples you want/allocated memory block size then you stop sampling.

Code:
*sample (not real) code*

void sample(void)
{
	block[pointer] = <whatever>
	pointer++;
	<check pointer value and stop sampling if reached max value>
}
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top