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 support@ccsinfo.com

quick math question

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



Joined: 24 Jun 2005
Posts: 206

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

quick math question
PostPosted: Sat Feb 25, 2006 8:01 pm     Reply with quote

Hi All, i need to the the following sum in C.

CO2 = 3 * KH * 10( 7-pH )

To do this i have tried:

Code:

currentph = 7.8;
currentkh = 4;
phsum = 7 - currentph;
currentco2 = 3 * currentkh * pow(10,phsum);
 


but it will always give back strange numbers, eg number that have nothing in common with what i am doing. Can sombody help??

Thanks, Mark
kender



Joined: 09 Aug 2004
Posts: 768
Location: Silicon Valley

View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger

PostPosted: Sat Feb 25, 2006 8:30 pm     Reply with quote

I don't see the problem with the code you have posted, but the problem might exist elsewhere.

Please post also:
- Declarations of variables
- Your erroneous results
- Inputs that produce these erroneous results
- type of PIC you are writing for
jecottrell



Joined: 16 Jan 2005
Posts: 559
Location: Tucson, AZ

View user's profile Send private message

PostPosted: Sat Feb 25, 2006 8:51 pm     Reply with quote

Try some of these additions to your code to circumvent the possibility of CCS 'features'.

Code:


float phsum     = 0.0;
float currentph = 7.8;
float currentkh = 4.0;

phsum = 7.0 - currentph;
currentco2 = 3 * currentkh * pow(10,phsum);

printf("currentC02 = %7.2f\n\r", currentco2 );



I can't guarantee my approach is the correct one, but I bet this will probably spark a lot of good posts from the pros on what does and does not work with mixing of data types and the printf problems inherent in some of the later versions of the CCS compiler. So, with that said, your problem is most likely caused by either data types or printf formats, and without a code snippet that clarifies anything having to do with those problems, you probably won't get a very complete answer.

Good luck,

John
Markdem



Joined: 24 Jun 2005
Posts: 206

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

PostPosted: Sun Feb 26, 2006 2:30 am     Reply with quote

Thanks for the replys guys.
I have now found out that if i do the following, it all works fine

Code:

currentco2 = 3 * currentkh * pow(10.0,-1.0);


but as soon as i change it the the following, all i get as a output is a big fat 0

Code:

currentco2 = 3 * currentkh * pow(10.0,phsum);


Here is the whole code.


Code:

#include <16F88.h>

#FUSES NOWDT, INTRC_IO, NOPUT, NOMCLR, NOBROWNOUT, NOLVP, NODEBUG, NOPROTECT, NOFCMEN, NOIESO, NOWDT
#use delay(clock=4000000)
#use rs232(baud=2400,parity=N,bits=8,xmit=PIN_B5,rcv=PIN_B2)

#include<math.h>
#include<ds1820hood.c>
#include<ds1820water.c>

int serialreq, i;
int currentph;
long readadc, currentwatertemp, currenthoodtemp, currentco2i;
float mathtemp, currentkh, currentco2, phsum;

void tempwork()
   {
      currenthoodtemp = onewire_ds1822_read_temp_c_lite_hood();
      currentwatertemp = onewire_ds1822_read_temp_c_lite_water();
   }



void phwork()
   {
//      readadc = Read_ADC();   
//      delay_ms(200);
//      mathtemp = (readadc / 21.7659) * 100;
//      currentph = (9900 - mathtemp) / 100;
   
           currentph = 8;

      phsum = 7.0 - currentph;
      currentco2 = 3 * currentkh * pow(10.0,phsum);
      currentco2i = currentco2 * 10;
   }



void main ()
{
   
   
   setup_port_a(ALL_ANALOG);                              //setup adc
      setup_adc(ADC_CLOCK_INTERNAL);
      set_adc_channel(2);   
   
   enable_interrupts(INT_RDA);
   enable_interrupts(GLOBAL);
   

      
   while (TRUE)
      {
      
         output_low(pin_b4);
         tempwork();
         phwork();
                        output_high(pin_b4);
         delay_ms(2500);
      
      }
}


Thanks very much.

Mark
kender



Joined: 09 Aug 2004
Posts: 768
Location: Silicon Valley

View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger

PostPosted: Sun Feb 26, 2006 2:46 am     Reply with quote

In your complete code I could not find a place where you initialize currentkh variable. Try this:

Code:

      currentph = 8.0; // force to float
      currentkh  = 1.0; // or whatever

      phsum = 7.0 - currentph;
      currentco2 = 3.0 * currentkh * pow(10.0,phsum);


Also I couldn't find your calls to printf(). Do you stop the execution at the break point to view the variables?
Markdem



Joined: 24 Jun 2005
Posts: 206

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

PostPosted: Sun Feb 26, 2006 3:19 am     Reply with quote

Hi kender, thanks for you help.

You could not find when i define currentkh because i am no thinking very well today. Embarassed

Lets try again

Code:

#include <16F88.h>

#FUSES NOWDT, INTRC_IO, NOPUT, NOMCLR, NOBROWNOUT, NOLVP, NODEBUG, NOPROTECT, NOFCMEN, NOIESO, NOWDT
#use delay(clock=4000000)
#use rs232(baud=2400,parity=N,bits=8,xmit=PIN_B5,rcv=PIN_B2)

#include<math.h>
#include<ds1820hood.c>
#include<ds1820water.c>

int serialreq, i;
int currentph;
long readadc, currentwatertemp, currenthoodtemp, currentco2i;
float mathtemp, currentkh, currentco2, phsum;

void tempwork()
   {
      currenthoodtemp = onewire_ds1822_read_temp_c_lite_hood();
      currentwatertemp = onewire_ds1822_read_temp_c_lite_water();
   }



void phwork()
   {
//      readadc = Read_ADC();   
//      delay_ms(200);
//      mathtemp = (readadc / 21.7659) * 100;
//      currentph = (9900 - mathtemp) / 100;
      
      phsum = 7.0 - 8.0;
      currentco2 = 3 * currentkh * pow(10.0,-1.0);
      currentco2i = currentco2 * 10;
                printf("co2 =%u",currentco2i);
   }



void main ()
{
   setup_port_a(ALL_ANALOG);                              //setup adc
      setup_adc(ADC_CLOCK_INTERNAL);
      set_adc_channel(2);   
   
   enable_interrupts(INT_RDA);
   enable_interrupts(GLOBAL);
   
   readadc = 607;
   currentph = 8;
        currentkh = 4
         
   while (TRUE)
      {
             
         output_low(pin_b4);
         tempwork();
         phwork();
         output_high(pin_b4);
         delay_ms(3000);
      
      }
}




Thanks again, Mark
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