Convert DS18B20 temperature to string
-
I want to convert DS18B20( [link removed by moderator]: )
temperature (4-bytes code from datasheet) to string with accuracy 0.1°C (like sprintf %.1f). AVR. C language (avr-gcc). I need for small code, so sprintf, floating-point types and round from math.h is bad idea. My following current code is bad also:void reverse_string(char *s)
{
char *p, c;for (p = s + strlen(s) - 1; s <= p; ++s, --p) { c = *p; *p = *s; *s = c; }
}
void ts_to_string(uint16_t ts, char *s)
{
int8_t n = (int8_t)(ts >> 4);
uint8_t neg = n & 0x80;
char *p = s;
float f;if (neg) n = -n-1; do { *p++ = n % 10 + '0'; } while ((n /= 10) > 0); if (neg) *p++ = '-'; *p = '\0'; reverse_string(s); *p++ = '.'; f = (float)(ts & 0xf) / 16.0; if (neg) f = 1.0-f; *p++ = (char)round(f * 10.0) + '0'; *p = '\0';
-
Accound banned for spam. Promotes a vendor and has copied a question from https://electronics.stackexchange.com/questions/33103/convert-ds18b20-temperature-to-string
1 out of 2
Suggested Topics
-
Ds18b20/ nodemcu fluctuations in temp
Hardware • 18 May 2020, 06:19 • mjdula 20 May 2020, 23:51 -
My SHT21 and ds18b20 sensor
My Project • 5 Mar 2015, 18:13 • Blom 31 Mar 2019, 13:47 -
Openhab 2 Mysensors smart chicken coop
My Project • 1 Mar 2017, 02:08 • Ticupolu 8 Mar 2017, 15:48 -
DS18B20 ESP32 nodemanager problem
NodeManager • 13 Sept 2022, 10:59 • A3V 15 Sept 2022, 22:11 -
Dallas sensors puzzle - sensors.begin()
Troubleshooting • 1 Dec 2019, 14:28 • zboblamont 2 Dec 2019, 11:49 -
💬 MySFreezer - Node for Freezer and Lab Equipment monitoring
OpenHardware.io • 12 Dec 2016, 06:13 • openhardware.io 12 Dec 2016, 06:13 -
💬 PiHome Temperature Sensor
OpenHardware.io • 27 Jun 2019, 00:58 • openhardware.io 27 Jun 2019, 00:58 -
💬 MDMSensor "Multisensor"
OpenHardware.io • 12 Oct 2016, 07:52 • openhardware.io 17 Apr 2017, 10:49