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 -

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers DS1820.h Source File

DS1820.h

00001 /* mbed DS1820 Library, for the Dallas (Maxim) 1-Wire Digital Thermometer
00002  * Copyright (c) 2010, Michael Hagberg Michael@RedBoxCode.com
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining a copy
00005  * of this software and associated documentation files (the "Software"), to deal
00006  * in the Software without restriction, including without limitation the rights
00007  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00008  * copies of the Software, and to permit persons to whom the Software is
00009  * furnished to do so, subject to the following conditions:
00010  *
00011  * The above copyright notice and this permission notice shall be included in
00012  * all copies or substantial portions of the Software.
00013  *
00014  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00017  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00019  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00020  * THE SOFTWARE.
00021  */
00022 
00023 #ifndef MBED_DS1820_H
00024 #define MBED_DS1820_H
00025 
00026 #include "mbed.h"
00027 #include "LinkedList.h"
00028 
00029 #define FAMILY_CODE _ROM[0]
00030 #define FAMILY_CODE_DS1820 0x10
00031 #define FAMILY_CODE_DS18B20 0x28
00032 #define FAMILY_CODE_DS1822  0x22
00033 
00034 /** DS1820 Dallas 1-Wire Temperature Probe
00035  *
00036  * Example:
00037  * @code
00038  * #include "mbed.h"
00039  * #include "DS1820.h"
00040  *
00041  * DS1820 probe(DATA_PIN);
00042  *  
00043  * int main() {
00044  *     while(1) {
00045  *         probe.convertTemperature(true, DS1820::all_devices);         //Start temperature conversion, wait until ready
00046  *         printf("It is %3.1foC\r\n", probe.temperature());
00047  *         wait(1);
00048  *     }
00049  * }
00050  * @endcode
00051  */
00052 class DS1820 {
00053 public:
00054     enum devices{
00055         this_device,     // command applies to only this device
00056         all_devices };   // command applies to all devices
00057     
00058     enum {
00059         invalid_conversion = -1000
00060     };
00061 
00062     /** Create a probe object connected to the specified pins
00063      *
00064      * @param data_pin DigitalInOut pin for the data bus
00065      */
00066     DS1820(PinName data_pin);
00067     ~DS1820();
00068 
00069     /** This routine will initiate the temperature conversion within
00070       * one or all DS1820 probes. 
00071       *
00072       * @param wait if true or parisitic power is used, waits up to 750 ms for 
00073       * conversion otherwise returns immediatly.
00074       * @param device allows the function to apply to a specific device or
00075       * to all devices on the 1-Wire bus.
00076       * @returns milliseconds untill conversion will complete.
00077       */
00078     int convertTemperature(bool wait, devices device=all_devices);
00079 
00080     /** This function will return the probe temperature. Approximately 10ms per
00081       * probe to read its RAM, do CRC check and convert temperature on the LPC1768.
00082       *
00083       * @param scale, may be either 'c' or 'f'
00084       * @returns temperature for that scale, or DS1820::invalid_conversion (-1000) if CRC error detected.
00085       */
00086     float temperature(char scale='c');
00087 
00088     /** This function sets the temperature resolution for the DS18B20
00089       * in the configuration register.
00090       *
00091       * @param a number between 9 and 12 to specify resolution
00092       * @returns true if successful
00093       */ 
00094     bool setResolution(unsigned int resolution);       
00095 
00096 private:
00097    
00098     static char CRC_byte(char _CRC, char byte );
00099     bool onewire_reset();
00100     void match_ROM();
00101     void skip_ROM();
00102     bool search_ROM_routine(char command, char *ROM_address);
00103     void onewire_bit_out (bool bit_data);
00104     void onewire_byte_out(char data);
00105     bool onewire_bit_in();
00106     char onewire_byte_in();
00107     static bool ROM_checksum_error(char *_ROM_address);
00108     bool RAM_checksum_error();
00109     void read_RAM();
00110     bool unassignedProbe(char *ROM_address);
00111     void write_scratchpad(int data);
00112 
00113     DigitalInOut _datapin;
00114     
00115     char _ROM[8];
00116     char RAM[9];
00117     
00118     static LinkedList<node> probes;
00119 };
00120 
00121 
00122 #endif