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.

MSP430FR2355: Simple C question

Part Number: MSP430FR2355


Being a hardware guy I'm sure when it comes to coding I do many things incorrectly even though they give the appearance of 'working'.  My question is in the following block of code (notice wr), is it ok to re-use wr in the next for loop down the line rather than re-introducing another variable?  I have many for loop type activity and it seems to me that once the loop has been completed and the next loop is purposeful in re-assignment that it would be an ok thing to do...

                            adv = 0;
                            for (wr = 4; wr < 8; wr++) {
                                if (permanent_Info[wr] == '0')
                                    adv++;
                            }
                            if (adv == 4) {
                                adv = 0;
                                SYSCFG0 =0xA501;  //unlock write to info memory
                                for (wr = 4; wr < 8; wr++)
                                    permanent_Info[wr] = rxID[wr - 4];
                                SYSCFG0 =0xA503;  //lock write to info memory
 

Thank you

Steve

  • Sure, note that the first expression in the for loop is for initiation, any previous value of wr is thrown in the bit bucket.

  • Thanks Keith....

    Sort of what I thought.  Now suppose however that we are asynchronously waking up from ISRs.  In a for loop specifically is it possible to run into an issue where the code wakes completes all of main then circles back around OR will a for loop ALWAYS finish before moving on?

    Steve

  • I am not sure what you mean, but an ISR will save the contents of the stack and the current program counter. When the ISR returns the state of the program should pick up exactly where it left off.

    Obviously, this does *not* apply to global variables. If you are using a global variable for the loop counter and the ISR mucks with it, it could end the loop prematurely - or restart it.

  • Thank you .

    Will take that into consideration.....

    This is one of my bad habits.  I have all my variables defined 'outside' of main.....Seems that is NOT a good thing to do

  • Oh, and when defined inside a function, and even with different names, and not static ( i.e., an "auto" variable), the compiler could very well use the same register for each loop counter in the function, and never touch memory at all, unless you use the & unary operator or refer to its value later on.

**Attention** This is a public forum