I am using the experimental_ble_app_uart for s110. I want to use the sd_temp_get() and convert the temperature value to a uint8_t, but if understand correctly when i do this i only get information from the first bytes of the int32_t and these are not the actual temperature value. I was wondering if someone could help me with this problem. This is part of the code i am using at the moment.
static int32_t temp;
err_code = sd_temp_get(&temp);
uint8_t temp1 = temp;
The value from sd_temp_get() is in units of 0.25 degrees Celsius (source). This means a value of 4 is 1 degree Celsius, a value of 8 is 2 degrees Celsius, a value of 12 is 3 degrees Celsius, etc.
Assuming you wish to convert the result into a whole number, you would simply divide it by four. I wouldn't worry about any integer overflow as a result of going from a 32 bit to an 8 bit number since the operating range of the nrf514122 is between -25 and 75 degrees C. However, since the temperature can drop below zero, you should use an int8_t instead of a uint8_t.
So your code would look like this:
int8_t temp = raw_temp >> 2; // divide by 4
In addition to Nick's answer, you may need to calibrate the output of the TEMP peripheral.
The potential error read on the TEMP peripheral should be pure offset error. You can calibrate the temperature value in software, i.e. if you know the temperature in your room, just add or subtract from the measured value to get the correct value.
Thanks! This helped and was able to make it work.
Can't understand, what will be the value for -5C? Will this code work for negative values?