Using Math function on DEMO9S08QG8

cancel
Showing results for 
Search instead for 
Did you mean: 

Using Math function on DEMO9S08QG8

2,156 Views
left
Contributor I

Hi all, 

 

I want to use sine, cosine function in my application. I have created a new project and included ansitfs.lib. When I called the sin function, it halted in modf() inside function sincos().

 

What should I do to call the trigonometric functions?

 

CPKwok 

Labels (1)
0 Kudos
6 Replies

80 Views
jag
Contributor IV

Hi,

 

trigonometric functions, as others had already told you, are very resource hungry.

 

One way to solve this, if you don't need speed and/or much precision, is to use the CORDIC algorythm. It's very simply and give very good results.

 

And don't use floats if you really don't need them (integer math rules!! :smileywink: ) 

 

Hope this helps

 

Bye Jack 

0 Kudos

80 Views
CrasyCat
Specialist III

Hello

 

Are you debugging on real hardware or on simulator?

Can you provide a code snippet or the project showing the issue?

 

CrasyCat

0 Kudos

80 Views
left
Contributor I

Hi,

 

I debug on real hardware, the demo board DEMO9SQG8.

 

I created my project using the "New Project" Wizard of CodeWarrior IDE (version 5.7.0).

+ Device and connection : MC9S08QG8 , P&E multilink/ Cyclone Pro 

+ Processor Expert : Device Initialization

+ C/C++ option: None, tiny, None 

 

I changed the linked library from ansitis.lib to ansitfs.lib and added 3 lines of code.

 

This is the code of main.c

========================================================

#include <hidef.h> /* for EnableInterrupts macro */

#include "derivative.h" /* include peripheral declarations */

#include "math.h// (1)

 

void MCU_init(void); /* Device initialization function declaration */

 

void main(void) {

  double x=0; // (2)

 

  MCU_init(); /* call Device Initialization */

  x = sin(30); // (3)

 

  for(;:smileywink: {

  } /* loop forever */

======================================================== 

 

 

CPKwok 

0 Kudos

80 Views
Lundin
Senior Contributor IV
1) QG8 is a tiny microcontroller with limited memory, and float numbers will slaughter every resource on that poor mcu, especially the stack and the flash. Have you checked how much memory these routines consume in the .map file?

2) The ISO C function sin() expects radians.
0 Kudos

80 Views
left
Contributor I

Yes, the problem should be ram lackage.

Does anyone know how much ram is required to call these functions

 

CPKwok 

0 Kudos

80 Views
bigmac
Specialist III

Hello,

 

It is possible to ascertain stack usage for a function using full chip simulation.  Set up a test project using a much larger device to give sufficient RAM capacity, and then set the remaining unused stack to a known (non-zero) value, just prior to stepping over the trigononmetric function. Then examine the stack contents to determine the lowest address that still contains the known value.  Subtract this from the current stack pointer, and you will have the stack usage for the function.

 

For initialising the stack contents, the following function might be of use.  The first four positions below the current stack pointer are not initialised.  The parameter lolimit represents the address of  the bottom of the stack.

 

 void clr_stack( word lolimit, byte val)
{
  __asm {
        tsx
        lda  val
LOOP:   aix  #-1
        sta  ,x
        cphx (lolimit)
        bhs  LOOP
  }
}

 

Regards,

Mac

 

0 Kudos