ADC dsPIC not working. Please help.

Thread Starter

Electron_23

Joined May 6, 2014
8
Hello everyone.

Im trying to use the ADC module of the dsPIC33ep512mu810, reading the potentiometer of the Explorar 16 board, and show the binary conversion in PORT A (LEDs). Up to now I did not have success and I don't know whats missing or wrong. Could someone help please?

Rich (BB code):
//DEFINES or PARAMETERS
#define FOSC 8000000 //OSCILLATION FREQUENCY
#define PLL 1 //SETS PLL
#define FCY (FOSC*PLL/2) //CYCLE FREQUENCY



#include <stdio.h>
#include <stdlib.h>
#include <p33ep512mu810.h>

//MACROS or CONFIGURATION BITS
_FOSCSEL(FNOSC_PRI & IESO_OFF)
// Primary oscillator as source
// Start up with user-selected oscillator source
_FOSC(FCKSM_CSDCMD & POSCMD_XT & OSCIOFNC_ON)
// Both Clock switching and Fail-safe Clock Monitor are disabled
// XT Crystal Oscillator Mode
// OSC2 is general purpose digital I/O pin
_FWDT(FWDTEN_OFF)
// Watchdog timer enabled/disabled by user software
_FPOR(FPWRT_PWR16 & BOREN_ON)
// Power-on Reset Timer Value and bor enabled
_FGS(GWRP_OFF & GSS_OFF & GSSK_OFF)
// General Segment may be written
// General Segment Code protect is disabled
// General Segment Write Protection and Code Protection is Disabled


// FUNCTION DECLARATIONS
void adc(void);
void delay_ms(long ms);


/*
 * 
 */
int main() {
    TRISA = 0;
    adc();

    //loop to sample and acquire data
    while(1){
        AD1CON1bits.SAMP = 1;   //sampling
        delay_ms(10);            //sample time
        AD1CON1bits.SAMP = 0;   //holding

        while(!AD1CON1bits.DONE);   //is the conversion done?
        PORTA = ADC1BUF1;  //data is shown in port A (LEDs)
    }

    return (EXIT_SUCCESS);
}



void adc(void){

    ANSELA = ANSELB = ANSELC = ANSELD = ANSELE = ANSELG = 0x0000;
    // Set ports A, B, C, D, E, G as I/O digital ports.
    ANSELBbits.ANSB5 = 1;  //makes AN5/RB5 analog input.

    AD1CON1bits.AD12B = 1; // Sets 12 bits ADC.

    AD1CON1 = 0x0000;
    AD1CON2 = 0x0000;
    AD1CON3 = 0x000F;
    AD1CON4 = 0x0000;
    AD1CHS0 = 0x0005;
    AD1CHS123 = 0x0000;
    AD1CSSH = 0x0000;
    AD1CSSL = 0x0000;
    AD1CON1bits.ADON = 1;  //enables ADC
    delay_ms(10);
}



void delay_ms(long ms){

    T1CON = 0;   //reset timer
    TMR1 = 0;    //load timer count register to 0

    IPC0bits.T1IP = 1; // interrupt priority count
    PR1 = ((FCY/256)*ms)/1000; //20ms delay. Comes from (((Fosc/4)/prescaler)*20ms)/(1000ms)
    IFS0bits.T1IF = 0;
    IEC0bits.T1IE = 0;
    T1CON = 0X8030; //hexadecimal of 1000000000110000
    // which means start the timer and select 256 prescaler
    while(IFS0bits.T1IF == 0);

}
Thank you very much in advance.
 

THE_RB

Joined Feb 11, 2008
5,438
I don't use that chip, but in 90% of cases the reason a PIC ADC is not working right is becuase it has not been configured right. :)

That also includes the port pins configuration, if some other hardware feature has taken over the pin and forced it to be an output or force it to another function.
 
Top