I'm using the MCBSTM32C evaluation board. I would like to calculate the angle of the board in relation to the gravity vector. I have no prolems reading the accelerometer. I basically have the gravity vector components as int values. To do what I want to do I have to calculate the length of the vector using the sqrt function in math.h. The sqrt function expects double numbers as input and produces a double result as far as I can tell. I than would like to print the result onto the screen. As soon as I print to result onto the screen the program crashes. I'm not sure if it is the sqrt function which causes the trouble or the print function. Below is some code that leads to a crash. I can print any other value to screen just not double.
double acc_length; acc_length = sqrt(45.98745); sprintf(print_LCD[0], " %lf ", acc_length); GLCD_DisplayString (1, 0, 1, print_LCD[0]);
Thanks, Karsten
It looks like it is a stack alignment problem. Google search finds similar problems. The program works perfectly without the sprintf line. Just out of interest - how do you align the stack correctly? Are there any uVision specific instructions?
I don't really have to print a double number. Couldn't see the forrest from all the trees late last night. I would like to print the angle in degrees to the screen and would be happy with int resolution since the accelerometer only has 8 bit precision anyway. The 'printing double' problem therefore goes away and it becomes a type casting problem which I'm also struggling with. I would like to use the sqrt and acos functions (probably don't have to) to calculate the angle. Both require double variables as input and produce double output. So I would like to type cast my int vector components to double so that sqrt works and then type cast the double result from acos to int and print it to screen. Will give it a go later in the week.
Why do you think that?
Again, you haven't shown the definition of print_LCD, but are you sure this is correct:
sprintf( print_LCD[0], " %lf ", acc_length );
Are you sure that shouldn't be:
sprintf( print_LCD, " %lf ", acc_length );
Or:
sprintf( &print_LCD[0], " %lf ", acc_length );
The same line of code works if I replace %lf with %d and the double variable with an int variable. Print_LCD is declared as a matrix since I want to print several lines of data to the screen and this way it is easier to keep track of which data is printed to which line. The declaration is
char print_LCD [5][50];
I'm using the same functions in other places with int and float without problems. It is just that printing of double leads to crashes for some reason which is consistent with what other people reported if the stack is not aligned correctly. Since I only want to print int anyway I should be able to use some type casting as a work around without fixing the stack alignment problem (if that is really the problem).
two more possibilities:
1) you may need to increase stack size to accommodate printf(). read the manual on that; 2) you may need to turn on float support for printf(). read the manual on that.