简体   繁体   中英

Expression Evaluation in 32 bit CPU

I am working with TIs 32 bit microcontroller TMS320F280049. I am using an external ADC for temperature measurement. The ADC output code is of 24 bit data, now I want convert this ADC output code to resistance value by using the following expression

RTD = (1080 * ADC output code) / (4194304 * 16)

and I wrote the code as follows,

int32 RTD = 0;
int32 adc = 0x005EEC17;

RTD = (1080*adc)/(16*4194304);

I wrote this expression as it is but got RTD value some random negative value instead of 100 which I am expecting. I wonder how to correctly evaluate the expression. I am beginner in coding any explanation that put into simple words will be greatly appreciated.

1080*0x005EEC17 overflows int32 . Therefore you need to do the math in a wider type. You can use the LL suffix to make the literal long long which is at least 64-bit

int32 RTD = 0;
int32 adc = 0x005EEC17;

RTD = (1080LL*adc)/(16*4194304);

An int32_t can only store values up to 2 31 –1 (approximately 2.15e9). Multiplying 1080 * adc (approx. 6.7e9) will result an overflow. Here are some alternatives that seem to work with gcc:

uint32_t rtd = adc / 16 * 1080 / 4194304;  // Dividing first.
uint32_t rtd = (adc >> 4) * 1080 / 4194304; // Same as above.
uint32_t rtd = (adc * 1080.0) / (16 * 4194304); // Implicitly convert to double, so that larger values can be stored.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM