CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Simple division not working

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
uni_student



Joined: 01 Aug 2007
Posts: 38
Location: AUckland, NEW ZEALAND

View user's profile Send private message Send e-mail MSN Messenger

Simple division not working
PostPosted: Wed May 12, 2010 3:55 am     Reply with quote

Hi there, getting unrealistic values for average velocity(avspd) on display. No problems getting current speed (tpspd) from gps and displaying it, but calculating average speed is not working. Am i missing something here or do i have to use some modulus method?? It is really bugging me, help please.


Code:

//intialise
int tpspd=0;
int avcnt=0;
int cycles=1;

//while(1) loop

  tpspd=atoi(temp);   //temp is velocity string from gps
  avcnt+= tpspd;           
  avspd = avcnt/cycles;
  cycles++;
  fprintf(LCD, "Current Speed  %i", tpspd);
  fprintf(LCD, "Average Speed  %i", avspd);

//

Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

PostPosted: Wed May 12, 2010 4:19 am     Reply with quote

it is most likely to do with the fact all vars are 8 bits.

What are values for tpspd (typical, min and max) ?
Does it work for x loops ?
After avcnt > 255 it will go horribly wrong.
After 255 cycles it will go horribly wrong.
uni_student



Joined: 01 Aug 2007
Posts: 38
Location: AUckland, NEW ZEALAND

View user's profile Send private message Send e-mail MSN Messenger

PostPosted: Wed May 12, 2010 4:46 pm     Reply with quote

Cheers, dude.
int16 made it work, knew it was something simple:)
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group