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.

MSP430G2553 Global Variable Declaration

Other Parts Discussed in Thread: MSP430G2553, MAX232

I am trying to define global variables for a simple PWM code but for some reason, CCS does NOT recognize the "Frequency" and "DutyCycle" variables as global. I have attached my code below. Are you able to determine what is going on?

SpeedSensorTest.zip

Thank you,

Andrew

  • What do you mean with "does not recognize"? What specific problem are you seeing?

    (It's likely that these variables got optimized away because they never change.)

  • I believe CCS thinks those two variables are local because when I try to vary them in the expression window of the debug session, it says "Error: Could not read 0x200: Execution state prevented access".
  • Yes, this is what happens when they got optimized away.

    The compiler knows that these variables never get changed by your code.
    If you want to modify them in ways that the compiler does not know about, declare them as volatile.

  • Thank you for that suggestion but I am still having the same issue. I have copied and pasted my updated main code below. Please let me know if I redefined those variables correctly.

    /*
    * ======== Standard MSP430 includes ========
    */
    #include <msp430.h>

    /*
    * ======== Grace related includes ========
    */
    #include <ti/mcu/msp430/Grace.h>

    volatile unsigned long Frequency = 16000;
    volatile unsigned long prevFrequency = 0;
    volatile unsigned long DutyCycle = 8000;
    volatile unsigned long prevDutyCycle = 0;

    /*
    * ======== main ========
    */
    void main(void)
    {
    Grace_init(); // Activate Grace-generated configuration

    while(1)
    {
    if( prevFrequency != Frequency )
    {
    TA0CCR0 = Frequency; // TA0CCR0, Timer_A Capture/Compare Register 0
    prevFrequency = Frequency;
    }

    if( prevDutyCycle != DutyCycle )
    {
    TA0CCR1 = DutyCycle; // TA0CCR1, Timer_A Capture/Compare Register 1
    prevDutyCycle = DutyCycle;
    }
    }
    }
  • Okay, volatile is a bad idea.

    Instead let's pretend to actually change these variables:

    if (WDTCTL == 0) { /* never actually happens */
        Frequency = 0;
        DutyCycle = 0;
    }

  • I never had problems with global variables within CCS before now but I am still having the same problem. I tried two different things: one was pasting that code snippet before the while(1) loop and another time pasting it within the while(1) loop and had no resolution for either situation. I have copied and pasted my main code below.

    /*
    * ======== Standard MSP430 includes ========
    */
    #include <msp430.h>

    /*
    * ======== Grace related includes ========
    */
    #include <ti/mcu/msp430/Grace.h>

    volatile unsigned long Frequency = 16000;
    volatile unsigned long prevFrequency = 0;
    volatile unsigned long DutyCycle = 8000;
    volatile unsigned long prevDutyCycle = 0;

    /*
    * ======== main ========
    */
    void main(void)
    {
    Grace_init(); // Activate Grace-generated configuration

    while(1)
    {
    if (WDTCTL == 0) // never actually happens
    {
    Frequency = 0;
    DutyCycle = 0;
    }

    if( prevFrequency != Frequency )
    {
    TA0CCR0 = Frequency; // TA0CCR0, Timer_A Capture/Compare Register 0
    prevFrequency = Frequency;
    }

    if( prevDutyCycle != DutyCycle )
    {
    TA0CCR1 = DutyCycle; // TA0CCR1, Timer_A Capture/Compare Register 1
    prevDutyCycle = DutyCycle;
    }
    }
    }
  • Replace

    #include<msp430.h>

    with

    #include <msp430g2553.h>

    and check

  • Andrew, do you have any optimizations enabled?
  • Andrew Butler said:
    I believe CCS thinks those two variables are local because when I try to vary them in the expression window of the debug session, it says "Error: Could not read 0x200: Execution state prevented access".

    Are you trying to modify those globals while the program is actually running (as opposed to when it's paused in the debugger)?

  • Yeah, this won't work :-) Good question!
  • Yes, I am trying to modify those variables while the program is running because I have been able to do it before. However, my real goal is to attach a GUI to this program and it won't work because it is not recognizing those variables as global.

  • Modifying variables or registers is only possible when the program is paused.
  • So how do I get my GUI to work because it is not recognizing those variables?
  • If you pause your program, are you able to modify them now?

  • And will that work if I run it as a stand alone program with the runtime app?
  • Do you plan to establish a connection to the PC where something is running that modifies the PWM of the micro?
    This no problem - the micro can change those variables during runtime, of course.

    But the debugger cannot.

  • Establish a connection like UART? Is that the only way to get it to work without having to pause the program?
  • When using a GUI on the PC, some kind of RS232-UART (MAX232) or USB-UART bridge (FT232) is common. But this variable can be changed from several sources like input buttons, encoders or any other user-interface to act with the running program.

**Attention** This is a public forum