+ Post New Thread
Results 1 to 7 of 7
  1. #1
    Newbie level 5
    Points: 101, Level: 1

    Join Date
    May 2019
    Posts
    9
    Helped
    0 / 0
    Points
    101
    Level
    1

    ADC reading , Timer interrupt - dspic33ch128mp508

    Hi,

    I am trying to read ADC channel 0 using Timer interrupt to trigger it. I am using dspic33ch128mp508 curiosity development board for anologue reading using potentiometer annologue channel 0. When I debug, I dont get any value on the Anlogue channel 0.
    I am using MPLAB X IDE 5.10 for debugging. Here is my code. Any thoughts on this?

    Code C - [expand]
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    
    int main(void) {
        
        TRISE = 0x0000;
       
        adc_init();
     
          PORTEbits.RE0 = 0;
         
           T1CON =0;
            TMR1 =0x0000;
            
            T1CONbits.TCS = 0;
            T1CONbits.TCKPS =0; 
            T1CONbits.TGATE = 0;
            IFS0bits.T1IF = 0;  //Timer interrupt flag
            IFS5bits.ADCAN0IF =0;
            IEC0bits.T1IE =0;   //Interrupt enable
            IPC22bits.ADCAN0IP =1;
            IEC5bits.ADCAN0IE =1;
            PR1 =0x0002;
            ADCON1Lbits.ADON = 1;
            T1CONbits.TON = 1;  //Timer 1 T1CON enable 
                 
        
          while(1);  
          
    }
     
    void adc_init(){
        
        TRISAbits.TRISA0=1;
        ANSELAbits.ANSELA0=1; 
        ADCON3Hbits.CLKSEL0= 1; //FOSC clock selection 
        ADCON3Hbits.CLKSEL1 =0;
        ADCON3Hbits.CLKDIV =0;
        ADCON3Lbits.CNVCHSEL0 =1; // Channel selection 1  
        ADCON1Hbits.SHRRES =1; // 12 bit resolution
        ADCON2Lbits.EIEN =0;
        ADCON1Hbits.FORM =0;  //Output format
        ADMOD0Lbits.SIGN0 =0; 
           
        ADCON3Lbits.REFSEL =0; //ADC Reference voltage selection
      
        ADCON3Hbits.SHREN =0;
        ADCON5Hbits.WARMTIME = 0x2; 
        
    }
     
    void __attribute__((interrupt, no_auto_psv)) _ADCAN0Interrupt(void)
    {
        
    i = ADCBUF0; // read conversion result
    PORTEbits.RE0 = i;
    _ADCAN0IF = 0; // clear interrupt flag
     
    }

    •   AltAdvertisement

        
       

  2. #2
    Super Moderator
    Points: 29,548, Level: 41
    andre_teprom's Avatar
    Join Date
    Nov 2006
    Location
    Brazil
    Posts
    8,818
    Helped
    1114 / 1114
    Points
    29,548
    Level
    41
    Blog Entries
    9

    Re: ADC reading , Timer interrupt - dspic33ch128mp508

    It seems like you are missing the command for starting the A/D conversion, either at the main() and at the interrupt() vectors:

    Code:
    ADCON3Lbits.CNVRTCH = 1;
    Moreover, you are not enabling individual interrupts anywhere, one would expect something like:

    Code:
    ADIELbits.IE0=1;
    --------------------------------------------------------------------------------------------------
    Part of the world that you live in, You are the part that you're giving ( Renaissance )



  3. #3
    Newbie level 5
    Points: 101, Level: 1

    Join Date
    May 2019
    Posts
    9
    Helped
    0 / 0
    Points
    101
    Level
    1

    Re: ADC reading , Timer interrupt - dspic33ch128mp508

    Hi,

    Thank you for the reply. I did setup on my main code. but it does not go to ADCAN0Interrupt vector.

    Code:
    ADIELbits.IE0 =1;
            
           IPC22bits.ADCAN0IP =1;
            IEC5bits.ADCAN0IE =1;
             ADCON3Lbits.CNVRTCH =1;
            ADCON3Lbits.CNVCHSEL0 =1;
    Anything I missed out on here.



    •   AltAdvertisement

        
       

  4. #4
    Super Moderator
    Points: 29,548, Level: 41
    andre_teprom's Avatar
    Join Date
    Nov 2006
    Location
    Brazil
    Posts
    8,818
    Helped
    1114 / 1114
    Points
    29,548
    Level
    41
    Blog Entries
    9

    Re: ADC reading , Timer interrupt - dspic33ch128mp508

    Quote Originally Posted by chaminda12001 View Post
    I did setup on my main code. but it does not go to ADCAN0Interrupt vector
    Don't seem it is likely missing something like that at the init routine?

    Code:
    INTCON2.GIE = 1 // enable global interrupt
    Seriously speaking, take a look at other extensively available examples on the Web, or read the datasheet, as previously suggested.
    --------------------------------------------------------------------------------------------------
    Part of the world that you live in, You are the part that you're giving ( Renaissance )



  5. #5
    Newbie level 5
    Points: 101, Level: 1

    Join Date
    May 2019
    Posts
    9
    Helped
    0 / 0
    Points
    101
    Level
    1

    Re: ADC reading , Timer interrupt - dspic33ch128mp508

    Hi

    I checked the examples on the web. I tried those methods but still no luck



    •   AltAdvertisement

        
       

  6. #6
    Advanced Member level 4
    Points: 7,740, Level: 20

    Join Date
    Jan 2015
    Posts
    1,086
    Helped
    345 / 345
    Points
    7,740
    Level
    20

    Re: ADC reading , Timer interrupt - dspic33ch128mp508

    You should not need to set the GIE bit to 1 as that is its Power-on default setting.\
    Look at the ADC FRM section for that MCU - it has a whole section of examples.
    Suan



  7. #7
    Newbie level 5
    Points: 101, Level: 1

    Join Date
    May 2019
    Posts
    9
    Helped
    0 / 0
    Points
    101
    Level
    1

    Re: ADC reading , Timer interrupt - dspic33ch128mp508

    Hi,

    I am still cant read value into ADCBUF0. Could you have a look on my ADC initilisation code,adc_read and Enablecalibrate function. Anything I missed.

    Code C# - [expand]
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    
    void adc_init(){
        
        
        //Configuration of the I/O pins to be used as analog inputs
        TRISA = 0xFFFF;   
        ANSELA =0xFFFF;  
        
        ADCON1Lbits.ADSIDL =0;   //ADC module operate in idle mode
        ADCON3Hbits.CLKSEL0= 1; //FOSC clock selection 
        ADCON3Hbits.CLKSEL1 =0; 
        ADCON3Hbits.CLKDIV =0;  //no clock divider
        ADCON1Hbits.FORM =0;  //Integer Output format
        ADMOD0Lbits.SIGN0 =0; //unsigned output format
        ADCON3Lbits.REFSEL =0; //ADC Reference voltage selection
        ADSTATL =0;            // ADC Data ready status register
        ADCON3Hbits.SHREN =1;    //Shared ADC core is enabled
        ADCON2Lbits.EIEN = 0;    //Early interrupt  is disabled
       ADCON1Lbits.ADON =1;  // Turn on ADC 
       ADCON5Hbits.WARMTIME =15;
       
       Enableandcalibrate();
        ADIEL=0xFFFF;              //   ADC interrupt enable register low
        IEC5=0xF100;          // enable interrupts for AN0 -AN4
        IEC6=0x07FF;           //enable interrupts for AN4 -AN15
        IFS5 =0x0000;         //clear interrupts flags
        IFS6 =0x0000;         //clear interrupts flags
        
    } 
     
    void Enableandcalibrate(){
        
      
        // Turn on analog power for shared core
    ADCON5Lbits.SHRPWR = 1;
     
    // Wait when the shared core is ready for operation
    while(ADCON5Lbits.SHRRDY == 0);
     
    // Turn on digital power to enable triggers to the shared core
    ADCON3Hbits.SHREN = 1;        
        
    }
     
    unsigned int read_adc(int channel){
        
        int i =0; 
        ptr = &ADCBUF0;         
        
        ADCON3Lbits.CNVCHSEL = channel | 0x0100; // Channel selection 
        ADCON3Lbits.CNVRTCH =1;  //Starts  sampling
            
        while (ADSTATL & (1<<channel)){
                     
                conversionresults[i++] = *ptr++ ;
                
            
            }
        }
    Please help on this. Thank you very much.
    Last edited by BradtheRad; 17th July 2019 at 08:40. Reason: Added code formatted window



--[[ ]]--