PlayaSlaya69
Junior Member level 1
Hi there. i've been having trouble with using the ADC of my dsPIC30F4011. All i'm trying to do is read the voltage a from potential divider circuit, betwen 0 and 5V, on channel AN0 in fractional format and display the result on PORTB. However, i get no output for any input voltage. Heres my code:
Even if i try shifting the result to view other bits of the result, i get no output. Also if i change the return type to integer, i get all the PORTB LED's coming on for any voltages, except 0V, and the intensity of the LED's on PORTB increases as the voltage on AN0 increases. This isn't right is it.
Hope someone can help me out with this issue, regards.
Code:
#include <p30fxxxx.h>
#include <dsp.h>
_FOSC(CSW_FSCM_OFF & XT_PLL16); //Run this project using an external crystal
//routed via the PLL in 16x multiplier mode
//For the 7.3728 MHz crystal we will derive a
//throughput of 7.3728e+6*16/4 = 29.4 MIPS(Fcy)
//,~33.9 nanoseconds instruction cycle time(Tcy).
_FWDT(WDT_OFF); //Turn off the Watch-Dog Timer.
_FBORPOR(MCLR_EN & PWRT_OFF); //Enable MCLR reset pin and turn off the
//power-up timers.
_FGS(CODE_PROT_OFF); //Disable Code Protection
//////////// Function Prototypes////////////////////
void ADCInit(void);
void delay(void);
fractional ReadADC(void);
////////////////////////////////////////////////////
//////////////Global Variable Initialization/////////////////////
fractional res;
////////////////////////////////////////////////////////////////
int main(void)
{
ADPCFG = 0xFFFF; // Configure AN pins as digital I/O
TRISB = 0; // Initialize PORTB as output
LATB = 0; // Set PORTB to zero
ADCInit();
while(1)
{
ADCON1bits.SAMP = 0;
delay();
res = ReadADC();
LATB = res;
delay();
}
return 0;
}
/////////////////////////Function Definitions/////////////////////////////////////////////////////
void ADCInit(void)
{
ADCON1bits.FORM = 0x03; // data output in fractional format
ADCON1bits.SSRC = 0x00; //clearing of SAMP bit ends sampling and starts conversion
ADCON1bits.ASAM = 1; // automatic start to sampling after conversion
ADCON2bits.VCFG = 0b000; // voltage reference as AVdd and Avss
ADCON2bits.SMPI = 0x00; // single sample and convert process
ADCON3bits.ADRC = 0; //ADC clock derived from system clock
ADCON3bits.ADCS = 0b1001; // conversion clock select = 9
ADCON3bits.SAMC = 3; //????????????
ADPCFGbits.PCFG2 = 0; //AN0 set as analog input
ADPCFGbits.PCFG3 = 0; //AN1 set as analog input
ADCSSL = 0; //no scanning required
ADCHSbits.CH0SA = 0x00; //select channel 0 for ADC
ADCON1bits.ADON = 1; //activate ADC module
}
fractional ReadADC(void)
{
while (!ADCON1bits.DONE); // wait to complete the conversion
return ADCBUF0;
}
void delay(void)
{
int x;
int y;
for (x=0;x<5000;x++)
{
for (y=0;y<10000;y++)
{
asm ("nop");
}
}
}
Even if i try shifting the result to view other bits of the result, i get no output. Also if i change the return type to integer, i get all the PORTB LED's coming on for any voltages, except 0V, and the intensity of the LED's on PORTB increases as the voltage on AN0 increases. This isn't right is it.
Hope someone can help me out with this issue, regards.