Connect with us

Unable to read ADC Value

Discussion in 'Microcontrollers, Programming and IoT' started by CuriousMind, Feb 10, 2017.

  1. CuriousMind

    CuriousMind

    2
    0
    Feb 8, 2017
    Hello All!
    I am trying to read ADCValue in my motorized slide pot.So,I have used a break point before the variable which I want to read the ADCValue.But,unfortunately no ADC value is showing in the variables window(not even the type of the variable).
    Can someone tell me what is wrong here?Thanks
    PS:using dsPIC33EP512MU810

    Below is my code:

    #include <stdio.h>
    #include <stdlib.h>
    #include <p33EP512MU810.h>
    #include <xc.h>
    #include <libpic30.h>
    /****************************CONFIGURATION****************************/
    _FOSCSEL(FNOSC_FRC);
    _FOSC(FCKSM_CSECMD & POSCMD_XT & OSCIOFNC_OFF & IOL1WAY_OFF);
    _FWDT(FWDTEN_OFF);_FPOR(FPWRT_PWR128 & BOREN_ON & ALTI2C1_ON & ALTI2C2_ON);
    _FICD(ICS_PGD1 & RSTPRI_PF & JTAGEN_OFF);
    void initAdc1(void);
    void Delay_us(unsigned int);
    int ADCValue, i, j;
    int main(void)
    {

    // Configure the device PLL to obtain 60 MIPS operation. The crystal frequency is 8 MHz.// Divide 8 MHz by 2, multiply by 60 and divide by 2. This results in Fosc of 120 MHz.// The CPU clock frequency is Fcy = Fosc/2 = 60 MHz.
    PLLFBD = 58;/* M = 60*/
    CLKDIVbits.PLLPOST = 0;/* N1 = 2*/
    CLKDIVbits.PLLPRE = 0;/* N2 = 2*/
    OSCTUN = 0;
    /* Initiate Clock Switch to Primary Oscillator with PLL (NOSC = 0x3) */
    __builtin_write_OSCCONH(0x03);
    __builtin_write_OSCCONL(0x01);
    while (OSCCONbits.COSC != 0x3);// Wait for Clock switch to occur
    while (_LOCK == 0);/* Wait for PLL lock at 40 MIPS */
    initAdc1();
    while(1)
    {

    Delay_us(100);// Sample for 100 us
    AD1CON1bits.SAMP = 0;// Start the conversion
    while (!AD1CON1bits.DONE);// Wait for the conversion to complete
    AD1CON1bits.DONE = 0;// Clear conversion done status bit
    ADCValue = ADC1BUF0;// Read the ADC conversion result
    }
    }
    void initAdc1(void)
    {

    /* Set port configuration */
    ANSELA = ANSELB = ANSELC = ANSELD = ANSELE = ANSELG = 0x0000;
    ANSELBbits.ANSB5 = 1;// Ensure AN5/RB5 is analog/* Initialize and enable ADC module */
    AD1CON1 = 0x0004;
    AD1CON2 = 0x0000;
    AD1CON3 = 0x000F;
    AD1CON4 = 0x0000;
    AD1CHS0 = 0x0005;
    AD1CHS123 = 0x0000;
    AD1CSSH = 0x0000;
    AD1CSSL = 0x0000;
    AD1CON1bits.ADON = 1;
    Delay_us(20);
    }
    void Delay_us(unsigned int delay)
    {
    for (i = 0; i < delay; i++)
    {__asm__ volatile ("repeat #39");
    __asm__ volatile ("nop");
    }
    }
     

    Attached Files:

Ask a Question
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Electronics Point Logo
Continue to site
Quote of the day

-