Continue to Site

stack overflow in pic

Status
Not open for further replies.

shreyas_patel21

Full Member level 3
Full Member level 3
Joined
Jan 4, 2011
Messages
181
Helped
15
Reputation
30
Reaction score
14
Trophy points
1,298
Activity points
2,325
I am using pic18f66k90 with MPLAB IDE and C18 compiler.

My program stuck at some point and controller does not give any response.

All the thing in my code works fine up to some point but when I add some more code, the problem occurs.

some time I solved the problem by enabling all optimization from options.

and some time my problem was solved by using following code:
Code:
if((state==STATE_VOLTAGE&&VtgIndex==SelectedVtg)||(OverloadFlag==1))
            {

            }
            else if(state==STATE_UNKNOWN)
            {
            }
instead of following code:
Code:
if((state==STATE_VOLTAGE&&VtgIndex==SelectedVtg)||(OverloadFlag==1)||(state==STATE_UNKNOWN))
            {

            }


Can it be a stack overflow issue?

How can I identify stack overflow? is there any option in MPLAB?
 

A simulation in MPLAB will normally halt with a warning of stack underflow or overflow so it's unlikely to be the issue.

I'm suspicious of the bracketing, should it be ((state==STATE_VOLTAGE) && (VtgIndex==SelectedVtg)) ??

Brian.
 

thank you brian,

it was my mistake it should be ((state==STATE_VOLTAGE) && (VtgIndex==SelectedVtg))

but the same problem here also.
when I put last condition in else if, i.e.
else if(state==STATE_UNKNOWN)
{
}
it worked.

i will try to check in simulation.

is there any option to define the stack or extend its limit?
 

You cannot extend the size of the hardware stack without creating your own software stack version of it and manipulating return addresses onto it as and when you need to, but you can view the hardware stack by going to view->hardware stack which should allow you to see how many return addresses you have on there at anytime meaning you should be able to see if you have an overflow.
When a PIC is reset via a stack overflow there is a bit somewhere in memory that becomes set indicating the overflow.

Pete
 

thanks pete,

I found that there was problem in my software stack, I extended the size of software stack from linker script and for now there is no problem of not responding.
 

hi friends.

I have read this post and got confused about providing return address to stack.

Can any one help me to solve this problem. Because, whenever i m executing this program in proteus, i get a message saying "stack overflow executing call instruction"

I didnt get any of the meaning. Also, i am little bit weak in english.

Please help me to overcome this problem.

Here is the program.


#include<pic.h>
#include"delay.c"
__CONFIG(0x1932);
void switchoff(void);
void filltoptank1(void);
void filltoptank2(void);
void filltoptank3(void);
void fillbottomtank1(void);
void fillbottomtank2(void);
void fillbottomtank3(void);
void filltoptank13(void);
void filltoptank15(void);
void filltoptank35(void);
void check(void);
void fillalltop(void);
void main()
{
TRISB=0x3F; //FIRST 6 PINS OF PORT B IS ASSIGNED TO BE INPUT PINS
TRISC=0x3F; //FIRST 6 PINS OF PORT C IS ASSIGNED TO BE INPUT PINS
PORTB=0x00;
PORTC=0x00;
RBPU=1;
while(1)
{
check();
}
}
void check(void)
{

{
if(RB1==0&&RB3==0&&RB5==0) //Switch off the 3 MOTORS
{
switchoff(); //SWITCHES OFF ALL THE MOTOR.
}
else if(RB1==1&&RB3==0&&RB5==0) //if top tank 1 is empty
{
filltoptank1(); //SWITCH ON 1st MOTOR.
}
else if(RB1==0&&RB3==1&&RB5==0) //if top tank 2 is empty
{
filltoptank2(); //SWITCH ON 2nd MOTOR.
}
else if(RB1==0&&RB3==0&&RB5==1) //if top tank 3 is empty
{
filltoptank3(); //SWITCH ON 3rd MOTOR.
}
else if(RB1==1&&RB3==1&&RB5==0) //if top tank 1 and 2 is empty
{
filltoptank13(); //SWITCH ON 1st and 2nd MOTOR.
}
else if(RB1==1&&RB3==0&&RB5==1) //if top tank 1 and 3 is empty
{
filltoptank15(); //SWITCH ON 1st and 3rd MOTOR.
}
else if(RB1==0&&RB3==1&&RB5==1) //if top tank 2 and 3 is empty
{
filltoptank35(); //SWITCH ON 2nd and 3rd MOTOR.
}
else if(RB1==1&&RB3==1&&RB5==1) //if top tank 1, 2 and 3 is empty
{
fillalltop(); //Switch ON all the MOTORS.
}



}

}

void filltoptank1(void)
{
do
{

RB6=1;
}while(RB1==1&&RB3==0&RB5==0);
RB6=0;
RC6=0;
RC7=0;
check();
}



void filltoptank2(void)
{
do
{

RC6=1;

}while(RB1==0&RB3==1&&RB5==0);
RB6=0;
RC6=0;
RC7=0;
check();
}

void filltoptank3(void)
{
do
{

RC7=1;

}while(RB1==0&RB3==0&RB5==1);
RB6=0;
RC6=0;
RC7=0;
check();
}

void filltoptank13(void)
{
do
{

RB6=1;
RC6=1;

}while(RB1==1&&RB3==1&&RB5==0);
RB6=0;
RC6=0;
RC7=0;
check();
}

void filltoptank15(void)
{
do
{

RB6=1;
RC7=1;

}while(RB1==1&&RB3==0&RB5==1);
RB6=0;
RC6=0;
RC7=0;
check();
}

void filltoptank35(void)
{
do
{

RC6=1;
RC7=1;

}while(RB1==0&RB3==1&&RB5==1);
RB6=0;
RC6=0;
RC7=0;
check();
}

void fillalltop(void)
{
do
{

RB6=1;
RC6=1;
RC7=1;

}while(RB1==1&&RB3==1&&RB5==1);
RB6=0;
RC6=0;
RC7=0;
check();
}

void switchoff(void)
{
do
{
RB6=0;RC6=0;RC7=0;
check();
}while(RB1==0&&RB3==0&&RB5==0);
}



i dunno how to change the hardware stack. Here is the photo,.

Please tell me how to correct the hardware stack to simulate the program without any error.
 

First - upgrade to MPLAB 8.73

The hardware stack is a silicon structure, it is fabricated on the silicon die and there is no way you can adjust or extend it.

Simulate your code one line at a time and watch the stack window. It should show the address added to the stack every time you 'call' a subroutine and it should show the stack pointer moving up every time you return from the subroutine. The idea is to return after each call. You get a stack overflow when one call has called another and so on, 8 times without returning. I suspect one of your calls is not returning so the stack pointer doesn't move up again.

It is possible to make a "Software stack" to do the same as the hardware one but it is quite complicated and much slower than the hardware one.

Brian.
 

Thanks for your valuable reply, betwixt. :)

I have done a mistake and now, i rectified it. I didnt give delay for each function call and now, i did it.
And my Circuit is working correct without any error or warning.

:) Thank you very much. :)
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top