Problems with cosf() and sinf()

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Problems with cosf() and sinf()

4,326 Views
thisisbrians
Contributor I
Using CodeWarrior IDE for HC(S)08 version 5.7.0, with processor expert plug-in. Code is in C and target is the Freescale MC908GP32. The problem is that whenever I try to use cosf() or sinf() (or sin() or cos() for that matter...) the microcontroller goes crazy (random pins going high and low) and eventually the main loop just stops running altogether. As soon as I remove the lines of code containing the trig functions everything else works fine. Anyone know what's up???

Code:

#include "math.h"

float theta=3.14159; //it doesn't matter what value
float x;

for(;; ){
x=sinf(theta); //or x=cosf(theta), once again it doesn't matter
}

P.S. It's urgent!!! This is for a school project due by October, see http://www.geocities.com/thisisbrians/ for details.
Labels (1)
Tags (1)
0 Kudos
10 Replies

1,038 Views
UK_CF_FAE
NXP Employee
NXP Employee
Watchdog (COP) on or off???
0 Kudos

1,038 Views
thisisbrians
Contributor I
No Watchdog/COP is running on this project. Instead I have a snippet of code in the main loop that blinks an LED at a set frequency, 1 Hz. But when I have the cosf() or sinf() functions, the LED blinks sporadically and eventually completely stops blinking, staying either on or off.
0 Kudos

1,038 Views
J2MEJediMaster
Specialist I
Can you do pure floating-point arithmetic operations without trouble? That should pin the problem down to either the FP math or the trig function calls. What libraries are you linking in?

---Tom
0 Kudos

1,038 Views
thisisbrians
Contributor I

J2MEJediMaster wrote:
Can you do pure floating-point arithmetic operations without trouble? That should pin the problem down to either the FP math or the trig function calls. What libraries are you linking in?

---Tom




This project is for a robot controller board, and I am running a PID controller in the software to control the motors using plenty of floating point arithmetic with no trouble at all. I am linking only the ansif.lib library. Also, I get no errors or warnings with the trig function calls, they just simply mess up the execution of the program.

Message Edited by thisisbrians on 2006-07-31 08:26 AM

0 Kudos

1,038 Views
J2MEJediMaster
Specialist I
OK, so it's a library problem. Go the CodeWarrior's lib directory and see what other libraries there are in that directory. Look for one with a 'b' or 'x' or 'l', like ansibf.lib, ansixl.lib, and similar. (Caveat, I'm basing this on a quick scan of my HC12 lib directory.) To reduce the memory footprint, some of the ansi libraries don't include the trigonometric functions. You need to link in a library that does. Of course, this doesn't explain why you wouldn't be getting linker errors.

---Tom
0 Kudos

1,038 Views
thisisbrians
Contributor I
Okay, I know that the library I'm linking in has the trig functions in it, because upon introducting another library to the project, the linker tells me that there are duplicate definitions for sin(), cos(), etc. And running the program produces the same results even with an entirely different library. I'm stumped...

Message Edited by thisisbrians on 2006-07-31 09:40 AM

0 Kudos

1,038 Views
CompilerGuru
NXP Employee
NXP Employee
I think there are two main reasons for such a failure.
One is the COP, it has already been ruled out.
The second one is the stack size. Try to increase the stack size in the prm file. Check if the values on the bottom area of the stack get overwritten.

Daniel
0 Kudos

1,038 Views
rhinoceroshead
Contributor I
It doesn't sound like a COP problem to me at all since the flashing light goes crazy, then stops.  This seems like a stack issue.  That would explain the randomness, anyway.  The floating point operations that you're doing are probably using up the last bit of stack space and then one of the floating op functions is looking to the corrupted stack to find the return address and that sends the CPU executing 'code' from who-knows-where.  I think CW defaults to only 100 bytes of stack by default.  Try editing your *.prm file and change the line that says "STACKSIZE 0x100" to "STACKTOP 0x3FFF" and then edit the line that says "RAM = READ_WRITE 0x2000 TO 0x3FFF" to "RAM = READ_WRITE 0x2000 TO 0x3FFE".
0 Kudos

1,038 Views
rhinoceroshead
Contributor I
Oops.  I posted too late.  :smileyhappy:
0 Kudos

1,038 Views
thisisbrians
Contributor I
So, I went in and doubled the stack from 128 to 256 bytes, and it appears to be working fine as such. I have yet to test whether the trig functions are returning the correct values, but if I find later that they do I will submit another post to the thread. Thanks for the help!
0 Kudos