View previous topic :: View next topic |
Author |
Message |
kideltn
Joined: 08 Jun 2010 Posts: 13
|
Problem with conversion ADC in CCS |
Posted: Sat Jul 17, 2010 4:56 am |
|
|
Hello every one !
I made a real circuit with the following schematic:
I used USB voltage 5V.Temperature sensor:LM35
The circuit well worked,but the result not precise.
My code:
Code: | void main()
{
setup_adc(ADC_OFF);
setup_psp(PSP_DISABLED);
setup_spi(SPI_SS_DISABLED);
setup_wdt(WDT_OFF);
setup_timer_0(RTCC_INTERNAL);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
//Setup_Oscillator parameter not selected from Intr Oscillator Config tab
// TODO: USER CODE!!
lcd_init();
setup_ADC(ADC_CLOCK_INTERNAL);
setup_ADC_ports(AN0|VSS_VDD);//Vref=Vdd-Vss=5V
set_ADC_channel(0);
delay_us(10);
while(1)
{
adc=read_ADC();
adc=adc*500/1024;//i fused Vref=5V,#device adc=10
LCD_putc("Temperature: ");
LCD_putc("\n");
LCD_putc("T=");
printf(lcd_putc,"%f",adc);
LCD_putc("*C");
delay_ms(100);lcd_gotoxy(1,1);
}
} |
But the above code I simulated on Proteus precisely worked.
I really don't understand what happened.
I have debugged so much, I think my code did not mistake, and every thing perhaps very logic. But...
Please help me!!! |
|
|
jbmiller
Joined: 07 Oct 2006 Posts: 73 Location: Greensville,Ontario
|
|
Posted: Sat Jul 17, 2010 5:57 am |
|
|
Welcome to the REAL world where simulations always fail !
I've used the LM35 for almost 3 decades..
In ANY analog circuit there will be noise, so..
add 100mfd// .01 disk caps on the VCC of the LM35.
add 10 mfd cap on the output pin as well.
Depending on distance of LM35 to PIC, it'll pickup noise. You can add an opamp voltage buffer to minimize this effect. I do this for runs up to 100' all the time.
On the PIC side...
take several readings and then average the result
this should help get you stable in the real world.
Jay |
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Sat Jul 17, 2010 6:49 am |
|
|
If the readings are mostly correct with occasional bad ones, search for "olympic average" for a very simple routine that kills bad data points to get the correct average with very little processing. _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
|