PIC16 memory management

Thread Starter

odm4286

Joined Sep 20, 2009
265
Hello all, I'm trying to teach myself some manual memory management. My approach so far.

Using the datasheet I find a bank of memory. I use a pointer to point to the first byte in this block and iterate through filling it with zeros. After, I set my pointer back to the first byte and then allocate memory as needed.

My question is, how do I "control" where this pointer is stored? I'm pretty sure this is handled at compile time. I'd like to have "control" of where everything is stored. Trying my best to write a simple RTOS.
 

Ian Rogers

Joined Dec 12, 2012
1,136
The pointer on the mid range pic16 is called the INDF reg and its controlled by the FSR register, Newer pics even have two FSR registers.

If you point the FSR at location 0x89 then you can read / write that location with the INDF.. I hope this is what you need... If not you can elaborate..

Good luck, an RTOS is a mountain!!
 

Thread Starter

odm4286

Joined Sep 20, 2009
265
Good luck, an RTOS is a mountain!!
That's exactly why I'm attempting this. I won't finish school for a while and I feel like this will be a good resume builder in the meantime. I'll check those registers out, thanks!

Also, is it safe to use the bytes in the GPRs as ram?
 
Last edited by a moderator:

JohnInTX

Joined Jun 26, 2012
4,787
If you are using PIC16, try a 16F1xxx (Enhanced Midrange Architecture). These have more to play with as well as an accessible stack. So does the 18F. Without stack access, an RTOS will be limited to simple state machines.

The 16F1xxx stuff has some good lessons in memory management. ROM is banked like the older stuff. RAM is likewise but banks are easier to select (BSR vs bits on the STATUS register). But the FSRs can be used to access the banked RAM as a linear address space from 2000h to the end of available RAM. A nice lesson in memory mapping. The instruction set provides a wealth of FSR-based instructions to access memory pointed to by FSR in either mode. Understand those and you'll be well on your way.

16F1xxx FSRs can be used to read tables in ROM. A couple of members here have improved on that technique to use the ROM data more efficiently as well.

Task switching on either 16F1xxx or 18F can be accomplished by (laboriously) pulling and saving everything off the stack for the suspended task, then pushing the saved stack from the task to run and doing a 'return'. That's not possible in the older 16F stuff so multitasking on those is usually a state machine that pretty much at stack level 0 for each switch.

Have fun!
 
Top