Fork of Erik's DS1820 library working on OS6

Dependencies:   LinkedList2

Dependents:   DS1820-example DS1820mitWebserver DS1820ohneWebserver

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers DS1820.h Source File

DS1820.h

00001 
00002 #ifndef MBED_DS1820_H
00003 #define MBED_DS1820_H
00004 
00005 #include "mbed.h"
00006 #include "LinkedList2.h"
00007 
00008 #define FAMILY_CODE _ROM[0]
00009 #define FAMILY_CODE_DS1820 0x10
00010 #define FAMILY_CODE_DS18B20 0x28
00011 #define FAMILY_CODE_DS1822  0x22
00012 
00013 /** DS1820 Dallas 1-Wire Temperature Probe
00014  *
00015  * Example for single sensor:
00016  * @code
00017  * #include "mbed.h"
00018  * #include "DS1820.h"
00019  *
00020  * DS1820 probe(DATA_PIN);
00021  *  
00022  * int main() {
00023  *     while(1) {
00024  *         probe.convertTemperature(true, DS1820::all_devices); //Start temperature conversion, wait until completed
00025  *         printf("It is %3.1foC\r\n", probe.temperature());
00026  *         ThisThread::sleep_for(chrono::milliseconds(1000));
00027  *     }
00028  * }
00029  * @endcode
00030  */
00031 class DS1820 {
00032 public:
00033     enum devices{
00034         this_device,     // command applies to only this device
00035         all_devices };   // command applies to all devices
00036     
00037     enum {
00038         invalid_conversion = -1000
00039     };
00040 
00041     /** Create a probe object connected to the specified pins
00042     *
00043     * The probe might either by regular powered or parasite powered. If it is parasite
00044     * powered and power_pin is set, that pin will be used to switch an external mosfet connecting
00045     * data to Vdd. If it is parasite powered and the pin is not set, the regular data pin
00046     * is used to supply extra power when required. This will be sufficient as long as the 
00047     * number of probes is limitted.
00048      *
00049      * @param data_pin DigitalInOut pin for the data bus
00050      * @param power_pin DigitalOut (optional) pin to control the power MOSFET
00051      * @param power_polarity bool (optional) which sets active state (0 for active low (default), 1 for active high)
00052      */
00053     DS1820(PinName data_pin, PinName power_pin = NC, bool power_polarity = 0); // Constructor with parasite power pin
00054     ~DS1820();
00055 
00056     /** Function to see if there are DS1820 devices left on a pin which do not have a corresponding DS1820 object
00057     *
00058     * @return - true if there are one or more unassigned devices, otherwise false
00059       */
00060     static bool unassignedProbe(PinName pin);
00061 
00062     /** This routine will initiate the temperature conversion within
00063       * one or all DS1820 probes. 
00064       *
00065       * @param wait if true or parisitic power is used, waits up to 750 ms for 
00066       * conversion otherwise returns immediatly.
00067       * @param device allows the function to apply to a specific device or
00068       * to all devices on the 1-Wire bus.
00069       * @returns milliseconds untill conversion will complete.
00070       */
00071     int convertTemperature(bool wait, devices device=all_devices);
00072 
00073     /** This function will return the probe temperature. Approximately 10ms per
00074       * probe to read its RAM, do CRC check and convert temperature on the LPC1768.
00075       *
00076       * @param scale, may be either 'c' or 'f'
00077       * @returns temperature for that scale, or DS1820::invalid_conversion (-1000) if CRC error detected.
00078       */
00079     float temperature(char scale='c');
00080 
00081     /** This function sets the temperature resolution for the DS18B20
00082       * in the configuration register.
00083       *
00084       * @param a number between 9 and 12 to specify resolution
00085       * @returns true if successful
00086       */ 
00087     bool setResolution(unsigned int resolution);  
00088     
00089     static LinkedList2<node> probes;     
00090 
00091 private:
00092     bool _parasite_power;
00093     bool _power_mosfet;
00094     bool _power_polarity;
00095     
00096     static char CRC_byte(char _CRC, char byte );
00097     static bool onewire_reset(DigitalInOut *pin);
00098     void match_ROM();
00099     void skip_ROM();
00100     static bool search_ROM_routine(DigitalInOut *pin, char command, char *ROM_address);
00101     static void onewire_bit_out (DigitalInOut *pin, bool bit_data);
00102     void onewire_byte_out(char data);
00103     static bool onewire_bit_in(DigitalInOut *pin);
00104     char onewire_byte_in();
00105     static bool ROM_checksum_error(char *_ROM_address);
00106     bool RAM_checksum_error();
00107     void read_RAM();
00108     static bool unassignedProbe(DigitalInOut *pin, char *ROM_address);
00109     void write_scratchpad(int data);
00110     bool read_power_supply(devices device=this_device);
00111 
00112     DigitalInOut _datapin;
00113     DigitalOut _parasitepin;
00114     
00115     char _ROM[8];
00116     char RAM[9];
00117 };
00118 
00119 
00120 #endif