![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
misel0019
Joined: 04 Oct 2010 Posts: 53 Location: Dhaka
|
help about ADC |
Posted: Mon Jan 10, 2011 2:17 am |
|
|
hi,
I'm again with my problem.
I can't solve it.
My simple program for read adc.
I use 100t0 ground for pull down.
And sense analog voltage.
My program
Code: |
void adc_init()
{
setup_adc_ports(AN0_TO_AN3);
setup_vref(FALSE);
setup_adc(ADC_CLOCK_DIV_4);
}
void adc_read()
{
set_adc_channel( 0 );
delay_ms(3);
read_adc(ADC_START_ONLY);
delay_ms(20);
adc1=read_adc(ADC_READ_ONLY);
set_adc_channel( 1 );
delay_ms(3);
read_adc(ADC_START_ONLY);
delay_ms(20);
adc2=read_adc(ADC_READ_ONLY);
set_adc_channel( 2 );
delay_ms(3);
read_adc(ADC_START_ONLY);
delay_ms(20);
FF=read_adc(ADC_READ_ONLY);
set_adc_channel( 3 );
delay_ms(3);
read_adc(ADC_START_ONLY);
delay_ms(20);
LL=read_adc(ADC_READ_ONLY);
if(ad1cntr<5)
{
ad1cntr++;
ad1AVG=adc1+ad1AVG;
ad2AVG=adc2+ad2AVG;
ad3AVG=FF+ad3AVG;
ad4AVG=LL+ad4AVG;
}
else if(ad1cntr==5)
{
ADV1=(ad1AVG/5);
ADV2=(ad2AVG/5);
F=(ad3AVG/5);
L=(ad4AVG/5);
ad1AVG=0;
ad2AVG=0;
ad4AVG=0;
ad3AVG=0;
ad1cntr=0;
}
Hum=ADV2*0.1252847;
voltage=ADV1*.058651026;
}
void adc_show()
{
lcd_gotoXY(10,1);
printf(lcd_putc, " Volt:%2.1fV",voltage);
lcd_gotoXY(10,2);printf(lcd_putc, " Hum :%d%% ",Hum);
lcd_gotoXY(1,4);
printf(lcd_putc, "V1=%lu V2=%lu ",F,L);
}
void main()
{
adc_iint();
while(1)
{
adc_read();
adc_show();
}
}
|
but the problem is in AN3 Pin.
lcd shows F value with flactuating means 5,10,23,5,20,6,45,30etc.....
But while I measure in PIN theres constant voltage.
It happens only for AN3 Pin.
Why I can't understand.
Regards misel |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19378
|
|
Posted: Mon Jan 10, 2011 3:30 am |
|
|
What chip?.
What compiler version?.
What pin number are you connecting to?.
Why are you 'fiddling around', starting the ADC, waiting, then reading?. The simple 'read_adc', operation, without any value, does this automatically, starting the ADC, waiting for the conversion to finish, then reading the result. The separate versions are _only_ wanted, when you are doing other things while the conversion takes place.
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
misel0019
Joined: 04 Oct 2010 Posts: 53 Location: Dhaka
|
reply for Ttelmah |
Posted: Mon Jan 10, 2011 3:35 am |
|
|
Chip 18F4520
Compiler version 4.049
RA0,RA1,RA2,RA3 these pin are connected with analog sensing.
i thought that uc require time to convert ADC value .
thanks for the reply.
Misel |
|
![](templates/subSilver/images/spacer.gif) |
nia
Joined: 23 Dec 2010 Posts: 8
|
|
Posted: Mon Jan 10, 2011 5:05 am |
|
|
yes. uc require approx 26us for 18F4520 for adc conversion. |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19378
|
|
Posted: Mon Jan 10, 2011 5:08 am |
|
|
I said what pin _numbers_, not names.
On some chips for example, 'AN3', is not on pin A3, hence wanted to check what pin you were actually using....
However, on the 4520, the analog pins at this point, are 'sequential', and there is nothing 'extra' on this pin (except Vref+), so this should not be the problem.
Yes, the ADC, does require 'time' 12 cycles of the ADC clock (typically about 25uSec). The read_adc function, automatically, does _not_ read the generated value, till the ADC signals that the conversion has completed. So you just need to select the channel _wait for the internal capacitor to charge - allow 20uSec for this_, and then just take the reading. The code will trigger the conversion, and then not read it until the 'finished' flag is set. Automatic, and quick.
Ouch. Problem may well be the compiler version. 4.049, was before the V4 compilers really started to work. When this was available, 3.249, was the 'working' compiler. I'd suspect this may be your problem.....
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
misel0019
Joined: 04 Oct 2010 Posts: 53 Location: Dhaka
|
reply for Ttelmah |
Posted: Mon Jan 10, 2011 5:14 am |
|
|
Pin number
2,3,4,5
means i use +Vref pin also
anyway thanks nia for information
waiting for reply |
|
![](templates/subSilver/images/spacer.gif) |
|
|
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
|