View previous topic :: View next topic |
Author |
Message |
lackoblacko
Joined: 19 Oct 2010 Posts: 4
|
(SOLVED) dsPIC33: adc_read() trouble |
Posted: Tue Oct 19, 2010 4:16 pm |
|
|
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
|
|
Posted: Wed Oct 20, 2010 12:19 am |
|
|
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
|
|
Posted: Wed Oct 20, 2010 9:45 am |
|
|
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
|
|
Posted: Wed Oct 20, 2010 2:41 pm |
|
|
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
|
|
Posted: Wed Oct 20, 2010 3:12 pm |
|
|
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
|
|
Posted: Wed Oct 27, 2010 9:07 am |
|
|
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 |
|
|
|