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

math problem

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



Joined: 27 Nov 2006
Posts: 9

View user's profile Send private message

math problem
PostPosted: Mon Dec 04, 2006 5:48 pm     Reply with quote

I went through and did these math calculations by hand and got the correct answer. My adc is at approximately 512 at room temperature. I am not sure if I am dealing with negative numbers correctly because my output on the LCD does not match my calculations at all. Any ideas on what may be going wrong?

thanks,
Jeff


Code:
#include <math.h>
float W0,W1,W2,W3,W4,W5,VB,RT,TC,TF;

W0=0.001116401465500;
W1=0.000237982973213;
W2=-0.000000372283234;
W3=0.000000099063233;   
                                                  
gettmp:   set_adc_channel(3);         //(ADC Channel Input-3)
W4=read_adc();      //Sample ADC Input Voltage
VB=W4*(5/1023);
RT=VB/((5-VB)/10100);
W5=(LOG(RT));
TC=(1/(W0+(W1*(LOG(RT)))+(W2*W5*W5)+(W3*W5*W5*W5)))-273.15;
TF=(TC*1.8)+32;

lcd_putc(0x0c);         //Clear the LCD
lcd_gotoxy(1,1);      //Set the Cursor to Position-1 of Line-1
printf(lcd_putc,"Degrees C=%lf",TC);   //Send, Degrees C=, to the LCD in Long Integer Floating Point Form
      
lcd_gotoxy(1,2);      //Set the Cursor to Position-1 of Line-2
printf(lcd_putc,"Degrees F=%lf",TF);          //Send, Degrees F=, to the LCD in Long Integer Floating Point Form

delay_ms(2000);            //Pause or Delay for 2 seconds

goto gettmp;
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Dec 04, 2006 6:07 pm     Reply with quote

Quote:
printf(lcd_putc,"Degrees F=%lf",TF);

The floating point format specifier ("%f") doesn't need or accept
an "l" (lower case L) modifier.

Download the CCS manual.
http://www.ccsinfo.com/downloads/ccs_c_manual.pdf
Look at the printf() function parameters on page 196 in the Acrobat
reader. It shows which ones can accept an "L" modifer.

There are other errors.
When you set or change the A/D channel, you need to put a delay
after it. This is in the data sheet. Add the line shown in bold below:
Quote:

set_adc_channel(3);
delay_us(20);


The compiler doesn't know that you want to do floating point math
unless you tell it to do so. For example, in your code below, the
compiler will do integer math on the "5/1023" expression. The compiler
will evaluate that expression as 0. It will then multiply W4 by 0.
Quote:
VB=W4*(5/1023);


You need to tell the compiler that the numeric values are floating point.
Do this by adding a '.0' after each number. Example:
Code:
VB=W4*(5.0/1023.0);

There are other lines in your program that need to be fixed in the
same way.

Also, we don't normally use "goto" statements in C. Instead, you
should use a while(1) loop. Example:
Code:

while(1)
{

// Put your loop code in here.


}
jab351c



Joined: 27 Nov 2006
Posts: 9

View user's profile Send private message

PostPosted: Mon Dec 04, 2006 10:45 pm     Reply with quote

Ok, I took your advice on everything except the goto. I will change it to a while loop eventually probably. I am still not getting the correct output on the LCD. Actually I am getting the exact same output as the last time. My question is if I have other things hooked upto port a(sending in other signals) will this interfere with my adc on pin_A3? I dont think it should if I am only using pin_A3 should it?

thanks,
Jeff

Code:
#include <math.h>
float W0,W1,W2,W3,W4,W5,VB,RT,TC,TF;

setup_port_a(AN3);      
setup_adc(VSS_VDD);                        
setup_adc(ADC_CLOCK_DIV_4);                  
setup_adc(ADC_TAD_MUL_12);                  
set_adc_channel(0);                        
delay_ms(100);

W0=0.001116401465500;
W1=0.000237982973213;
W2=-0.000000372283234;
W3=0.000000099063233;   
                                                  
gettmp:   set_adc_channel(3);   //Analog Input  (ADC Channel Input-3)
      
delay_us(20);
W4=read_adc();                           
VB=W4*(5.0/1023.0);
RT=VB/((5-VB)/10100.0);
W5=(LOG(RT));
TC=(1/(W0+(W1*(LOG(RT)))+(W2*W5*W5)+(W3*W5*W5*W5)))-273.15;
      TF=(TC*1.8)+32.0;

lcd_putc(0x0c);            //Clear the LCD
lcd_gotoxy(1,1);         //Set the Cursor to Position-1 of Line-1
printf(lcd_putc,"Degrees C=%f",TC);         
lcd_gotoxy(1,2);         //Set the Cursor to Position-1 of Line-2
printf(lcd_putc,"Degrees F=%f",TF);          

delay_ms(2000);            //Pause or Delay for 2 seconds

goto gettmp;
                
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Dec 04, 2006 11:05 pm     Reply with quote

Quote:

setup_adc(VSS_VDD);
setup_adc(ADC_CLOCK_DIV_4);
setup_adc(ADC_TAD_MUL_12);

You can't call setup_adc() three times with individual constants.
You will never see this, in any of the CCS example files or in the
sample code posted by the people who answer questions on this forum.
The constants have to be bitwise OR'ed together, as described in
the .H file for your PIC.
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