Hot![SOLVED]PIC24FV16KM202 ADC wont start sampling HELP!

Author
Eventhorizon.m
New Member
  • Total Posts : 4
  • Reward points : 0
  • Joined: 2016/09/08 07:23:08
  • Location: 0
  • Status: offline
2018/09/17 10:20:22 (permalink)
0

[SOLVED]PIC24FV16KM202 ADC wont start sampling HELP!

I couldn't make my MCU start sampling in auto sampling mode and i cant figure out why!
here is my code:
int main(void) 
{
 Setup_OSC();
 Setup_Ports();
 Setup_ADC();
 //Setup_MCCPs();
 
 while(1)
 {
  Nop();
  for(i=0;i<AN_INPUT1;i++){
   __delay_us(150);
  }
  
  DIR_OUT1 =! DIR_OUT1;
 }
 
 return 0;
}

void __attribute__((interrupt, no_auto_psv)) _ADC1Interrupt(void){
 _AD1IF = 0; // Clear Interrupt Flag.
 Nop();
}
 
 
 
 
 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~/
void Setup_OSC(void)
{
//The settings below setup the oscillator and PLL for 16 MIPS as
//follows:
// RCDIV FRC/1; DOZE 1:8; DOZEN disabled; ROI disabled;
CLKDIV = 0x3000;
// TUN Center frequency;
OSCTUN = 0x0000;
// ROEN disabled; ROSEL disabled; RODIV Base clock value; ROSSLP disabled;
REFOCON = 0x0000;
// CF no clock failure; NOSC FRCPLL; SOSCEN disabled; SOSCDRV disabled; CLKLOCK unlocked; OSWEN Switch is Complete;
__builtin_write_OSCCONH((uint8_t) ((0x0100 >> _OSCCON_NOSC_POSITION) & 0x00FF));
__builtin_write_OSCCONL((uint8_t) ((0x0100 | _OSCCON_OSWEN_MASK) & 0xFF));
// Wait for Clock switch to occur
while (OSCCONbits.OSWEN != 0);
 
}
 
 
 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~/
void Setup_Ports(void)
{
 /****************************************************************************
     * Setting the Output Latch SFR(s)
     ***************************************************************************/
    LATA = 0x0000;
    LATB = 0x0000;

    /****************************************************************************
     * Setting the GPIO Direction SFR(s)
     ***************************************************************************/
    TRISA = 0xFFFF; // Default all pins Inputs.
    TRISB = 0xFFFF; // Default all pins Inputs.
 
 TRISBbits.TRISB2 = 0; // Output DIR_OUT2.
 TRISBbits.TRISB5 = 0; // Output PULSE_OUT1.
 TRISBbits.TRISB6 = 0; // Output DIR_OUT1.
 TRISBbits.TRISB11 = 0; // Output PULSE_OUT2.

    /****************************************************************************
     * Setting the Weak Pull Up and Weak Pull Down SFR(s)
     ***************************************************************************/
    CNPD1 = 0x0000;
    CNPD2 = 0x0000;
    CNPU1 = 0x0000;
    CNPU2 = 0x0000;
 
 CNPU1bits.CN4PUE = 1; // Pullup nMID_POINT_DIR_EN
 CNPU1bits.CN5PUE = 1; // Pullup nPOS/SPEED_MODE
 CNPU1bits.CN7PUE = 1; // Pullup nPOS/SPEED_X4
 CNPU1bits.CN9PUE = 1; // Pullup nDIR_CCW2
 CNPU1bits.CN11PUE = 1; // Pullup nDIR_CW1
 CNPU1bits.CN12PUE = 1; // Pullup nDIR_CCW1
 CNPU1bits.CN13PUE = 1; // Pullup nCW_LIMIT1
 CNPU1bits.CN14PUE = 1; // Pullup nCCW_LIMIT1
 
 CNPU2bits.CN16PUE = 1; // Pullup nDIR_CW2
 CNPU2bits.CN21PUE = 1; // Pullup nCW_LIMIT2
 CNPU2bits.CN22PUE = 1; // Pullup nCCW_LIMIT2
 CNPU2bits.CN23PUE = 1; // Pullup nHOLD_F
 
    /****************************************************************************
     * Setting the Open Drain SFR(s)
     ***************************************************************************/
    ODCA = 0x0000;
    ODCB = 0x0000;
 
    /****************************************************************************
     * Setting the Analog/Digital Configuration SFR(s)
     ***************************************************************************/
 
 ANSA = 0; // Default all pins to digital.
 ANSB = 0; // Default all pins to digital.
 
 ANSAbits.ANSA0 = 1; // AN_INPUT1
 ANSAbits.ANSA1 = 1; // AN_INPUT2
 ANSAbits.ANSA2 = 1; // AN_DECELERATION
 ANSAbits.ANSA3 = 1; // AN_ACCELERATION
 ANSAbits.ANSA4 = 1; // AN_FREQ_UPPER_LIMIT
 ANSBbits.ANSB4 = 1; // AN_FREQ_LOWER_LIMIT
 
 
// PPS_Unlock();
// PPS_Lock();
 
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~/
void Setup_ADC(void)
{
 AD1CON1bits.MODE12 = 1; // 12-bit A/D operation.
 AD1CON1bits.SSRC = 1; // Internal counter ends sampling and starts conversion (auto-convert).
 AD1CON1bits.ASAM = 1; // Sampling begins immediately after the last conversion; SAMP bit is auto-set.
 
 AD1CON2bits.PVCFG = 0; // AVDD as A/D Converter Positive Voltage Reference
 AD1CON2bits.NVCFG0 = 0; // AVSS as A/D Converter Negative Voltage Reference.
 AD1CON2bits.BUFREGEN = 1; // Conversion result is loaded into a buffer location determined by the converted channel.
 AD1CON2bits.CSCNA = 1; // Scan Input Selections for CH0+ S/H Input for MUX A Setting bit.
 AD1CON2bits.SMPI = 5; // Interrupts at the completion of the conversion for each 6th sample.
 AD1CON2bits.ALTS = 0; // Always uses channel input selects for Sample A.
 
 AD1CON3bits.ADCS = 16; // A/D Conversion Clock Select bits. (T_Acq = 16*100ns = 1.6 us)
 AD1CON3bits.SAMC = 15; // Auto-Sample Time Select bits. (16*6.25ns = 100ns)
 
 AD1CON5bits.ASEN = 1; // Auto-scan is enabled.
 
 //AD1CHSbits.CH0SA = 0; // This bits are ignored and the channels specified by the AD1CSSL register are sequentially sampled.
 AD1CHSbits.CH0NA = 0; // AVSS as MUX A Channel 0 Negative Input.
 
 AD1CSSLbits.CSS0 = 1; // Scan RA0 / AN_INPUT1.
 AD1CSSLbits.CSS1 = 1; // Scan RA1 / AN_INPUT2.
 AD1CSSLbits.CSS13 = 1; // Scan RA2 / DECELERATION.
 AD1CSSLbits.CSS14 = 1; // Scan RA3 / ACCELERATION.
 AD1CSSLbits.CSS15 = 1; // Scan RB4 / FREQ_LOWER_LIMIT.
 AD1CSSHbits.CSS16 = 1; // Scan RA4 / FREQ_UPPER_LIMIT.
 
 _AD1IF = 0; // Clear Interrupt Flag.
 _AD1IE = 1; // Enable Interrupt.
 
 AD1CON1bits.ADON = 1; // A/D Converter Start Operating.
 
}

post edited by Eventhorizon.m - 2018/09/17 11:26:24
#1

1 Reply Related Threads

    Eventhorizon.m
    New Member
    • Total Posts : 4
    • Reward points : 0
    • Joined: 2016/09/08 07:23:08
    • Location: 0
    • Status: offline
    Re: PIC24FV16KM202 ADC wont start sampling HELP! 2018/09/17 10:46:05 (permalink)
    4 (1)
    OK i figured out what the problem was..
    i have 2 mistakes:
    First: the AD1CON1bits.SSRC bits was wrong (=1) , it should be AD1CON1bits.SSRC = 7;
    second: the AD1CON5bits.ASEN bit was set and that's also wrong, it must be cleared because of the following strange note in the Datasheet...  
    (When using auto-scan with Threshold Detect (ASEN = 1), do not configure the sample clock source to
    Auto-Convert mode (SSRC<3:0> = 7). Any other available SSRC selection is valid. To use auto-convert as
    the sample clock source (SSRC<3:0> = 7), make sure ASEN is cleared.)
    #2
    Jump to:
    © 2018 APG vNext Commercial Version 4.5