|
|
View previous topic :: View next topic |
Author |
Message |
jubilee Guest
|
funny adc output |
Posted: Tue Aug 26, 2003 11:22 pm |
|
|
Hi i amhaving a tough time solving the problem i have been facing. It is like this=== i am having a input from a power supply. using a PIC , i am required to readout the voltage and output it to the PC. Using the following codes..., i am getting some increadible values like 1000++ values. IS there anything worng with the codes??
PLeasseeeee HELP!!
setup_adc_ports(RA0_RA1_RA3_ANALOG);
setup_adc(ADC_CLOCK_DIV_2);
setup_spi(FALSE);
setup_counters(RTCC_INTERNAL,RTCC_DIV_2);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_ccp1(CCP_OFF);
setup_ccp2(CCP_OFF);
{
long value,min,max,mean,old,new,total;
int i;
printf("Sampling:");
do {
min=255;
max=0;
old,new,total=0;
for(i=0;i<=100;++i) {
delay_ms(5);
value = Read_ADC();
new=value;
total=old+new;
if(value
min=value;
if(value>max)
max=value;
mean=(max + min)/2;
old=total;
}
mean=old/100;
printf("\n\rMean: \%4lu Max: \%4lu\r\n",mean,Max);
printf("\n\rold: \%4lu Max: \%4lu\r\n",old,Max);
} while (TRUE);
___________________________
This message was ported from CCS's old forum
Original Post ID: 144517295 |
|
|
Kenny
Joined: 07 Sep 2003 Posts: 173 Location: Australia
|
Re: funny adc output |
Posted: Wed Aug 27, 2003 1:01 am |
|
|
<font face="Courier New" size=-1>:=Hi i amhaving a tough time solving the problem i have been facing. It is like this=== i am having a input from a power supply. using a PIC , i am required to readout the voltage and output it to the PC. Using the following codes..., i am getting some increadible values like 1000++ values. IS there anything worng with the codes??
:=PLeasseeeee HELP!!
:=
:=
:=
:=
:= setup_adc_ports(RA0_RA1_RA3_ANALOG);
:= setup_adc(ADC_CLOCK_DIV_2);
:= setup_spi(FALSE);
:= setup_counters(RTCC_INTERNAL,RTCC_DIV_2);
:= setup_timer_1(T1_DISABLED);
:= setup_timer_2(T2_DISABLED,0,1);
:= setup_ccp1(CCP_OFF);
:= setup_ccp2(CCP_OFF);
:= {
:=
:= long value,min,max,mean,old,new,total;
:= int i;
:=
:= printf("Sampling:");
:=
:=
:= do {
:= min=255;
:= max=0;
:= old,new,total=0;
:= for(i=0;i<=100;++i) {
:=
:= delay_ms(5);
:= value = Read_ADC();
:= new=value;
:=
:= total=old+new;
:= if(value
:= min=value;
:= if(value>max)
:= max=value;
:= mean=(max + min)/2;
:= old=total;
:= }
:= mean=old/100;
:= printf("\n\rMean: \%4lu Max: \%4lu\r\n",mean,Max);
:= printf("\n\rold: \%4lu Max: \%4lu\r\n",old,Max);
:= } while (TRUE);
Funny indeed :-)
Several problems apparent.
Since PIC type or clock frequency not given, I'll assume a 16F876 and >= 4MHz.
setup_adc(ADC_CLOCK_DIV_2);
The maximum clock frequency is 1.25MHz for this division, change to:
setup_adc(ADC_CLOCK_DIV_32);
Need to select a/d channel
eg.
set_adc_channel(0); // Select channel 0
From your code you seem to be doing 8 bit a/d
Check that it's not set for 10 bit in the 16F876.h file as:
#device PIC16F876 ADC=10
Also, in the code, variables will accumulate past the 16 bit boundary and wrap, but your not seeing that because the output is limited to four digits.
Modified code:
void main(void)
{
long total;
int min,max,mean,new,i;
setup_adc_ports(RA0_RA1_RA3_ANALOG);
setup_adc(ADC_CLOCK_DIV_32);
set_adc_channel(0); // Select channel 0
printf("Sampling:\n\r");
while (1) {
min=255;
max=0;
new = 0;
total=0;
for(i=0;i<=100;++i) {
delay_ms(5);
new = read_adc();
if(new < min) min = new;
if(new > max) max = new;
total += new;
}
mean = total/100;
printf("Max: \%3u Min: \%3u Mean: \%3u\n\r",max,min,mean);
}
}
HTH
Kenny
</font>
___________________________
This message was ported from CCS's old forum
Original Post ID: 144517303 |
|
|
R.J.Hamlett Guest
|
Re: funny adc output |
Posted: Wed Aug 27, 2003 2:24 am |
|
|
:=Hi i amhaving a tough time solving the problem i have been facing. It is like this=== i am having a input from a power supply. using a PIC , i am required to readout the voltage and output it to the PC. Using the following codes..., i am getting some increadible values like 1000++ values. IS there anything worng with the codes??
:=PLeasseeeee HELP!!
:=
:=
:=
:=
:= setup_adc_ports(RA0_RA1_RA3_ANALOG);
:= setup_adc(ADC_CLOCK_DIV_2);
What clock rate is your chip?. This may be faster than the ADC can properly run.
:= setup_spi(FALSE);
:= setup_counters(RTCC_INTERNAL,RTCC_DIV_2);
:= setup_timer_1(T1_DISABLED);
:= setup_timer_2(T2_DISABLED,0,1);
:= setup_ccp1(CCP_OFF);
:= setup_ccp2(CCP_OFF);
:= {
:=
:= long value,min,max,mean,old,new,total;
:= int i;
:=
:= printf("Sampling:");
:=
:=
:= do {
:= min=255;
:= max=0;
:= old,new,total=0;
This is not the way to clear three variables. The syntax is:
old=new=total=0;
:= for(i=0;i<=100;++i) {
:=
:= delay_ms(5);
:= value = Read_ADC();
If you want only 8bit values, then on most chips, you will need to specify #device adc=8
This has changed from older compilers, where the default was 8bit, unless told otherwise. Now the compiler appears to default to the full ADC resolution, unless told otherwise. Hence on most chips, I'd expect this call to return 0-1023.
Nowhere in the code, have you set _which_ ADC channel is to be read...
:= new=value;
:=
:= total=old+new;
:= if(value
:= min=value;
:= if(value>max)
:= max=value;
:= mean=(max + min)/2;
This does nothing, since 'mean', gets overwritten when you leave the loop, with the value 'old/100', and 'mean' is not being used anywhere in the loop. Hence this calculation is wasted...
:= old=total;
:= }
:= mean=old/100;
You have an error here, since the loop will execute 101 times (0 to 100 inclusive).
You are also wasting quite a bit of time, moving numbers around. Clear 'old' when you come into the loop (you allready try to do this), then just have:
for(i=0;i<100;++i) {
delay_ms(5);
value = Read_ADC();
old+=value;
if(value
min=value;
if(value>max)
max=value;
}
:= printf("\n\rMean: \%4lu Max: \%4lu\r\n",mean,Max);
:= printf("\n\rold: \%4lu Max: \%4lu\r\n",old,Max);
:= } while (TRUE);
Best Wishes
___________________________
This message was ported from CCS's old forum
Original Post ID: 144517306 |
|
|
|
|
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
|