Electronic – Code Composer Studio 6.1.0 debugger is not acting as it should

idemsp430ti-ccstudio

I am learning to code on a microcontroller from Texas Instruments, MSP430FR6969 (link), using MSP-EXP430FR6969 launch pad (link).

I am also using an IDE CCS 6.1.0 (link).

I am doing a simple odometer, where the program counts from 0 to 99,999 and then resets again.

code below

#include <msp430.h> 

#define DEVELOPMENT 0x5A80
#define ENABLE_PINS 0xFFFE

int main(void)
{
    WDTCTL = DEVELOPMENT;//disable WDT
    PM5CTL0 = ENABLE_PINS;//this is needed in order to enable pins

    P1DIR = 0x01;//P1.1 is output
    P1OUT = 0x00;//port 1 = 0x00

    unsigned int tnth, thou, hund, tens, ones;

    unsigned long km = 0;

    while(1)
    {
        for(tnth = 0;tnth < 10;tnth++)
        {
            for(thou = 0;thou<10;thou++)
            {
                for(hund = 0;hund<10;hund++)
                {
                    for(tens = 0;tens<10;tens++)
                    {
                        for(ones = 0;ones<10;ones++)
                        {
                            km = 10000*tnth + 1000*thou + 100*hund +10*tens+ 
                            ones;
                        }

                    }
                }
            }
        }

        P1OUT = ~P1OUT;//I added a breakpoint there.
        km = 0;
    }


    return 0;
}

I added a breakpoint at P1OUT = ~P1OUT;, build it and debugged it like in the image below.

enter image description here

When the debugger reaches the breakpoint, I see in the variable watch window ones, tens, hunds, thou, and tnth all equal to 10, which is what I expect.

But I also expected km = 99999, instead I have km = 34463, which doesn't make sense as km is an unsigned long

Also.

When I modify the code and change unsigned int tnth, thou, hund, tens, ones to signed int, put a breakpoint at P1OUT = ~P1OUT build it and debug I get km = 4294936223 in the variable watch window.

I asked the question in TI forum (link), and a member said that in my build

"../main.c", line 16: warning #552-D: variable "km" was set but never used

Kindly help me on this matter, and sorry for the long post.

Best Answer

Your MSP430 is a 16-bit machine, so the compiler is doing the (10000*tnth) as a multiplication of 16-bit numbers and truncating the result to 16 bits before continuing with the rest of the calculation.

10000 * 9 = 90000 = 0x15F90  
truncate to 16-bits = 0x5F90 = 24464  
24464 + 9999 = 34463  

If you cast the 10000 integer constant to 32-bits with something like 10000UL and/or cast the variable with (unsigned long)tnth you should see your expected results.

The warning for 'variable "km" was set but never used' is expected.
You're assigning values to km, but never reading them back or using km in any other way, but this has nothing to do with your undesired behavior.

When you changed your 16-bit data type from unsigned to signed, what happens is that the 34463 (0x869F) value is treated as a negative 16-bit value of -31073 (since the MSB is a 1).
When converting this into a 32-bit value, it first gets sign-extended to a 32-bit signed value - so now it's 0xFFFF869F.
But since you're putting this into an unsigned 32-bit integer variable this value is now represented as 4294936223.