![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Temperature, Voltage & RTC via SMS
Dependencies: DS1307 OLED160G1 mbed uOLED
Fork of Astromed by
DS18B20.cpp@0:03ec282c2908, 2010-01-29 (annotated)
- Committer:
- feabhas
- Date:
- Fri Jan 29 19:01:56 2010 +0000
- Revision:
- 0:03ec282c2908
- Child:
- 1:b2ea1e9d90df
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
feabhas | 0:03ec282c2908 | 1 | #include "DS18B20.h" |
feabhas | 0:03ec282c2908 | 2 | #include "DS1Wire.h" |
feabhas | 0:03ec282c2908 | 3 | #include "mbed.h" |
feabhas | 0:03ec282c2908 | 4 | #include <stdint.h> |
feabhas | 0:03ec282c2908 | 5 | |
feabhas | 0:03ec282c2908 | 6 | // Device byte commands over 1-wire serial |
feabhas | 0:03ec282c2908 | 7 | enum COMMANDS { READ_ROM = 0x33, CONVERT = 0x44, READ_SCRATCHPAD = 0xBE, SKIP_ROM = 0xCC }; |
feabhas | 0:03ec282c2908 | 8 | |
feabhas | 0:03ec282c2908 | 9 | // device onboard register layout |
feabhas | 0:03ec282c2908 | 10 | typedef struct { |
feabhas | 0:03ec282c2908 | 11 | uint8_t LSB; |
feabhas | 0:03ec282c2908 | 12 | uint8_t MSB; |
feabhas | 0:03ec282c2908 | 13 | uint8_t Th; |
feabhas | 0:03ec282c2908 | 14 | uint8_t Tl; |
feabhas | 0:03ec282c2908 | 15 | uint8_t config; |
feabhas | 0:03ec282c2908 | 16 | uint8_t reserved0xFF; |
feabhas | 0:03ec282c2908 | 17 | uint8_t reserved0xCH; |
feabhas | 0:03ec282c2908 | 18 | uint8_t reserved0x10; |
feabhas | 0:03ec282c2908 | 19 | uint8_t CRC; |
feabhas | 0:03ec282c2908 | 20 | } ScratchPad_t; |
feabhas | 0:03ec282c2908 | 21 | |
feabhas | 0:03ec282c2908 | 22 | |
feabhas | 0:03ec282c2908 | 23 | DigitalOut conversionInProgress(LED4); // conversion in progress |
feabhas | 0:03ec282c2908 | 24 | DigitalOut resetFailure(LED1); // for error reporting |
feabhas | 0:03ec282c2908 | 25 | extern DigitalInOut sensor; // sensor pin |
feabhas | 0:03ec282c2908 | 26 | |
feabhas | 0:03ec282c2908 | 27 | static void inError() { |
feabhas | 0:03ec282c2908 | 28 | while (1) { |
feabhas | 0:03ec282c2908 | 29 | resetFailure = !resetFailure; |
feabhas | 0:03ec282c2908 | 30 | wait(0.2); |
feabhas | 0:03ec282c2908 | 31 | } |
feabhas | 0:03ec282c2908 | 32 | } |
feabhas | 0:03ec282c2908 | 33 | |
feabhas | 0:03ec282c2908 | 34 | void DoConversion() { |
feabhas | 0:03ec282c2908 | 35 | if (Reset(sensor) != 0) { |
feabhas | 0:03ec282c2908 | 36 | inError(); |
feabhas | 0:03ec282c2908 | 37 | } else { |
feabhas | 0:03ec282c2908 | 38 | conversionInProgress = 1; // led on |
feabhas | 0:03ec282c2908 | 39 | WriteByte(sensor, SKIP_ROM); // Skip ROM |
feabhas | 0:03ec282c2908 | 40 | WriteByte(sensor, CONVERT); // Convert |
feabhas | 0:03ec282c2908 | 41 | while (ReadBit(sensor) == 0) { |
feabhas | 0:03ec282c2908 | 42 | // wait for conversion to complete |
feabhas | 0:03ec282c2908 | 43 | } |
feabhas | 0:03ec282c2908 | 44 | conversionInProgress = 0; // led off |
feabhas | 0:03ec282c2908 | 45 | } |
feabhas | 0:03ec282c2908 | 46 | } |
feabhas | 0:03ec282c2908 | 47 | |
feabhas | 0:03ec282c2908 | 48 | uint32_t GetTemperature() { |
feabhas | 0:03ec282c2908 | 49 | uint32_t result = 0; |
feabhas | 0:03ec282c2908 | 50 | if (Reset(sensor) != 0) { |
feabhas | 0:03ec282c2908 | 51 | inError(); |
feabhas | 0:03ec282c2908 | 52 | } else { |
feabhas | 0:03ec282c2908 | 53 | ScratchPad_t scratchpad; |
feabhas | 0:03ec282c2908 | 54 | WriteByte(sensor, SKIP_ROM); // Skip ROM |
feabhas | 0:03ec282c2908 | 55 | WriteByte(sensor, READ_SCRATCHPAD); // Read Scrachpad |
feabhas | 0:03ec282c2908 | 56 | scratchpad.LSB = ReadByte(sensor); |
feabhas | 0:03ec282c2908 | 57 | scratchpad.MSB = ReadByte(sensor); |
feabhas | 0:03ec282c2908 | 58 | Reset(sensor); // terminate read as we only want temperature |
feabhas | 0:03ec282c2908 | 59 | result = ((scratchpad.MSB << 8) | scratchpad.LSB); |
feabhas | 0:03ec282c2908 | 60 | } |
feabhas | 0:03ec282c2908 | 61 | return result; |
feabhas | 0:03ec282c2908 | 62 | } |
feabhas | 0:03ec282c2908 | 63 | |
feabhas | 0:03ec282c2908 | 64 | ROM_Code_t ReadROM() { |
feabhas | 0:03ec282c2908 | 65 | ROM_Code_t ROM_Code; |
feabhas | 0:03ec282c2908 | 66 | if (Reset(sensor) != 0) { |
feabhas | 0:03ec282c2908 | 67 | inError(); |
feabhas | 0:03ec282c2908 | 68 | } else { |
feabhas | 0:03ec282c2908 | 69 | |
feabhas | 0:03ec282c2908 | 70 | WriteByte(sensor, READ_ROM); // Read ROM |
feabhas | 0:03ec282c2908 | 71 | for (uint32_t i = 0; i < 8; ++i) { |
feabhas | 0:03ec282c2908 | 72 | ROM_Code.rom[i] = ReadByte(sensor); |
feabhas | 0:03ec282c2908 | 73 | } |
feabhas | 0:03ec282c2908 | 74 | } |
feabhas | 0:03ec282c2908 | 75 | return ROM_Code; |
feabhas | 0:03ec282c2908 | 76 | } |
feabhas | 0:03ec282c2908 | 77 | |
feabhas | 0:03ec282c2908 | 78 | // temperature is store as 7.4 fixed point format (assuming 12 bit conversion) |
feabhas | 0:03ec282c2908 | 79 | void displayTemperature(Serial& s) { |
feabhas | 0:03ec282c2908 | 80 | DoConversion(); |
feabhas | 0:03ec282c2908 | 81 | uint32_t temp = GetTemperature(); |
feabhas | 0:03ec282c2908 | 82 | float f = (temp & 0x0F) * 0.0625; // calculate .4 part |
feabhas | 0:03ec282c2908 | 83 | f += (temp >> 4); // add 7.0 part to it |
feabhas | 0:03ec282c2908 | 84 | s.printf("Temp is %2.1fC\n\r", f); // display in 2.1 format |
feabhas | 0:03ec282c2908 | 85 | } |
feabhas | 0:03ec282c2908 | 86 |