Jump to content
43oh

Recommended Posts

Hi,
 
I am having trouble getting the ADC interrupt working for the msp430g2452. I am simply trying to turn on a flag when the interrupt goes off but it doesn't seem to be working.

 

Code below....

 

 

#include <msp430g2452.h>


void initAdc();

volatile char flag = 0;

int main(void) {

   
    WDTCTL = WDTPW | WDTHOLD;    // Stop watchdog timer
    P1DIR |= BIT6;
    P1OUT &= ~BIT6;
    initAdc();
    __bis_SR_register(GIE);


    while(1){

        if(flag == 1){
            P1OUT |= BIT6;
        }else{
            P1OUT &= ~BIT6;
        }
    }
    return 0;
}

void initAdc(){
    
    ADC10CTL1 = CONSEQ_2 + ADC10DIV_3;        //Channel A0 for conversion, ADC10OSC clock used for sampling, repeat single channel
    ADC10CTL0 = ADC10SHT_2 + MSC + ADC10ON + ADC10IE + ADC10SC + ENC;    //sample and hold time every 16 clocks, Multiple sample conversion, turn on ADC, enable interrupts for ADC, every 16 clock cycles take a sample
    ADC10DTC1 |= 0x01;    //Number of transers in each block
    ADC10AE0 |= 0x01;    //Enables the corresponding pins for analog inputs

}

#pragma vector=ADC10_VECTOR
__interrupt void ADC10_ISR(void) {

    flag = 1;

}


 

Link to post
Share on other sites

Don't know if it helps, but http://homepages.ius.edu/RWISMAN/C335/HTML/msp430Interrupts.HTM says:

The ADC ISR is unusual in requiring the CPU core to be turned On by clearing (setting to 0) the CPUOFF bit. While its not quite clear why it is necessary, failure to do so results in no further executions of the ADC ISR.


       _BIC_SR(CPUOFF);                       // Clear CPUOFF bit to re-enable CPU interrupt
 

Link to post
Share on other sites

I got the code to work by incorporating the suggestions of ToF above and Rick Nardone at e2e http://e2e.ti.com/support/microcontrollers/msp430/f/166/t/249231.aspx

 

#include <msp430g2452.h>


void initAdc();

volatile int flag = 0;
volatile unsigned int cnt = 0;/////changed char var to int var because
volatile unsigned int cntInterrupt = 0;/////debugger showed period '.' for char
int main(void) {


    WDTCTL = WDTPW | WDTHOLD;    // Stop watchdog timer
    P1DIR |= BIT6;
    P1OUT &= ~BIT6;
    initAdc();
    __bis_SR_register(GIE);


    while(1){

            ADC10CTL0 &= ~ENC;                      ///// You need to do this, but not sure why
            while (ADC10CTL1 & BUSY);               ///// Wait if ADC10 core is active
            cnt++;                                  ///// To use debugger to see how many times in loop
            ADC10SA = 0x200;                        ///// Need Data buffer start
            ADC10CTL0 |= ENC + ADC10SC;             ///// Sampling and conversion start
            __bis_SR_register(CPUOFF + GIE);        ///// LPM0, ADC10_ISR will force exit


        if(flag ==1){
            P1OUT |= BIT6;
        }else{
            P1OUT &= ~BIT6;
        }
    }
    return 0;
}

void initAdc(){

    ADC10CTL1 = CONSEQ_2 + ADC10DIV_3;        //Channel A0 for conversion, ADC10OSC clock used for sampling, repeat single channel
    ADC10CTL0 = ADC10SHT_2 + MSC + ADC10ON + ADC10IE;// + ADC10SC;// + ENC;    //sample and hold time every 16 clocks, Multiple sample conversion, turn on ADC, enable interrupts for ADC, every 16 clock cycles take a sample
    ADC10DTC1 |= 0x01;    //Number of transfers in each block
    ADC10AE0 |= 0x01;    //Enables the corresponding pins for analog inputs

}

#pragma vector=ADC10_VECTOR
__interrupt void ADC10_ISR(void) {

    flag = 1;
    cntInterrupt++;
    __bic_SR_register_on_exit(CPUOFF);            ///// Clear CPUOFF bit from 0(SR)
}
 

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...