This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Variable set in ADC ISR not keeping value in main.

Other Parts Discussed in Thread: MSP430FR5725

I'm using the MSP430FR5725.

Simply put, I have a motor that is attached to a position-sensing potentiometer.  The analog value from the pot is read through an ADC.  The ADC value tells me what position the motor is in.  I would like the motor to stop when the pot hits certain values (aka the motor reaches certain positions).  Think of a circle; the "max_position" would be about the top middle (peak) and the "min_position" would be the bottom middle (valley).

My problem is that the position read in during my ADC interrupt isn't being compared correctly against my hardcoded minimum and maximum positions, so the motor ends up driving past these barriers.  I speculate that the position isn't being continuously read in/compared in the while loop.

I've linked the relevant code below.  I've redacted some extraneous variables, functions, etc. that don't pertain to the problem.

http://pastebin.com/raw.php?i=vfteZ4cg

  • Hello,

    After looking through your code, I could not find any real faults within it. The only thing I could think of was that perhaps your ISR is being triggered by something other than the ADC10 memory. This could be causing your variable to fill with inaccurate or null values.

    I would suggest that you set your interrupt similar to the attached code example. This will distinguish each of the interrupt sources individually and may solve your problem.

     

    1563.MSP430FR57xx_adc10_01.c
    /* --COPYRIGHT--,BSD_EX
     * Copyright (c) 2012, Texas Instruments Incorporated
     * All rights reserved.
     *
     * Redistribution and use in source and binary forms, with or without
     * modification, are permitted provided that the following conditions
     * are met:
     *
     * *  Redistributions of source code must retain the above copyright
     *    notice, this list of conditions and the following disclaimer.
     *
     * *  Redistributions in binary form must reproduce the above copyright
     *    notice, this list of conditions and the following disclaimer in the
     *    documentation and/or other materials provided with the distribution.
     *
     * *  Neither the name of Texas Instruments Incorporated nor the names of
     *    its contributors may be used to endorse or promote products derived
     *    from this software without specific prior written permission.
     *
     * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
     * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
     * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
     * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
     * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     *
     *******************************************************************************
     * 
     *                       MSP430 CODE EXAMPLE DISCLAIMER
     *
     * MSP430 code examples are self-contained low-level programs that typically
     * demonstrate a single peripheral function or device feature in a highly
     * concise manner. For this the code may rely on the device's power-on default
     * register values and settings such as the clock configuration and care must
     * be taken when combining code from several examples to avoid potential side
     * effects. Also see www.ti.com/grace for a GUI- and www.ti.com/msp430ware
     * for an API functional library-approach to peripheral configuration.
     *
     * --/COPYRIGHT--*/
    //******************************************************************************
    //  MSP430FR57xx Demo - ADC10, Sample A1, AVcc Ref, Set P1.0 if A1 > 0.5*AVcc
    //
    //  Description: A single sample is made on A1 with reference to AVcc.
    //  Software sets ADC10SC to start sample and conversion - ADC10SC
    //  automatically cleared at EOC. ADC10 internal oscillator times sample (16x)
    //  and conversion. In Mainloop MSP430 waits in LPM0 to save power until ADC10
    //  conversion complete, ADC10_ISR will force exit from LPM0 in Mainloop on
    //  reti. If A1 > 0.5*AVcc, P1.0 set, else reset.
    //  
    //
    //                MSP430FR5739
    //             -----------------
    //         /|\|              XIN|-
    //          | |                 |
    //          --|RST          XOUT|-
    //            |                 |
    //        >---|P1.1/A1      P1.0|-->LED
    //
    //   Priya Thanigai
    //   Texas Instruments Inc.
    //   August 2010
    //   Built with IAR Embedded Workbench Version: 5.10 & Code Composer Studio V4.0
    //******************************************************************************
    #include <msp430.h>
    
    unsigned int ADC_Result;
    
    int main(void)
    {
      volatile unsigned int i;
    
      WDTCTL = WDTPW+WDTHOLD;                   // Stop WDT
      P1DIR |= BIT0;                            // Set P1.0/LED to output direction
      
      // Configure ADC
      P1SEL1 |= BIT1; 
      P1SEL0 |= BIT1; 
      
      ADC10CTL0 |= ADC10SHT_2 + ADC10ON;        // ADC10ON, S&H=16 ADC clks
      ADC10CTL1 |= ADC10SHP;                    // ADCCLK = MODOSC; sampling timer
      ADC10CTL2 |= ADC10RES;                    // 10-bit conversion results
      ADC10MCTL0 |= ADC10INCH_1;                // A1 ADC input select; Vref=AVCC
      ADC10IE |= ADC10IE0;                      // Enable ADC conv complete interrupt
      
      for (;;)
      {
        __delay_cycles(5000);
        ADC10CTL0 |= ADC10ENC + ADC10SC;        // Sampling and conversion start
        __bis_SR_register(CPUOFF + GIE);        // LPM0, ADC10_ISR will force exit
        __no_operation();                       // For debug only
        if (ADC_Result < 0x1FF)
          P1OUT &= ~BIT0;                       // Clear P1.0 LED off
        else
          P1OUT |= BIT0;                        // Set P1.0 LED on
      }
    }
    
    // ADC10 interrupt service routine
    #pragma vector=ADC10_VECTOR
    __interrupt void ADC10_ISR(void)
    {
      switch(__even_in_range(ADC10IV,12))
      {
        case  0: break;                          // No interrupt
        case  2: break;                          // conversion result overflow
        case  4: break;                          // conversion time overflow
        case  6: break;                          // ADC10HI
        case  8: break;                          // ADC10LO
        case 10: break;                          // ADC10IN
        case 12: ADC_Result = ADC10MEM0;         
                 __bic_SR_register_on_exit(CPUOFF);                                              
                 break;                          // Clear CPUOFF bit from 0(SR)                         
        default: break; 
      }  
    }
    
    

  • I have checked the ADC result by setting a break point after an ADC read.  I reads the correct value.  This is in main when I'm doing this.  However, in that while loop, I feel that's where my problem is.  I paused the program after returning to main from the while loop.  The analog value was "0", but the potentiometer's position was not.  Weird.

  • void move_down(volatile unsigned int current_position, unsigned int min_position)
    
    These parameters are passed by value; making them "volatile" does not change this.
    What this means is that, in this function, the variable current_position is not the same
    as the global one; thus it is not updated by the ISR (triggered by pos_read()).

    The simplest fix is probably to get rid of that first argument, so move_down references
    the global copy.
  • Thanks, Bruce.  That did the trick.  I appreciate your input.

  • After some more testing, I see the problem again.  After a successful run of a "move" function, using the correction Bruce provided, it then resets the "current_position" variable to 0.  I have 4 different move functions that work on the first try, but when I try another, it reverts back to the problem I had before.

  • mpgorans said:
     I have 4 different move functions that work on the first try, but when I try another, it reverts back to the problem I had before.

    And your code is... ?

  • http://pastebin.com/raw.php?i=ug5hBR4V

    This is my pertinent code.  I redacted a lot of stuff so as not to overwhelm whoever might read it.  As I was on pastebin, I realized that I was disabling the potentiometer (position-sensing pot), but never re-enabling it.  That might explain the one-and-done situation.  I'll fix that.  I'm away from my board right now and won't be able to try anything for ~9 hours, so I won't blame anyone for not replying between now and then.  I'll update once I've tried that.

  • mpgorans said:
    As I was on pastebin

    You can use the new Syntax Highlighter instead of pastebin. If you mark the 'collapse' checkmark, the code will appear collapsed in the initial thread view, but can be expanded with  a mouseclick - and apperase highlighted and with line number. You can even mark specific lines.

    Example:

    Some or much more code here

    Unfortunately, editing the configuration  later isn't possible (yet). Unless you go fior the HTML view and adjust the parameters for the code span manually.

  • The lack of re-enabling the pot was the cause of the problem.  This was giving my current_position as 0, which caused other problems.  Adding the enable line has fixed the issues.  Thanks to all who offered help.

**Attention** This is a public forum