Working version for L-tek FF1705
DS1820 Class Reference
Dallas' DS1820 family temperature sensor. More...
#include <DS1820.h>
Public Member Functions | |
| DS1820 (PinName pin) | |
| Constructs a generic DS1820 sensor. | |
| DS1820 (OneWire *wire) | |
| Constructs a generic DS1820 sensor. | |
| bool | begin (void) |
| Detects and initializes the actual DS1820 model. | |
| bool | isPresent () |
| Informs about presence of a DS1820 sensor. | |
| void | setResolution (uint8_t res) |
| Sets temperature-to-digital conversion resolution. | |
| void | startConversion (void) |
| Starts temperature conversion. | |
| float | read (void) |
| Reads temperature from the chip's Scratchpad. | |
| uint8_t | read (float &temp) |
| Reads temperature from chip's scratchpad. | |
Detailed Description
Dallas' DS1820 family temperature sensor.
This library depends on the OneWire library (Dallas' 1-Wire bus protocol implementation) available at <http://developer.mbed.org/users/hudakz/code/OneWire/>
Example of use:
Single sensor. #include "mbed.h" #include "DS1820.h" Serial pc(USBTX, USBRX); DigitalOut led(LED1); DS1820 ds1820(D8); // substitute D8 with actual mbed pin name connected to 1-wire bus float temp = 0; int result = 0; int main() { pc.printf("\r\n--Starting--\r\n"); if (ds1820.begin()) { while (1) { ds1820.startConversion(); // start temperature conversion from analog to digital wait(1.0); // let DS1820 complete the temperature conversion result = ds1820.read(temp); // read temperature from DS1820 and perform cyclic redundancy check (CRC) switch (result) { case 0: // no errors -> 'temp' contains the value of measured temperature pc.printf("temp = %3.1f%cC\r\n", temp, 176); break; case 1: // no sensor present -> 'temp' is not updated pc.printf("no sensor present\n\r"); break; case 2: // CRC error -> 'temp' is not updated pc.printf("CRC error\r\n"); } led = !led; } } else pc.printf("No DS1820 sensor found!\r\n"); } More sensors connected to the same 1-wire bus. #include "mbed.h" #include "DS1820.h" #define SENSORS_COUNT 64 // number of DS1820 sensors to be connected to the 1-wire bus (max 256) Serial pc(USBTX, USBRX); DigitalOut led(LED1); OneWire oneWire(D8); // substitute D8 with actual mbed pin name connected to the DS1820 data pin DS1820* ds1820[SENSORS_COUNT]; int sensors_found = 0; // counts the actually found DS1820 sensors float temp = 0; int result = 0; int main() { int i = 0; pc.printf("\r\n Starting \r\n"); //Enumerate (i.e. detect) DS1820 sensors on the 1-wire bus for(i = 0; i < SENSORS_COUNT; i++) { ds1820[i] = new DS1820(&oneWire); if(!ds1820[i]->begin()) { delete ds1820[i]; break; } } sensors_found = i; if (sensors_found == 0) { pc.printf("No DS1820 sensor found!\r\n"); return -1; } else pc.printf("Found %d sensors.\r\n", sensors_found); while(1) { pc.printf("-------------------\r\n"); for(i = 0; i < sensors_found; i++) ds1820[i]->startConversion(); // start temperature conversion from analog to digital wait(1.0); // let DS1820s complete the temperature conversion for(int i = 0; i < sensors_found; i++) { if(ds1820[i]->isPresent()) pc.printf("temp[%d] = %3.1f%cC\r\n", i, ds1820[i]->read(), 176); // read temperature } } }
Note: Don't forget to connect a 4.7k Ohm resistor between the DS1820's data pin and the +3.3V pin
Definition at line 110 of file DS1820.h.
Constructor & Destructor Documentation
| DS1820 | ( | PinName | pin ) |
Constructs a generic DS1820 sensor.
- Note:
- begin() must be called to detect and initialize the actual model
- Parameters:
-
pin,: Name of data pin
- Return values:
-
Definition at line 111 of file DS1820.cpp.
| DS1820 | ( | OneWire * | wire ) |
Constructs a generic DS1820 sensor.
- Note:
- begin() must be called to detect and initialize the actual model
- Parameters:
-
pin,: Name of data pin
- Return values:
-
Definition at line 123 of file DS1820.cpp.
Member Function Documentation
| bool begin | ( | void | ) |
Detects and initializes the actual DS1820 model.
- Note:
- Parameters:
-
@retval true: if a DS1820 family sensor was detected and initialized false: otherwise
Definition at line 136 of file DS1820.cpp.
| bool isPresent | ( | void | ) |
Informs about presence of a DS1820 sensor.
- Note:
- begin() shall be called before using this function if a generic DS1820 instance was created by the user. No need to call begin() for a specific DS1820 instance.
- Parameters:
-
@retval true: when a DS1820 sensor is present false: otherwise
Definition at line 216 of file DS1820.cpp.
| float read | ( | void | ) |
Reads temperature from the chip's Scratchpad.
- Note:
- Parameters:
-
@retval Floating point temperature value
Definition at line 276 of file DS1820.cpp.
| uint8_t read | ( | float & | temp ) |
Reads temperature from chip's scratchpad.
- Note:
- Verifies data integrity by calculating cyclic redundancy check (CRC). If the calculated CRC dosn't match the one stored in chip's scratchpad register the temperature variable is not updated and CRC error code is returned.
- Parameters:
-
temp,: The temperature variable to be updated by this routine. (It's passed as reference to floating point.)
- Return values:
-
error code: 0 - no errors ('temp' contains the temperature measured) 1 - sensor not present ('temp' is not updated) 2 - CRC error ('temp' is not updated)
Definition at line 338 of file DS1820.cpp.
| void setResolution | ( | uint8_t | res ) |
Sets temperature-to-digital conversion resolution.
- Note:
- The configuration register allows the user to set the resolution of the temperature-to-digital conversion to 9, 10, 11, or 12 bits. Defaults to 12-bit resolution for DS18B20. DS18S20 allows only 9-bit resolution.
- Parameters:
-
res,: Resolution of the temperature-to-digital conversion in bits.
- Return values:
-
Definition at line 229 of file DS1820.cpp.
| void startConversion | ( | void | ) |
Starts temperature conversion.
- Note:
- The time to complete the converion depends on the selected resolution: 9-bit resolution -> max conversion time = 93.75ms 10-bit resolution -> max conversion time = 187.5ms 11-bit resolution -> max conversion time = 375ms 12-bit resolution -> max conversion time = 750ms
- Parameters:
-
@retval
Definition at line 262 of file DS1820.cpp.
Generated on Fri Jul 22 2022 18:19:23 by
1.7.2