2
\$\begingroup\$

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.

\$\endgroup\$
1
  • \$\begingroup\$ Code Composer Studio 8.0 is available. \$\endgroup\$
    – Tyler
    Jul 10, 2018 at 19:23

1 Answer 1

5
\$\begingroup\$

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.

\$\endgroup\$
6
  • 1
    \$\begingroup\$ Any chance of the code being optimized away (by the compiler) because km is never used? \$\endgroup\$ Jul 10, 2018 at 19:57
  • 2
    \$\begingroup\$ Some compilers might do that, but the result the OP is getting (34463) is exactly what you'd get when the first multiplication gets truncated to 16-bits, so it looks like it's not being optimized out. \$\endgroup\$
    – brhans
    Jul 10, 2018 at 20:03
  • \$\begingroup\$ @ChrisKnudsen, Optimization was off \$\endgroup\$ Jul 11, 2018 at 3:30
  • \$\begingroup\$ @brhans Then how come when I changed unsigned int to int, I get km > 4000000, shouldn't I get 34463 as well \$\endgroup\$ Jul 11, 2018 at 3:33
  • \$\begingroup\$ @brahans your answer makes sense for when I get km = 34463, but if I change unsigned int tnth to int tnth90,000 will still be 24465, which should give me km = 34463 \$\endgroup\$ Jul 11, 2018 at 6:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.