Skip to content
  • MySensors
  • OpenHardware.io
  • Categories
  • Recent
  • Tags
  • Popular
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo
  1. Home
  2. Hardware
  3. Convert DS18B20 temperature to string

Convert DS18B20 temperature to string

Scheduled Pinned Locked Moved Hardware
ds18b20
2 Posts 2 Posters 1.5k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Maki YoungM Offline
    Maki YoungM Offline
    Maki Young
    Banned
    wrote on last edited by mfalkvidd
    #1

    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';
    
    1 Reply Last reply
    0
    • mfalkviddM Offline
      mfalkviddM Offline
      mfalkvidd
      Mod
      wrote on last edited by
      #2

      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 Reply Last reply
      1

      Hello! It looks like you're interested in this conversation, but you don't have an account yet.

      Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

      With your input, this post could be even better 💗

      Register Login
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      15

      Online

      12.0k

      Users

      11.2k

      Topics

      113.4k

      Posts


      Copyright 2025 TBD   |   Forum Guidelines   |   Privacy Policy   |   Terms of Service
      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • MySensors
      • OpenHardware.io
      • Categories
      • Recent
      • Tags
      • Popular