stm32-DS18B20-Sensor-Communication

by Marwen Maghrebi
#include "ds18b20.h"
#include "uart.h"
#include "string.h"
#include "stdio.h"

// DS18B20 Functions
float DS18b20_temp(void) {
    uint8_t temp_lsb, temp_msb;
    int16_t raw_temp;

    onewire_reset();       // Initialize communication
    onewire_Write(0xCC);   // Skip ROM command
    onewire_Write(0x44);   // Start temperature conversion
    HAL_Delay(750);        // Wait for conversion (750 ms for 12-bit resolution)

    onewire_reset();       // Reset the bus
    onewire_Write(0xCC);   // Skip ROM command
    onewire_Write(0xBE);   // Read Scratchpad

    // Read temperature data
    temp_lsb = onewire_Read();
    temp_msb = onewire_Read();

    // Convert raw temperature to Celsius
    raw_temp = (temp_msb << 8) | temp_lsb;
    return (float)raw_temp / 16.0;
}

void delay_us_dwt_init(void) {
    CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;  // Enable DWT
    DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk;             // Enable cycle counter
}

void delay_us_dwt(uint32_t us) {
    uint32_t start_cycle = DWT->CYCCNT;
    uint32_t delay_cycles = (HAL_RCC_GetHCLKFreq() / 1000000) * us;
    while ((DWT->CYCCNT - start_cycle) < delay_cycles);
}

uint8_t onewire_reset(void) {
    uint8_t sensor_present = 0;

    Output_Pin(ONEWIRE_GPIO_Port, ONEWIRE_Pin);
    HAL_GPIO_WritePin(ONEWIRE_GPIO_Port, ONEWIRE_Pin, GPIO_PIN_RESET);
    delay_us_dwt(480);  // Pull low for 480µs

    Input_Pin(ONEWIRE_GPIO_Port, ONEWIRE_Pin);
    delay_us_dwt(60);   // Wait for sensor response

    if (HAL_GPIO_ReadPin(ONEWIRE_GPIO_Port, ONEWIRE_Pin) == GPIO_PIN_RESET) {
        sensor_present = 1;
    }

    delay_us_dwt(480);  // Complete the reset sequence
    return sensor_present;
}

void onewire_Write(uint8_t dato) {
    for (int i = 0; i < 8; i++) {
        if (dato & (1 << i)) {
            Output_Pin(ONEWIRE_GPIO_Port, ONEWIRE_Pin);
            HAL_GPIO_WritePin(ONEWIRE_GPIO_Port, ONEWIRE_Pin, GPIO_PIN_RESET);
            delay_us_dwt(6);
            Input_Pin(ONEWIRE_GPIO_Port, ONEWIRE_Pin);
            delay_us_dwt(64);
        } else {
            Output_Pin(ONEWIRE_GPIO_Port, ONEWIRE_Pin);
            HAL_GPIO_WritePin(ONEWIRE_GPIO_Port, ONEWIRE_Pin, GPIO_PIN_RESET);
            delay_us_dwt(60);
            Input_Pin(ONEWIRE_GPIO_Port, ONEWIRE_Pin);
            delay_us_dwt(10);
        }
    }
}

uint8_t onewire_Read(void) {
    uint8_t read_byte = 0;
    for (int i = 0; i < 8; i++) {
        Output_Pin(ONEWIRE_GPIO_Port, ONEWIRE_Pin);
        HAL_GPIO_WritePin(ONEWIRE_GPIO_Port, ONEWIRE_Pin, GPIO_PIN_RESET);
        delay_us_dwt(6);
        Input_Pin(ONEWIRE_GPIO_Port, ONEWIRE_Pin);
        delay_us_dwt(9);
        if (HAL_GPIO_ReadPin(ONEWIRE_GPIO_Port, ONEWIRE_Pin) == GPIO_PIN_SET) {
            read_byte |= (1 << i);
        }
        delay_us_dwt(55);
    }
    return read_byte;
}
Are you sure want to unlock this post?
Unlock left : 0
Are you sure want to cancel subscription?
-
00:00
00:00
Update Required Flash plugin
-
00:00
00:00