| View previous topic :: View next topic |
| Author |
Message |
stinky
Joined: 05 Mar 2012 Posts: 99 Location: Central Illinois
|
|
Posted: Fri Mar 09, 2012 12:51 pm |
|
|
So far I haven't found any problems with using the separate command. The only "speed" issue that I'm really concerned about is how quickly the PIC powers up and I don't see any change in that as compared to v4.007.
Does someone have a reason not to use the #separate command so lavishly? |
|
 |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Mar 09, 2012 1:15 pm |
|
|
If you use #separate, the CCS compiler disables stack level limit
checking. It will no longer give an error message if you if use more
than the 8 stack levels in the 16F819.
This means the compiler can wrongly use more than the 8 stack levels
if you construct your program with a lot of nested routines or if you
call routines from an interrupt. If it it does this, your program will crash
at some point during its operation. You can manually check to see if the
compiler has used more than 8 stacks by looking at the top of the .LST
file after each compilation.
For example, the program shown below uses #separate, and it uses
9 stack levels, which is very bad because the 16F819 only has 8 levels
available. Here is the top of the .LST file. Note how it lists the number
of stack levels used.
| Code: |
CCS PCM C Compiler, Version 4.107, xxxxx 09-Mar-12 11:08
Filename: C:\Program Files\PICC\Projects\PCM_Test\pcm_test.lst
ROM used: 39 words (2%)
Largest free fragment is 2009
RAM used: 5 (2%) at main() level
6 (2%) worst case
Stack: 9 locations
|
Here is the test program.
| Code: |
#include <16F819.H>
#fuses INTRC_IO, NOWDT, BROWNOUT, PUT, NOLVP
#use delay(clock = 4M)
//=======================
#separate
void func9(void)
{
char c;
c = 0x55;
}
//----------------------
#separate
void func8(void)
{
func9();
}
//----------------------
#separate
void func7(void)
{
func8();
}
//----------------------
#separate
void func6(void)
{
func7();
}
//----------------------
#separate
void func5(void)
{
func6();
}
//----------------------
#separate
void func4(void)
{
func5();
}
//----------------------
#separate
void func3(void)
{
func4();
}
//----------------------
#separate
void func2(void)
{
func3();
}
//----------------------
#separate
void func1(void)
{
func2();
}
//==================
void main()
{
func1();
while(1);
} |
|
|
 |
stinky
Joined: 05 Mar 2012 Posts: 99 Location: Central Illinois
|
|
Posted: Fri Mar 09, 2012 3:17 pm |
|
|
What kind of crash? PIC becomes unresponsive?
I only ask because you're right, I've gone past the stack limit, but I
haven't found a problem with the program yet. |
|
 |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Mar 09, 2012 3:29 pm |
|
|
If you use more than 8 stack locations, and because it's a circular stack,
you will over-write the return addresses for the initial calls to sub-routines.
So when the PIC returns to the caller, going backwards through the chain
of nested sub-routines, eventually it will retrieve a return address from the
stack, except it will be the wrong address. |
|
 |
stinky
Joined: 05 Mar 2012 Posts: 99 Location: Central Illinois
|
|
Posted: Fri Mar 09, 2012 3:42 pm |
|
|
Is the stack cleared when the PIC is powered down and up?
Or does it retain those values like eeprom? |
|
 |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Mar 09, 2012 4:29 pm |
|
|
The stack values and the stack pointer should fade away when the power
is removed, because it's RAM. I can't find any information about the
state after reset.
But it doesn't matter what happens during power-down or reset. That's
because upon power-up, the program will begin executing code, and when
it does its first CALL or interrupt, etc., it will place the return address on
the hardware stack. When it returns from the CALL, it will pop the return
address off the stack. The previous state before power-up or reset
doesn't matter. Also, because it's a circular stack, it doesn't even matter
where the stack pointer is, after reset. These comments apply to the
traditional 16F-series PICs (not enhanced 16F, or any other series). |
|
 |
stinky
Joined: 05 Mar 2012 Posts: 99 Location: Central Illinois
|
|
Posted: Mon Mar 12, 2012 7:21 am |
|
|
Let me see if I understand this.
On power up, it doesn't really matter where "stack level 1" was on the previous run; the PIC uses the first available stack and calls it 1.
Is that about right? |
|
 |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Mar 12, 2012 11:18 am |
|
|
I don't know the power-on-reset behavior of the hardware stack pointer
in the 16F-series. I was speculating. But I made an observation that
it doesn't matter whether or not the stack pointer is reset to the first
stack location. It would still work anyway, because it's a circular stack.
My comments above and in previous posts come from reading the PIC
data sheet and the Reference manual. If you want more opinions, then
do a Google search like this:
| Quote: |
site:microchip.com/forums circular stack -buffer
|
|
|
 |
stinky
Joined: 05 Mar 2012 Posts: 99 Location: Central Illinois
|
|
Posted: Mon Mar 12, 2012 12:14 pm |
|
|
| Thank you PCM programmer. |
|
 |
Ttelmah
Joined: 11 Mar 2010 Posts: 20161
|
|
Posted: Tue Mar 13, 2012 3:07 am |
|
|
It is perhaps worth 'commenting', that on latter chips, where the stack is more accessible, Microchip, _do_ tell us where it is set on a reset. On the 18F chips, they say that the top of stack location, resets to '0', which is a special location, containing 0, in the stack.
Generally, the RAM cells in the PIC, default to resetting to all 1's, so I'd guess this would be the reset value of the PIC16. This is possibly 'confirmed', since I have one of the old full hardware ICE systems, where Microchip made versions of the chips with the registers accessible through external hardware, and this wakes up with the stack pointer addressing the _last_ location in the stack, and the first write addresses the first location in the stack (the pointer updates _before_ data is written).
Best Wishes |
|
 |
|