Jump to content
Sign in to follow this  
Sillycrackers

ADC Interrupt not working

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;

}


 

Share this post


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
 

Share this post


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)
}
 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×