stm32f103rb_hal_ds18b20

Index Commits Files Refs README
app/src/ds18b20.c (3398B)
   1 /*
   2  * Copyright (c) 2026 Martin Javier Klöckner <mklockner@fi.uba.ar>
   3  *
   4  * See file `LICENSE` for full details
   5  */
   6 
   7 #include <stdio.h>
   8 #include <stdint.h>
   9 #include <stdbool.h>
  10 #include <string.h>
  11 
  12 #include "main.h"
  13 
  14 #include "ds18b20.h"
  15 #include "logger.h"
  16 
  17 uint8_t b_DS18B20_Received_data;
  18 uint8_t g_DS18B20_Received_data_buffer[8];
  19 // float g_DS18B20_temp;
  20 // extern UART_HandleTypeDef huart1;
  21 
  22 void UART1_SetBaud(uint32_t baud)
  23 {
  24     huart1.Instance = USART1;
  25     huart1.Init.BaudRate = baud;
  26     huart1.Init.WordLength = UART_WORDLENGTH_8B;
  27     huart1.Init.StopBits = UART_STOPBITS_1;
  28     huart1.Init.Parity = UART_PARITY_NONE;
  29     huart1.Init.Mode = UART_MODE_TX_RX;
  30     huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  31     huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  32     if (HAL_HalfDuplex_Init(&huart1) != HAL_OK)
  33     {
  34         Error_Handler();
  35     };
  36 }
  37 
  38 uint8_t DS18B20_Reset(void)
  39 {
  40     uint8_t data = 0xF0;
  41     UART1_SetBaud(9600);
  42     HAL_UART_Transmit(&huart1, &data, 1, 100);
  43 
  44     if ((HAL_UART_Receive(&huart1, &data, 1, 1000) != HAL_OK) || (data == 0xF0))
  45     {
  46         // initialization failed
  47         LOGGER_ERROR("Couldn't read temperature sensor");
  48         return 1;
  49     }
  50 
  51     UART1_SetBaud(115200);
  52     return 0;
  53 }
  54 
  55 void DS18B20_SetResolution(uint8_t resolution)
  56 {
  57     uint8_t config;
  58 
  59     switch(resolution)
  60     {
  61         case 9:  config = 0x1F; break;
  62         case 10: config = 0x3F; break;
  63         case 11: config = 0x5F; break;
  64         case 12: config = 0x7F; break;
  65         default: config = 0x7F;
  66     }
  67 
  68     DS18B20_Reset();
  69     DS18B20_Write(0xCC); // send 'Skip ROM' command
  70     DS18B20_Write(0x4E); // send 'write scratchpad' command
  71 
  72     DS18B20_Write(0x00); // TH
  73     DS18B20_Write(0x00); // TL
  74     DS18B20_Write(config);
  75 
  76     DS18B20_Reset();
  77 
  78     DS18B20_Write(0xCC);   // Skip ROM
  79     DS18B20_Write(0x48);   // Write scratchpad to EEPROM
  80 }
  81 
  82 void DS18B20_Init(void)
  83 {
  84     if (DS18B20_Reset() == 0)
  85     {
  86         LOGGER_INFO("Temperature sensor initialized successfully");
  87     }
  88     DS18B20_SetResolution(10);
  89 }
  90 
  91 void DS18B20_Write(uint8_t data)
  92 {
  93     uint8_t buffer[8];
  94 
  95     for (uint8_t i = 0; i < 8; ++i)
  96     {
  97         if (data & (1 << i))
  98         {
  99             buffer[i] = 0xFF;
 100         }
 101         else
 102         {
 103             buffer[i] = 0x00;
 104         }
 105     }
 106 
 107     HAL_UART_Transmit(&huart1, buffer, 8, 100);
 108 }
 109 
 110 uint8_t DS18B20_Read(void)
 111 {
 112     uint8_t buffer[8];
 113     uint8_t received_value = 0;
 114 
 115     for (uint8_t i = 0; i < 8; ++i)
 116     {
 117         buffer[i] = 0xFF;
 118     }
 119 
 120     HAL_UART_Transmit_DMA(&huart1, buffer, 8);
 121     HAL_UART_Receive_DMA(&huart1, g_DS18B20_Received_data_buffer, 8);
 122 
 123     // while(b_DS18B20_Received_data == false)
 124     //     ;
 125 
 126     // b_DS18B20_Received_data = false;
 127 
 128     for (uint8_t i = 0; i < 8; ++i)
 129     {
 130         if (g_DS18B20_Received_data_buffer[i] == 0xFF)
 131         {
 132             received_value |= (1 << i);
 133         }
 134     }
 135 
 136     return received_value;
 137 }
 138 
 139 void DS18B20_Read_temp(void)
 140 {
 141     DS18B20_Reset();
 142     DS18B20_Write(0xCC); // send 'Skip ROM' command
 143     DS18B20_Write(0x44); // send 'Convert T' command
 144 
 145     DS18B20_Reset();
 146     DS18B20_Write(0xCC); // send 'Skip ROM' command
 147     DS18B20_Write(0xBE); // send 'read scratchpad' command
 148 
 149     // uint8_t Temp_LSB = DS18B20_Read();
 150     // uint8_t Temp_MSB = DS18B20_Read();
 151 
 152     // uint16_t temp = (Temp_MSB << 8) | Temp_LSB;
 153     // g_DS18B20_temp = (float) temp / 16.0;
 154 }