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.

  • Resolved

Variable set in ADC ISR not keeping value in main.

Intellectual 400 points

Replies: 9

Views: 1150

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

    Regards,
    Tyler Witt 

     ---------------------------------------------------------------------------------------------------------

    Please click the Verify Answer button on this post if it answers your question.

    ---------------------------------------------------------------------------------------------------------

  • In reply to Tyler Witt:

    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.

  • In reply to mpgorans:

    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.
  • In reply to Bruce McKenney47378:

    Thanks, Bruce.  That did the trick.  I appreciate your input.

  • In reply to Bruce McKenney47378:

    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.

  • In reply to mpgorans:

    mpgorans
     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... ?

    _____________________________________

    Time to say goodbye - I don't have the time anymore to read and answer forum posts. See my bio for details.

    Before posting bug reports or ask for help, do at least quick scan over this article. It applies to any kind of problem reporting. On any forum. And/or look here.
    I'm sorry that  I can no longer provide help  in the forum or by private conversation.

  • In reply to Jens-Michael Gross:

    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.

  • In reply to mpgorans:

    mpgorans
    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.

    _____________________________________

    Time to say goodbye - I don't have the time anymore to read and answer forum posts. See my bio for details.

    Before posting bug reports or ask for help, do at least quick scan over this article. It applies to any kind of problem reporting. On any forum. And/or look here.
    I'm sorry that  I can no longer provide help  in the forum or by private conversation.

  • In reply to Jens-Michael Gross:

    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.

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.