Library 12- OneWire library for STM32F4

onewire devices1-Wire is a device communications bus system designed by Dallas Semiconductor Corp. that provides low-speed data, signaling, and power over a single signal.

It’s used in some temperature sensors, like DS18B20 temp sensor and many others products from Dallas.

OneWire key features

  • Single Contact Sufficient for Control and Operation
  • Unique ID Factory-Lasered in Each Device
  • Power Derived from Signal Bus (“Parasitically Powered”)
  • Multidrop Capable: Supports Multiple Devices on Single Line
  • Exceptional ESD Performance

source: maximintegrated.com

17th May, 2014: There was some new functions added!

Library

Features

  • Read/write byte
  • Search for devices on 1-wire pin
  • Read rom from device
  • Version 2.0 – January 04, 2015
    • Library rewritten
    • It supports unlimited pins and gpios for OneWire
    • This allows you to group sensors to separate ports to prevent communication failures if one sensor is broken
  • Version 2.1 – March 10, 2015
    • Supported new GPIO system

Dependencies

  • CMSIS
    • STM32F4xx
    • STM32F4xx RCC
    • STM32F4xx GPIO
  • TM
    • TM DELAY
    • TO GPIO
    • defines.h
    • attributes.h

Initialize

Before you can work with 1-wire bus, you have to initialize it

Reset pulse

If you need to reset 1-wire bus, simple call reset function

Write byte

Read byte

Search devices

Select device

Because you have more devices on one line, you have to somehow select device which you want to communicate with. You can do this with

Functions and enumerations

Example

I connected 2 DS18B20 sensors to PD0 (make sure, you have external pullup resistor) and this code returns me on terminal.

One Wire devices connected to STM32F4

One Wire devices connected to STM32F4

Example 2

  • 4 devices are connected to STM32F4 via OneWire:
  • 2 OneWire ports:
    • PA3
    • PD0
OneWire example with multi instances

OneWire example with multi instances

Projects available on Github, download library below.

Icon
TM STM32F4 OneWire Library

OneWire library for onewire devices

tilz0R

Owner of this site. Also electronic enthusiasts, web developer, 3D printer fan, handball player and more. Big fan of STM32F4 devices. In anticipation of the new Discovery board for STM32F7 lines.

You may also like...

Read before commenting!

Before you make a new comment, make sure you agree with things listed below:

  • - Read post to make sure if it is already posted what you are asking for,
  • - Make sure you have the latest version of libraries used in your project,
  • - Make a clean and grammatically correct written message,
  • - Report as many details as possible, including what have you done so far,
  • - Do NOT post any code here. Use Pastebin,
  • - Do NOT post any error codes here. Use Pastebin,
  • - Specify STM32Fxxx family and used Discovery/EVAL/Nucleo or custom made board,
  • - Make sure your clock is set correct for PLL,
  • - If you are using my HAL drivers, please check this post how to start.
Comment will be deleted on breaking these rules without notification!
  • Thamanoon Kedwiriyakarn

    Thank you very much for your code, I do to finish one with stm32f103rb connect to DS18S20+.

    http://www.electoday.com/index.php/topic,12412.0.html

  • TC Zeynel Abidin Delikan

    Sory you are begining with systick but didnt write how can we use it !

    Please write about some example for systick controlling.

    Thank you for your sharing.

    • What?
      Please post this to post related to systick.

  • TC Zeynel Abidin Delikan

    Hi Tilen,

    Firstly thank you for your sharing.

    I try to make a delay time with systick timer. I’m using STM32F429 Disco kit for developing.

    And my code is…

    #include “stm32f4xx.h”
    #include “stm32f4xx_rcc.h”
    #include “stm32f4xx_gpio.h”
    #include “core_cm4.h” // SysTick Registerlarının bulunduğu kütüphane

    void Delay(__IO uint32_t time);
    __IO uint32_t TimingDelay;

    int main(void) {

    // Disco bordumuzun kırmızı ve yeşil ledlerimizi kullanmak için AHB1 i hazırlıyoruz
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOG,ENABLE);

    // GPIO port tiplerini kullanabilmek için GPIO_InitDef değişkenimizi tanımlıyoruz
    GPIO_InitTypeDef GPIO_InitDef;

    // İlk olarak GPIOG portunun pinlerini düzenliyoruz

    // Pinlerimizi Belirliyoruz
    GPIO_InitDef.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14;
    // Pinlerimizi çıkış tipini Push Pull olarak belirliyoruz
    GPIO_InitDef.GPIO_OType = GPIO_OType_PP;
    // Pinlerimizi çıkış olarak tanımlıyoruz.
    GPIO_InitDef.GPIO_Mode = GPIO_Mode_OUT;
    // Pinlerimizin çekme direncini ayarlıyoruz.
    GPIO_InitDef.GPIO_PuPd = GPIO_PuPd_NOPULL;
    // Pinlerimizin çalışma frekanslarını ayarlıyoruz
    GPIO_InitDef.GPIO_Speed = GPIO_Speed_2MHz;

    //Şimdi portlarımızı kuralım
    GPIO_Init(GPIOG, &GPIO_InitDef);

    // Şimdi ledlerimizin ilk değerlerini tanımlıyoruz.
    // Kırmızı Led “Düşük”, yeşilled ise “Yüksek” durumda olacak
    GPIO_SetBits(GPIOG, GPIO_Pin_14);
    GPIO_ResetBits(GPIOG, GPIO_Pin_13);

    //Son Olarak Systick Timer’ı 1ms’ye ayarlayalım. (sistem saatimiz ön tanımlı olarak 180Mhz dir)
    SysTick_Config(SystemCoreClock/11270);

    while (1)
    {
    Delay(500);
    GPIO_ToggleBits(GPIOG, GPIO_Pin_13 | GPIO_Pin_14);
    }

    return 0;
    }

    void SysTick_Handler(void)
    {
    if (TimingDelay != 0)
    {
    TimingDelay –;
    }
    }
    void Delay(__IO uint32_t time)
    {
    TimingDelay = time;
    while(TimingDelay != 0);
    }

    In this case my “SysTick_Config(SystemCoreClock/11270);” command very nearly 1ms.

    But its not true !!!!

    How can fix it to the 1ms with using 180Mhz clock frequency ?

    when I try to use SystemCoreClock/100 its running on over the 1second !

    Thank you for your help !

    • Your config is wrong for systick.
      If you have 180MHz clock, then to get every 1ms interrupts, you need to divide your clock with 1000. This will do 1000interrupts in one second which is 1ms interval.

      PS: wrong post for that.

      • TC Zeynel Abidin Delikan

        Thank you for your feedback.

        I was checked it.
        if the coocox Ide can be select MCU type. All directions is true at the stm32f4xx.h files !

        But when I try to use to divide 1000 my led blink

        the led’s are burning more then the 1ms…

        • Ok, again. POST THIS TO SYSTICK RELATED POST!
          Or I will ignore any future comments from you.

  • TC Zeynel Abidin Delikan

    Im so sory Majerle i think ım at the systick subject … Please delete my all post please :(.

  • Pingback: Library 03- STM32F4 system clock and delay functions - STM32F4 Discovery()

  • Jura

    Hey Tilen,

    I am trying to connect stm32f429I with DHT11 using your OneWire library but there seems to be no response from sensor. I am trying to use your devices = TM_OneWire_First(&OneWire1); function but response is always 0x00. I got 3 same sensors so there isn’t possibility that it’s broken. When I try to read data using
    data[0] = TM_OneWire_ReadByte(&OneWireStruct);

    It gives me fixed output, different on every pin. There shouldn’t be timer error cause it’s working fine with my other sensors. I tried connecting it with 4K7Ohms and without and there is no difference. Where could be error?
    LP, Jura

    • are you sure, this sensor works with OneWire?

      • Jura

        Well I checked deeper in datasheet and seems that it’s not totally compatible. I have written a simple program with putting GPIOx high and low and there is some response but it seems that it is timer problem. I can’t make it delay using only 1 us. Nvm it’s for some other discussion then.
        Thanks for reply 🙂

  • Glett

    You have a bug in the size of buf vector “char buf[30]”!!!
    When you use:
    sprintf(buf, “Devices found on 1-wire instance: %dn”, count);
    the size of the string copied to buf is 36.
    The same occur in all the examples

    Thank you for share, this library was very helpfull to me!!!!

    Regards!!!

    • Uau, totally sorry, its example, not lib 😉 no need to scream.

      • Glett

        Lol, is true!