View previous topic :: View next topic |
Author |
Message |
Sid2286
Joined: 12 Aug 2010 Posts: 119
|
ADC values are not constant |
Posted: Tue Sep 07, 2010 11:29 pm |
|
|
hi,
I tried ADC program with a pot of 1k and it was working fine,
however I tried with different adc values but the values are not constant.
Like I tried with adc i/p from .18v to .94V but the values are not constant without any linearity. :(
Code that I tried is as follows:
Code: |
#include <18F452.h>
#device ICD=TRUE ADC=10
#fuses HS,NOLVP,NOWDT
#use delay (clock=20000000)
#use rs232 (baud= 9600,xmit = PIN_C6)
const char TABLE[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0X82,0XF8,0X80,0X98};
void display(int x)
{
int i,j,k,temp; //define four temporary variable
temp=x; //temporary keep AD convert result
i=temp/100; //get display hundred bit
j=(temp%100)/10; //get display ten bit
k=(temp%100)%10; //get display Entries bit
output_d(TABLE[i]); //get the display hundred bit code from table
output_low(PIN_A5);
delay_ms(1); //delay some time,ensure display brightness
output_high(PIN_A5);
output_d(TABLE[j]); //get the display ten bit code from table
output_low(PIN_A4); //RA4 OUTPUT low,light ten bit display
delay_ms(1); //delay some time,ensure display brightness
output_high(PIN_A4);
output_d(TABLE[k]); //get the display Entries bit code from table
output_low(PIN_A3); //RA5 OUTPUT low,light Entries bit display
delay_ms(1); //delay some time,ensure display brightness
output_high(PIN_A3);
/* if(j>=5)
{
output_high(PIN_C0);
}
else
if(i>=1)
{
output_high(PIN_C2);
}
else
output_low(PIN_C0);
*/
}
void main ()
{
float z;
setup_adc_ports(AN0_AN1_AN3);
setup_adc(ADC_CLOCK_INTERNAL);
output_low(PIN_C2);
output_low(PIN_C3);
output_low(PIN_C6);
output_low(PIN_C7);
SET_TRIS_c(0x00);
while(1)
{
set_adc_channel( 0 );
z = read_adc();
delay_ms(10);
z=z*1000;
display(z);
}
}
|
|
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Sep 07, 2010 11:58 pm |
|
|
Quote: | #use delay (clock=20000000)
setup_adc(ADC_CLOCK_INTERNAL);
I tried with different adc values but the values are not constant.
|
We probably have discussed this topic about one million times but it still
comes up, so here's a recent thread on it:
http://www.ccsinfo.com/forum/viewtopic.php?t=43419 |
|
![](templates/subSilver/images/spacer.gif) |
Sid2286
Joined: 12 Aug 2010 Posts: 119
|
|
Posted: Wed Sep 08, 2010 3:35 am |
|
|
Thanks PCM,
I tried with changing the clock by using 32Tosc for 20Mhz frequency. But still my values are fluctuating.
My circuit is simple. I'm giving voltage varying from .18v to .94v. But I'm not getting anything.
I'm trying to learn since I'm new to PIC.
Please help,
Thanks |
|
![](templates/subSilver/images/spacer.gif) |
temtronic
Joined: 01 Jul 2010 Posts: 9174 Location: Greensville,Ontario
|
|
Posted: Wed Sep 08, 2010 9:21 am |
|
|
You might want to just display the raw 10 bit ADC result to PC or similar.
This would show whether it's a hardware glitch or a math problem.
If your pot is in one place, the readings hould be within a couple of bits. Having a filter cap on the input will smooth the result. |
|
![](templates/subSilver/images/spacer.gif) |
Sid2286
Joined: 12 Aug 2010 Posts: 119
|
|
Posted: Thu Sep 09, 2010 5:35 am |
|
|
hi,
I got the bug, i was having floating ground :(
i was trying to use analog input of another circuit to the input of PIC.
Ok, i'm wondering what is the return type of read_adc() function. is it ascii or int..? |
|
![](templates/subSilver/images/spacer.gif) |
Geps
Joined: 05 Jul 2010 Posts: 129
|
|
Posted: Thu Sep 09, 2010 7:19 am |
|
|
Sid2286 wrote: | hi,
I got the bug, i was having floating ground :(
i was trying to use analog input of another circuit to the input of PIC.
Ok, i'm wondering what is the return type of read_adc() function. is it ascii or int..? |
According to the help files:
Quote: | Returns:
Either a 8 or 16 bit int depending on #DEVICE ADC= directive.
|
|
|
![](templates/subSilver/images/spacer.gif) |
|