CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug Reports on this forum. Send them to support@ccsinfo.com

stack undeflow on 18F452, example included

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Kurt Franke
Guest







stack undeflow on 18F452, example included
PostPosted: Tue Dec 17, 2002 11:48 am     Reply with quote

I traced an intermittant reset in my application to a
relatively simple line of math. Below I have included
a sample program with illustrates this. It causes a
stack underflow when simulated in MPLAB and continually
resets when running on my hardware at the line

g = f - ADC_TO_U(b>>4);

Has anyone else seen anything like this before?
I have tested this with PCW 3.129 (current version).

Any help greatly appreciated. I will send this to CCS also.

--- test.c

#include <18F452.h>
#include <math.h>

// SET CLOCK FOR WHATEVER YOU HAVE
#use delay (clock = XXXXXXX)
#fuses HS, NOPROTECT, NOWDT, PUT, NOBROWNOUT, NOLVP

#use rs232 (baud=9600, xmit=PIN_C6, rcv=PIN_C7)

//#define DEBUG printf // use to print messages
#define DEBUG // use to skip messages

#define OFFSET (50)
#define ADC_TO_U(x) (((x) <= OFFSET)? 0.0 : sqrt((float)((x)-OFFSET)))

void main() {

int16 a,b;
float f,g;

DEBUG ("Starting : cause 0x\%x\r\n", restart_cause());

a = 815;
b = 815;
DEBUG ("a = \%lu\r\n", a);
DEBUG ("b = \%lu\r\n", b);
f = ADC_TO_U(a>>4);
DEBUG ("got f\r\n");
DEBUG ("f = \%f\r\n", f);
g = f - ADC_TO_U(b>>4); // CRASH!
DEBUG ("got g\r\n");
DEBUG ("g = \%f\r\n", g);

while (1);
}
___________________________
This message was ported from CCS's old forum
Original Post ID: 10094
chas
Guest







Re: stack undeflow on 18F452, example included
PostPosted: Tue Dec 17, 2002 1:02 pm     Reply with quote

:=I traced an intermittant reset in my application to a
:=relatively simple line of math. Below I have included
:=a sample program with illustrates this. It causes a
:=stack underflow when simulated in MPLAB and continually
:=resets when running on my hardware at the line
:=
:= g = f - ADC_TO_U(b>>4);
:=
:=Has anyone else seen anything like this before?
:=I have tested this with PCW 3.129 (current version).
:=
:=Any help greatly appreciated. I will send this to CCS also.
:=
:=--- test.c
:=
:=#include <18F452.h>
:=#include <math.h>
:=
:=// SET CLOCK FOR WHATEVER YOU HAVE
:=#use delay (clock = XXXXXXX)
:=#fuses HS, NOPROTECT, NOWDT, PUT, NOBROWNOUT, NOLVP
:=
:=#use rs232 (baud=9600, xmit=PIN_C6, rcv=PIN_C7)
:=
:=//#define DEBUG printf // use to print messages
:=#define DEBUG // use to skip messages
:=
:=#define OFFSET (50)
:=#define ADC_TO_U(x) (((x) <= OFFSET)? 0.0 : sqrt((float)((x)-OFFSET)))
:=
:=void main() {
:=
:= int16 a,b;
:= float f,g;
:=
:= DEBUG ("Starting : cause 0x\%x\r\n", restart_cause());
:=
:= a = 815;
:= b = 815;
:= DEBUG ("a = \%lu\r\n", a);
:= DEBUG ("b = \%lu\r\n", b);
:= f = ADC_TO_U(a>>4);
:= DEBUG ("got f\r\n");
:= DEBUG ("f = \%f\r\n", f);
:= g = f - ADC_TO_U(b>>4); // CRASH!
:= DEBUG ("got g\r\n");
:= DEBUG ("g = \%f\r\n", g);
:=
:= while (1);
:=}

I'm wondering if your #define OFFSET (50) is defaulting to an int and causing problems. Try changing the define to #define OFFSET (int16)(50) and see if it helps.
___________________________
This message was ported from CCS's old forum
Original Post ID: 10098
Kurt Franke
Guest







further investigations...
PostPosted: Tue Dec 17, 2002 2:17 pm     Reply with quote

Here's a simpler program:

#include <18F452.h>

#nolist
#include <math.h>
#list

#fuses HS, NOPROTECT, NOWDT, PUT, NOBROWNOUT, NOLVP

#define OFFSET ((int16)(50))
#define ADC_TO_U(x) (((x) <= OFFSET)? 0.0 : 2.0)

void main() {

int16 a,b;
float f,g;

a = 815;
b = 815;
f = ADC_TO_U(a>>4);
g = f - ADC_TO_U(b>>4);

while (1);
}

This program goes into an infinite loop (I mean, before the
while(1) statement).

Looking at the listing there is a bad goto statement in the
g= line that jumps back into the middle of the f= line.

OK. Now things get very weird because if you don't include
math.h, this program seems to work. Huh??

(Listing of assembly of g= line with bad goto statement marked)

.................... g = f - ADC_TO_U(b>>4);
03A0: RRCF 09,W
03A2: MOVWF 13
03A4: RRCF 08,W
03A6: MOVWF 12
03A8: RRCF 13,F
03AA: RRCF 12,F
03AC: RRCF 13,F
03AE: RRCF 12,F
03B0: RRCF 13,F
03B2: RRCF 12,F
03B4: MOVLW 0F
03B6: ANDWF 13,F
03B8: BTFSS FD8.2
03BA: GOTO 03D4
03BE: MOVF 12,W
03C0: SUBLW 32
03C2: BTFSS FD8.0
03C4: GOTO 03D4
03C8: CLRF 00
03CA: CLRF 01
03CC: CLRF 02
03CE: CLRF 03
03D0: GOTO 0370 <---- 370 is in the middle of the previous c statement.
03D4: MOVLW 80
03D6: MOVWF 00
03D8: CLRF 01
03DA: CLRF 02
03DC: CLRF 03
03DE: BSF FD8.0
03E0: MOVFF 0D,17
03E4: MOVFF 0C,16
03E8: MOVFF 0B,15
03EC: MOVFF 0A,14
03F0: MOVFF 03,1B
03F4: MOVFF 02,1A
03F8: MOVFF 01,19
03FC: MOVFF 00,18
0400: GOTO 0004
0404: MOVFF 03,11
0408: MOVFF 02,10
040C: MOVFF 01,0F
0410: MOVFF 00,0E
....................
___________________________
This message was ported from CCS's old forum
Original Post ID: 10102
Kurt Franke
Guest







Re: stack undeflow on 18F452, example included
PostPosted: Tue Dec 17, 2002 5:24 pm     Reply with quote

:=I'm wondering if your #define OFFSET (50) is defaulting to an int and causing problems. Try changing the define to #define OFFSET (int16)(50) and see if it helps.

Nope, doesn't seem to change anything.

-Kurt
___________________________
This message was ported from CCS's old forum
Original Post ID: 10105
chas
Guest







Re: stack undeflow on 18F452, example included
PostPosted: Wed Dec 18, 2002 11:20 am     Reply with quote

:=:=I'm wondering if your #define OFFSET (50) is defaulting to an int and causing problems. Try changing the define to #define OFFSET (int16)(50) and see if it helps.
:=
:=Nope, doesn't seem to change anything.
:=
:=-Kurt

Kurt,
I tried it and several other permutations, and got the same results as you. It didn't work (strange goto) with the math.h included, even though no calls to math.h functions were used in the simple program. But it did work without the math.h included. Very strange!?!

The next thing to try might be ditching the macro and rewriting as a function.

-chas
___________________________
This message was ported from CCS's old forum
Original Post ID: 10122
chas
Guest







Re: stack undeflow on 18F452, example included
PostPosted: Wed Dec 18, 2002 8:45 pm     Reply with quote

:=:=I'm wondering if your #define OFFSET (50) is defaulting to an int and causing problems. Try changing the define to #define OFFSET (int16)(50) and see if it helps.
:=
:=Nope, doesn't seem to change anything.
:=
:=-Kurt

Try this and see if it works:

#include <18F452.h>

#include <math.h>

#fuses HS, NOPROTECT, NOWDT, PUT, NOBROWNOUT, NOLVP

#define OFFSET ((int16)(50))
// #define ADC_TO_U(x) (float)(((x) <= OFFSET)? 0.0 : 2.0)
float ADC_TO_U(int16 x)
{
if (x <= OFFSET)
{
return 0.0;
}
else
{
return 2.0;
}
}

void main() {

int16 a,b;
float f,g;

a = 815;
b = 815;
f = ADC_TO_U(a>>4);
g = f - ADC_TO_U(b>>4);

while (1);
}
___________________________
This message was ported from CCS's old forum
Original Post ID: 10130
Kurt Franke
Guest







Re: stack undeflow on 18F452, example included
PostPosted: Thu Dec 19, 2002 8:50 am     Reply with quote

:=Try this and see if it works:
:=

:= (my macro converted to function using if/else instead of ?:)

--
I'm pretty certain this will work. CCS told me the problem
was arose from using a conditional inside a larger expression.
I changed the g = line to:

temp = ADC_TO_U(b);
g = f-temp;

and this works.

.. Unfortunately I am now having a different problem elsewhere
(more on this later)

Thanks for taking a look at this chas,

--Kurt
___________________________
This message was ported from CCS's old forum
Original Post ID: 10138
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group