Embedded System Design 2 - Project
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 (void)
 Get a temperature value from the DS18B20. More...
 

Detailed Description

All code for the DS18B20 temperature sensor.

Version
3.1
Author
Alec Vanderhaegen & Sarah Goossens
Modified by Brecht Van Eeckhoudt

License

Copyright (C) 2019 - 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 ( void  )

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.

Returns
The read temperature data.

Definition at line 113 of file DS18B20.c.

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