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

Problems with Division Result = 0.0000000 when debugging

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








Problems with Division Result = 0.0000000 when debugging
PostPosted: Sat Apr 01, 2006 2:20 am     Reply with quote

Hi all,
I have following problem: the division does not work. The result is always 0. I have a ICD2 module with the testboard. Connecting,and debugging is working, I can set breakpoints... but always 0!
The value from adc is greater than 0. Do I have to include something like <math.h>? I tried this but doesn�t work
Here my short program:


#include<16F877.h>

#device adc=10

long PULSWEITE_LOW;
long PULSWEITE_HIGH;
long DIFFERENZ;

float TESTZAHL;
float TEILER;

void main()
{

setup_ADC(ADC_CLOCK_INTERNAL);
setup_ADC_PORTS(RA0_ANALOG);
set_adc_channel(0);

PULSWEITE_LOW = 0;
PULSWEITE_HIGH = 0;
TEILER = 0;
TESTZAHL = 0;

for(;;)
{



PULSWEITE_LOW = read_adc();

DIFFERENZ = PULSWEITE_LOW - PULSWEITE_HIGH;

TEILER = 255 / DIFFERENZ;
TESTZAHL = TEILER * 255;
}

}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Apr 01, 2006 3:14 am     Reply with quote

You have to tell the compiler to do floating point division.
In your code it's doing integer division, so that explains why you get 0.

The compiler will do floating point math if at least one of the numbers
is floating point in the division equation. So you should make the "255"
into a floating point number as shown below in bold:
Quote:
TEILER = 255.0 / DIFFERENZ;
Guest








PostPosted: Sat Apr 01, 2006 4:19 am     Reply with quote

Jipieee, yes this works! Such a simple error- a huge fault

Thanks a lot!!
migsantiago



Joined: 01 Apr 2006
Posts: 3

View user's profile Send private message Visit poster's website

PostPosted: Sat Apr 01, 2006 8:07 pm     Reply with quote

You can also use:

TEILER = (float)255 / DIFFERENZ;

Casting forces CCS to get a float type value.
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