Author Topic: AVR C and Math  (Read 2603 times)

0 Members and 1 Guest are viewing this topic.

Offline ResilientTopic starter

  • Full Member
  • ***
  • Posts: 111
  • Helpful? 4
AVR C and Math
« 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
« Last Edit: March 31, 2009, 08:14:34 PM by Resilient »

Offline Webbot

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 2,132
  • Helpful? 108
Re: AVR C and Math
« Reply #1 on: March 31, 2009, 08:15:04 PM »
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.
Webbot Home: http://webbot.org.uk/
WebbotLib online docs: http://webbot.org.uk/WebbotLibDocs
If your in the neighbourhood: http://www.hovinghamspa.co.uk

 


Get Your Ad Here

data_list