avr - floating point operations anomaly -
I'm using temperature at temp sensor tmp36 using atmega2560. After reading the temperature sensor digital values and converting two etemeda into readable form in 2560 microcontrollers, I get different answers. Why do I get answers like this? ? The code snippet is present below:
Float temp; // global variable {unsigned long temp_volt; Unsigned four temp_h, temp_l; Unsigned integer temp_buf; Temp_l = ADCL; Temp_h = ADCH; Temp_buf = ((int) temp_h & lt; & lt; 8) | Temp_l; Temp_volt = (((unsigned long) temp_buf * 256 * 10) / 1023) - 993; // Decrease the offset giant tempo = ((float) temp_devault * 1000/1014 * 100/1996) / 10; // gain prints ("temp_buf:% d, temp_volt:% d, temp:% 0.2f \ r \ n", temp_buf, temp_volt, temp); } I have an ATMEGA2560 answer:
temp_buf: 55, temp_volt: 447, temp: 22.4
What's on another ATMEGA2560 Found:
temp_buf: 53, temp_volt: -861, temp: 0.00
For this reason I made this adjustment
temp_volt = (( (Unsigned long) temp_buf * 256 * 100) / 1023) - 904; Why do two microcontrollers behave differently when I am using the same code?
is double type for double, temp_volt and temp_buf, so you can not lose data due to integer arithmetic. Temp_volt; Double taps_buff; and as your computations:
temp_volt = temp_buf * 256.0 * 10.0) /1023.0) - 993.0; // Offset Gain Temp = ((Float) Temp_Vault * 1000.0 / 1014.0 * 100.0 / 196.0) /10.0 Decrease; // Adjust the profit If you need your results, in the final step, e.g.
temp_volt = (double) (int) (temp_buf * 256.0 * 10.0) /1023.0) - 993.0);
Comments
Post a Comment