Skip to content
  • 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
Maki YoungM

Maki Young

Banned
  • Getting Started
  • Controller
  • Build
  • Hardware
  • Download/API
  • Forum
  • Store
About
Posts
1
Topics
1
Shares
0
Groups
0
Followers
1
Following
0

Posts

Recent Best Controversial

  • Convert DS18B20 temperature to string
    Maki YoungM Maki Young

    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';
    
    Hardware ds18b20
  • Login

  • Don't have an account? Register

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