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

adc problems

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
guest*
Guest







adc problems
PostPosted: Thu Apr 08, 2004 11:07 am     Reply with quote

Wondering if any body can help me,

i'm trying to read from the a/d however the rading keeps jumping between 0.00 and 244.99

vref- =0v
vref+ =2.5v

vin= 394mv-412mv it jumps a bit but shouldn't cause the reading to jump this much should it.
my code is below.

*****************************************************
include <16f877.h>
device ADC=10; // setting it to 10 bit a/d default is 8
#include <stdlib.h>
#include <math.h>

#use i2c(MASTER, SDA=PIN_C4, SCL=PIN_C3)
#use delay(clock=20000000)
#use rs232(baud=9600,xmit=PIN_C6,RCV=PIN_C7)


#include <lcd_d.c>
#define button PIN_B0 //defines b0 as an input button

main()
{

float value=0.00;
setup_timer_1(T1_INTERNAL|T1_DIV_BY_8);
delay_ms(50);
lcd_init();
delay_ms(10);

SETUP_ADC_PORTS(ALL_ANALOG);

setup_port_a( ALL_ANALOG ); //use with 20mhz
//setup_adc( ADC_CLOCK_INTERNAL ); only use with less than 1mhz colck

setup_adc(ADC_CLOCK_DIV_32);
set_adc_channel( 1 ); //specify whcich a/d channel to use


printf(lcd_putc,"\fDesigned by\nguest!! 2004"); //boot up screen
delay_ms(250);
printf(lcd_putc,"\f");

while(1)
{
value = read_adc();
delay_ms(50);
output_c(value);
printf(lcd_putc,"\n%3.2f value ",value);
delay_ms(50);
}
return 0;
}
***********************************************
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Apr 08, 2004 12:08 pm     Reply with quote

Your problem is that you have not setup the ADC module
in the PIC to use the Vref pins.

You have it setup like this:
setup_adc(ALL_ANALOG);

and you really need this:
setup_adc(RA0_RA1_ANALOG_RA3_RA2_REF);

You can find the information on how to properly setup
the ADC port by looking in the 16F877 data sheet.
Look in Section 11 of the data sheet, at the table for:
PCFG3:PCFG0: A/D Port Configuration Control bits:
This table gives a list of all possible settings for the ADC port.

Also, look in the 16F877.H file, in this section:
Constants used in SETUP_ADC_PORTS()
This section shows you the constants used by CCS for
the setup_adc_ports() function. These constants correspond
to the table in the data sheet, that I referred to above.
You should study them to understand how to use the ADC.

------------

You have a few other strange things in your code.

These two statements do the same thing,
so get rid of one of them.
SETUP_ADC_PORTS(ALL_ANALOG);
setup_port_a( ALL_ANALOG );

At the end of main(), you have return(0);
That line of code won't really do anything.
It just moves 0 into a temporary register.
Remove that line of code.
Guest








PostPosted: Thu Apr 08, 2004 12:54 pm     Reply with quote

ok so instead of,

i use
SETUP_ADC_PORTS(ALL_ANALOG);
setup_port_a( ALL_ANALOG );


i use: setup_adc(RA0_RA1_ANALOG_RA3_RA2_REF);

forgot to mention i'm using a 20mhz clock.
is it also possible to set vref- to -2.5, its not quite clear on the datasheets.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Apr 08, 2004 1:13 pm     Reply with quote

Quote:
is it also possible to set vref- to -2.5, its not quite clear on the datasheets.

If you look in section 15 of the 16F877 data sheet, at Table 15-12,
at item A22, it has a specification for Vref-. It says the minimum
voltage allowed for Vref- is: Avss - 0.3v
It doesn't define Avss anywhere, so I'm going to assume it's
the same as Vss, which is 0v. That means that Vref- must
not go below -0.3 volts. So you can't connect -2.5v to the Vref- pin.
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