Fork of Erik's DS1820 library working on OS6
Dependents: DS1820-example DS1820mitWebserver DS1820ohneWebserver
Diff: DS1820.h
- Revision:
- 0:a43dcf6ca539
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/DS1820.h Tue Dec 29 13:08:05 2020 +0000 @@ -0,0 +1,120 @@ + +#ifndef MBED_DS1820_H +#define MBED_DS1820_H + +#include "mbed.h" +#include "LinkedList2.h" + +#define FAMILY_CODE _ROM[0] +#define FAMILY_CODE_DS1820 0x10 +#define FAMILY_CODE_DS18B20 0x28 +#define FAMILY_CODE_DS1822 0x22 + +/** DS1820 Dallas 1-Wire Temperature Probe + * + * Example for single sensor: + * @code + * #include "mbed.h" + * #include "DS1820.h" + * + * DS1820 probe(DATA_PIN); + * + * int main() { + * while(1) { + * probe.convertTemperature(true, DS1820::all_devices); //Start temperature conversion, wait until completed + * printf("It is %3.1foC\r\n", probe.temperature()); + * ThisThread::sleep_for(chrono::milliseconds(1000)); + * } + * } + * @endcode + */ +class DS1820 { +public: + enum devices{ + this_device, // command applies to only this device + all_devices }; // command applies to all devices + + enum { + invalid_conversion = -1000 + }; + + /** Create a probe object connected to the specified pins + * + * The probe might either by regular powered or parasite powered. If it is parasite + * powered and power_pin is set, that pin will be used to switch an external mosfet connecting + * data to Vdd. If it is parasite powered and the pin is not set, the regular data pin + * is used to supply extra power when required. This will be sufficient as long as the + * number of probes is limitted. + * + * @param data_pin DigitalInOut pin for the data bus + * @param power_pin DigitalOut (optional) pin to control the power MOSFET + * @param power_polarity bool (optional) which sets active state (0 for active low (default), 1 for active high) + */ + DS1820(PinName data_pin, PinName power_pin = NC, bool power_polarity = 0); // Constructor with parasite power pin + ~DS1820(); + + /** Function to see if there are DS1820 devices left on a pin which do not have a corresponding DS1820 object + * + * @return - true if there are one or more unassigned devices, otherwise false + */ + static bool unassignedProbe(PinName pin); + + /** This routine will initiate the temperature conversion within + * one or all DS1820 probes. + * + * @param wait if true or parisitic power is used, waits up to 750 ms for + * conversion otherwise returns immediatly. + * @param device allows the function to apply to a specific device or + * to all devices on the 1-Wire bus. + * @returns milliseconds untill conversion will complete. + */ + int convertTemperature(bool wait, devices device=all_devices); + + /** This function will return the probe temperature. Approximately 10ms per + * probe to read its RAM, do CRC check and convert temperature on the LPC1768. + * + * @param scale, may be either 'c' or 'f' + * @returns temperature for that scale, or DS1820::invalid_conversion (-1000) if CRC error detected. + */ + float temperature(char scale='c'); + + /** This function sets the temperature resolution for the DS18B20 + * in the configuration register. + * + * @param a number between 9 and 12 to specify resolution + * @returns true if successful + */ + bool setResolution(unsigned int resolution); + + static LinkedList2<node> probes; + +private: + bool _parasite_power; + bool _power_mosfet; + bool _power_polarity; + + static char CRC_byte(char _CRC, char byte ); + static bool onewire_reset(DigitalInOut *pin); + void match_ROM(); + void skip_ROM(); + static bool search_ROM_routine(DigitalInOut *pin, char command, char *ROM_address); + static void onewire_bit_out (DigitalInOut *pin, bool bit_data); + void onewire_byte_out(char data); + static bool onewire_bit_in(DigitalInOut *pin); + char onewire_byte_in(); + static bool ROM_checksum_error(char *_ROM_address); + bool RAM_checksum_error(); + void read_RAM(); + static bool unassignedProbe(DigitalInOut *pin, char *ROM_address); + void write_scratchpad(int data); + bool read_power_supply(devices device=this_device); + + DigitalInOut _datapin; + DigitalOut _parasitepin; + + char _ROM[8]; + char RAM[9]; +}; + + +#endif \ No newline at end of file