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

Read_ADC reads wrong values ?

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







Read_ADC reads wrong values ?
PostPosted: Fri Dec 09, 2005 12:10 pm     Reply with quote

Hello, I am using a TMP36 temperature sensor interfaced to my PIC18F252. I have defined #device ADC = 8 bits resolution. When I checked the voltage output at the temperature sensor or at the pin of the microcontroller using a multimeter I see almost constant value ( say 0.65 corresponding to 15 deg C) But when I print the Read_ADC value I see it fluctuates a lot. (for ex 5 deg C to 27 deg C) and yeah basically it gives wrong digitized readings most of the time. Here is the code I use to read the temperature reading. Is this got to do with delay I set ? I am calling the function to read temperature continuosly every 1 second.


Code:



#include <18f252.h> //18f252 pic mcu
#device ADC=8;

// inside the function to calculate temperature
 delay_ms(100);
 set_adc_channel(4);
 delay_us(10);
 V_temp1 = Read_ADC();
 Vout_temp = (float) V_temp1;
 printf(" Temperature (digitized value) = %f \r\n", Vout_temp);



I am using PIC C Compiler IDE version 3.215 PCH 3.217
Guest








PostPosted: Fri Dec 09, 2005 12:14 pm     Reply with quote

I forgot to add these two lines of code I use to set up the ADC ports


Code:



 setup_adc_ports(ALL_ANALOG);
 setup_adc(ADC_CLOCK_DIV_32);

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: Fri Dec 09, 2005 12:35 pm     Reply with quote

If you think that it's a settling delay - increase it from 10uS to 1mS and see if it helps. Also, the line conecting the temperature sensor to the PIC might be picking up noise (probably 60hZ, especially if it's an off-board sensor), which you don't see with the multimeter. So, you could try adding an RC filter or just a capacitor to ground.
deepak
Guest







PostPosted: Fri Dec 09, 2005 12:45 pm     Reply with quote

Thanks for your reply. I already did that and some other delay values as well but it seems to give me the same results. The sensor is on board and it gives a voltage output which I have directly interfaced to the PIC.

is Vref may be something of concern ? I have defined all ports as analog and so it should take my VDD as Vref ? I checked the VDD on the PIC and it was 5.1V. Or is the number of bits causing any problem ? any suggestions most welcome
Humberto



Joined: 08 Sep 2003
Posts: 1215
Location: Buenos Aires, La Reina del Plata

View user's profile Send private message

PostPosted: Fri Dec 09, 2005 12:49 pm     Reply with quote

Quote:

Hello, I am using a TMP36 temperature sensor interfaced to my PIC18F252.


Are you aware that the TMP36 has on open emitter output and that it need a pull down resistor to get a measurable voltage ?

Quote:

is Vref may be something of concern ? I have defined all ports as analog and so it should take my VDD as Vref ?

Yes.

Pls post a short and complete code that we can paste and test.

Humberto


Last edited by Humberto on Fri Dec 09, 2005 1:04 pm; edited 2 times in total
deepak
Guest







PostPosted: Fri Dec 09, 2005 1:01 pm     Reply with quote

here is the code

Code:



#include <18f252.h>
#device ADC=10;
#include <stdlib.h>
#include <stdio.h>
#use delay(clock = 20000000)
#fuses HS,NOPROTECT,NOCPD,NOCPB, NOWDT,NOLVP, NOBROWNOUT
#use rs232(baud = 9600, xmit = PIN_C6, rcv = PIN_C7)

#separate
void Setup_int()
{
 enable_interrupts(global);
 enable_interrupts(int_rda);
 setup_adc_ports(ALL_ANALOG);
 setup_adc(ADC_CLOCK_DIV_32);
}


#separate
void temp_sensor()
{
 // measure the sensor output voltage
 set_adc_channel(4);
 delay_ms(5);
 V_temp1 = Read_ADC();
 Vout = (float) V_temp1 ;
 Temp = ((Vout/204.8) - 0.5) * 100.0; // div by 1024/5 (=204.8) for 10 bit
 printf("%f %f \r\n\n",Vout, Temp);

}

void main()
{
 printf("Starting\r\n");
 Setup_int();
  do {
  delay_ms(1000);
  // Calculate Temperature
  temp_sensor();
 
  } while (TRUE);
}


deepak
Guest







PostPosted: Fri Dec 09, 2005 1:06 pm     Reply with quote

updated code, i am using 8 bit ADC since I defined my variable that accepts the digitized values as byte

Code:


#include <18f252.h>
#device ADC=8;
#include <stdlib.h>
#include <stdio.h>
#use delay(clock = 20000000)
#fuses HS,NOPROTECT,NOCPD,NOCPB, NOWDT,NOLVP, NOBROWNOUT
#use rs232(baud = 9600, xmit = PIN_C6, rcv = PIN_C7)

byte V_temp1;
float Vout, Temp;

#separate
void Setup_int()
{
 enable_interrupts(global);
 enable_interrupts(int_rda);
 setup_adc_ports(ALL_ANALOG);
 setup_adc(ADC_CLOCK_DIV_32);
}


#separate
void temp_sensor()
{
 set_adc_channel(4);
 delay_ms(5);
 V_temp1 = Read_ADC();
 Vout = (float) V_temp1 ;
 Temp = ((Vout/204.8) - 0.5) * 100.0; // div by 1024/5 (=204.8) for 10 bit
 printf("%f %f \r\n\n",Vout, Temp);

}

void main()
{
 printf("Starting\r\n");
 Setup_int();
  do {
  delay_ms(1000);
  temp_sensor();
 
  } while (TRUE);
}

deepak
Guest







PostPosted: Fri Dec 09, 2005 1:30 pm     Reply with quote

One thing I found interesting is this,

I had defined ADC as 10 bit using # device ADC = 10. Then I defined a variable as long int.

When I did this,

Code:

long int temp1;
temp1 = Read_ADC();


it should return a digitized value in the 10 bit range right ? but I see it gives a value that is corresponding to the 8 bit range.
Guest








PostPosted: Fri Dec 09, 2005 2:17 pm     Reply with quote

Humberto wrote:


Are you aware that the TMP36 has on open emitter output and that it need a pull down resistor to get a measurable voltage ?


no. so what resistance do I need to add ? a 4.7K ? But I do get almost right values and once in 10 readings get a wrong reading. It may be VDD for the slight variations I see. I measured the VDD and its 5.2V and voltage output at temperature sensor is 0.68V. Now this means 0.68/5.2 * 255 ( for 8 bit ADC) = 33.4 but I get 32.0 or 33.0 digitized reading. But I do not understand that some times I get ( once in 10 readings or so ) strange values.
Guest








PostPosted: Fri Dec 09, 2005 2:20 pm     Reply with quote

I only saw this in the datasheet

Quote:

The output voltage of this circuit is not referenced to the circuit’s common ground. If this output voltage were applied directly to the input of an ADC, the ADC’s common ground should be adjusted accordingly.
Humberto



Joined: 08 Sep 2003
Posts: 1215
Location: Buenos Aires, La Reina del Plata

View user's profile Send private message

PostPosted: Fri Dec 09, 2005 4:41 pm     Reply with quote

Quote:

The output voltage of this circuit is not referenced to the circuit’s common ground. If this output voltage were applied directly to the input of an ADC, the ADC’s common ground should be adjusted accordingly.


Figure 25 (Page 10) of the datasheet shows you an example on how to create a virtual Gnd to compensate the ADC’s common ground.
Also shows the expected R values that the TMP36 needs in it's output.

Quote:

But I do not understand that some times I get ( once in 10 readings or so ) strange values.


I assume:
1) you are not testing this in a protoboard or alike.
2) the decoupling .1uF (ceramic type) is connected across +Vs and Gnd.
3) the SHUTDOWN Pin (if present on your package) is connected to +Vs.
4) you had read that this device operate in very little supply current and that it's very sensitive to radio frequency interference or hostile electrical enviroments.
5) To minimize RFI, did you try with a 2.2 tantalum cap across the .1 as spec recommend ?

According to your description you have a hardware problem. Have not time to test your code today, but tomorrow I will do.

Humberto
Guest








PostPosted: Wed Dec 14, 2005 12:54 pm     Reply with quote

Humberto wrote:
Quote:

The output voltage of this circuit is not referenced to the circuit’s common ground. If this output voltage were applied directly to the input of an ADC, the ADC’s common ground should be adjusted accordingly.


Figure 25 (Page 10) of the datasheet shows you an example on how to create a virtual Gnd to compensate the ADC’s common ground.
Also shows the expected R values that the TMP36 needs in it's output.
Humberto



But this is for TMP35 and TMP37. Fig 26 represents the version for TMP36. But this says it is deg C to Fahrenheit conversion ?


Humberto wrote:
Quote:

But I do not understand that some times I get ( once in 10 readings or so ) strange values.


I assume:
1) you are not testing this in a protoboard or alike.
2) the decoupling .1uF (ceramic type) is connected across +Vs and Gnd.
3) the SHUTDOWN Pin (if present on your package) is connected to +Vs.
4) you had read that this device operate in very little supply current and that it's very sensitive to radio frequency interference or hostile electrical enviroments.
5) To minimize RFI, did you try with a 2.2 tantalum cap across the .1 as spec recommend ?

According to your description you have a hardware problem. Have not time to test your code today, but tomorrow I will do.

Humberto


We have a proto board with 0.1uF cap is connected. shutdown pin is connected to Vs. But planning to respin the board. Hence if I want to use simple RC filter at the output, what values do i put in ? the output resistance on the TMP36 is 30 ohms.

Also Vref is 5.2 V. So I understood it digitizes from 0-5.2V rather than 0-5V ? there is a lower value reported because of this right ?

And the digitizing range is 0-Vdd whereas the output is from 0.5V to 1.75V. The data sheet says temperature variation is +/- 2 deg C. But I see a lot of fluctuation. Please provide any suggestions to improve the performance.
Guest








PostPosted: Wed Dec 14, 2005 2:15 pm     Reply with quote

Will it help if I just have a buffer at the output of the temperature sensor before sending it to the 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