ADC not giving the result on dsPIC33fj16gs504

Author
Cime
Starting Member
  • Total Posts : 47
  • Reward points : 0
  • Joined: 2007/05/22 00:09:02
  • Location: Slovenia, Ljubljana
  • Status: offline
2011/08/30 03:48:08 (permalink)
0

ADC not giving the result on dsPIC33fj16gs504

Hi,

I am using dsPIC33fjgs504, MPLAB 8.10, C30 v3.24. The external cristal works at 7.372 MHz.
I have problems to get result from the ADC.
The triger source is PWM generator 1, which is working ok, so I can enter into the interrupt routine.
The problem could be in the initialization of the ADC or with the auxiliary clock.
ADC init code:

void init_ADC (void)
{
TRISBbits.TRISB10        = 1;               
TRISBbits.TRISB9        = 1;               

ADPCFGbits.PCFG4        = 0;               
ADPCFGbits.PCFG5         = 0;               

//AD control register
ADCONbits.ADSIDL        = 0;                    //Continue module operation in Idle mode
//ADCONbits.SLOWCLK        = 1;                    //ADC is clocked by the auxiliary PLL (ACLK) 
ADCONbits.GSWTRG        = 0;                    //When this bit is set, it triggers conversions if selected by the TRGSRC<4:0> bits in the ADCPCx
ADCONbits.FORM            = 0;                    //0 = Integer (DOUT = 0000 00dd dddd dddd)
ADCONbits.EIE            = 0;                    //Interrupt is generated after second conversion is completed
ADCONbits.ORDER            = 0;                    //Even numbered analog input is converted first, followed by conversion of odd numbered input ????
ADCONbits.SEQSAMP        = 0;                    //Shared S&H circuit and dedicated S&H circuit are sampled simultaneously, if the shared S&H circuit is not ????
                                                //currently busy with an existing conversion process. If the shared S&H circuit is busy at the time the dedicated
                                                //S&H circuit is sampled, the shared S&H circuit will sample at the start of the new conversion cycle
ADCONbits.ASYNCSAMP        = 0;                    //The dedicated S&H circuit starts sampling when the trigger event is detected and completes the sampling process
                                                //in two ADC clock cycles
ADCONbits.ADCS            = 0x2;                    //ADC Conversion Clock Divider Select bits

ADSTATbits.P2RDY        = 0;                    //Bit is set when data is ready in buffer, cleared when a ‘0’ is written to this bit

ADCPC2bits.IRQEN5        = 0;                    //IRQ is generated, AN2, AN3
ADCPC2bits.PEND5        = 0;                    //Conversion is complete???
ADCPC2bits.SWTRG5        = 0;                    //Conversion is not started
ADCPC2bits.TRGSRC5        = 0;                    //No conversion enabled
ADCPC2bits.IRQEN4        = 1;                    //Enable the IRQ generation when requested conversion of channels AN1 and AN0 is completed
ADCPC2bits.PEND4        = 0;                    //Conversion is complete
ADCPC2bits.SWTRG4        = 0;                    //Conversion is not started
ADCPC2bits.TRGSRC4        = 0x4;                    //PWM Generator 1 primary trigger selected

//nastavitve prekinitev
IFS7bits.ADCP2IF        = 0;                    //Clear ADC Pair 2 interrupt flag
IPC28bits.ADCP2IP        = 5;                    //Set ADC Pair 2 interrupt priority
IEC7bits.ADCP2IE        = 1;                    //Enable the ADC Pair 2 interrupt


ADCONbits.ADON              = 0;                    //ADC module is not operating (pres START key)
}


Oscilator setup:

void init_Oscilator(void)
{
/*
If the Primary PLL is used as a source for the auxiliary clock, then the Primary PLL
should be configured up to a maximum operation of 30 MIPS or less.

Fosc = Fin (M/(N1*N2)) = 7.372 * (32/8) = 7.372 * 4 = 29,488 MHz

Fvco = Fin * (M/N1) = 7.372 * 16 = 117.952 MHz
*/

    PLLFBD                 = 30;                    //M = 32
    CLKDIVbits.PLLPOST    = 1;                    //N2 = 4
    CLKDIVbits.PLLPRE    = 0;                    //N1 = 2

    //switch from FRC to Primary HS oscilator
    __builtin_write_OSCCONH(0x03);
    __builtin_write_OSCCONL(0x01);

    while(OSCCONbits.COSC != 0b011){};

    while(OSCCONbits.LOCK != 1) {};



/*
 Fvco (117.952 MHz)
*/
    ACLKCONbits.FRCSEL    = 0;                    //Input clock source is determined by ASRCSEL bit setting
    ACLKCONbits.ASRCSEL    = 0;                    //No clock input is selected
    ACLKCONbits.ENAPLL    = 1;                    //Auxiliary PLL is enabled
    ACLKCONbits.SELACLK    = 0;                    //PLL output (FVCO) provides the source clock for the Auxiliary Clock Divider
    ACLKCONbits.APSTSCLR= 7;                    //Divided by 1

    while(ACLKCONbits.APLLCK !=1){};
}



This is the interrupt routine:

void __attribute__((__interrupt__,no_auto_psv)) _ADCP2Interrupt (void)
{
ADSTATbits.P2RDY        = 0;

Measure_ch4             = ADCBUF4;
Measure_ch5             = ADCBUF5;

adcin++;

if(adcin >= 0xfffe)
adcin = 0;
IFS7bits.ADCP2IF        = 0;

}


Could somebody take a look to my code please?
Thanks
#1

7 Replies Related Threads

    sanne
    New Member
    • Total Posts : 17
    • Reward points : 0
    • Joined: 2009/07/08 03:52:37
    • Location: 0
    • Status: offline
    Re:ADC not giving the result on dsPIC33fj16gs504 2011/08/30 22:27:56 (permalink)
    0
    Hi Cime,
    In your ACLK configuration you are selecting Fvco to generate ACLK, I don't know why this would not work.
    You could set ARCSEL, clear FRCSEL, set ENAPLL and set SELACLK to use the cristal as the base for ACLK.
    This works for me anyways.
     
    I think that if the AD would not be clocked, you would not be getting the interrupt either.
     
    Hope this helps,
     
    Sanne
    post edited by sanne - 2011/08/30 22:42:07
    #2
    Cime
    Starting Member
    • Total Posts : 47
    • Reward points : 0
    • Joined: 2007/05/22 00:09:02
    • Location: Slovenia, Ljubljana
    • Status: offline
    Re:ADC not giving the result on dsPIC33fj16gs504 2011/08/31 00:05:30 (permalink)
    0
    Hi sanne,
    thanks for the reply.
    I have set the ARCSEL, clear FRCSEL, set ENAPLL and set SELACLK but it did not work and now I get no interrupt.
    I am realy confused, I think I will read the section 42. Oscillator again.pink

    Thanks
    Cime



    #3
    sanne
    New Member
    • Total Posts : 17
    • Reward points : 0
    • Joined: 2009/07/08 03:52:37
    • Location: 0
    • Status: offline
    Re:ADC not giving the result on dsPIC33fj16gs504 2011/08/31 01:26:31 (permalink)
    0
    Hi Cime,

    If you are not getting any interrupts at all anymore the adc is no longer being clocked...
    This is the oscillator code that works for me... (external 8MHz crystal)
     
          #if FIN==8000000
     /* Configure Oscillator to operate the device at 40Mhz */
     /* Fin = 8MHz */
     /* Frev = Fin/PLLPRE+2 = 8M/2= 4MHz */
     /* Fvco = Frev * (PLLFBD+2) = 4M * 40 = 160MHz */
     /* Fosc = Fvco / (2*(PLLPOST+1)) = 160M/2 = 80MHz */
     /* Fcy  = Fosc/2 = 80M/2 = 40MHz */
     /* Configure PLL prescaler, PLL postscaler, PLL divisor */
     PLLFBD = 38;      /* M = PLLFBD + 2 */
     CLKDIVbits.PLLPOST = 0;   /* N1 = 2 */
     CLKDIVbits.PLLPRE = 0;    /* N2 = 2 */ #else
        #error Do not know how to configure main oscillator!
    #endif     /* New Oscillator External Crystal w/ PLL */
        __builtin_write_OSCCONH(0x03);
        __builtin_write_OSCCONL(0x01);     /* Wait for Clock switch to occur */
     while(OSCCONbits.COSC != 0b011);
     /* Wait for Pll to Lock */
        while(OSCCONbits.LOCK != 1);  /* ADC and PWM clock for 120MHz */
     /* ((primary OSC * 16) / APSTSCLR ) = (7.68M * 16) / 1 = ~ 122.88MHz */
     /* ((primary OSC * 16) / APSTSCLR ) = (8.00M * 16) / 1 = ~ 128MHz */  /* Primary Oscillator provides the clock for APLL */
     ACLKCONbits.ASRCSEL = 1;
     /* ASRCSEL provides input for Auxiliary PLL (x16) */
     ACLKCONbits.FRCSEL = 0;
     /* Auxiliary Oscillator provides clock source for PWM & ADC */
     ACLKCONbits.SELACLK = 1;
     /* Divide Auxiliary clock by 1 */
     ACLKCONbits.APSTSCLR = 7;
     /* Enable Auxiliary PLL */
     ACLKCONbits.ENAPLL = 1;  /* Wait for Auxiliary PLL to Lock */
     while (ACLKCONbits.APLLCK != 1);

     
    Hope this helps.
     
    Regards,
     
    Sanne
    #4
    Cime
    Starting Member
    • Total Posts : 47
    • Reward points : 0
    • Joined: 2007/05/22 00:09:02
    • Location: Slovenia, Ljubljana
    • Status: offline
    Re:ADC not giving the result on dsPIC33fj16gs504 2011/09/01 04:58:50 (permalink)
    0
    After changing crystals, oscilator settings and coping your program, I can not get any ADC interrupt.
    PWM genrator is working ok, so the problem could be something else (not the oscilator).
    Here is my program:

    #include"p33FJ16GS504.h"

    /* Configuration Bit Settings */

    _FOSCSEL(FNOSC_FRC)
    _FOSC(POSCMD_HS & OSCIOFNC_OFF & IOL1WAY_ON & FCKSM_CSECMD)
    _FPOR(FPWRT_PWR32)
    _FGS(GWRP_OFF & GSS_OFF)
    _FBS(BSS_NO_FLASH)
    _FWDT(FWDTEN_OFF)

    /*PROTOTIPI-inicializacije*/
    void init_Oscilator(void);
    void init_UART(void);
    void init_PWM(void);
    void init_ADC(void);
    void init_port(void);
    void init_spremenljivke(void);

    /*PROTOTIPI-funkcije*/
    void komande(void);

    /*PROTOTIPI-prekinitve*/
    void __attribute__((__interrupt__,no_auto_psv)) _ADCP2Interrupt (void);

    //PWM
    unsigned int Frekvenca_startna = 3000;
    unsigned int Meritev_ch4        = 0;
    unsigned int Meritev_ch5       = 0;

    /*Testne spremenljivke*/
    unsigned long adcin               = 0;

    int main (void)
    {
    init_Oscilator();
    init_PWM();
    init_ADC();
    init_port();
    init_spremenljivke();

    while(1)
    {
        komande();
    }


    }

    void komande(void)
    {   
    if(PORTBbits.RB15 == 0)
        {
       
            Controls.Start = 1;
        //Vklop tranzistorjev PWMgen1
        IOCON1bits.OVRENH         = 0;                     // Remove override for PWM1H output
        IOCON1bits.OVRENL         = 0;                     // Remove override for PWM1L output
        IOCON1bits.PENH         = 1;                     // Assign pin ownership of PWM1H/RA4 to PWM module
        IOCON1bits.PENL         = 1;                     // Assign pin ownership of PWM1L/RA3 to PWM module
       
        //Vklop tranzistorjev PWMgen2
        IOCON2bits.OVRENH         = 0;                     // Remove override for PWM1H output
        IOCON2bits.OVRENL         = 0;                     // Remove override for PWM1L output
        IOCON2bits.PENH         = 1;                     // Assign pin ownership of PWM1H/RA4 to PWM module
        IOCON2bits.PENL         = 1;                     // Assign pin ownership of PWM1L/RA3 to PWM module

    ADSTATbits.P2RDY        = 0;                    //Bit is set when data is ready in buffer, cleared when a ‘0’ is written to this bit
    IFS7bits.ADCP2IF        = 0;                    //Clear ADC Pair 2 interrupt flag   
    ADCONbits.ADON              = 1;                    //ADC module is operating
        }
        if(PORTCbits.RC8 == 0)
        {
        //Izklop tranzistorjev PWMgen1
        IOCON1bits.OVRENH        = 1;                    //PWM generator provides data for output on PWMxH pin
        IOCON1bits.OVRENL        = 1;                    //PWM generator provides data for output on PWMxL pin
        IOCON1bits.OVRDAT        = 0x3;                    //Izhodna pina sta postavljena na visoko stanje
       
        //Izklop tranzistorjev PWMgen1
        IOCON2bits.OVRENH        = 1;                    //OVRDAT<1> provides data for output on PWMxH pin
        IOCON2bits.OVRENL        = 1;                    //OVRDAT<0> provides data for output on PWMxL pin
        IOCON2bits.OVRDAT        = 0x3;                    //Izhodna pina sta postavljena na visoko stanje
           
    ADSTATbits.P2RDY        = 1;                    //Bit is set when data is ready in buffer, cleared when a ‘0’ is written to this bit
    IFS7bits.ADCP2IF        = 0;                    //Clear ADC Pair 2 interrupt flag
           
        }

    }//end void komande(void)

    void init_port(void)
    {
    /******ADPCFG, pini digitalni ali analogni*****/
    ADPCFGbits.PCFG8 = 1;                            //pin RC1 digital


    /**************digitalni vhodi*****************/
    //PORTB
    TRISBbits.TRISB15        = 1;                    //DI01-key START
    TRISBbits.TRISB4        = 1;                    //DI03-key RESET
       
    //PORTC
    TRISCbits.TRISC8        = 1;                    //DI02-key STOP

    /**************digitalni izhodi*****************/
    TRISCbits.TRISC1        = 0;                    //relay1

    //zacetno stanje izhodov
    LATCbits.LATC1 = 0;

    }

    void init_spremenljivke(void)
    {

    /**************PWM*************************/
    //periodni register
    PTPER                    = Frekvenca_startna;    //16-bitni periodni register

    //Duty cycle register
    PDC1                         = PTPER/2;                //PWM Master Duty Cycle Register
    PDC2                         = PTPER/2;                //PWM Master Duty Cycle Register

    //Dead time registers
    DTR1                     = 100;                    //dead time PWM1
    ALTDTR1                 = 100;                    //alt dead time PWM1

    DTR2                     = 100;                    //dead time PWM2
    ALTDTR2                 = 100;                    //alt dead time PWM2

    //FAZNI REGISTRI
    PHASE1                     = 0;                     //fazni register PWM1
    PHASE2                     = PTPER/2;                 //fazni register PWM2

    }

    //ADC par 2
    void __attribute__((__interrupt__,no_auto_psv)) _ADCP2Interrupt (void)
    {
    ADSTATbits.P2RDY        = 0;

    Meritev_ch4             = ADCBUF4;
    Meritev_ch5             = ADCBUF5;

    IFS7bits.ADCP2IF        = 0;                    //Clear ADC Pair 2 interrupt flag

    adcin++;

    if(adcin >= 0xffffe)
    adcin = 0;

    }

    void init_PWM (void)
    {
    //PTCON register
    PTCONbits.PTSIDL        = 0;                //PWM time base runs in CPU Idle mode
    PTCONbits.SESTAT        = 0;                //Special Event Interrupt is not pending
    PTCONbits.SEIEN            = 0;                //Special Event Interrupt is disabled
    PTCONbits.EIPU            = 1;                //Active Period register is updated immediately
    PTCONbits.SYNCPOL        = 0;                //SYNCIx/SYNCO is active-high
    PTCONbits.SYNCEN        = 0;                //External synchronization of primary time base is disabled
    //PTCONbits.SYNCSRC        = 0;                //Synchronous Source Selection bits
    PTCONbits.SEVTPS        = 1;                //Postscaler generates Special Event Trigger on every second compare match event

    //PTCON2 register
    PTCON2bits.PCLKDIV        = 2;                //PWM Input Clock Prescaler (Divider) Select bits

    //SEVTCMP = PTPER - 300;                    //PWM Special Event Compare Register

    //PWMCON1 register
    PWMCON1bits.FLTSTAT        = 0;                //No Fault interrupt is pending
    PWMCON1bits.CLSTAT        = 0;                //No current-limit interrupt is pending
    PWMCON1bits.TRGSTAT        = 0;                //No trigger interrupt is pending
    PWMCON1bits.FLTIEN        = 0;                //Fault interrupt is disabled and FLTSTAT bit is cleared
    PWMCON1bits.CLIEN        = 0;                //Current-Limit Interrupt Enable bit
    PWMCON1bits.TRGIEN        = 0;                //Trigger event interrupts are disabled and TRGSTAT bit is cleared
    PWMCON1bits.ITB            = 0;                //PTPER register provides timing for this PWM generator
    PWMCON1bits.MDCS        = 0;                //PDCx and SDCx registers provide duty cycle information for this PWM generator
    PWMCON1bits.DTC            = 0;                //Positive dead time actively applied for all output modes
    //PWMCON1bits.DTCP        = 0;                //If DTCMPx = 0, PWMHx is shortened and PWMLx is lengthened.
                                                //If DTCMPx = 1, PWMLx is shortened and PWMHx is lengthened.                                       
    //PWMCON1bits.MTBS        = 0;                //PWM generator uses the primary master time base for synchronization and as the clock source
                                                //for the PWM generation logic
    PWMCON1bits.CAM            = 0;                //Edge-Aligned mode is enabled
    PWMCON1bits.XPRES        = 0;                //External pins do not affect PWM time base
    PWMCON1bits.IUE            = 1;                //Updates to the active MDC/PDCx/SDCx registers are synchronized to the PWM time base --ERRATA!!!!


    //PWMCON2 register
    PWMCON2bits.FLTSTAT        = 0;                //No Fault interrupt is pending
    PWMCON2bits.CLSTAT        = 0;                //No current-limit interrupt is pending
    PWMCON2bits.TRGSTAT        = 0;                //No trigger interrupt is pending
    PWMCON2bits.FLTIEN        = 0;                //Fault interrupt is disabled and FLTSTAT bit is cleared
    PWMCON2bits.CLIEN        = 0;                //Current-Limit Interrupt Enable bit
    PWMCON2bits.TRGIEN        = 0;                //Trigger event interrupts are disabled and TRGSTAT bit is cleared
    PWMCON2bits.ITB            = 0;                //PTPER register provides timing for this PWM generator
    PWMCON2bits.MDCS        = 0;                //PDCx and SDCx registers provide duty cycle information for this PWM generator
    PWMCON2bits.DTC            = 0;                //Positive dead time actively applied for all output modes
    //PWMCON2bits.DTCP        = 0;                //If DTCMPx = 0, PWMHx is shortened and PWMLx is lengthened.
                                                //If DTCMPx = 1, PWMLx is shortened and PWMHx is lengthened.                                       
    //PWMCON2bits.MTBS        = 0;                //PWM generator uses the primary master time base for synchronization and as the clock source
                                                //for the PWM generation logic
    PWMCON2bits.CAM            = 0;                //Edge-Aligned mode is enabled
    PWMCON2bits.XPRES        = 0;                //External pins do not affect PWM time base
    PWMCON2bits.IUE            = 1;                //Updates to the active MDC/PDCx/SDCx registers are synchronized to the PWM time base --ERRATA!!!!


    /***Zagotavljanje pravilnega stanja izhodov PWM1 modula********************/
    TRISAbits.TRISA4 = 1;                         // Configure PWM1H/RA4 as digital input
                                                // Ensure output is in safe state using pull-up or
                                                // pull-down resistors
    TRISAbits.TRISA3 = 1;                         // Configure PWM1L/RA3 as digital input
                                                // Ensure output is in safe state using pull-up or
                                                // pull-down resistors

    IOCON1bits.OVRDAT = 0x3;                     // Configure override state of the PWM outputs to
                                                // desired safe state.
    IOCON1bits.OVRENH = 1;                         // Override PWM1H output
    IOCON1bits.OVRENL = 1;                         // Override PWM1L output
    /**************************************************************************/

    /***Zagotavljanje pravilnega stanja izhodov PWM2 modula********************/
    TRISBbits.TRISB13 = 1;                         // Configure PWM2H/RB13 as digital input
                                                // Ensure output is in safe state using pull-up or
                                                // pull-down resistors
    TRISBbits.TRISB14 = 1;                         // Configure PWM2L/RB14 as digital input
                                                // Ensure output is in safe state using pull-up or
                                                // pull-down resistors
    IOCON2bits.OVRDAT = 0x3;                     // Configure override state of the PWM outputs to
                                                // desired safe state.
    IOCON2bits.OVRENH = 1;                         // Override PWM1H output
    IOCON2bits.OVRENL = 1;                         // Override PWM1L output
    /***************************************************************************/


    //PWM1 I/O Control Register
    IOCON1bits.PENH            = 1;                //PWM module controls PWMxH pin
    IOCON1bits.PENL            = 1;                //PWM module controls PWMxL pin
    IOCON1bits.POLH            = 0;                //PWMxH pin is active-high
    IOCON1bits.POLL            = 0;                //PWMxL pin is active-high
    IOCON1bits.PMOD            = 0;                //PWM I/O pin pair is in the Complementary PWM Output mode
    IOCON1bits.OSYNC        = 0;                //Output overrides via the OVDDAT<1:0> bits occur on next CPU clock boundary
    IOCON1bits.SWAP            = 0;                //PWMxH and PWMxL pins are mapped to their respective pins


    //PWM2 I/O Control Register
    IOCON2bits.PENH            = 1;                //PWM module controls PWMxH pin
    IOCON2bits.PENL            = 1;                //PWM module controls PWMxL pin
    IOCON2bits.POLH            = 0;                //PWMxH pin is active-high
    IOCON2bits.POLL            = 0;                //PWMxL pin is active-high
    IOCON2bits.PMOD            = 0;                //PWM I/O pin pair is in the Complementary PWM Output mode
    IOCON2bits.OSYNC        = 0;                //Output overrides via the OVDDAT<1:0> bits occur on next CPU clock boundary
    IOCON2bits.SWAP            = 0;                //PWMxH and PWMxL pins are mapped to their respective pins

    PTCONbits.PTEN            = 1;                //PWM module is enabled

    }

    void init_ADC (void)
    {

    /*****definicija analognih pinov kot vhodov******/
    TRISBbits.TRISB10        = 1;                //AN5 je vhod
    TRISBbits.TRISB9        = 1;                //AN4 je vhod

    ADPCFGbits.PCFG4        = 0;                //AN4 je analogen
    ADPCFGbits.PCFG5         = 0;                //AN5 je analogen

    //AD control register
    ADCONbits.ADSIDL        = 0;                    //Continue module operation in Idle mode
    ADCONbits.SLOWCLK        = 1;                    //ADC is clocked by the auxiliary PLL (ACLK)  ERRATA!!!
    ADCONbits.GSWTRG        = 0;                    //When this bit is set, it triggers conversions if selected by the TRGSRC<4:0> bits in the ADCPCx
    ADCONbits.FORM            = 0;                    //0 = Integer (DOUT = 0000 00dd dddd dddd)
    ADCONbits.EIE            = 0;                    //Interrupt is generated after second conversion is completed
    ADCONbits.ORDER            = 0;                    //Even numbered analog input is converted first, followed by conversion of odd numbered input ????
    ADCONbits.SEQSAMP        = 0;                    //Shared S&H circuit and dedicated S&H circuit are sampled simultaneously, if the shared S&H circuit is not ????
                                                    //currently busy with an existing conversion process. If the shared S&H circuit is busy at the time the dedicated
                                                    //S&H circuit is sampled, the shared S&H circuit will sample at the start of the new conversion cycle
    ADCONbits.ASYNCSAMP        = 0;                    //The dedicated S&H circuit starts sampling when the trigger event is detected and completes the sampling process
                                                    //in two ADC clock cycles
    ADCONbits.ADCS            = 5;                    //ADC Conversion Clock Divider Select bits

    /*
    //nastavitve za 0. conversion pair, AN0-AN1
    ADSTATbits.P0RDY        = 0;                    //Bit is set when data is ready in buffer, cleared when a ‘0’ is written to this bit

    ADPCFGbits.PCFG0        = 0;                    //Port pin in Analog mode, port read input disabled, ADC samples pin voltage
    ADPCFGbits.PCFG1        = 0;                    //Port pin in Analog mode, port read input disabled, ADC samples pin voltage

    ADCPC0bits.IRQEN1        = 0;                    //IRQ is not generated, AN2, AN3
    ADCPC0bits.PEND1        = 0;                    //Conversion is complete???
    ADCPC0bits.SWTRG1        = 0;                    //Conversion is not started
    ADCPC0bits.TRGSRC1        = 0;                    //No conversion enabled
    ADCPC0bits.IRQEN0        = 1;                    //Enable the IRQ generation when requested conversion of channels AN1 and AN0 is completed
    ADCPC0bits.PEND0        = 0;                    //Conversion is complete
    ADCPC0bits.SWTRG0        = 0;                    //Conversion is not started
    ADCPC0bits.TRGSRC0        = 0x4;                    //PWM Generator 1 primary trigger selected
    */

    //nastavitve za 2. conversion pair, AN4-AN5
    ADSTATbits.P2RDY        = 0;                    //Bit is set when data is ready in buffer, cleared when a ‘0’ is written to this bit
    //nastavitve para
    ADCPC2bits.IRQEN5        = 0;                    //IRQ is generated, AN2, AN3
    ADCPC2bits.PEND5        = 0;                    //Conversion is complete???
    ADCPC2bits.SWTRG5        = 0;                    //Conversion is not started
    ADCPC2bits.TRGSRC5        = 0;                    //No conversion enabled
    ADCPC2bits.IRQEN4        = 1;                    //Enable the IRQ generation when requested conversion of channels AN1 and AN0 is completed
    ADCPC2bits.PEND4        = 0;                    //Conversion is complete
    ADCPC2bits.SWTRG4        = 0;                    //Conversion is not started
    ADCPC2bits.TRGSRC4        = 0x4;                    //PWM Generator 1 primary trigger selected

    //nastavitve prekinitev
    IFS7bits.ADCP2IF        = 0;                    //Clear ADC Pair 2 interrupt flag
    IPC28bits.ADCP2IP        = 6;                    //Set ADC Pair 2 interrupt priority
    IEC7bits.ADCP2IE        = 1;                    //Enable the ADC Pair 2 interrupt


    ADCONbits.ADON              = 0;                    //ADC module is operating
    }

    void init_Oscilator(void)
    {
    /*
    If the Primary PLL is used as a source for the auxiliary clock, then the Primary PLL
    should be configured up to a maximum operation of 30 MIPS or less.

    Nastavitve oscilatorja
    Fvco = Fin * (M/N1) = 7.37 * (32/2) = 117.92 MHz

    Fosc = Fin (M/(N1*N2)) = 7.37 * (32/4) = 7.37 * 4 = 58.96 MHz

    Fcy = Fosc/2 = 29.48 MHz
    */

        PLLFBD                 = 30;                    //M = 32
        CLKDIVbits.PLLPOST    = 0;                    //N2 = 2
        CLKDIVbits.PLLPRE    = 0;                    //N1 = 2

        //preklop iz notranjega na Primary HS oscilator
        __builtin_write_OSCCONH(0x03);
        __builtin_write_OSCCONL(0x01);

        //pocakamo dokler ura ne preklopi iz FRC v PRIM
        while(OSCCONbits.COSC != 0b011){};

        //pocakamo da se PLL zaklene
        while(OSCCONbits.LOCK != 1) {};


    //Nastavitve Auxiliary PLL, clock za PWM in ADC modul
    /*
    Za clock ADC,PWM uporabimo Fvco (117.92 MHz)
    */

    ACLKCONbits.ASRCSEL    = 1;                    //Primary Oscillator is the clock source
        ACLKCONbits.FRCSEL    = 0;                    //Input clock source is determined by ASRCSEL bit setting
    ACLKCONbits.SELACLK    = 1;                    //Auxiliary PLL or FRC or Primary Oscillator provides the source clock for the Auxiliary Clock Divider
        ACLKCONbits.APSTSCLR= 7;                    //Divided by 1
        ACLKCONbits.ENAPLL    = 1;                    //Auxiliary PLL is enabled

        while(ACLKCONbits.APLLCK !=1){};
    }



    If someone sees anything suspicious please let me know....
    Thanks

    #5
    Mike017
    Super Member
    • Total Posts : 2698
    • Reward points : 0
    • Joined: 2007/08/25 08:11:31
    • Location: Lincoln, RI, USA
    • Status: offline
    Re:ADC not giving the result on dsPIC33fj16gs504 2011/09/01 14:43:18 (permalink)
    0
    Hi,

    The AN5 & AN4 pair live in the lower 8 bits of ADCP1.

    Good Luck,
    Mike
    #6
    Cime
    Starting Member
    • Total Posts : 47
    • Reward points : 0
    • Joined: 2007/05/22 00:09:02
    • Location: Slovenia, Ljubljana
    • Status: offline
    Re:ADC not giving the result on dsPIC33fj16gs504 2011/09/02 01:43:39 (permalink)
    0
    Hi Mike,

    that was it. I was wasting time on oscillator, interrupts,....anyway thaks Smile.


    Regards
    Cime



    #7
    Mike017
    Super Member
    • Total Posts : 2698
    • Reward points : 0
    • Joined: 2007/08/25 08:11:31
    • Location: Lincoln, RI, USA
    • Status: offline
    Re:ADC not giving the result on dsPIC33fj16gs504 2011/09/02 06:54:29 (permalink)
    0
    Hi,

    Great. You probably checked this already but that PIC has a few PWM issues in the Errata sheet.  I didn't check if they applied to your code but it's worth a look.

    Good  Luck,
    Mike 


    #8
    Jump to:
    © 2018 APG vNext Trial Version 4.5