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

(SOLVED) dsPIC33: adc_read() trouble

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



Joined: 19 Oct 2010
Posts: 4

View user's profile Send private message

(SOLVED) dsPIC33: adc_read() trouble
PostPosted: Tue Oct 19, 2010 4:16 pm     Reply with quote

Hello everybody,

I'm trying to program a dspic33FJ16GS504 and I need to use the adc on AN0(RA0) but when I try reading the adc using read_adc(); function, the program stops working there.

Here is my code:
Code:

#include <33FJ16GS504.h>

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES NOPUT                    //No Power Up Timer
#FUSES NOPROTECT                //Code not protected from reading
#FUSES FRC                      //Internal Fast RC Oscillator
#FUSES NOOSCIO                  //OSC2 is clock output
#FUSES ICSP1                    //ICD uses PGC1/PGD1 pins
#FUSES NOJTAG
#FUSES XT
#FUSES PR

#device adc=10

#use delay(clock=7370000)

void main()
{
   int16 value;
   
   SETUP_WDT(WDT_OFF);
   setup_oscillator(OSC_INTERNAL);
   setup_adc( ADC_CLOCK_INTERNAL );
   setup_adc_ports( ALL_ANALOG );
   set_adc_channel(0);
   
   while(TRUE)
   {
      delay_ms( 1 );
      value = read_adc();
      if (value > 100)
         output_high(PIN_B3);
      else
         output_low(PIN_B3);
    }
}


Am I missing a fuse or a setup which is causing adc not to work. I can controls the pin outputs so i know its only the adc. Any help would be appreciated. I used CCS Compiler 4.112 if it helps at all.


Last edited by lackoblacko on Wed Oct 27, 2010 9:05 am; edited 1 time in total
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Wed Oct 20, 2010 12:19 am     Reply with quote

Without an external crystal, the processor won't never run, because the fuses set XT primary oscillator and clock switching disabled.
So the setup-oscillator() statement can't be executed at all.

If you want FRC, you must not specify XT or PR fuse.
lackoblacko



Joined: 19 Oct 2010
Posts: 4

View user's profile Send private message

PostPosted: Wed Oct 20, 2010 9:45 am     Reply with quote

I removed the xt and pr fuses but there is still no clock output on osc2 pin.

here is my revised code
Code:

#include <33FJ16GS504.h>

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES NOPUT                    //No Power Up Timer
#FUSES NOPROTECT                //Code not protected from reading
#FUSES FRC                      //Internal Fast RC Oscillator
#FUSES NOOSCIO                  //OSC2 is clock output
#FUSES ICSP1                    //ICD uses PGC1/PGD1 pins
#FUSES NOJTAG

#device adc=10

#use delay(clock=7370000)

void main()
{
   int16 value;
   
   SETUP_WDT(WDT_OFF);
   setup_oscillator(OSC_INTERNAL);
   setup_adc( ADC_CLOCK_INTERNAL );
   setup_adc_ports( ALL_ANALOG );
   set_adc_channel(0);
   
   while(TRUE)
   {
      delay_ms( 1 );
      value = read_adc();
      if (value > 100)
         output_high(PIN_B3);
      else
         output_low(PIN_B3);
    }
}


Am I still missing a fuse or setup? Any feedback would be great. It would be great if you can show me an example of setting up the internal oscillator properly. Also, I am able to control the output pins to either high or low so I thought it was only the adc not working instead of the internal oscillator. Thanks again.
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Wed Oct 20, 2010 2:41 pm     Reply with quote

What makes you think that the internal oscillator can be output at the OSC2 pin? According the the dsPIC33 oscillator diagram, it can't.
lackoblacko



Joined: 19 Oct 2010
Posts: 4

View user's profile Send private message

PostPosted: Wed Oct 20, 2010 3:12 pm     Reply with quote

I was only looking at the fuse options in CCS. Besides that point, the adc function still doesn't work.
lackoblacko



Joined: 19 Oct 2010
Posts: 4

View user's profile Send private message

PostPosted: Wed Oct 27, 2010 9:07 am     Reply with quote

turns out i needed to upgrade to PCWHD 4.114 to use the high speed ADC functions. Here is the posted code for anyone who needs help too.

Code:

#include <33FJ16GS504.h>

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES NOPUT                    //No Power Up Timer
#FUSES NOPROTECT                //Code not protected from reading
#FUSES FRC_PLL                  //Internal Fast RC Oscillator
#FUSES NOOSCIO                  //OSC2 is clock output
#FUSES ICSP1                    //ICD uses PGC1/PGD1 pins
#FUSES CKSFSM
#FUSES NOJTAG
#FUSES PR

#device adc=10

#use delay(clock=7370000)

void main()
{
   int16 value;
   
   SETUP_WDT(WDT_OFF);
   setup_oscillator(OSC_INTERNAL);
   setup_high_speed_adc( ADC_CLOCK_DIV_1 );
   setup_high_speed_adc_pair(0,INDIVIDUAL_SOFTWARE_TRIGGER);
   
   while(TRUE)
   {
      delay_ms( 1 );
      read_high_speed_adc(0,ADC_START_AND_READ,&value);
      if (value > 512)
         output_high(PIN_B3);
         else
         output_low(PIN_B3);
   }
   
}


Thanks for all the help
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