Skip to content

1-Wire

Introduction

The MikroSDK.Driver.OneWire library provides an API for interfacing with One Wire devices using the MikroSDK framework.

This documentation covers all the necessary information for setting up and using the One Wire Master Driver.

The One Wire Master Driver allows communication with One Wire devices, which typically include sensors, memory devices, and other peripherals. This driver is compatible with MikroSDK2.0 and can be used on various microcontroller platforms.

API Name

  • MikroSDK.Driver.OneWire

API Files

Prerequisites

  • Library Manager:

    • Select Driver.OneWire to add the API to your project using the Library Manager in NECTO Studio.
  • Headers:

    • Include the Driver.OneWire header in your source files to access the GPIO output functions.
    • This header pops up in the lower right part of NECTO Studio once you perform the previous task (while selecting Driver.OneWire in Library Manager)
    • Alternatively, include the drv_one_wire.h header in your source files to access the GPIO output functions.
  • CMakeLists:

    • You do not have to perform anything, configuration (CMake) file is already taken care of automatically while you performed the first task in this Prerequisites!
    • You can ensure that your CMakeLists.txt includes the necessary configurations to link against the MikroSDK.Driver.OneWire library.

Code Examples

  • How do I initialize the 1-Wire driver using drv_one_wire.h?
/* Project name:
 *   How do I initialize the 1-Wire driver?
 * Copyright:
 *   (c) MIKROE, 2024.
 * Description:
 *   - This example demonstrates the functionality of 1-Wire
 *     using mikroSDK v2.0.
 *   - 1-Wire shall be initialized, 1-Wire default setting shall be applied,
 *     1-Wire reset sequence shall be applied, reading of "Family Code"
 *     (1 byte), serial number (6 bytes) and CRC (1 byte) shall be conducted.
 * Library dependencies?
 *   - Make sure `Driver.OneWire` and `Board` libraries are enabled in NECTO's
 *     Library Manager to ensure a successful build.
 * How to test this code example?
 *   - Insert some
 *     [1-Wire Click board](https://www.mikroe.com/click?interface*=1-wire,1-wire)
 *     onto mikroBUS1 slot;
 *   - Run debug (press F9 on your keyboard);
 *   - Insert `one_wire_rom_addr` variable into Debug Watch Window;
 *   - Press F8 on your keyboard (Step Over) until you have read ROM
 *     address of the silicon on the selected click board
 *     within the `one_wire_rom_addr` variable.
 */

 // ------------------------------------------------------------------ INCLUDES
/**
 * Any initialization code needed for MCU to function properly.
 * Do not remove this line or clock might not be set correctly.
 */
#ifdef PREINIT_SUPPORTED
#include "preinit.h"
#endif

#include "board.h"
#include "drv_one_wire.h"
#include "delays.h"

// ----------------------------------------------------------------- VARIABLES
static one_wire_t one_wire_pin;
static one_wire_rom_address_t one_wire_rom_addr;

int main(void)
{
    /* Do not remove this line or clock might not be set correctly. */
    #ifdef PREINIT_SUPPORTED
    preinit();
    #endif

    // Configure default One Wire properties
    one_wire_configure_default( &one_wire_pin );

    // Choose One Wire data pin
    one_wire_pin.data_pin = MIKROBUS_1_PWM;

    // Enable clock for appropriate PORT, configure this pin as digital output
    if ( ONE_WIRE_SUCCESS != one_wire_open( &one_wire_pin ) ) {
        // Error handling strategy
        return ONE_WIRE_ERROR; // To give an example...
    };

    // Send reset sequence on One Wire bus
    if ( ONE_WIRE_SUCCESS != one_wire_reset( &one_wire_pin ) ) {
        // Error handling strategy
        return ONE_WIRE_ERROR; // To give an example...
    }

    // Read "Family Code" (1 byte), serial number (6 bytes) and CRC (1 byte)
    if ( ONE_WIRE_SUCCESS != one_wire_read_rom( &one_wire_pin, &one_wire_rom_addr ) ) {
        // Error handling strategy
        return ONE_WIRE_ERROR; // To give an example...
    };

    return 0;
}
  • How do I reconfigure the 1-Wire driver after it has been initialized?
/* Project name:
 *   How do I reconfigure the 1-Wire driver after it has been initialized?
 * Copyright:
 *   (c) MIKROE, 2024.
 * Description:
 *   - This example demonstrates the functionality of 1-Wire
 *     using mikroSDK v2.0.
 *   - 1-Wire shall be initialized, 1-Wire default setting shall be applied,
 *     1-Wire reset sequence shall be applied, reading of "Family Code"
 *     (1 byte), serial number (6 bytes) and CRC (1 byte) shall be conducted,
 *     1-Wire default setting shall be applied (to reset logical state of the
 *     1-Wire pin), reading of "Family Code" (1 byte), serial number (6 bytes)
 *     and CRC (1 byte) shall be conducted once again.
 * Library dependencies?
 *   - Make sure `Driver.OneWire` and `Board` libraries are enabled in NECTO's
 *     Library Manager to ensure a successful build.
 * How to test this code example?
 *   - Make sure you enabled necessary libraries (previous step);
 *   - Make sure you selected/created desired hardware setup -
 *     check lower right corner of NECTO's environment whilst being in the project;
 *   - Insert some
 *     [1-Wire Click board](https://www.mikroe.com/click?interface*=1-wire,1-wire)
 *     onto mikroBUS1 slot;
 *   - Run debug (press F9 on your keyboard);
 *   - Insert `one_wire_rom_addr` variable into Debug Watch Window;
 *   - Press F8 on your keyboard (Step Over) until you have read ROM
 *     address of the silicon on the selected Click board
 *     within the `one_wire_rom_addr` variable (read the ROM address twice,
 *     the address must be the same in both cases).
 */

 // ------------------------------------------------------------------ INCLUDES
/**
 * Any initialization code needed for MCU to function properly.
 * Do not remove this line or clock might not be set correctly.
 */
#ifdef PREINIT_SUPPORTED
#include "preinit.h"
#endif

#include "board.h"
#include "drv_one_wire.h"
#include "delays.h"

// ----------------------------------------------------------------- VARIABLES
static one_wire_t one_wire_pin;
static one_wire_rom_address_t one_wire_rom_addr;

// One Wire function which performs tasks for reading device's ROM address.
static one_wire_error_t read_rom_address( one_wire_t *obj ) {

    // Choose One Wire data pin
    one_wire_pin.data_pin = MIKROBUS_1_PWM;

    // Enable clock for appropriate PORT, configure this pin as digital output
    if ( ONE_WIRE_SUCCESS != one_wire_open( obj ) ) {
        // Error handling strategy
        return ONE_WIRE_ERROR; // To give an example...
    };

    // Send reset sequence on One Wire bus
    if ( ONE_WIRE_SUCCESS != one_wire_reset( obj ) ) {
        // Error handling strategy
        return ONE_WIRE_ERROR; // To give an example...
    }

    // Read "Family Code" (1 byte), serial number (6 bytes) and CRC (1 byte)
    if ( ONE_WIRE_SUCCESS != one_wire_read_rom( obj, &one_wire_rom_addr ) ) {
        // Error handling strategy
        return ONE_WIRE_ERROR; // To give an example...
    };

    return ONE_WIRE_SUCCESS;
}

int main(void)
{
    /* Do not remove this line or clock might not be set correctly. */
    #ifdef PREINIT_SUPPORTED
    preinit();
    #endif

    // Configure default One Wire properties
    one_wire_configure_default( &one_wire_pin );

    // Read ROM address of a device.
    if ( ONE_WIRE_SUCCESS != read_rom_address( &one_wire_pin ) ) {
        // Error handling strategy
        return ONE_WIRE_ERROR; // To give an example...
    };

    // Reset state of the One Wire object.
    one_wire_configure_default( &one_wire_pin );

    // Read ROM address of a device, once again.
    if ( ONE_WIRE_SUCCESS != read_rom_address( &one_wire_pin ) ) {
        // Error handling strategy
        return ONE_WIRE_ERROR; // To give an example...
    };

    return ONE_WIRE_SUCCESS;
}
  • How do I utilize two 1-Wire drivers in the same time?
/* Project name:
 *   How do I utilize two 1-Wire drivers in the same time?
 * Copyright:
 *   (c) MIKROE, 2024.
 * Description:
 *   - This example demonstrates the functionality of 1-Wire
 *     using mikroSDK v2.0.
 *   - 1-Wire shall be initialized, 1-Wire default setting shall be applied,
 *     1-Wire reset sequence shall be applied, reading of "Family Code"
 *     (1 byte), serial number (6 bytes) and CRC (1 byte) shall be conducted.
 * Library dependencies?
 *   - Make sure `Driver.OneWire` and `Board` libraries are enabled in NECTO's
 *     Library Manager to ensure a successful build.
 * How to test this code example?
 *   - Make sure you enabled necessary libraries (previous step);
 *   - Make sure you selected/created desired hardware setup -
 *     check lower right corner of NECTO's environment whilst being in the project;
 *   - Insert some
 *     [1-Wire Click board](https://www.mikroe.com/click?interface*=1-wire,1-wire)
 *     onto mikroBUS1 slot;
 *   - Run debug (press F9 on your keyboard);
 *   - Insert `one_wire_rom_addr` variable into Debug Watch Window;
 *   - Press F8 on your keyboard (Step Over) until you have read ROM
 *     address of the silicon on the selected click board
 *     within the `one_wire_rom_addr` variable.
 */

 // ------------------------------------------------------------------ INCLUDES
/**
 * Any initialization code needed for MCU to function properly.
 * Do not remove this line or clock might not be set correctly.
 */
#ifdef PREINIT_SUPPORTED
#include "preinit.h"
#endif

#include "board.h"
#include "drv_one_wire.h"
#include "delays.h"

// ----------------------------------------------------------------- VARIABLES
static one_wire_t one_wire_pin_1;
static one_wire_t one_wire_pin_2;
static one_wire_rom_address_t one_wire_rom_addr;

int main(void)
{
    /* Do not remove this line or clock might not be set correctly. */
    #ifdef PREINIT_SUPPORTED
    preinit();
    #endif

    // Configure default One Wire properties
    one_wire_configure_default( &one_wire_pin_1 );

    // Choose One Wire data pin
    one_wire_pin_1.data_pin = MIKROBUS_1_PWM;

    // Enable clock for appropriate PORT, configure this pin as digital output
    if ( ONE_WIRE_SUCCESS != one_wire_open( &one_wire_pin_1 ) ) {
        // Error handling strategy
        return ONE_WIRE_ERROR; // To give an example...
    };

    // Send reset sequence on One Wire bus
    if ( ONE_WIRE_SUCCESS != one_wire_reset( &one_wire_pin_1 ) ) {
        // Error handling strategy
        return ONE_WIRE_ERROR; // To give an example...
    }

    // Read "Family Code" (1 byte), serial number (6 bytes) and CRC (1 byte)
    if ( ONE_WIRE_SUCCESS != one_wire_read_rom( &one_wire_pin_1, &one_wire_rom_addr ) ) {
        // Error handling strategy
        return ONE_WIRE_ERROR; // To give an example...
    };

    // Choose another One Wire data pin
    one_wire_pin_2.data_pin = MIKROBUS_2_PWM;

    // Enable clock for appropriate PORT, configure this pin as digital output
    if ( ONE_WIRE_SUCCESS != one_wire_open( &one_wire_pin_2 ) ) {
        // Error handling strategy
        return ONE_WIRE_ERROR; // To give an example...
    };

    // Send reset sequence on One Wire bus
    if ( ONE_WIRE_SUCCESS != one_wire_reset( &one_wire_pin_2 ) ) {
        // Error handling strategy
        return ONE_WIRE_ERROR; // To give an example...
    }

    // Read "Family Code" (1 byte), serial number (6 bytes) and CRC (1 byte)
    if ( ONE_WIRE_SUCCESS != one_wire_read_rom( &one_wire_pin_2, &one_wire_rom_addr ) ) {
        // Error handling strategy
        return ONE_WIRE_ERROR; // To give an example...
    };

    return 0;
}
  • How can I check if the 1-Wire driver is properly initialized?
/* Project name:
 *   How can I check if the 1-Wire driver is properly initialized?
 * Copyright:
 *   (c) MIKROE, 2024.
 * Description:
 *   - This example demonstrates the functionality of 1-Wire using mikroSDK v2.0.
 *   - PWM shall be initialized, logging (printing characters)
 *     shall be initialized to be able to track proper allocation for 1-Wire.
 * Library dependencies?
 *   - Make sure `Driver.OneWire`, `Log` and `Board` libraries are enabled in NECTO's
 *     Library Manager to ensure a successful build.
 * How to test this code example?
 *   - Make sure you enabled necessary libraries (previous step);
 *   - Make sure you selected/created desired hardware setup -
 *     check lower right corner of NECTO's environment whilst being in the project;
 *   - Run debug mode by pressing F9 on your keyboard;
 *   - Continue debug (F6 on your keyboard);
 *   - Monitor the `Application Output` in the lower part of the NECTO's environment
 *     to check proper initialization of an OneWire driver.
 */

 // ------------------------------------------------------------------ INCLUDES
/**
 * Any initialization code needed for MCU to function properly.
 * Do not remove this line or clock might not be set correctly.
 */
#ifdef PREINIT_SUPPORTED
#include "preinit.h"
#endif

#include "drv_one_wire.h"   // API for 1-Wire driver
#include "delays.h"         // API for microcontroller time delays
#include "board.h"          // Main board pin mapping
#include "log.h"            // API for Logger driver

// ----------------------------------------------------------------- VARIABLES
static one_wire_t one_wire_pin; // 1-Wire driver context struct
static one_wire_rom_address_t one_wire_rom_addr; // 1-Wire ROM address struct

static log_t logger;        // Logger driver object.

int main(void)
{
    /* Do not remove this line or clock might not be set correctly. */
    #ifdef PREINIT_SUPPORTED
    preinit();
    #endif

    // Logger settings
    log_cfg_t log_cfg;              // Logger configuration structure instance
    log_init( &logger, &log_cfg );  // Initialize Logger
    log_printf( &logger, "Logger initialized.\n" ); // Print message

    // Configure default One Wire properties
    one_wire_configure_default( &one_wire_pin );
    log_printf( &logger, "Configured default 1-Wire settings.\n" );

    // Choose One Wire data pin
    one_wire_pin.data_pin = MIKROBUS_1_PWM;
    log_printf( &logger, "Selected `MIKROBUS_1_PWM` as an 1-Wire pin.\n" );

    // Enable clock for appropriate PORT, configure this pin as digital output
    if ( ONE_WIRE_SUCCESS != one_wire_open( &one_wire_pin ) ) {
        // Error handling strategy
        log_printf( &logger, "1-Wire open failed.\n" );
        return ONE_WIRE_ERROR; // To give an example...
    };
    log_printf( &logger, "1-Wire open successful.\n" );

    // Send reset sequence on One Wire bus
    if ( ONE_WIRE_SUCCESS != one_wire_reset( &one_wire_pin ) ) {
        // Error handling strategy
        log_printf( &logger, "1-Wire reset sequence failed.\n" );
        return ONE_WIRE_ERROR; // To give an example...
    }
    log_printf( &logger, "1-Wire reset successful.\n" );

    // Read "Family Code" (1 byte), serial number (6 bytes) and CRC (1 byte)
    if ( ONE_WIRE_SUCCESS != one_wire_read_rom( &one_wire_pin, &one_wire_rom_addr ) ) {
        log_printf( &logger, "1-Wire read ROM failed.\n" );
        // Error handling strategy
        return ONE_WIRE_ERROR; // To give an example...
    };
    log_printf( &logger, "1-Wire read ROM successful.\n" );

    return ONE_WIRE_SUCCESS;
}
  • How do I skip the ROM command when only one device is on the One Wire bus?
/* Project name:
 *   How do I skip the ROM command when only one device is on the One Wire bus?
 * Copyright:
 *   (c) MIKROE, 2024.
 * Description:
 *   - This example demonstrates the functionality of 1-Wire using mikroSDK v2.0.
 *   - 1-Wire shall be initialized, logging (printing characters)
 *     shall be initialized to be able to track proper allocation for 1-Wire.
 * Library dependencies?
 *   - Make sure `Driver.OneWire`, `Log` and `Board` libraries are enabled in NECTO's
 *     Library Manager to ensure a successful build.
 * How to test this code example?
 *   - Make sure you enabled necessary libraries (previous step);
 *   - Make sure you selected/created desired hardware setup -
 *     check lower right corner of NECTO's environment whilst being in the project;
 *   - Run debug mode by pressing F9 on your keyboard;
 *   - Continue debug (F6 on your keyboard);
 *   - Monitor the `Application Output` in the lower part of the NECTO's environment
 *     to check proper initialization of an OneWire driver and `Skip ROM` command.
 */

 // ------------------------------------------------------------------ INCLUDES
/**
 * Any initialization code needed for MCU to function properly.
 * Do not remove this line or clock might not be set correctly.
 */
#ifdef PREINIT_SUPPORTED
#include "preinit.h"
#endif

#include "drv_one_wire.h"   // API for 1-Wire driver
#include "delays.h"         // API for microcontroller time delays
#include "board.h"          // Main board pin mapping
#include "log.h"            // API for Logger driver

// ----------------------------------------------------------------- VARIABLES
static one_wire_t one_wire_pin; // 1-Wire driver context struct
static one_wire_rom_address_t one_wire_rom_addr; // 1-Wire ROM address struct

static log_t logger;        // Logger driver object.

// ----------------------------------------------------------------- MAIN
int main(void)
{
    /* Do not remove this line or clock might not be set correctly. */
    #ifdef PREINIT_SUPPORTED
    preinit();
    #endif

    // Logger settings
    log_cfg_t log_cfg;              // Logger configuration structure instance
    log_init( &logger, &log_cfg );  // Initialize Logger
    log_printf( &logger, "Logger initialized.\n" ); // Print message

    // Configure default One Wire properties
    one_wire_configure_default( &one_wire_pin );
    log_printf( &logger, "Configured default 1-Wire settings.\n" );

    // Choose One Wire data pin
    one_wire_pin.data_pin = MIKROBUS_1_PWM;
    log_printf( &logger, "Selected `MIKROBUS_1_PWM` as an 1-Wire pin.\n" );

    // Enable clock for appropriate PORT, configure this pin as digital output
    if ( ONE_WIRE_SUCCESS != one_wire_open( &one_wire_pin ) ) {
        // Error handling strategy
        log_printf( &logger, "1-Wire open failed.\n" );
        return ONE_WIRE_ERROR; // To give an example...
    };
    log_printf( &logger, "1-Wire open successful.\n" );

    // Send reset sequence on One Wire bus
    if ( ONE_WIRE_SUCCESS != one_wire_reset( &one_wire_pin ) ) {
        // Error handling strategy
        log_printf( &logger, "1-Wire reset sequence failed.\n" );
        return ONE_WIRE_ERROR; // To give an example...
    }
    log_printf( &logger, "1-Wire reset successful.\n" );

    // Read "Family Code" (1 byte), serial number (6 bytes) and CRC (1 byte)
    if ( ONE_WIRE_SUCCESS != one_wire_read_rom( &one_wire_pin, &one_wire_rom_addr ) ) {
        log_printf( &logger, "1-Wire read ROM failed.\n" );
        // Error handling strategy
        return ONE_WIRE_ERROR; // To give an example...
    };
    log_printf( &logger, "1-Wire read ROM successful.\n" );

    // One Wire Reset sequence and "Skip ROM" command.
    if ( ONE_WIRE_SUCCESS != one_wire_skip_rom( &one_wire_pin ) ) {
        log_printf( &logger, "1-Wire skip ROM failed.\n" );
        // Error handling strategy
        return ONE_WIRE_ERROR; // To give an example...
    };
    log_printf( &logger, "1-Wire skip ROM successful.\n" );

    /* NOTE: THE FOLLOWING IS THE PRACTICAL EXAMPLE
             WITH THE CLICK BOARD:
    */
    // Convert temperature command sequence for
    // [Thermo 19 Click](https://www.mikroe.com/thermo-19-click)
    uint8_t cmd_convert_temperature = 0x44;

    // Start temperature conversion of a device.
    if ( ONE_WIRE_SUCCESS != one_wire_write_byte( &one_wire_pin, &cmd_convert_temperature, 1 ) ) {
        // Error handling strategy
        log_printf( &logger, "1-Wire write byte failed.\n" );
        return ONE_WIRE_ERROR; // To give an example...
    }
    log_printf( &logger, "1-Wire write byte successful.\n" );

    return ONE_WIRE_SUCCESS;
}
  • How do I match a specific device on the One Wire bus using its ROM address?
/* Project name:
 *   How do I match a specific device on the One Wire bus using its ROM address?
 * Copyright:
 *   (c) MIKROE, 2024.
 * Description:
 *   - This example demonstrates the functionality of 1-Wire using mikroSDK v2.0.
 *   - 1-Wire shall be initialized, logging (printing characters)
 *     shall be initialized to be able to track proper allocation for 1-Wire,
 *     `Match ROM` command shall be conducted.
 * Library dependencies?
 *   - Make sure `Driver.OneWire`, `Log` and `Board` libraries are enabled in NECTO's
 *     Library Manager to ensure a successful build.
 * How to test this code example?
 *   - Make sure you have some 1-Wire Click board connected to mikroBUS 1 slot
 *     on the dev board
 *     (example: [Thermo 19 Click](https://www.mikroe.com/thermo-19-click) );
 *   - Make sure you enabled necessary libraries (previous step);
 *   - Make sure you selected/created desired hardware setup -
 *     check lower right corner of NECTO's environment whilst being in the project;
 *   - Run debug mode by pressing F9 on your keyboard;
 *   - Continue debug (F6 on your keyboard);
 *   - Monitor the `Application Output` in the lower part of the NECTO's environment
 *     to check proper initialization of an OneWire driver and `Skip ROM` command.
 */

 // ------------------------------------------------------------------ INCLUDES
/**
 * Any initialization code needed for MCU to function properly.
 * Do not remove this line or clock might not be set correctly.
 */
#ifdef PREINIT_SUPPORTED
#include "preinit.h"
#endif

#include "drv_one_wire.h"   // API for 1-Wire driver
#include "delays.h"         // API for microcontroller time delays
#include "board.h"          // Main board pin mapping
#include "log.h"            // API for Logger driver

// ----------------------------------------------------------------- VARIABLES
static one_wire_t one_wire_pin; // 1-Wire driver context struct
static one_wire_rom_address_t one_wire_rom_addr; // 1-Wire ROM address struct

static log_t logger;        // Logger driver object.

// ----------------------------------------------------------------- MAIN
int main(void)
{
    /* Do not remove this line or clock might not be set correctly. */
    #ifdef PREINIT_SUPPORTED
    preinit();
    #endif

    // Logger settings
    log_cfg_t log_cfg;              // Logger configuration structure instance
    log_init( &logger, &log_cfg );  // Initialize Logger
    log_printf( &logger, "Logger initialized.\n" ); // Print message

    // Configure default One Wire properties
    one_wire_configure_default( &one_wire_pin );
    log_printf( &logger, "Configured default 1-Wire settings.\n" );

    // Choose One Wire data pin
    one_wire_pin.data_pin = MIKROBUS_1_PWM;
    log_printf( &logger, "Selected `MIKROBUS_1_PWM` as an 1-Wire pin.\n" );

    // Enable clock for appropriate PORT, configure this pin as digital output
    if ( ONE_WIRE_SUCCESS != one_wire_open( &one_wire_pin ) ) {
        // Error handling strategy
        log_printf( &logger, "1-Wire open failed.\n" );
        return ONE_WIRE_ERROR; // To give an example...
    };
    log_printf( &logger, "1-Wire open successful.\n" );

    // Send reset sequence on One Wire bus
    if ( ONE_WIRE_SUCCESS != one_wire_reset( &one_wire_pin ) ) {
        // Error handling strategy
        log_printf( &logger, "1-Wire reset sequence failed.\n" );
        return ONE_WIRE_ERROR; // To give an example...
    }
    log_printf( &logger, "1-Wire reset successful.\n" );

    // Read "Family Code" (1 byte), serial number (6 bytes) and CRC (1 byte)
    if ( ONE_WIRE_SUCCESS != one_wire_read_rom( &one_wire_pin, &one_wire_rom_addr ) ) {
        log_printf( &logger, "1-Wire read ROM failed.\n" );
        // Error handling strategy
        return ONE_WIRE_ERROR; // To give an example...
    };
    log_printf( &logger, "1-Wire read ROM successful.\n" );

    // One Wire Reset sequence and "Match ROM" command.
    if ( ONE_WIRE_SUCCESS != one_wire_match_rom( &one_wire_pin, &one_wire_rom_addr ) ) {
        log_printf( &logger, "1-Wire match ROM failed.\n" );
        // Error handling strategy
        return ONE_WIRE_ERROR; // To give an example...
    };
    log_printf( &logger, "1-Wire match ROM successful.\n" );

    /* NOTE: THE FOLLOWING IS THE PRACTICAL EXAMPLE
       WITH THE [Thermo 19 Click](https://www.mikroe.com/thermo-19-click):
    */

    // Convert temperature command sequence
    uint8_t cmd_convert_temperature = 0x44;

    // Start temperature conversion of a device
    if ( ONE_WIRE_SUCCESS != one_wire_write_byte( &one_wire_pin, &cmd_convert_temperature, 1 ) ) {
        // Error handling strategy
        log_printf( &logger, "1-Wire write byte failed.\n" );
        return ONE_WIRE_ERROR; // To give an example...
    }
    log_printf( &logger, "1-Wire write byte successful.\n" );

    return ONE_WIRE_SUCCESS;
}
  • How do I search for the first device on the One Wire bus?
/* Project name:
 *   How do I search for the first device on the One Wire bus?
 * Copyright:
 *   (c) MIKROE, 2024.
 * Description:
 *   - This example demonstrates the functionality of 1-Wire using mikroSDK v2.0.
 *   - 1-Wire shall be initialized, logging (printing characters)
 *     shall be initialized to be able to track proper allocation for 1-Wire,
 *     `Search First Device` command shall be conducted.
 * Library dependencies?
 *   - Make sure `Driver.OneWire`, `Log` and `Board` libraries are enabled in NECTO's
 *     Library Manager to ensure a successful build.
 * How to test this code example?
 *   - Make sure you have some 1-Wire Click board connected to mikroBUS 1 slot
 *     on the dev board
 *     (example: [Thermo 19 Click](https://www.mikroe.com/thermo-19-click) );
 *   - Make sure you enabled necessary libraries (previous step);
 *   - Make sure you selected/created desired hardware setup -
 *     check lower right corner of NECTO's environment whilst being in the project;
 *   - Run debug mode by pressing F9 on your keyboard;
 *   - Insert `first_device` variable in the Debug's Watch Window;
 *   - Step over until the end of the example (F8 on your keyboard);
 *   - Monitor the `Application Output` in the lower part of the NECTO's environment
 *     to check proper initialization of an OneWire driver and `Search First Device`
 *     command.
 */

 // ------------------------------------------------------------------ INCLUDES
/**
 * Any initialization code needed for MCU to function properly.
 * Do not remove this line or clock might not be set correctly.
 */
#ifdef PREINIT_SUPPORTED
#include "preinit.h"
#endif

#include "drv_one_wire.h"   // API for 1-Wire driver
#include "delays.h"         // API for microcontroller time delays
#include "board.h"          // Main board pin mapping
#include "log.h"            // API for Logger driver

// ----------------------------------------------------------------- VARIABLES
static one_wire_t one_wire_pin; // 1-Wire driver context struct
static one_wire_rom_address_t first_device; // 1-Wire ROM address struct

static log_t logger;        // Logger driver object.

// ----------------------------------------------------------------- MAIN
int main(void)
{
    /* Do not remove this line or clock might not be set correctly. */
    #ifdef PREINIT_SUPPORTED
    preinit();
    #endif

    // Logger settings
    log_cfg_t log_cfg;              // Logger configuration structure instance
    log_init( &logger, &log_cfg );  // Initialize Logger
    log_printf( &logger, "Logger initialized.\n" ); // Print message

    // Configure default One Wire properties
    one_wire_configure_default( &one_wire_pin );
    log_printf( &logger, "Configured default 1-Wire settings.\n" );

    // Choose One Wire data pin
    one_wire_pin.data_pin = MIKROBUS_1_PWM;
    log_printf( &logger, "Selected `MIKROBUS_1_PWM` as an 1-Wire pin.\n" );

    // Enable clock for appropriate PORT, configure this pin as digital output
    if ( ONE_WIRE_SUCCESS != one_wire_open( &one_wire_pin ) ) {
        // Error handling strategy
        log_printf( &logger, "1-Wire open failed.\n" );
        return ONE_WIRE_ERROR; // To give an example...
    };
    log_printf( &logger, "1-Wire open successful.\n" );

    // Send reset sequence on One Wire bus
    if ( ONE_WIRE_SUCCESS != one_wire_reset( &one_wire_pin ) ) {
        // Error handling strategy
        log_printf( &logger, "1-Wire reset sequence failed.\n" );
        return ONE_WIRE_ERROR; // To give an example...
    }
    log_printf( &logger, "1-Wire reset successful.\n" );

    // Search for the first device on the 1-Wire bus
    if ( ONE_WIRE_SUCCESS != one_wire_search_first_device( &one_wire_pin, &first_device ) ) {
        log_printf( &logger, "1-Wire Search First Device failed.\n" );
        // Error handling strategy
        return ONE_WIRE_ERROR; // To give an example...
    };
    log_printf( &logger, "1-Wire Search First Device successful.\n" );

    // One Wire Reset sequence and "Match ROM" command
    if ( ONE_WIRE_SUCCESS != one_wire_match_rom( &one_wire_pin, &first_device ) ) {
        log_printf( &logger, "1-Wire match ROM failed.\n" );
        // Error handling strategy
        return ONE_WIRE_ERROR; // To give an example...
    };
    log_printf( &logger, "1-Wire match ROM successful.\n" );

    /* NOTE: THE FOLLOWING IS THE PRACTICAL EXAMPLE
       WITH THE [Thermo 19 Click](https://www.mikroe.com/thermo-19-click):
    */

    // Convert temperature command sequence
    uint8_t cmd_convert_temperature = 0x44;

    // Start temperature conversion of a device
    if ( ONE_WIRE_SUCCESS != one_wire_write_byte( &one_wire_pin, &cmd_convert_temperature, 1 ) ) {
        // Error handling strategy
        log_printf( &logger, "1-Wire write byte failed.\n" );
        return ONE_WIRE_ERROR; // To give an example...
    }
    log_printf( &logger, "1-Wire write byte successful.\n" );

    return ONE_WIRE_SUCCESS;
}
  • How do I search for the next device on the One Wire bus after the first?
/* Project name:
 *   How do I search for the next device on the One Wire bus after the first?
 * Copyright:
 *   (c) MIKROE, 2024.
 * Description:
 *   - This example demonstrates the functionality of 1-Wire using mikroSDK v2.0.
 *   - 1-Wire shall be initialized, logging (printing characters)
 *     shall be initialized to be able to track proper allocation for 1-Wire,
 *     `Search Next Device` command shall be conducted.
 * Library dependencies?
 *   - Make sure `Driver.OneWire`, `Log` and `Board` libraries are enabled in NECTO's
 *     Library Manager to ensure a successful build.
 * How to test this code example?
 *   - Make sure you have at least two 1-Wire Click boards connected to mikroBUS 1 slot
 *     on the dev board
 *     (example: [Thermo 19 Click](https://www.mikroe.com/thermo-19-click) );
 *   - Make sure you enabled necessary libraries (previous step);
 *   - Make sure you selected/created desired hardware setup -
 *     check lower right corner of NECTO's environment whilst being in the project;
 *   - Run debug mode by pressing F9 on your keyboard;
 *   - Insert `first_device` variable in the Debug's Watch Window;
 *   - Insert `next_device` variable in the Debug's Watch Window;
 *   - Step over until the end of the example (F8 on your keyboard);
 *   - Monitor the `Application Output` in the lower part of the NECTO's environment
 *     to check proper initialization of an OneWire driver and `Search Next Device`
 *     command.
 */

 // ------------------------------------------------------------------ INCLUDES
/**
 * Any initialization code needed for MCU to function properly.
 * Do not remove this line or clock might not be set correctly.
 */
#ifdef PREINIT_SUPPORTED
#include "preinit.h"
#endif

#include "drv_one_wire.h"   // API for 1-Wire driver
#include "delays.h"         // API for microcontroller time delays
#include "board.h"          // Main board pin mapping
#include "log.h"            // API for Logger driver

// ----------------------------------------------------------------- VARIABLES
static one_wire_t one_wire_pin; // 1-Wire driver context struct
static one_wire_rom_address_t first_device; // 1-Wire ROM address struct
static one_wire_rom_address_t next_device; // 1-Wire ROM address struct

static log_t logger;        // Logger driver object.

// ----------------------------------------------------------------- MAIN
int main(void)
{
    /* Do not remove this line or clock might not be set correctly. */
    #ifdef PREINIT_SUPPORTED
    preinit();
    #endif

    // Logger settings
    log_cfg_t log_cfg;              // Logger configuration structure instance
    log_init( &logger, &log_cfg );  // Initialize Logger
    log_printf( &logger, "Logger initialized.\n" ); // Print message

    // Configure default One Wire properties
    one_wire_configure_default( &one_wire_pin );
    log_printf( &logger, "Configured default 1-Wire settings.\n" );

    // Choose One Wire data pin
    one_wire_pin.data_pin = MIKROBUS_1_PWM;
    log_printf( &logger, "Selected `MIKROBUS_1_PWM` as an 1-Wire pin.\n" );

    // Enable clock for appropriate PORT, configure this pin as digital output
    if ( ONE_WIRE_SUCCESS != one_wire_open( &one_wire_pin ) ) {
        // Error handling strategy
        log_printf( &logger, "1-Wire open failed.\n" );
        return ONE_WIRE_ERROR; // To give an example...
    };
    log_printf( &logger, "1-Wire open successful.\n" );

    // Send reset sequence on One Wire bus
    if ( ONE_WIRE_SUCCESS != one_wire_reset( &one_wire_pin ) ) {
        // Error handling strategy
        log_printf( &logger, "1-Wire reset sequence failed.\n" );
        return ONE_WIRE_ERROR; // To give an example...
    }
    log_printf( &logger, "1-Wire reset successful.\n" );

    // Search for the first device on the 1-Wire bus
    if ( ONE_WIRE_SUCCESS != one_wire_search_first_device( &one_wire_pin, &first_device ) ) {
        log_printf( &logger, "1-Wire Search First Device failed.\n" );
        // Error handling strategy
        return ONE_WIRE_ERROR; // To give an example...
    };
    log_printf( &logger, "1-Wire Search First Device successful.\n" );

    // Search for the next device on the 1-Wire bus
    do {
        if ( ONE_WIRE_SUCCESS != one_wire_search_next_device( &one_wire_pin, &next_device ) ) {
            log_printf( &logger, "1-Wire Search Next Device failed.\n" );
            // Error handling strategy
            return ONE_WIRE_ERROR; // To give an example...
        };
    } while (next_device.address[0]);

    return ONE_WIRE_SUCCESS;
}
  • How do I write a byte to the One Wire bus?
/* Project name:
 *   How do I write a byte to the One Wire bus?
 * Copyright:
 *   (c) MIKROE, 2024.
 * Description:
 *   - This example demonstrates the functionality of 1-Wire using mikroSDK v2.0.
 *   - 1-Wire shall be initialized, logging (printing characters)
 *     shall be initialized to be able to track proper allocation for 1-Wire,
 *     `Write Byte` command shall be conducted.
 * Library dependencies?
 *   - Make sure `Driver.OneWire`, `Log` and `Board` libraries are enabled in NECTO's
 *     Library Manager to ensure a successful build.
 * How to test this code example?
 *   - Make sure you have some 1-Wire Click board connected to mikroBUS 1 slot
 *     on the dev board
 *     (example: [Thermo 19 Click](https://www.mikroe.com/thermo-19-click) );
 *   - Make sure you enabled necessary libraries (previous step);
 *   - Make sure you selected/created desired hardware setup -
 *     check lower right corner of NECTO's environment whilst being in the project;
 *   - Run debug mode by pressing F9 on your keyboard;
 *   - Step over until the end of the example (F8 on your keyboard);
 *   - Monitor the `Application Output` in the lower part of the NECTO's environment
 *     to check proper initialization of an OneWire driver and `Write Byte`
 *     command.
 */

 // ------------------------------------------------------------------ INCLUDES
/**
 * Any initialization code needed for MCU to function properly.
 * Do not remove this line or clock might not be set correctly.
 */
#ifdef PREINIT_SUPPORTED
#include "preinit.h"
#endif

#include "drv_one_wire.h"   // API for 1-Wire driver
#include "delays.h"         // API for microcontroller time delays
#include "board.h"          // Main board pin mapping
#include "log.h"            // API for Logger driver

// ----------------------------------------------------------------- VARIABLES
static one_wire_t one_wire_pin; // 1-Wire driver context struct
static one_wire_rom_address_t first_device; // 1-Wire ROM address struct

static log_t logger;        // Logger driver object.

// ----------------------------------------------------------------- MAIN
int main(void)
{
    /* Do not remove this line or clock might not be set correctly. */
    #ifdef PREINIT_SUPPORTED
    preinit();
    #endif

    // Logger settings
    log_cfg_t log_cfg;              // Logger configuration structure instance
    log_init( &logger, &log_cfg );  // Initialize Logger
    log_printf( &logger, "Logger initialized.\n" ); // Print message

    // Configure default One Wire properties
    one_wire_configure_default( &one_wire_pin );
    log_printf( &logger, "Configured default 1-Wire settings.\n" );

    // Choose One Wire data pin
    one_wire_pin.data_pin = MIKROBUS_1_PWM;
    log_printf( &logger, "Selected `MIKROBUS_1_PWM` as an 1-Wire pin.\n" );

    // Enable clock for appropriate PORT, configure this pin as digital output
    if ( ONE_WIRE_SUCCESS != one_wire_open( &one_wire_pin ) ) {
        // Error handling strategy
        log_printf( &logger, "1-Wire open failed.\n" );
        return ONE_WIRE_ERROR; // To give an example...
    };
    log_printf( &logger, "1-Wire open successful.\n" );

    // Send reset sequence on One Wire bus
    if ( ONE_WIRE_SUCCESS != one_wire_reset( &one_wire_pin ) ) {
        // Error handling strategy
        log_printf( &logger, "1-Wire reset sequence failed.\n" );
        return ONE_WIRE_ERROR; // To give an example...
    }
    log_printf( &logger, "1-Wire reset successful.\n" );

    // One Wire Reset sequence and "Skip ROM" command
    if ( ONE_WIRE_SUCCESS != one_wire_skip_rom( &one_wire_pin ) ) {
        log_printf( &logger, "1-Wire skip ROM failed.\n" );
        // Error handling strategy
        return ONE_WIRE_ERROR; // To give an example...
    };
    log_printf( &logger, "1-Wire skip ROM successful.\n" );

    /* NOTE: THE FOLLOWING IS THE PRACTICAL EXAMPLE
       WITH THE [Thermo 19 Click](https://www.mikroe.com/thermo-19-click):
    */

    // Convert temperature command sequence
    uint8_t cmd_convert_temperature = 0x44;

    // Start temperature conversion of a device
    if ( ONE_WIRE_SUCCESS != one_wire_write_byte( &one_wire_pin, &cmd_convert_temperature, 1 ) ) {
        // Error handling strategy
        log_printf( &logger, "1-Wire write byte failed.\n" );
        return ONE_WIRE_ERROR; // To give an example...
    }
    log_printf( &logger, "1-Wire write byte successful.\n" );

    return ONE_WIRE_SUCCESS;
}
  • How do I read a byte from the One Wire bus?
/* Project name:
 *   How do I read a byte from the One Wire bus?
 * Copyright:
 *   (c) MIKROE, 2024.
 * Description:
 *   - This example demonstrates the functionality of 1-Wire using mikroSDK v2.0.
 *   - 1-Wire shall be initialized, logging (printing characters)
 *     shall be initialized to be able to track proper allocation for 1-Wire,
 *     `Read Byte` command shall be conducted.
 * Library dependencies?
 *   - Make sure `Driver.OneWire`, `Log` and `Board` libraries are enabled in NECTO's
 *     Library Manager to ensure a successful build.
 * How to test this code example?
 *   - Make sure you have some 1-Wire Click board connected to mikroBUS 1 slot
 *     on the dev board
 *     (example: [Thermo 19 Click](https://www.mikroe.com/thermo-19-click) );
 *   - Make sure you enabled necessary libraries (previous step);
 *   - Make sure you selected/created desired hardware setup -
 *     check lower right corner of NECTO's environment whilst being in the project;
 *   - Run debug mode by pressing F9 on your keyboard;
 *   - Step over until the end of the example (F8 on your keyboard);
 *   - Monitor the `Application Output` in the lower part of the NECTO's environment
 *     to check proper initialization of an OneWire driver and `Read Byte`
 *     command.
 */

 // ------------------------------------------------------------------ INCLUDES
/**
 * Any initialization code needed for MCU to function properly.
 * Do not remove this line or clock might not be set correctly.
 */
#ifdef PREINIT_SUPPORTED
#include "preinit.h"
#endif

#include "drv_one_wire.h"   // API for 1-Wire driver
#include "delays.h"         // API for microcontroller time delays
#include "board.h"          // Main board pin mapping
#include "log.h"            // API for Logger driver

// ----------------------------------------------------------------- MACROS
#define THERMO19_CMD_CONVERT_TEMPERATURE (0x44) // Command for Thermo 19 Click

// ----------------------------------------------------------------- VARIABLES
static one_wire_t one_wire_pin; // 1-Wire driver context struct
static log_t logger;        // Logger driver object

// ----------------------------------------------------------------- MAIN
int main(void)
{
    /* Do not remove this line or clock might not be set correctly. */
    #ifdef PREINIT_SUPPORTED
    preinit();
    #endif

    // Logger settings
    log_cfg_t log_cfg;              // Logger configuration structure instance
    log_init( &logger, &log_cfg );  // Initialize Logger
    log_printf( &logger, "Logger initialized.\n" ); // Print message

    // Configure default One Wire properties
    one_wire_configure_default( &one_wire_pin );
    log_printf( &logger, "Configured default 1-Wire settings.\n" );

    // Choose One Wire data pin
    one_wire_pin.data_pin = MIKROBUS_1_PWM;
    log_printf( &logger, "Selected `MIKROBUS_1_PWM` as an 1-Wire pin.\n" );

    // Enable clock for appropriate PORT, configure this pin as digital output
    if ( ONE_WIRE_SUCCESS != one_wire_open( &one_wire_pin ) ) {
        // Error handling strategy
        log_printf( &logger, "1-Wire open failed.\n" );
        return ONE_WIRE_ERROR; // To give an example...
    };
    log_printf( &logger, "1-Wire open successful.\n" );

    // Send reset sequence on One Wire bus
    if ( ONE_WIRE_SUCCESS != one_wire_reset( &one_wire_pin ) ) {
        // Error handling strategy
        log_printf( &logger, "1-Wire reset sequence failed.\n" );
        return ONE_WIRE_ERROR; // To give an example...
    }
    log_printf( &logger, "1-Wire reset successful.\n" );

    // One Wire Reset sequence and "Skip ROM" command
    if ( ONE_WIRE_SUCCESS != one_wire_skip_rom( &one_wire_pin ) ) {
        log_printf( &logger, "1-Wire skip ROM failed.\n" );
        // Error handling strategy
        return ONE_WIRE_ERROR; // To give an example...
    };
    log_printf( &logger, "1-Wire skip ROM successful.\n" );

    /* NOTE: THE FOLLOWING IS THE PRACTICAL EXAMPLE
       WITH THE [Thermo 19 Click](https://www.mikroe.com/thermo-19-click):
    */

    // Convert temperature command sequence
    uint8_t cmd_convert_temperature = THERMO19_CMD_CONVERT_TEMPERATURE;

    // Start temperature conversion of a device
    if ( ONE_WIRE_SUCCESS != one_wire_write_byte( &one_wire_pin, &cmd_convert_temperature, 1 ) ) {
        // Error handling strategy
        log_printf( &logger, "1-Wire write byte failed.\n" );
        return ONE_WIRE_ERROR; // To give an example...
    }
    log_printf( &logger, "1-Wire write byte successful.\n" );

    // Buffer for reading a data
    uint8_t data_buf[ 9 ] = {0};

    // Read Scratchpad
    if ( ONE_WIRE_SUCCESS != one_wire_read_byte( &one_wire_pin, data_buf, 9 ) ) {
        // Error handling strategy
        log_printf( &logger, "1-Wire read byte failed.\n" );
        return ONE_WIRE_ERROR; // To give an example...
    }
    log_printf( &logger, "1-Wire read byte successful.\n" );

    return ONE_WIRE_SUCCESS;
}
  • How do I ensure that the One Wire driver is using the correct timing?

For custom timing, you must override weak functions declared in hal_one_wire.h. Example:

void one_wire_timing_value_h() {
    asm {nop}; // Example of custom timing sequence for `mikroC for AI` compilers
    // asm("nop"); // Example of custom timing sequence compilers other than `mikroC for AI`
}