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.

Msp430 32bit integer comparison Implementation

In MSP430 I know its possible to use 32bit variables using "unsigned long"  type . I have implemented a 32bit variable that counts upto a max 32bit value

But for some reason it gets truncated and actually the counting stops to its truncated equivalent for 16bit int. 

The MSP430 eries im using is MSP43F5510. please suggest me if i'm missing anything.

 P.S I have Included "stdint.h" header file . I have tried the CCS Library support with full and minimal. 

  • Naveen,

    can you show the part of code related to the variable?

    Dennis
  • Hi Dennis,

    I have put the part of the code that uses the 32bit variable below:

    The variable lAudioFileLength initialized as 0x20202010 (32 bit)

    The while loop should break when FileIndex counts to lAudioFileLength(0x20202010)
    Instead it breaks when FileIndex reaches 0x2010

    Note : when i'm printing lAudioFileLength as hex value also displays 0x2010.
    I'm getting the value for lAudioFileLength through UART byte by byte so i have to Initialize it this way

    -----------------------------------------------------------------------------------------------------------------------------------------------------------------------
    uint32_t lAudioFileLength=0x0; //Global variable
    uint32_t FileIndex=0;
    void loadfile_function()
    {
    *(((UINT8*)&lAudioFileLength)+ (UINT8)0) = 10;
    *(((UINT8*)&lAudioFileLength)+ (UINT8)1) = 20;
    *(((UINT8*)&lAudioFileLength)+ (UINT8)2) = 20;
    *(((UINT8*)&lAudioFileLength)+ (UINT8)3) = 20;
    ;

    while(FileTransferComplete != TRUE)
    {
    FileIndex+=1;

    if( FileIndex > (lAudioFileLength) )
    {

    FileTransferComplete = TRUE;
    }

    }
    }
    -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

  • This is a very strange method to initialize a variable:

    *(((UINT8*)&lAudioFileLength)+ (UINT8)0) = 10;
    *(((UINT8*)&lAudioFileLength)+ (UINT8)1) = 20;
    *(((UINT8*)&lAudioFileLength)+ (UINT8)2) = 20;
    *(((UINT8*)&lAudioFileLength)+ (UINT8)3) = 20;

    Did you double check if lAudioFileLength is 0x20202010 after calling the function? Have you tried if it works if you simply write

    lAudioFileLength = 538976272; // or 0x20202010

    ...just for testing. Why do you use that complex way to assign a value to the variable? Do you receive this data byte-wise and create the 32 bit value from the four bytes?

    Dennis

  • Wait...

    If you want to set the variable to 0x20202010, then you cannot assign 20, 20, 20 and 10 as the single bytes. The first is hexadecimal, the second is decimal. You then have to write 0x20, 0x20, 0x20 and 0x10, otherwise your variable would be 0x1414140A. But your initialization should work by the way.

    Dennis

  • Thanks for your answer Dennis,

    Still when i initialize the 32bit integer with individual hex bytes (0x20,0x20,0x20 and 0x10)
    when i print the value displays 0x2020 not 0x20202010 ,I mean whatever the byte order i initialize the 32bit integer ,
    I only get a 16bit value displayed and also the while loop breaks at the 16bit integer variable not for the 32bit variable,

    Is there any special requirements or procedure to be followed to use a 32bit integer in MSP4305xx series.

    Thanks Again.
  • I don't know about any limitation.

    Could you then please explain your "printing" of the value a little bit in detail and show some code of that?
  • sprintf(print_str," FileIndex %x\n",FileIndex);
    tx_str(print_str);

    The above method is I'm using to print the variables .sprintf is same as the standard function provided in the CCS C library
    tx_str just sends the formated string as stream of bytes to the UART TX buffer which can be viewed in the teraterm console for debug purpose
  • OK, so you're using sprintf - that makes a difference! First you have to change your sprintf-settings in CCS to support 32 bit values. It is set to "minimal" per default when starting a new project. The other two options would be "nofloat" and "full". When you do not plan to use floating point numbers, then "nofloat" is enough for you. But "minimal" cannot handle 32 bit numbers. To do this - right click on your project's name in the project explorer and select "Properties". Then go to this option and select "nofloat" on the right:

    Now your sprintf will support your 32 bit number in general, but furthermore you have to tell it that you are using a long variable, therefore add an additional "l" to the function call. Your initial

    sprintf( print_str, " FileIndex %x\n", FileIndex );

    will change to

    sprintf( print_str, " FileIndex %lx\n", FileIndex );

    and if you want the hexadecimals to be all capital, then make the "x" a capital

    sprintf( print_str, " FileIndex %lX\n", FileIndex );

    And of course make sure your print_str buffer has enough bytes to hold the result.

    Dennis

  • Thanks Dennis,
    That helped :)
    one more doubt. The "nofloat" settings is needed to use a 32bit variable with sprintf alone or it must be set if we are using a 32bit variable anywhere in the program?
  • It is only for sprintf support. If you want to use sprintf somewhere inside your program and want to pass a 32 bit value to it, you have to use at least the nofloat support variant. Full support, of course, will do everything. The use of sprintf blows up your code size - have a look before and after using it. If you enable float support it will rise again. So for smaller microcontrollers using the float support does not even work because of the large code size. That is why there are three types of implementation, causing different code size depending on the functionality you get.

    Code size: small ------------------ medium ------------------------> large
    Support:   minimum                  nofloat                          full

    Dennis

**Attention** This is a public forum