|
|
View previous topic :: View next topic |
Author |
Message |
Markdem
Joined: 24 Jun 2005 Posts: 206
|
quick math question |
Posted: Sat Feb 25, 2006 8:01 pm |
|
|
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
|
|
Posted: Sat Feb 25, 2006 8:30 pm |
|
|
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
|
|
Posted: Sat Feb 25, 2006 8:51 pm |
|
|
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
|
|
Posted: Sun Feb 26, 2006 2:30 am |
|
|
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
|
|
Posted: Sun Feb 26, 2006 2:46 am |
|
|
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
|
|
Posted: Sun Feb 26, 2006 3:19 am |
|
|
Hi kender, thanks for you help.
You could not find when i define currentkh because i am no thinking very well today.
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 |
|
|
|
|
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
|