Mukodik tobb DS18B20 de nem lehet updatolni az mbedet mert azutan nem mukodik mar
Fork of DS1820 by
Revision 5:2cd4928e8147, committed 2014-02-24
- Comitter:
- Sissors
- Date:
- Mon Feb 24 21:23:14 2014 +0000
- Parent:
- 4:29264b0a2c9f
- Child:
- 6:abfdd851218a
- Commit message:
- Simplified code, works also on KL25Z now, added support for DS1822
Changed in this revision
--- a/DS1820.cpp Wed Aug 07 11:02:10 2013 +0000 +++ b/DS1820.cpp Mon Feb 24 21:23:14 2014 +0000 @@ -1,53 +1,61 @@ #include "DS1820.h" -#include "mbed.h" - -// Global variables shared between all DS1820 objects -bool DS1820_done_flag; -int DS1820_last_descrepancy; -char DS1820_search_ROM[8]; + +LinkedList<node> DS1820::probes; -DS1820::DS1820 (PinName data_pin, PinName power_pin) : _datapin(data_pin), _parasitepin(power_pin) { +DS1820::DS1820 (PinName data_pin, PinName power_pin, bool power_polarity) : _datapin(data_pin), _parasitepin(power_pin) { int byte_counter; - _parasite_power = true; - for(byte_counter=0;byte_counter<8;byte_counter++) - ROM[byte_counter] = 0xFF; + _power_polarity = power_polarity; + if (power_pin != NC) + _power_mosfet = true; + for(byte_counter=0;byte_counter<9;byte_counter++) RAM[byte_counter] = 0x00; + + if (!unassignedProbe(&_datapin, _ROM)) + error("No unassigned DS1820 found!\n"); + else { + _datapin.input(); + probes.append(this); + _parasite_power = !read_power_supply(); + } } -DS1820::DS1820 (PinName data_pin) : _datapin(data_pin), _parasitepin(NC) { - int byte_counter; - _parasite_power = false; - for(byte_counter=0;byte_counter<8;byte_counter++) - ROM[byte_counter] = 0xFF; - for(byte_counter=0;byte_counter<9;byte_counter++) - RAM[byte_counter] = 0x00; + +DS1820::~DS1820 (void) { + node *tmp; + for(int i=1; i<=probes.length(); i++) + { + tmp = probes.pop(i); + if (tmp->data == this) + probes.remove(i); + } } + -bool DS1820::onewire_reset() { +bool DS1820::onewire_reset(DigitalInOut *pin) { // This will return false if no devices are present on the data bus bool presence=false; - _datapin.output(); - _datapin = 0; // bring low for 500 us + pin->output(); + pin->write(0); // bring low for 500 us wait_us(500); - _datapin.input(); // let the data line float high + pin->input(); // let the data line float high wait_us(90); // wait 90us - if (_datapin.read()==0) // see if any devices are pulling the data line low + if (pin->read()==0) // see if any devices are pulling the data line low presence=true; wait_us(410); return presence; } -void DS1820::onewire_bit_out (bool bit_data) { - _datapin.output(); - _datapin = 0; +void DS1820::onewire_bit_out (DigitalInOut *pin, bool bit_data) { + pin->output(); + pin->write(0); wait_us(3); // DXP modified from 5 if (bit_data) { - _datapin.input(); // bring data line high + pin->input(); // bring data line high wait_us(55); } else { wait_us(55); // keep data line low - _datapin.input(); + pin->input(); wait_us(10); // DXP added to allow bus to float high before next bit_out } } @@ -55,19 +63,19 @@ void DS1820::onewire_byte_out(char data) { // output data character (least sig bit first). int n; for (n=0; n<8; n++) { - onewire_bit_out(data & 0x01); + onewire_bit_out(&this->_datapin, data & 0x01); data = data >> 1; // now the next bit is in the least sig bit position. } } -bool DS1820::onewire_bit_in() { +bool DS1820::onewire_bit_in(DigitalInOut *pin) { bool answer; - _datapin.output(); - _datapin = 0; + pin->output(); + pin->write(0); wait_us(3); // DXP modofied from 5 - _datapin.input(); + pin->input(); wait_us(10); // DXP modified from 5 - answer = _datapin.read(); + answer = pin->read(); wait_us(45); // DXP modified from 50 return answer; } @@ -77,41 +85,47 @@ int i; for (i=0; i<8; i++) { answer = answer >> 1; // shift over to make room for the next bit - if (onewire_bit_in()) + if (onewire_bit_in(&this->_datapin)) answer = answer | 0x80; // if the data port is high, make this bit a 1 } return answer; } - -bool DS1820::search_ROM() { - return search_ROM_routine(0xF0); // Search ROM command + +bool DS1820::unassignedProbe(PinName pin) { + DigitalInOut _pin(pin); + char ROM_address[8]; + return search_ROM_routine(&_pin, 0xF0, ROM_address); } -bool DS1820::search_alarm() { - return search_ROM_routine(0xEC); // Search Alarm command +bool DS1820::unassignedProbe(DigitalInOut *pin, char *ROM_address) { + return search_ROM_routine(pin, 0xF0, ROM_address); } -bool DS1820::search_ROM_routine(char command) { - extern bool DS1820_done_flag; - extern int DS1820_last_descrepancy; - extern char DS1820_search_ROM[8]; +bool DS1820::search_ROM_routine(DigitalInOut *pin, char command, char *ROM_address) { + bool DS1820_done_flag = false; + int DS1820_last_descrepancy = 0; + char DS1820_search_ROM[8] = {0, 0, 0, 0, 0, 0, 0, 0}; + int descrepancy_marker, ROM_bit_index; bool return_value, Bit_A, Bit_B; char byte_counter, bit_mask; return_value=false; - if (!DS1820_done_flag) { - if (!onewire_reset()) { - DS1820_last_descrepancy = 0; // no devices present + while (!DS1820_done_flag) { + if (!onewire_reset(pin)) { + return false; } else { ROM_bit_index=1; descrepancy_marker=0; - onewire_byte_out(command); // Search ROM command or Search Alarm command + for (int n=0; n<8; n++) { // Search ROM command or Search Alarm command + onewire_bit_out(pin, command & 0x01); + command = command >> 1; // now the next bit is in the least sig bit position. + } byte_counter = 0; bit_mask = 0x01; while (ROM_bit_index<=64) { - Bit_A = onewire_bit_in(); - Bit_B = onewire_bit_in(); + Bit_A = onewire_bit_in(pin); + Bit_B = onewire_bit_in(pin); if (Bit_A & Bit_B) { descrepancy_marker = 0; // data read error, this should never happen ROM_bit_index = 0xFF; @@ -137,7 +151,7 @@ } } } - onewire_bit_out (DS1820_search_ROM[byte_counter] & bit_mask); + onewire_bit_out (pin, DS1820_search_ROM[byte_counter] & bit_mask); ROM_bit_index++; if (bit_mask & 0x80) { byte_counter++; @@ -149,12 +163,32 @@ } DS1820_last_descrepancy = descrepancy_marker; if (ROM_bit_index != 0xFF) { - for(byte_counter=0;byte_counter<8;byte_counter++) - ROM[byte_counter] = DS1820_search_ROM[byte_counter]; - if (ROM_checksum_error()) // Check the CRC - DS1820_last_descrepancy = 0; // Abort any more search - else - return_value = true; + int i = 1; + node *list_container; + while(1) { + list_container = probes.pop(i); + if (list_container == NULL) { //End of list, or empty list + if (ROM_checksum_error(DS1820_search_ROM)) { // Check the CRC + return false; + } + for(byte_counter=0;byte_counter<8;byte_counter++) + ROM_address[byte_counter] = DS1820_search_ROM[byte_counter]; + return true; + } else { //Otherwise, check if ROM is already known + bool equal = true; + DS1820 *pointer = (DS1820*) list_container->data; + char *ROM_compare = pointer->_ROM; + + for(byte_counter=0;byte_counter<8;byte_counter++) { + if ( ROM_compare[byte_counter] != DS1820_search_ROM[byte_counter]) + equal = false; + } + if (equal) + break; + else + i++; + } + } } } if (DS1820_last_descrepancy == 0) @@ -163,49 +197,28 @@ return return_value; } -void DS1820::search_ROM_setup() { - extern bool DS1820_done_flag; - extern int DS1820_last_descrepancy; - extern char DS1820_search_ROM[8]; - DS1820_done_flag = false; - DS1820_last_descrepancy = 0; - int i; - for (i=0; i<8; i++) - DS1820_search_ROM[i]=0x00; -} - -void DS1820::read_ROM() { - // NOTE: This command can only be used when there is one DS1820 on the bus. If this command - // is used when there is more than one slave present on the bus, a data collision will occur - // when all the DS1820s attempt to respond at the same time. - int i; - onewire_reset(); - onewire_byte_out(0x33); // Read ROM id - for (i=0; i<8; i++) - ROM[i]=onewire_byte_in(); -} - void DS1820::match_ROM() { // Used to select a specific device int i; - onewire_reset(); + onewire_reset(&this->_datapin); onewire_byte_out( 0x55); //Match ROM command - for (i=0;i<8;i++) - onewire_byte_out(ROM[i]); + for (i=0;i<8;i++) { + onewire_byte_out(_ROM[i]); + } } void DS1820::skip_ROM() { - onewire_reset(); + onewire_reset(&this->_datapin); onewire_byte_out(0xCC); // Skip ROM command } -bool DS1820::ROM_checksum_error() { +bool DS1820::ROM_checksum_error(char *_ROM_address) { char CRC=0x00; int i; for(i=0;i<7;i++) // Only going to shift the lower 7 bytes - CRC = CRC_byte(CRC, ROM[i]); + CRC = CRC_byte(CRC, _ROM_address[i]); // After 7 bytes CRC should equal the 8th byte (ROM CRC) - return (CRC!=ROM[7]); // will return true if there is a CRC checksum mis-match + return (CRC!=_ROM_address[7]); // will return true if there is a CRC checksum mis-match } bool DS1820::RAM_checksum_error() { @@ -249,7 +262,7 @@ return CRC; } -int DS1820::convert_temperature(bool wait, devices device) { +int DS1820::convertTemperature(bool wait, devices device) { // Convert temperature into scratchpad RAM for all devices at once int delay_time = 750; // Default delay time char resolution; @@ -257,7 +270,7 @@ skip_ROM(); // Skip ROM command, will convert for ALL devices else { match_ROM(); - if (FAMILY_CODE == FAMILY_CODE_DS18B20 ) { + if ((FAMILY_CODE == FAMILY_CODE_DS18B20 ) || (FAMILY_CODE == FAMILY_CODE_DS1822 )) { resolution = RAM[4] & 0x60; if (resolution == 0x00) // 9 bits delay_time = 94; @@ -267,12 +280,20 @@ delay_time = 375; } } + onewire_byte_out( 0x44); // perform temperature conversion if (_parasite_power) { - _parasitepin = 1; // Parasite power strong pullup - wait_ms(delay_time); - _parasitepin = 0; - delay_time = 0; + if (_power_mosfet) { + _parasitepin = _power_polarity; // Parasite power strong pullup + wait_ms(delay_time); + _parasitepin = !_power_polarity; + delay_time = 0; + } else { + _datapin.output(); + _datapin.write(1); + wait_ms(delay_time); + _datapin.input(); + } } else { if (wait) { wait_ms(delay_time); @@ -296,7 +317,7 @@ // crcerr = 1; } -bool DS1820::set_configuration_bits(unsigned int resolution) { +bool DS1820::setResolution(unsigned int resolution) { bool answer = false; resolution = resolution - 9; if (resolution < 4) { @@ -309,13 +330,6 @@ return answer; } -int DS1820::read_scratchpad() { - int answer; - read_RAM(); - answer = (RAM[2]<<8) + RAM[3]; - return answer; -} - void DS1820::write_scratchpad(int data) { RAM[3] = data; RAM[2] = data>>8; @@ -323,43 +337,11 @@ onewire_byte_out(0x4E); // Copy scratchpad into DS1820 ram memory onewire_byte_out(RAM[2]); // T(H) onewire_byte_out(RAM[3]); // T(L) - if ( FAMILY_CODE == FAMILY_CODE_DS18B20 ) { + if ((FAMILY_CODE == FAMILY_CODE_DS18B20 ) || (FAMILY_CODE == FAMILY_CODE_DS1822 )) { onewire_byte_out(RAM[4]); // Configuration register } } -void DS1820::store_scratchpad(devices device) { - if (device==all_devices) - skip_ROM(); // Skip ROM command, will store for ALL devices - else - match_ROM(); - onewire_byte_out(0x48); // Write scratchpad into E2 command - if (_parasite_power) - _parasitepin=1; - wait_ms(10); // Parasite power strong pullup for 10ms - if (_parasite_power) - _parasitepin=0; -} - -int DS1820::recall_scratchpad(devices device) { -// This copies the E2 values into the DS1820's memory. -// If you specify all_devices this will return zero, otherwise -// it will return the value of the scratchpad memory. - int answer=0; - if (device==all_devices) - skip_ROM(); // Skip ROM command, will recall for ALL devices - else - match_ROM(); - onewire_byte_out(0xB8); // Recall E2 data to scratchpad command - wait_ms(10); // not sure I like polling for completion - // it could cause an infinite loop - if (device==DS1820::this_device) { - read_RAM(); - answer = read_scratchpad(); - } - return answer; -} - float DS1820::temperature(char scale) { // The data specs state that count_per_degree should be 0x10 (16), I found my devices // to have a count_per_degree of 0x4B (75). With the standard resolution of 1/2 deg C @@ -378,7 +360,7 @@ reading = 0-((reading ^ 0xffff) + 1); // 2's comp then convert to signed int } answer = reading +0.0; // convert to floating point - if ( FAMILY_CODE == FAMILY_CODE_DS18B20 ) { + if ((FAMILY_CODE == FAMILY_CODE_DS18B20 ) || (FAMILY_CODE == FAMILY_CODE_DS1822 )) { answer = answer / 8.0; } else { @@ -403,7 +385,7 @@ else match_ROM(); onewire_byte_out(0xB4); // Read power supply command - return onewire_bit_in(); + return onewire_bit_in(&this->_datapin); }
--- a/DS1820.h Wed Aug 07 11:02:10 2013 +0000 +++ b/DS1820.h Mon Feb 24 21:23:14 2014 +0000 @@ -24,60 +24,31 @@ #define MBED_DS1820_H #include "mbed.h" - -// ****** THIS GLOBAL VARIABLES MUST BE DEFINED IN main.cpp +#include "LinkedList.h" -// Global variables shared between all DS1820 objects -//bool DS1820_done_flag; -//int DS1820_last_descrepancy; -//char DS1820_search_ROM[8]; +#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: * @code * #include "mbed.h" - * - * #include "TextLCD.h" * #include "DS1820.h" * - * TextLCD lcd(p25, p26, p21, p22, p23, p24, TextLCD::LCD16x2); // rs, e, d0-d3, layout - * - * const int MAX_PROBES = 16; - * DS1820* probe[MAX_PROBES]; - * + * DS1820 probe(DATA_PIN); + * * int main() { - * int i; - * int devices_found=0; - * // Initialize the probe array to DS1820 objects - * for (i = 0; i < MAX_PROBES; i++) - * probe[i] = new DS1820(p27); - * // Initialize global state variables - * probe[0]->search_ROM_setup(); - * // Loop to find all devices on the data line - * while (probe[devices_found]->search_ROM() and devices_found<MAX_PROBES-1) - * devices_found++; - * // If maximum number of probes are found, - * // bump the counter to include the last array entry - * if (probe[devices_found]->ROM[0] != 0xFF) - * devices_found++; - * - * lcd.cls(); - * if (devices_found==0) - * lcd.printf("No devices found"); - * else { - * while (true) { - * probe[0]->convert_temperature(true, DS1820::all_devices); - * lcd.cls(); - * for (i=0; i<devices_found; i++) { - * lcd.printf("%3.1f ",probe[i]->temperature('f')); - * } - * } + * while(1) { + * probe.convertTemperature(true, DS1820::all_devices); //Start temperature conversion, wait until ready + * printf("It is %3.1foC\r\n", probe.temperature()); + * wait(1); * } * } * @endcode */ - class DS1820 { public: enum devices{ @@ -85,84 +56,28 @@ all_devices }; // command applies to all devices /** Create a probe object connected to the specified pins - * - * @param data_pin DigitalInOut pin for the data bus - * @param power_pin DigitalOut pin to control the power MOSFET - */ - DS1820(PinName data_pin, PinName power_pin); // Constructor with parasite power pin - - /** Create a probe object connected to the specified pin - * this is used when all probes are externally powered + * + * 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); - - /** ROM is a copy of the internal DS1820's ROM - * It is created during the search_ROM() or search_alarm() commands - * - * ROM[0] is the Dallas Family Code - * ROM[1] thru ROM[6] is the 48-bit unique serial number - * ROM[7] is the device CRC - */ - char ROM[8]; - #define FAMILY_CODE ROM[0] - #define FAMILY_CODE_DS1820 0x10 - #define FAMILY_CODE_DS18S20 0x10 - #define FAMILY_CODE_DS18B20 0x28 - - /** RAM is a copy of the internal DS1820's RAM - * It's updated during the read_RAM() command - * which is automaticaly called from any function - * using the RAM values except RAM_checksum_error. - */ - char RAM[9]; - - /** This function copies the DS1820's RAM into the object's - * RAM[]. It is automaticaly called by temperature(), - * read_scratchpad(), and recall_scratchpad() of a single probe. - */ - void read_RAM(); + DS1820(PinName data_pin, PinName power_pin = NC, bool power_polarity = 0); // Constructor with parasite power pin + ~DS1820(); - /** This routine initializes the global variables used in - * the search_ROM() and search_alarm() funtions. It should - * be called once before looping to find devices. - */ - void search_ROM_setup(); - - /** This routine will search for an unidentified device - * on the bus. It uses the variables in search_ROM_setup - * to remember the pervious ROM address found. - * It will return FALSE if there were no new devices - * discovered on the bus. + /** 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 */ - bool search_ROM(); - - /** This routine will search for an unidentified device - * which has the temperature alarm bit set. It uses the - * variables in search_ROM_setup to remember the pervious - * ROM address found. It will return FALSE if there were - * no new devices with alarms discovered on the bus. - */ - bool search_alarm(); - - /** This routine will read the ROM (Family code, serial number - * and Checksum) from a dedicated device on the bus. - * - * NOTE: This command can only be used when there is only one - * DS1820 on the bus. If this command is used when there - * is more than one slave present on the bus, a data - * collision will occur when all the DS1820s attempt to - * respond at the same time. - */ - void read_ROM(); + static bool unassignedProbe(PinName pin); /** This routine will initiate the temperature conversion within - * one or all DS1820 probes. There is an optional built in delay - * (up to 750ms) to allow the conversion to complete. - * - * To update all probes on the bus, use a statement such as this: - * probe[0]->convert_temperature(true, DS1820::all_devices); + * one or all DS1820 probes. * * @param wait if true or parisitic power is used, waits up to 750 ms for * conversion otherwise returns immediatly. @@ -170,33 +85,15 @@ * to all devices on the 1-Wire bus. * @returns milliseconds untill conversion will complete. */ - int convert_temperature(bool wait, devices device=this_device); + int convertTemperature(bool wait, devices device=this_device); /** This function will return the probe temperature. Approximately 10ms per * probe to read its RAM, do CRC check and convert temperature on the LPC1768. - * This function uses the count remainding values to interpolate the temperature - * to about 1/150th of a degree. Whereas the probe is not spec to - * that precision. It does seem to give a smooth reading to the - * tenth of a degree. * * @param scale, may be either 'c' or 'f' * @returns temperature for that scale, or -1000.0 if CRC error detected. */ float temperature(char scale='c'); - - /** This function calculates the ROM checksum and compares it to the - * CRC value stored in ROM[7]. - * - * @returns true if the checksums mis-match, otherwise false. - */ - bool ROM_checksum_error(); - - /** This function calculates the RAM checksum and compares it to the - * CRC value stored in RAM[8]. Approx 10 us on LPC1768. - * - * @returns true if the checksums mis-matche, otherwise false. - */ - bool RAM_checksum_error(); /** This function sets the temperature resolution for the DS18B20 * in the configuration register. @@ -204,65 +101,36 @@ * @param a number between 9 and 12 to specify resolution * @returns true if successful */ - bool set_configuration_bits(unsigned int resolution); - - /** This function returns the values stored in the temperature - * alarm registers. - * - * @returns a 16 bit integer of TH (upper byte) and TL (lower byte). - */ - int read_scratchpad(); - - /** This function will store the passed data into the DS1820's RAM. - * Note: It does NOT save the data to the EEPROM for retention - * during cycling the power off and on. - * - * @param a 16 bit integer of TH (upper byte) and TL (lower byte). - */ - void write_scratchpad(int data); - - /** This function will transfer the TH and TL registers from the - * DS1820's RAM into the EEPROM. - * Note: There is a built in 10ms delay to allow for the - * completion of the EEPROM write cycle. - * - * @param allows the fnction to apply to a specific device or - * to all devices on the 1-Wire bus. - */ - void store_scratchpad(devices device=this_device); - - /** This function will copy the stored values from the EEPROM - * into the DS1820's RAM locations for TH and TL. - * - * @param allows the function to apply to a specific device or - * to all devices on the 1-Wire bus. - */ - int recall_scratchpad(devices device=this_device); - - /** This function will return the type of power supply for - * a specific device. It can also be used to query all devices - * looking for any device that is parasite powered. - * - * @returns true if the device (or all devices) are Vcc powered, - * returns false if the device (or ANY device) is parasite powered. - */ - bool read_power_supply(devices device=this_device); + bool setResolution(unsigned int resolution); private: bool _parasite_power; - char CRC_byte (char CRC, char byte ); - bool onewire_reset(); + 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(); - bool search_ROM_routine(char command); - void onewire_bit_out (bool bit_data); + 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); - bool onewire_bit_in(); + 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); -protected: DigitalInOut _datapin; DigitalOut _parasitepin; + + char _ROM[8]; + char RAM[9]; + + static LinkedList<node> probes; };
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LinkedList.lib Mon Feb 24 21:23:14 2014 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/Sissors/code/LinkedList/#4ed66162aaa8