[SOLVED] ADC reading , Timer interrupt - dspic33ch128mp508

Status
Not open for further replies.

chaminda12001

Newbie level 5
Joined
May 9, 2019
Messages
9
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
135
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
 
}

 

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;
 

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.
 

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.
 

Hi

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

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
 

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 a moderator:

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…