|
|
View previous topic :: View next topic |
Author |
Message |
phithuc
Joined: 06 Sep 2007 Posts: 4
|
LM335Z problems...? |
Posted: Sat Oct 20, 2007 1:42 am |
|
|
Hi all!
I design a circuit to measure temperature using LM335Z. The circuit as following:
LM335: pin1-not connect
pin2-connect to AN2 and 5v through R1k
pin3-connect to GND
And my code is following:
Code: |
#include "D:\Pic_Pro\LM35sensor.h"
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
int16 value;
int16 value1;
unsigned char level;
unsigned char Code7[]={
0x03//0xFC, // '0'
0x9F//0x60, // '1'
0x25//0xDA, // '2'
0x0D//0xF2, // '3'
0x99//0x66, // '4'
0x49//0xB6, // '5'
0x41//0xBE, // '6'
0x1F//0xE0, // '7'
0x01//0xFE, // '8'
0x09//0xF6, // '9'
};
void DisplayTemp(void);
void main()
{
level=0;
set_tris_a(0xff);//Port a as input
setup_adc (ADC_CLOCK_INTERNAL);
setup_adc_ports (AN0);
set_adc_channel (0);
while (true)
{
delay_ms (1000);
value = read_adc ();
value1 = (value-559L)/2;
DisplayTemp();
setup_adc(adc_off);
}
}
/////////////////
void DisplayTemp(void)
{
switch (level)
{
case 0:
{
output_d(Code7[value1/10]);
output_b(0x80);
break;
}
case 1:
{
output_d(Code7[value1%10]);
output_b(0x40);
break;
}
}
level++;
if(level==2)level=0;
}
|
The result of this, I see display 17C, and voltage on AN0 is ~3V I think
that I must calibrate sensor. But I don't how to do it. Please help me,
thanks! |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Oct 22, 2007 11:21 am |
|
|
Quote: | value = read_adc (); |
1. Use a printf() statement, and display the value that you read from
the adc. Post the value. Example of printf:
Code: | printf("adc value = %Lu \n\r", value); |
2. Given that number from the read_adc() function, tell us what value
you want to be displayed. Tell us why.
Quote: | value1 = (value-559L)/2; |
3. Explain this statement. The value of 559L is a "magic number".
It means that only the programmer (usually) knows what it means.
See this article on "magic numbers":
http://en.wikipedia.org/wiki/Magic_number_%28programming%29#Unnamed_numerical_constant |
|
|
Ttelmah Guest
|
|
Posted: Mon Oct 22, 2007 3:03 pm |
|
|
The conversion, looks like a clip, from an old post of mine. Using 5v as a reference, with the LM335, giving 2.7315v nominal at 0C, gives 559(.4) counts for 0C. Then each step, is 0.488 degrees, which means that for ranges around room temperature, using '/2', the conversion will give almost the centrigrade value (it'll actually go wrong by just 1 degree at 50C). At 20C, the expected voltage from the sensor, would be 2.9315v.
What is the 'real' temperature?. The result seems quite fair to me. However the way to calibrate to get closer, is to put the sensor into melting ice at 0C, tke the ADC count, and use this in place of the 559 value.
Remember the accuracy will depend on just how well smoothed and accurate the '5v' rail is.
Best Wishes |
|
|
phithuc
Joined: 06 Sep 2007 Posts: 4
|
Hi! |
Posted: Mon Oct 22, 2007 9:30 pm |
|
|
I've already do so. But when temp is 20C, the voltage on PinA0 is 3,2V. And I place a resistor var 10K on adj PIN of LM335. I adjust resistor var to have a real temp of environtment. Then, I see that temp is changed continuously. Is it correct? |
|
|
|
|
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
|