FG-HT / DS1820_new2

Dependencies:   OneWire

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers DS1820.h Source File

DS1820.h

00001 #ifndef DS1820_H_
00002     #define DS1820_H_
00003 
00004     #include "OneWire.h"
00005 
00006 /**
00007  * Dallas' DS1820 family temperature sensor.
00008  * This library depends on the OneWire library (Dallas' 1-Wire bus protocol implementation)
00009  * available at <http://developer.mbed.org/users/hudakz/code/OneWire/>
00010  *
00011  * Example of use:
00012  *
00013  * @code
00014  *
00015  * Single sensor.
00016  *
00017  * #include "mbed.h"
00018  * #include "DS1820.h"
00019  *
00020  * Serial      pc(USBTX, USBRX);
00021  * DigitalOut  led(LED1);
00022  * DS1820      ds1820(D8);  // substitute D8 with actual mbed pin name connected to 1-wire bus
00023  * float       temp = 0;
00024  * int         result = 0;
00025  *
00026  * int main()
00027  * {
00028  *     pc.printf("\r\n--Starting--\r\n");
00029  *     if (ds1820.begin()) {
00030  *         while (1) {
00031  *             ds1820.startConversion();   // start temperature conversion from analog to digital
00032  *             wait(1.0);                  // let DS1820 complete the temperature conversion
00033  *             result = ds1820.read(temp); // read temperature from DS1820 and perform cyclic redundancy check (CRC)
00034  *             switch (result) {
00035  *                 case 0:                 // no errors -> 'temp' contains the value of measured temperature
00036  *                     pc.printf("temp = %3.1f%cC\r\n", temp, 176);
00037  *                     break;
00038  *
00039  *                 case 1:                 // no sensor present -> 'temp' is not updated
00040  *                     pc.printf("no sensor present\n\r");
00041  *                     break;
00042  *
00043  *                 case 2:                 // CRC error -> 'temp' is not updated
00044  *                     pc.printf("CRC error\r\n");
00045  *             }
00046  *
00047  *             led = !led;
00048  *         }
00049  *     }
00050  *     else
00051  *         pc.printf("No DS1820 sensor found!\r\n");
00052  * }
00053  *
00054  *
00055  * More sensors connected to the same 1-wire bus.
00056  *
00057  * #include "mbed.h"
00058  * #include "DS1820.h"
00059  *
00060  * #define     SENSORS_COUNT   64      // number of DS1820 sensors to be connected to the 1-wire bus (max 256)
00061  *
00062  * Serial      pc(USBTX, USBRX);
00063  * DigitalOut  led(LED1);
00064  * OneWire     oneWire(D8);            // substitute D8 with actual mbed pin name connected to the DS1820 data pin
00065  * DS1820*     ds1820[SENSORS_COUNT];
00066  * int         sensors_found = 0;      // counts the actually found DS1820 sensors
00067  * float       temp = 0;
00068  * int         result = 0;
00069  *
00070  * int main() {
00071  *     int i = 0;
00072  *
00073  *     pc.printf("\r\n Starting \r\n");
00074  *     //Enumerate (i.e. detect) DS1820 sensors on the 1-wire bus
00075  *     for(i = 0; i < SENSORS_COUNT; i++) {
00076  *         ds1820[i] = new DS1820(&oneWire);
00077  *         if(!ds1820[i]->begin()) {
00078  *             delete ds1820[i];
00079  *             break;
00080  *         }
00081  *     }
00082  *
00083  *     sensors_found = i;
00084  *
00085  *     if (sensors_found == 0) {
00086  *         pc.printf("No DS1820 sensor found!\r\n");
00087  *         return -1;
00088  *     }
00089  *     else
00090  *         pc.printf("Found %d sensors.\r\n", sensors_found);
00091  *
00092  *     while(1) {
00093  *         pc.printf("-------------------\r\n");
00094  *         for(i = 0; i < sensors_found; i++)
00095  *             ds1820[i]->startConversion();   // start temperature conversion from analog to digital
00096  *         wait(1.0);                          // let DS1820s complete the temperature conversion
00097  *         for(int i = 0; i < sensors_found; i++) {
00098  *             if(ds1820[i]->isPresent())
00099  *                 pc.printf("temp[%d] = %3.1f%cC\r\n", i, ds1820[i]->read(), 176);     // read temperature
00100  *         }
00101  *     }
00102  * }
00103  *
00104  * @endcode
00105  *
00106  * Note: Don't forget to connect a 4.7k Ohm resistor
00107  *       between the DS1820's data pin and the +3.3V pin
00108  *
00109  */
00110 class   DS1820
00111 {
00112     OneWire*        _oneWire;
00113     bool            _present;
00114     bool            _model_s;
00115     uint8_t         _data[12];
00116     uint8_t         _addr[8];
00117     static uint8_t  _lastAddr[8];
00118 
00119     float   toFloat(uint16_t word);
00120 
00121 public:
00122     DS1820(PinName gpioPin, int samplePoint_us = 13);
00123     DS1820(PinName txPin, PinName rxPin);
00124     DS1820(OneWire* oneWire);
00125 
00126     bool    begin(void);
00127     bool    isPresent();
00128     void    setResolution(uint8_t res);
00129     void    startConversion(void);
00130     float   read(void);
00131     uint8_t read(float& temp);
00132     void getAddress(char * b);
00133 };
00134 #endif /* DS1820_H_ */