Tested and working library for the DS1621 temperature sensor with I2C
Dependents: condato_Coldchainlogger condato_Coldchainlogger 039847382-S3_DS1621_and_LCD_V1
ds1621.h@0:20bfb7df0470, 2012-07-06 (annotated)
- Committer:
- Bas
- Date:
- Fri Jul 06 20:51:31 2012 +0000
- Revision:
- 0:20bfb7df0470
fixed compiler warning LastTemp
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Bas | 0:20bfb7df0470 | 1 | #ifndef __DS1621_H |
Bas | 0:20bfb7df0470 | 2 | #define __DS1621_H |
Bas | 0:20bfb7df0470 | 3 | |
Bas | 0:20bfb7df0470 | 4 | #define VERSION 1.0 |
Bas | 0:20bfb7df0470 | 5 | |
Bas | 0:20bfb7df0470 | 6 | #include <mbed.h> |
Bas | 0:20bfb7df0470 | 7 | |
Bas | 0:20bfb7df0470 | 8 | //Temperature conversion commands |
Bas | 0:20bfb7df0470 | 9 | #define READ_TEMPERATURE 0xAA |
Bas | 0:20bfb7df0470 | 10 | #define READ_COUNTER 0xA8 |
Bas | 0:20bfb7df0470 | 11 | #define READ_SLOPE 0xA9 |
Bas | 0:20bfb7df0470 | 12 | #define START_CONVERT_T 0xEE |
Bas | 0:20bfb7df0470 | 13 | #define STOP_CONVERT_T 0x22 |
Bas | 0:20bfb7df0470 | 14 | |
Bas | 0:20bfb7df0470 | 15 | //Thermostat commands |
Bas | 0:20bfb7df0470 | 16 | #define ACCESS_TH 0xA1 |
Bas | 0:20bfb7df0470 | 17 | #define ACCESS_TL 0xA2 |
Bas | 0:20bfb7df0470 | 18 | #define ACCESS_CONFIG 0xAC |
Bas | 0:20bfb7df0470 | 19 | |
Bas | 0:20bfb7df0470 | 20 | //Config register |
Bas | 0:20bfb7df0470 | 21 | #define DONE 0x80 |
Bas | 0:20bfb7df0470 | 22 | #define THF 0x40 |
Bas | 0:20bfb7df0470 | 23 | #define TLF 0x20 |
Bas | 0:20bfb7df0470 | 24 | #define NVB 0x10 |
Bas | 0:20bfb7df0470 | 25 | #define POL 0x02 |
Bas | 0:20bfb7df0470 | 26 | #define ONESHOT 0x01 |
Bas | 0:20bfb7df0470 | 27 | |
Bas | 0:20bfb7df0470 | 28 | class DS1621 { |
Bas | 0:20bfb7df0470 | 29 | public: |
Bas | 0:20bfb7df0470 | 30 | DS1621(I2C* interface, unsigned char address); |
Bas | 0:20bfb7df0470 | 31 | |
Bas | 0:20bfb7df0470 | 32 | // Set configuration register |
Bas | 0:20bfb7df0470 | 33 | void SetConfig(unsigned char cfg); |
Bas | 0:20bfb7df0470 | 34 | |
Bas | 0:20bfb7df0470 | 35 | // Read a DS1621 register |
Bas | 0:20bfb7df0470 | 36 | unsigned char ReadReg(unsigned char reg); |
Bas | 0:20bfb7df0470 | 37 | |
Bas | 0:20bfb7df0470 | 38 | // Sets temperature limit |
Bas | 0:20bfb7df0470 | 39 | // -- works only with ACCESS_TL, and ACCESS_TH |
Bas | 0:20bfb7df0470 | 40 | void SetLimit(unsigned char reg, float temp); |
Bas | 0:20bfb7df0470 | 41 | |
Bas | 0:20bfb7df0470 | 42 | // Start/Stop DS1621 temperature conversion |
Bas | 0:20bfb7df0470 | 43 | void StartConversion(bool start); |
Bas | 0:20bfb7df0470 | 44 | |
Bas | 0:20bfb7df0470 | 45 | // Reads temperature or limit |
Bas | 0:20bfb7df0470 | 46 | // -- works only with READ_TEMPERATURE, ACCESS_TL, and ACCESS_TH |
Bas | 0:20bfb7df0470 | 47 | // -- for actual temperature, set ONESHOT mode off and start conversion |
Bas | 0:20bfb7df0470 | 48 | // -- returns true if reg valid |
Bas | 0:20bfb7df0470 | 49 | bool GetTemp(unsigned char reg, float *Temp); |
Bas | 0:20bfb7df0470 | 50 | |
Bas | 0:20bfb7df0470 | 51 | // Read high resolution temperature |
Bas | 0:20bfb7df0470 | 52 | // -- returns temperature in 1/100ths degrees |
Bas | 0:20bfb7df0470 | 53 | // -- DS1621 is set in ONESHOT mode |
Bas | 0:20bfb7df0470 | 54 | // -- returns true if conversion has finished, new temperature is calculated |
Bas | 0:20bfb7df0470 | 55 | // -- returns false if conversion is busy, previous temperature is given |
Bas | 0:20bfb7df0470 | 56 | bool GetHResTemp(float *Temp); |
Bas | 0:20bfb7df0470 | 57 | |
Bas | 0:20bfb7df0470 | 58 | private: |
Bas | 0:20bfb7df0470 | 59 | I2C* i2c; // Communication interface |
Bas | 0:20bfb7df0470 | 60 | unsigned char address; |
Bas | 0:20bfb7df0470 | 61 | void ReadChipTemp(unsigned char cmd, unsigned char *data); |
Bas | 0:20bfb7df0470 | 62 | bool conversion_busy; |
Bas | 0:20bfb7df0470 | 63 | float LastTemp; |
Bas | 0:20bfb7df0470 | 64 | }; |
Bas | 0:20bfb7df0470 | 65 | #endif |