HAL library 06- DS18B20 for STM32Fxxx
When you have onewire library done, it’s easy to make interface for DS18B20 sensors.
Library
Features
- Search sensors
- Read temperature
- Set resolution for each device from 9 to 12 bits
- Start temperature conversion on all devices simultaneously
- Set alarm low and high temperatures
- Disable alarm temperatures
- Search devices with alarm flag set
Dependencies
- HAL
- TM
- STM32Fxxx HAL
- defines.h
- GPIO
- DELAY
- ONEWIRE
Functions and enumerations
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
/** * @defgroup TM_DS18B20_Typedefs * @brief Library Typedefs * @{ */ /** * @brief DS18B0 Resolutions available */ typedef enum { TM_DS18B20_Resolution_9bits = 9, /*!< DS18B20 9 bits resolution */ TM_DS18B20_Resolution_10bits = 10, /*!< DS18B20 10 bits resolution */ TM_DS18B20_Resolution_11bits = 11, /*!< DS18B20 11 bits resolution */ TM_DS18B20_Resolution_12bits = 12 /*!< DS18B20 12 bits resolution */ } TM_DS18B20_Resolution_t; /** * @} */ /** * @defgroup TM_DS18B20_Functions * @brief Library Functions * @{ */ /** * @brief Starts temperature conversion for specific DS18B20 on specific onewire channel * @param *OneWireStruct: Pointer to @ref TM_OneWire_t working structure (OneWire channel) * @param *ROM: Pointer to first byte of ROM address for desired DS12B80 device. * Entire ROM address is 8-bytes long * @retval 1 if device is DS18B20 or 0 if not */ uint8_t TM_DS18B20_Start(TM_OneWire_t* OneWireStruct, uint8_t* ROM); /** * @brief Starts temperature conversion for all DS18B20 devices on specific onewire channel * @note This mode will skip ROM addressing * @param *OneWireStruct: Pointer to @ref TM_OneWire_t working structure (OneWire channel) * @retval None */ void TM_DS18B20_StartAll(TM_OneWire_t* OneWireStruct); /** * @brief Reads temperature from DS18B20 * @param *OneWireStruct: Pointer to @ref TM_OneWire_t working structure (OneWire channel) * @param *ROM: Pointer to first byte of ROM address for desired DS12B80 device. * Entire ROM address is 8-bytes long * @param *destination: Pointer to float variable to store temperature * @retval Temperature status: * - 0: Device is not DS18B20 or conversion is not done yet or CRC failed * - > 0: Temperature is read OK */ uint8_t TM_DS18B20_Read(TM_OneWire_t* OneWireStruct, uint8_t* ROM, float* destination); /** * @brief Gets resolution for temperature conversion from DS18B20 device * @param *OneWireStruct: Pointer to @ref TM_OneWire_t working structure (OneWire channel) * @param *ROM: Pointer to first byte of ROM address for desired DS12B80 device. * Entire ROM address is 8-bytes long * @retval Resolution: * - 0: Device is not DS18B20 * - 9 - 12: Resolution of DS18B20 */ uint8_t TM_DS18B20_GetResolution(TM_OneWire_t* OneWireStruct, uint8_t* ROM); /** * @brief Sets resolution for specific DS18B20 device * @param *OneWireStruct: Pointer to @ref TM_OneWire_t working structure (OneWire channel) * @param *ROM: Pointer to first byte of ROM address for desired DS12B80 device. * Entire ROM address is 8-bytes long * @param resolution: Resolution for DS18B20 device. This parameter can be a value of @ref TM_DS18B20_Resolution_t enumeration. * @retval Success status: * - 0: Device is not DS18B20 * - > 0: Resolution set OK */ uint8_t TM_DS18B20_SetResolution(TM_OneWire_t* OneWireStruct, uint8_t* ROM, TM_DS18B20_Resolution_t resolution); /** * @brief Checks if device with specific ROM number is DS18B20 * @param *ROM: Pointer to first byte of ROM address for desired DS12B80 device. * Entire ROM address is 8-bytes long * @retval Device status * - 0: Device is not DS18B20 * - > 0: Device is DS18B20 */ uint8_t TM_DS18B20_Is(uint8_t* ROM); /** * @brief Sets high alarm temperature to specific DS18B20 sensor * @param *OneWireStruct: Pointer to @ref TM_OneWire_t working structure (OneWire channel) * @param *ROM: Pointer to first byte of ROM address for desired DS12B80 device. * Entire ROM address is 8-bytes long * @param temp: integer value for temperature between -55 to 125 degrees * @retval Success status: * - 0: Device is not DS18B20 * - > 0: High alarm set OK */ uint8_t TM_DS18B20_SetAlarmHighTemperature(TM_OneWire_t* OneWireStruct, uint8_t* ROM, int8_t temp); /** * @brief Sets low alarm temperature to specific DS18B20 sensor * @param *OneWireStruct: Pointer to @ref TM_OneWire_t working structure (OneWire channel) * @param *ROM: Pointer to first byte of ROM address for desired DS12B80 device. * Entire ROM address is 8-bytes long * @param temp: integer value for temperature between -55 to 125 degrees * @retval Success status: * - 0: Device is not DS18B20 * - > 0: Low alarm set OK */ uint8_t TM_DS18B20_SetAlarmLowTemperature(TM_OneWire_t* OneWireStruct, uint8_t* ROM, int8_t temp); /** * @brief Disables alarm temperature for specific DS18B20 sensor * @param *OneWireStruct: Pointer to @ref TM_OneWire_t working structure (OneWire channel) * @param *ROM: Pointer to first byte of ROM address for desired DS12B80 device. * Entire ROM address is 8-bytes long * @retval Success status: * - 0: Device is not DS18B20 * - > 0: Alarm disabled OK */ uint8_t TM_DS18B20_DisableAlarmTemperature(TM_OneWire_t* OneWireStruct, uint8_t* ROM); /** * @brief Searches for devices with alarm flag set * @param *OneWireStruct: Pointer to @ref TM_OneWire_t working structure (OneWire channel) * @retval Alarm search status * - 0: No device found with alarm flag set * - > 0: Device is found with alarm flag * @note To get all devices on one onewire channel with alarm flag set, you can do this: @verbatim while (TM_DS18B20_AlarmSearch(&OneWireStruct)) { //Read device ID here //Print to user device by device } @endverbatim * @retval 1 if any device has flag, otherwise 0 */ uint8_t TM_DS18B20_AlarmSearch(TM_OneWire_t* OneWireStruct); /** * @brief Checks if all DS18B20 sensors are done with temperature conversion * @param *OneWireStruct: Pointer to @ref TM_OneWire_t working structure (OneWire channel) * @retval Conversion status * - 0: Not all devices are done * - > 0: All devices are done with conversion */ uint8_t TM_DS18B20_AllDone(TM_OneWire_t* OneWireStruct); /** * @} */ |
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
/** * Keil project example for DS18B20 temperature sensor * * Before you start, select your target, on the right of the "Load" button * * @author Tilen Majerle * @email tilen@majerle.eu * @website http://stm32f4-discovery.net * @ide Keil uVision 5 * @conf PLL parameters are set in "Options for Target" -> "C/C++" -> "Defines" * @packs STM32F4xx/STM32F7xx Keil packs are requred with HAL driver support * @stdperiph STM32F4xx/STM32F7xx HAL drivers required */ /* Include core modules */ #include "stm32fxxx_hal.h" /* Include my libraries here */ #include "defines.h" #include "tm_stm32_disco.h" #include "tm_stm32_delay.h" #include "tm_stm32_ds18b20.h" #include "tm_stm32_onewire.h" /* Onewire structure */ TM_OneWire_t OW; /* Array for DS18B20 ROM number */ uint8_t DS_ROM[8]; /* Temperature variable */ float temp; int main(void) { /* Init system clock for maximum system speed */ TM_RCC_InitSystem(); /* Init HAL layer */ HAL_Init(); /* Init leds */ TM_DISCO_LedInit(); /* Init ONEWIRE port on PB4 pin */ TM_OneWire_Init(&OW, GPIOB, GPIO_PIN_4); /* Check if any device is connected */ if (TM_OneWire_First(&OW)) { /* Set LED GREEN */ TM_DISCO_LedOn(LED_GREEN); /* Read ROM number */ TM_OneWire_GetFullROM(&OW, DS_ROM); } else { /* Set LED RED */ TM_DISCO_LedOn(LED_RED); } /* Start temp conversion */ if (TM_DS18B20_Is(DS_ROM)) { /* Set resolution */ TM_DS18B20_SetResolution(&OW, DS_ROM, TM_DS18B20_Resolution_12bits); /* Set high and low alarms */ TM_DS18B20_SetAlarmHighTemperature(&OW, DS_ROM, 30); TM_DS18B20_SetAlarmLowTemperature(&OW, DS_ROM, 10); /* Start conversion on all sensors */ TM_DS18B20_StartAll(&OW); } while (1) { /* Check if connected device is DS18B20 */ if (TM_DS18B20_Is(DS_ROM)) { /* Everything is done */ if (TM_DS18B20_AllDone(&OW)) { /* Read temperature from device */ if (TM_DS18B20_Read(&OW, DS_ROM, &temp)) { /* Temp read OK, CRC is OK */ /* Start again on all sensors */ TM_DS18B20_StartAll(&OW); /* Check temperature */ if (temp > 30) { TM_DISCO_LedOn(LED_RED); } else { TM_DISCO_LedOff(LED_RED); } } else { /* CRC failed, hardware problems on data line */ } } } } } |
Project is available on Github, download all libraries below.
STM32 libraries based on STM32Fxxx HAL drivers.
Recent comments