Working version for L-tek FF1705

Dependencies:   OneWire

Dependents:   FROST

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     float   toFloat(uint16_t word);
00118     static  uint8_t lastAddr[8];
00119     
00120 public:
00121     DS1820(PinName pin);
00122 //    DS1820(char model, PinName pin);
00123     DS1820(OneWire* wire);
00124     bool   begin(void);
00125     bool   isPresent();
00126     void   setResolution(uint8_t res);
00127     void   startConversion(void);
00128     float  read(void);
00129     uint8_t read(float& temp);
00130 };
00131 #endif /* DS1820_H_ */