CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

multiple ADC problem

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
ahmad-al3omar



Joined: 25 May 2007
Posts: 16

View user's profile Send private message

multiple ADC problem
PostPosted: Wed May 14, 2008 3:57 pm     Reply with quote

hello all;
i am working on
PIC16F877A
CRYSTAL 4MHz
i am using a program that read from two potentiometers to the channel (0) and channel (1). when play the code on the simulation every thing is ok.
but when we apply it on the circuit there is a problem which is the value read from the first channel when we change the pot. meter value changes and also the read value from the second channel changes why i don't know, and the read value does not print the maximum value which is +5 volts.
my code below and please help me!!
Code:

#include<16f877a.h>
#fuses noprotect,nowdt,xt
#use delay(clock=4000000)
#include<lcd.c>

#define A pin_c6
#define B pin_c7
#define C pin_c5


int tim,motor,con,timm;
int16 freq,count;
float dig_alc,ang_alc,ang_temp,dig_temp;
int over();

 
main()
{
lcd_init();

setup_adc_ports(all_analog);
setup_adc(adc_clock_internal);

output_low(pin_b3);
output_low(pin_b4);
output_low(pin_b5);
output_low(pin_b6);
output_low(pin_b2);
output_low(pin_b1);

con=1;
while(1)
{
set_adc_channel(0);
dig_alc=read_adc();
ang_alc=dig_alc/510;
delay_us(50);

set_adc_channel(1);
dig_temp=read_adc();
ang_temp=dig_temp/51;
delay_us(50);

motor = input(pin_e0);

lcd_gotoxy(1,1);
printf(lcd_putc," alcohol = %f%% \n t=%f        ",ang_alc,ang_temp);

if(ang_alc>0.25 && con==1)
{
con=0;
motor = input(pin_c1);
over();
}
if(ang_temp>2 && con==1)
{
con=0;
motor = input(pin_c1);
over();
}
if(freq > 200 && con==1)
{
con=0;
motor = input(pin_c1);
over();
}

}

}


over()
{
motor = input(pin_c1);
output_high(pin_b1);

enable_interrupts(INT_RTCC);

output_high(pin_c5);



if(motor)
{   
output_high(pin_b1);
delay_ms(1000);
output_low(pin_b1);
delay_ms(1500);
output_high(pin_b1);
delay_ms(1000);
output_low(pin_b1);
delay_ms(2500);
output_high(pin_b1);
delay_ms(1000);
output_low(pin_b1);
delay_ms(3500);
output_high(pin_b1);
delay_ms(1000);
output_low(pin_b1);
delay_ms(4500);
output_high(pin_b1);
delay_ms(1000);
output_low(pin_b1);
delay_ms(5500);
output_high(pin_b1);
delay_ms(1000);
output_low(pin_b1);
delay_ms(6500);
output_high(pin_b1);

}
output_low(pin_b3);
output_low(pin_b4);
output_low(pin_b5);
output_low(pin_b6);
output_low(pin_b2);
}

_________________
A.T.N
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed May 14, 2008 4:27 pm     Reply with quote

The compiler defaults to using 8-bit mode for the A/D result.
You have to add the line shown in bold below to get a 10-bit result.
Also, you should add the NOLVP fuse for safety.
Quote:
#include<16f877a.h>
#device adc=10
#fuses noprotect,nowdt,xt,NOLVP
#use delay(clock=4000000)
#include<lcd.c>
n-squared



Joined: 03 Oct 2006
Posts: 99

View user's profile Send private message Send e-mail Visit poster's website

PostPosted: Wed May 14, 2008 9:58 pm     Reply with quote

I found out that ADC readings work much better if you put a short delay between channel selection and the actual read:
Code:

  set_adc_channel(3);
  delay_us(10);
  x = read_adc();

_________________
Every solution has a problem.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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