Pumpkin, Inc.

Pumpkin User Forums

PIC 18 and MPLAB-C18 Problem

For issues specific to Microchip's PICmicro® MCUs, including compilers (e.g. HI-TECH PICC & PICC-18, Microchip MPLAB®-C18) and IDEs (e.g. Microchip MPLAB®).

PIC 18 and MPLAB-C18 Problem

Postby onset computer » Wed Nov 20, 2002 7:42 am

Here is a code fragment, that lives inside of a task that declares several local variables.
void task(void)
char try_counter,state;

try_counter = 5;
state = 0;

while (1)
while (try_counter)

Using MPLAB, I set a break point just before the call to OS_Delay(), which is just a macro to call OSDelay() and OS_Yield(), and observe the values of FSR0, 1 and 2. With MPLAB C compiler, FSR1 is the software stack pointer, and FSR2 is a frame pointer.

I change the break point to the try_counter-- statement, and when OS_Delay() returns, FSR1 and FSR2 have changed, and thus, variables local to the task are now bogus. Apparently, OS_Delay() changes FSR1 and FSR2, and does not restore them when returning to the main line code.

What am I doing wrong? I tried both sfc18sna.lib and sfc18sfa.lib (making the appropriate changes in salvocfg.h)


[This message has been edited by aek (edited November 21, 2002).]

onset computer
Posts: 1
Joined: Wed Nov 13, 2002 12:00 am
Location: Bourne, MA USA

Re: PIC 18 and MPLAB-C18 Problem

Postby aek » Thu Nov 21, 2002 8:38 am


You need to declare try_counter and state as static char.


Because when the task reaches OS_Delay(), it context-switches. That means it unloads the stack, goes back to the scheduler (OSSched()), and becomes "dormant" until the delay expires. In the meantime, other tasks run and other functions are executed, all using the stack, and hence overwriting your on-stack local auto variables try_counter and state.

So, just make them static, since they need to persist across context switches.

Note that you can use non-static local auto variables inside tasks in places where persistence across context switches is not necessary ...


Posts: 1888
Joined: Sat Aug 26, 2000 11:00 pm

Return to PICmicro MCUs

Who is online

Users browsing this forum: No registered users and 3 guests