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.
Hi, I'm writing a code on CCS for a MSP430FR5969 and I don't understand why it doesn't work as I want.
This is the code, and when the last value of the array (1280) is selected the X_Time is something around 64848 (it seems a problem with the size of the int) instead of 128000. I have changed the array from uint16_t to uint32_t and all works well.
uint32_t X_Time=0;
const uint16_t TABLE_X_TIME_MODE_2_0to3[16] = {5,10,15,20,30,40,60,80,120,160,240,320,480,640,960,1280};
X_Time=TABLE_X_TIME_MODE_2_0to3[POT_Frq_0_15]*100;
So I review the code and I find another array :
uint16_t Gap=0;
const uint8_t TABLE_MODE_0[8][2] ={{1,1},{2,1},{3,1},{4,1},{1,5},{2,5},{3,5},{4,5}};
Gap=TABLE_MODE_0[Program][1] *1000;
In this case all works well without change the array from uint8_t to uint16_t.
Anyone know why it happens ?
Thanks
Riccardo
As specified in section 6.3.1.8 of the C standard, arithmetic operations (like the multiplication) use type "int" or "unsigned int" unless you are explicitly using larger types. On the MSP430, these types have 16 bits.
Yes, the multiplication is independent of the assignment. To get a 32-bit multiplication, at least one of the operands must have 32 bits, or be explicitly cast to a 32-bit type:
uint32_t Gap = TABLE_MODE_0[Program][1] * (uint32_t)1000;
Both 100 and 1000 would be represented as 16 bits. The only difference is whether the result of the multiplication would fit into 16 bits.
**Attention** This is a public forum