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 support@ccsinfo.com

Trying to get ADC to work

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



Joined: 25 Feb 2006
Posts: 23

View user's profile Send private message

Trying to get ADC to work
PostPosted: Sat Feb 25, 2006 1:29 am     Reply with quote

Code:
#include <16F688.h>
#device  adc=8;
#use delay(clock=8000000)
#fuses INTRC_IO,NOPROTECT,NOPUT,NOWDT
void main()
{
int a;
setup_oscillator(OSC_8MHZ);

setup_adc_ports(1);
setup_adc(ADC_CLOCK_INTERNAL);
set_adc_channel(0);


set_tris_a (1);
set_tris_c (0);
while(true){
OUTPUT_LOW(PIN_A1);
delay_ms(1000);
OUTPUT_HIGH(PIN_A1);
delay_ms(1000);}

output_c(READ_ADC());

}


Above is my test code for the ADC. I'm using 16F688. I want to input my analog voltage at pin AN0 and output the 8bit ADC result to Port C.

I put leds on my port c pins but they aren''t lighting up when I put AN0 (pin 13) to VCC. Is something wrong with my code? I've tried everything. I made sure that the PIC is cycling thru the instruction by making an LED blinks on PIN A1.
kender



Joined: 09 Aug 2004
Posts: 768
Location: Silicon Valley

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

Re: Trying to get ADC to work
PostPosted: Sat Feb 25, 2006 1:43 am     Reply with quote

david90 wrote:
Code:
setup_adc_ports(1);


Why aren't you using the ADC constants provided by CCS (such as ALL_ANALOG) ?
david90



Joined: 25 Feb 2006
Posts: 23

View user's profile Send private message

PostPosted: Sat Feb 25, 2006 2:19 am     Reply with quote

It wouldn't compile if I use ALL_ANALOG because it is not define in the 16F688.H.

Update: i got the adc to work. A crystal oscillator for another uC on my board seems to interfere with the ADC. If I remove the crystal, the readings are stable but if I connect it back, the readings jump.
Ttelmah
Guest







PostPosted: Sat Feb 25, 2006 4:19 am     Reply with quote

The fact that 'ALL_ANALOG' is not defined, implies you are on a pretty early version of the compiler for this chip. However 'sAN0', is defined, and will do what your current constant does, while on the older compiler the assumed format for 'ALL_ANALOG', was:
aAN0|sAN1|sAN2|sAN3|sAN4|sAN5|sAN6|sAN7
You are not defining any Vref range, and you are not disabling the comparator, which shares with the same pins. So try:
Code:

#include <16F688.h>
#device  adc=8;
#use delay(clock=8000000)

//Have you got MCLR pulled up?. Otherwise add NOMCLR
#fuses INTRC_IO,NOPROTECT,NOPUT,NOWDT,NOBROWNOUT
void main() {
  int a;
  setup_oscillator(OSC_8MHZ);

  //Disable the comparator
  setup_comparators(NC_NC_NC_NC);

  //Select ADC channel, and input range
  setup_adc_ports(sAN0|VSS_VDD);
 
  setup_adc(ADC_CLOCK_DIV_16);
  //Note that 'internal', is geerally not recommended on most
  //chips above 4MHz
  set_adc_channel(0);


  //These TRIS ops do nothing, since you have not selected fast_IO
  //set_tris_a (1);
  //set_tris_c (0);
  while(true){
    OUTPUT_LOW(PIN_A1);
    delay_ms(1000);
    OUTPUT_HIGH(PIN_A1);
    delay_ms(1000);}

    output_c(READ_ADC());
  }
}


Best Wishes
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