Society of Robots - Robot Forum
Software => Software => Topic started by: Resilient on March 31, 2009, 08:01:50 PM
-
Does anyone know of a good tutorial talking about doing math with AVR?
I am having problems trying to deal with signed vs unsigned and 16 vs 32bit numbers and even more problems trying to print them out with rprintf.
I have an equation where I am trying to do this operation with these data types:
int16_t = ((uint32_t - uint32_t)*int16_t)/1000
And I am getting some funky results. Do I need to do some type casting or something? I seem to get normal positive results, but negative results are far to large.
This part sometimes is very large '((uint32_t - uint32_t)*int16_t)' like around 120,000. Might that cause problems?
Thanks
Justin
-
Ive had problems with avr-gcc when using a mix of variable sizes.
I'd recommend that you try to break it down into similar variables sizes eg:
// here are your 3 variables
uint32_t v1;
uint32_t v2;
uint16_t v3;
// Subtract the 32 bit stuff to start with
v1 = v1 - v2;
// Multiply the 32 bit answer by a 16 bit number and store 32 bit answer
v2 = v1 * v3;
// Divide by 1000(Unsigned) and store as 16 bit result
v3 = v2 / 1000U;
return v3;
That, or something similiar ::), should work.
avr-gcc seems VERY flakey when trying to work with a single expression of multiple data sizes. And it will mess it up regardless of any optimisation settings.