IoT-MBBR (masterthesis)
Masterthesis upgrading a Moving Bed BioReactor with extra sensors and cloud connectivity.
DS18B20.h File Reference

All code for the DS18B20 temperature sensor. More...

#include <stdint.h>

Go to the source code of this file.

Functions

int32_t readTempDS18B20 (uint8_t number)
 Get a temperature value from the DS18B20. More...
 

Detailed Description

All code for the DS18B20 temperature sensor.

Version
4.0
Author
Alec Vanderhaegen & Sarah Goossens
Heavily modified by Brecht Van Eeckhoudt

License

Copyright (C) 2020 - Brecht Van Eeckhoudt

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

A copy of the GNU General Public License can be found in the LICENSE file along with this source code.


Some methods use code obtained from examples from Silicon Labs' GitHub. These sections are licensed under the Silabs License Agreement. See the file "Silabs_License_Agreement.txt" for details. Before using this software for any purpose, you must agree to the terms of that agreement.

Definition in file DS18B20.h.

Function Documentation

◆ readTempDS18B20()

int32_t readTempDS18B20 ( uint8_t  number)

Get a temperature value from the DS18B20.

USTimer gets initialized, the sensor gets powered, the data-transmission takes place, the timer gets de-initialized to disable the clocks and interrupts, the data and power pin get disabled and finally the read values are converted to an int32_t value.
Negative temperatures work fine.

Parameters
[in]number
  • 0 - TEMP0 selected.
  • 1 - TEMP1 selected.
Returns
The read temperature data.

Definition at line 119 of file DS18B20.c.

120 {
121  /* Timeout counter */
122  uint16_t counter = 0;
123 
124  /* Variable to indicate if a conversion has been completed */
125  bool conversionCompleted = false;
126 
127  /* Variable to hold raw data bytes */
128  uint8_t rawDataDS18B20[9] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
129 
130  /* Initialize timer
131  * Initializing and disabling the timer again adds about 40 µs active time but should conserve sleep energy... */
132  USTIMER_Init();
133 
134  /* Initialize and power VDD pin */
135  powerDS18B20(number, true);
136 
137  /* Power-up delay of 5 ms */
138  delay(5);
139 
140  /* Initialize communication and only continue if successful */
141  if (init_DS18B20(number))
142  {
143  writeByteDS18B20(number, 0xCC); /* 0xCC = "Skip Rom" (address all devices on the bus simultaneously without sending out any ROM code information) */
144  writeByteDS18B20(number, 0x44); /* 0x44 = "Convert T" */
145 
146  /* MASTER now generates "read time slots", the DS18B20 will write HIGH to the bus if the conversion is completed
147  * The datasheet gives the following directions for time slots, but reading bytes also seems to work...
148  * - Read time slots have a 60 µs duration and 1 µs recovery between slots
149  * - After the master pulls the line low for 1 µs, the data is valid for up to 15 µs */
150  while ((counter < TIMEOUT_CONVERSION) && !conversionCompleted)
151  {
152  uint8_t testByte = readByteDS18B20(number);
153  if (testByte > 0) conversionCompleted = true;
154 
155  counter++;
156  }
157 
158  /* Exit the function if the maximum waiting time was reached */
159  if (counter == TIMEOUT_CONVERSION)
160  {
161 
162 #if DEBUG_DBPRINT == 1 /* DEBUG_DBPRINT */
163  dbcrit("Waiting time for DS18B20 conversion reached!");
164 #endif /* DEBUG_DBPRINT */
165 
166  /* Disable interrupts and turn off the clock to the underlying hardware timer. */
167  USTIMER_DeInit();
168 
169  /* Disable data pin (otherwise we got a "sleep" current of about 330 µA due to the on-board (10k) pull-up) */
170  if (number == 0) GPIO_PinModeSet(TEMP0_DATA_PORT, TEMP0_DATA_PIN, gpioModeDisabled, 0);
171  else if (number == 1) GPIO_PinModeSet(TEMP1_DATA_PORT, TEMP1_DATA_PIN, gpioModeDisabled, 0);
172 
173  /* Disable the VDD pin */
174  powerDS18B20(number, false);
175 
176  error(29);
177 
178  /* Exit function */
179  return (0);
180 
181  }
182 #if DBPRINT_TIMEOUT == 1 /* DBPRINT_TIMEOUT */
183  else
184  {
185 
186 #if DEBUG_DBPRINT == 1 /* DEBUG_DBPRINT */
187  dbwarnInt("DS18B20 conversion (", counter, ")");
188 #endif /* DEBUG_DBPRINT */
189 
190  }
191 #endif /* DBPRINT_TIMEOUT */
192 
193  init_DS18B20(number); /* Initialize communication */
194  writeByteDS18B20(number, 0xCC); /* 0xCC = "Skip Rom" */
195  writeByteDS18B20(number, 0xBE); /* 0xBE = "Read Scratchpad" */
196 
197  /* Read the bytes */
198  for (uint8_t i = 0; i < 9; i++) rawDataDS18B20[i] = readByteDS18B20(number);
199 
200  /* Disable interrupts and turn off the clock to the underlying hardware timer. */
201  USTIMER_DeInit();
202 
203  /* Disable data pin (otherwise we got a "sleep" current of about 330 µA due to the on-board (10k) pull-up) */
204  if (number == 0) GPIO_PinModeSet(TEMP0_DATA_PORT, TEMP0_DATA_PIN, gpioModeDisabled, 0);
205  else if (number == 1) GPIO_PinModeSet(TEMP1_DATA_PORT, TEMP1_DATA_PIN, gpioModeDisabled, 0);
206 
207  /* Disable the VDD pin */
208  powerDS18B20(number, false);
209 
210  /* Return the converted byte */
211  return (convertTempData(rawDataDS18B20[0], rawDataDS18B20[1]));
212  }
213  else
214  {
215  /* Disable interrupts and turn off the clock to the underlying hardware timer. */
216  USTIMER_DeInit();
217 
218 #if DEBUG_DBPRINT == 1 /* DEBUG_DBPRINT */
219  dbcrit("DS18B20 INIT failed!");
220 #endif /* DEBUG_DBPRINT */
221 
222  /* Disable data pin (otherwise we got a "sleep" current of about 330 µA due to the on-board (10k) pull-up) */
223  if (number == 0) GPIO_PinModeSet(TEMP0_DATA_PORT, TEMP0_DATA_PIN, gpioModeDisabled, 0);
224  else if (number == 1) GPIO_PinModeSet(TEMP1_DATA_PORT, TEMP1_DATA_PIN, gpioModeDisabled, 0);
225 
226  /* Disable the VDD pin */
227  powerDS18B20(number, false);
228 
229  /* Exit function */
230  return (0);
231  }
232 }
void dbwarnInt(char *message1, int32_t value, char *message2)
Print a warning value surrounded by two strings (char array) to USARTx.
Definition: dbprint.c:600
#define TIMEOUT_CONVERSION
Definition: DS18B20.c:85
void error(uint8_t number)
Error method.
Definition: util.c:122
static bool init_DS18B20(uint8_t number)
Initialize communication to the DS18B20.
Definition: DS18B20.c:309
static int32_t convertTempData(uint8_t tempLS, uint8_t tempMS)
Convert temperature data.
Definition: DS18B20.c:487
#define TEMP1_DATA_PORT
Definition: pin_mapping.h:103
void delay(uint32_t msDelay)
Wait for a certain amount of milliseconds in EM2/3.
Definition: delay.c:124
#define TEMP0_DATA_PORT
Definition: pin_mapping.h:99
static void powerDS18B20(uint8_t number, bool enabled)
Enable or disable the power to the temperature sensor.
Definition: DS18B20.c:254
#define TEMP1_DATA_PIN
Definition: pin_mapping.h:104
void dbcrit(char *message)
Print a critical error string (char array) in red to USARTx and go to the next line.
Definition: dbprint.c:541
static uint8_t readByteDS18B20(uint8_t number)
Read a byte (uint8_t) from the DS18B20.
Definition: DS18B20.c:435
unsigned short uint16_t
Typedef defining 16 bit unsigned short type. The developer should modify this to suit the platform be...
Definition: vl53l1_types.h:88
static void writeByteDS18B20(uint8_t number, uint8_t data)
Write a byte (uint8_t) to the DS18B20.
Definition: DS18B20.c:375
#define TEMP0_DATA_PIN
Definition: pin_mapping.h:100
unsigned char uint8_t
Typedef defining 8 bit unsigned char type. The developer should modify this to suit the platform bein...
Definition: vl53l1_types.h:98