C++ Library for interfacing the DS18B20 Temperature Sensor with a STM32 like the STM32F103C8 (BLUEPILL)
Based on the Library by https://controllerstech.com/ds18b20-and-stm32/
Using the Library is very easy. Follow these steps:
-
Create a hardware timer (htim) that ticks every microsecond. Calculate the necessary prescaler based on your mcu clock frequency.
-
Copy the .hpp and .cpp files to your source / include folder.
-
Include the header file
#include "DS18B20.hpp"
- Create a new DS18B20 object by passing the htim reference, the GPIO Port and the GPIO Pin of the sensor. If you are using htim1 and Pin PB13 for example, call
DS18B20 temp_sensor = DS18B20(&htim1, GPIOB, GPIO_Pin_13);
- Use the implemented functions to read the temperature
float temperature = temp_sensor.read_temp_celsius();
float temperature = temp_sensor.read_temp_fahrenheit();
- Make sure to use the right build flag for your microcontroller. See
DS18B20.hpp
for all types (you can include your own controller if you need to). For STM32F103C8Tx for example, use-D STM32F1
or simply use#define STM32F1
before including the header in your code.