A cut-down version of https://os.mbed.com/users/Sissors/code/DS1820/ tweaked for use with the STM32F103. It is all generic Mbed operations though, so should be usable anywhere. Non-essential functions have been removed, as this is intended for use within a tutorial.

Dependencies:   LinkedList

Dependents:   STM32F103C8T6_DS18B20 stm32f103c8t6-ds18b20

Fork of DS1820 by Erik -

Committer:
deece
Date:
Thu Jan 25 07:33:48 2018 +0000
Revision:
19:b9d69bad8185
Parent:
16:d490e11c466d
Fix timings

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Michael_ 2:ee820a991b95 1 /* mbed DS1820 Library, for the Dallas (Maxim) 1-Wire Digital Thermometer
Michael_ 2:ee820a991b95 2 * Copyright (c) 2010, Michael Hagberg Michael@RedBoxCode.com
Michael_ 2:ee820a991b95 3 *
Michael_ 2:ee820a991b95 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
Michael_ 2:ee820a991b95 5 * of this software and associated documentation files (the "Software"), to deal
Michael_ 2:ee820a991b95 6 * in the Software without restriction, including without limitation the rights
Michael_ 2:ee820a991b95 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
Michael_ 2:ee820a991b95 8 * copies of the Software, and to permit persons to whom the Software is
Michael_ 2:ee820a991b95 9 * furnished to do so, subject to the following conditions:
Michael_ 2:ee820a991b95 10 *
Michael_ 2:ee820a991b95 11 * The above copyright notice and this permission notice shall be included in
Michael_ 2:ee820a991b95 12 * all copies or substantial portions of the Software.
Michael_ 2:ee820a991b95 13 *
Michael_ 2:ee820a991b95 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Michael_ 2:ee820a991b95 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Michael_ 2:ee820a991b95 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Michael_ 2:ee820a991b95 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Michael_ 2:ee820a991b95 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Michael_ 2:ee820a991b95 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
Michael_ 2:ee820a991b95 20 * THE SOFTWARE.
Michael_ 2:ee820a991b95 21 */
Michael_ 2:ee820a991b95 22
Michael_ 2:ee820a991b95 23 #ifndef MBED_DS1820_H
Michael_ 2:ee820a991b95 24 #define MBED_DS1820_H
Michael_ 2:ee820a991b95 25
Michael_ 2:ee820a991b95 26 #include "mbed.h"
Sissors 5:2cd4928e8147 27 #include "LinkedList.h"
Michael_ 2:ee820a991b95 28
Sissors 5:2cd4928e8147 29 #define FAMILY_CODE _ROM[0]
Sissors 5:2cd4928e8147 30 #define FAMILY_CODE_DS1820 0x10
Sissors 5:2cd4928e8147 31 #define FAMILY_CODE_DS18B20 0x28
Sissors 5:2cd4928e8147 32 #define FAMILY_CODE_DS1822 0x22
Michael_ 2:ee820a991b95 33
Michael_ 2:ee820a991b95 34 /** DS1820 Dallas 1-Wire Temperature Probe
Michael_ 2:ee820a991b95 35 *
Michael_ 2:ee820a991b95 36 * Example:
Michael_ 2:ee820a991b95 37 * @code
Michael_ 2:ee820a991b95 38 * #include "mbed.h"
Michael_ 2:ee820a991b95 39 * #include "DS1820.h"
Michael_ 2:ee820a991b95 40 *
Sissors 5:2cd4928e8147 41 * DS1820 probe(DATA_PIN);
Sissors 5:2cd4928e8147 42 *
Michael_ 2:ee820a991b95 43 * int main() {
Sissors 5:2cd4928e8147 44 * while(1) {
Sissors 5:2cd4928e8147 45 * probe.convertTemperature(true, DS1820::all_devices); //Start temperature conversion, wait until ready
Sissors 5:2cd4928e8147 46 * printf("It is %3.1foC\r\n", probe.temperature());
Sissors 5:2cd4928e8147 47 * wait(1);
Michael_ 2:ee820a991b95 48 * }
Michael_ 2:ee820a991b95 49 * }
Michael_ 2:ee820a991b95 50 * @endcode
Michael_ 2:ee820a991b95 51 */
Michael_ 2:ee820a991b95 52 class DS1820 {
Michael_ 2:ee820a991b95 53 public:
Michael_ 2:ee820a991b95 54 enum devices{
Michael_ 2:ee820a991b95 55 this_device, // command applies to only this device
Michael_ 2:ee820a991b95 56 all_devices }; // command applies to all devices
Sissors 7:58b61681818f 57
Sissors 7:58b61681818f 58 enum {
Sissors 7:58b61681818f 59 invalid_conversion = -1000
Sissors 7:58b61681818f 60 };
Michael_ 2:ee820a991b95 61
Michael_ 2:ee820a991b95 62 /** Create a probe object connected to the specified pins
Michael_ 2:ee820a991b95 63 *
Michael_ 2:ee820a991b95 64 * @param data_pin DigitalInOut pin for the data bus
Michael_ 2:ee820a991b95 65 */
deece 16:d490e11c466d 66 DS1820(PinName data_pin);
Sissors 5:2cd4928e8147 67 ~DS1820();
Michael_ 2:ee820a991b95 68
Michael_ 2:ee820a991b95 69 /** This routine will initiate the temperature conversion within
Sissors 5:2cd4928e8147 70 * one or all DS1820 probes.
Michael_ 2:ee820a991b95 71 *
pairmand 3:8f2b7f4940b5 72 * @param wait if true or parisitic power is used, waits up to 750 ms for
pairmand 3:8f2b7f4940b5 73 * conversion otherwise returns immediatly.
pairmand 3:8f2b7f4940b5 74 * @param device allows the function to apply to a specific device or
Michael_ 2:ee820a991b95 75 * to all devices on the 1-Wire bus.
pairmand 3:8f2b7f4940b5 76 * @returns milliseconds untill conversion will complete.
Michael_ 2:ee820a991b95 77 */
Sissors 6:abfdd851218a 78 int convertTemperature(bool wait, devices device=all_devices);
Michael_ 2:ee820a991b95 79
pairmand 3:8f2b7f4940b5 80 /** This function will return the probe temperature. Approximately 10ms per
pairmand 3:8f2b7f4940b5 81 * probe to read its RAM, do CRC check and convert temperature on the LPC1768.
Michael_ 2:ee820a991b95 82 *
Michael_ 2:ee820a991b95 83 * @param scale, may be either 'c' or 'f'
Sissors 7:58b61681818f 84 * @returns temperature for that scale, or DS1820::invalid_conversion (-1000) if CRC error detected.
Michael_ 2:ee820a991b95 85 */
Michael_ 2:ee820a991b95 86 float temperature(char scale='c');
Michael_ 2:ee820a991b95 87
pairmand 3:8f2b7f4940b5 88 /** This function sets the temperature resolution for the DS18B20
pairmand 3:8f2b7f4940b5 89 * in the configuration register.
pairmand 3:8f2b7f4940b5 90 *
pairmand 3:8f2b7f4940b5 91 * @param a number between 9 and 12 to specify resolution
pairmand 3:8f2b7f4940b5 92 * @returns true if successful
pairmand 3:8f2b7f4940b5 93 */
Sissors 5:2cd4928e8147 94 bool setResolution(unsigned int resolution);
Michael_ 2:ee820a991b95 95
Michael_ 2:ee820a991b95 96 private:
deece 16:d490e11c466d 97
Sissors 11:1a3c3002b50c 98 static char CRC_byte(char _CRC, char byte );
deece 16:d490e11c466d 99 bool onewire_reset();
Michael_ 2:ee820a991b95 100 void match_ROM();
Michael_ 2:ee820a991b95 101 void skip_ROM();
deece 16:d490e11c466d 102 bool search_ROM_routine(char command, char *ROM_address);
deece 16:d490e11c466d 103 void onewire_bit_out (bool bit_data);
Michael_ 2:ee820a991b95 104 void onewire_byte_out(char data);
deece 16:d490e11c466d 105 bool onewire_bit_in();
Michael_ 2:ee820a991b95 106 char onewire_byte_in();
Sissors 5:2cd4928e8147 107 static bool ROM_checksum_error(char *_ROM_address);
Sissors 5:2cd4928e8147 108 bool RAM_checksum_error();
Sissors 5:2cd4928e8147 109 void read_RAM();
deece 16:d490e11c466d 110 bool unassignedProbe(char *ROM_address);
Sissors 5:2cd4928e8147 111 void write_scratchpad(int data);
Michael_ 2:ee820a991b95 112
Michael_ 2:ee820a991b95 113 DigitalInOut _datapin;
Sissors 5:2cd4928e8147 114
Sissors 5:2cd4928e8147 115 char _ROM[8];
Sissors 5:2cd4928e8147 116 char RAM[9];
Sissors 5:2cd4928e8147 117
Sissors 5:2cd4928e8147 118 static LinkedList<node> probes;
Michael_ 2:ee820a991b95 119 };
Michael_ 2:ee820a991b95 120
Michael_ 2:ee820a991b95 121
Michael_ 2:ee820a991b95 122 #endif