temperature & voltage sensor via SMS with uOLED 96 g1
Dependencies: OLED160G1 mbed uOLED
Fork of DS18B20GSM by
DS18B20.cpp@1:b2ea1e9d90df, 2012-10-31 (annotated)
- Committer:
- Renato
- Date:
- Wed Oct 31 12:26:00 2012 +0000
- Revision:
- 1:b2ea1e9d90df
- Parent:
- 0:03ec282c2908
Test v1
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 | |
Renato | 1:b2ea1e9d90df | 9 | |
feabhas | 0:03ec282c2908 | 10 | // device onboard register layout |
feabhas | 0:03ec282c2908 | 11 | typedef struct { |
feabhas | 0:03ec282c2908 | 12 | uint8_t LSB; |
feabhas | 0:03ec282c2908 | 13 | uint8_t MSB; |
feabhas | 0:03ec282c2908 | 14 | uint8_t Th; |
feabhas | 0:03ec282c2908 | 15 | uint8_t Tl; |
feabhas | 0:03ec282c2908 | 16 | uint8_t config; |
feabhas | 0:03ec282c2908 | 17 | uint8_t reserved0xFF; |
feabhas | 0:03ec282c2908 | 18 | uint8_t reserved0xCH; |
feabhas | 0:03ec282c2908 | 19 | uint8_t reserved0x10; |
feabhas | 0:03ec282c2908 | 20 | uint8_t CRC; |
feabhas | 0:03ec282c2908 | 21 | } ScratchPad_t; |
feabhas | 0:03ec282c2908 | 22 | |
feabhas | 0:03ec282c2908 | 23 | |
feabhas | 0:03ec282c2908 | 24 | DigitalOut conversionInProgress(LED4); // conversion in progress |
feabhas | 0:03ec282c2908 | 25 | DigitalOut resetFailure(LED1); // for error reporting |
feabhas | 0:03ec282c2908 | 26 | extern DigitalInOut sensor; // sensor pin |
feabhas | 0:03ec282c2908 | 27 | |
feabhas | 0:03ec282c2908 | 28 | static void inError() { |
feabhas | 0:03ec282c2908 | 29 | while (1) { |
feabhas | 0:03ec282c2908 | 30 | resetFailure = !resetFailure; |
feabhas | 0:03ec282c2908 | 31 | wait(0.2); |
feabhas | 0:03ec282c2908 | 32 | } |
feabhas | 0:03ec282c2908 | 33 | } |
feabhas | 0:03ec282c2908 | 34 | |
feabhas | 0:03ec282c2908 | 35 | void DoConversion() { |
feabhas | 0:03ec282c2908 | 36 | if (Reset(sensor) != 0) { |
feabhas | 0:03ec282c2908 | 37 | inError(); |
feabhas | 0:03ec282c2908 | 38 | } else { |
feabhas | 0:03ec282c2908 | 39 | conversionInProgress = 1; // led on |
feabhas | 0:03ec282c2908 | 40 | WriteByte(sensor, SKIP_ROM); // Skip ROM |
feabhas | 0:03ec282c2908 | 41 | WriteByte(sensor, CONVERT); // Convert |
feabhas | 0:03ec282c2908 | 42 | while (ReadBit(sensor) == 0) { |
feabhas | 0:03ec282c2908 | 43 | // wait for conversion to complete |
feabhas | 0:03ec282c2908 | 44 | } |
feabhas | 0:03ec282c2908 | 45 | conversionInProgress = 0; // led off |
feabhas | 0:03ec282c2908 | 46 | } |
feabhas | 0:03ec282c2908 | 47 | } |
feabhas | 0:03ec282c2908 | 48 | |
feabhas | 0:03ec282c2908 | 49 | uint32_t GetTemperature() { |
feabhas | 0:03ec282c2908 | 50 | uint32_t result = 0; |
feabhas | 0:03ec282c2908 | 51 | if (Reset(sensor) != 0) { |
feabhas | 0:03ec282c2908 | 52 | inError(); |
feabhas | 0:03ec282c2908 | 53 | } else { |
feabhas | 0:03ec282c2908 | 54 | ScratchPad_t scratchpad; |
feabhas | 0:03ec282c2908 | 55 | WriteByte(sensor, SKIP_ROM); // Skip ROM |
feabhas | 0:03ec282c2908 | 56 | WriteByte(sensor, READ_SCRATCHPAD); // Read Scrachpad |
feabhas | 0:03ec282c2908 | 57 | scratchpad.LSB = ReadByte(sensor); |
feabhas | 0:03ec282c2908 | 58 | scratchpad.MSB = ReadByte(sensor); |
feabhas | 0:03ec282c2908 | 59 | Reset(sensor); // terminate read as we only want temperature |
feabhas | 0:03ec282c2908 | 60 | result = ((scratchpad.MSB << 8) | scratchpad.LSB); |
feabhas | 0:03ec282c2908 | 61 | } |
feabhas | 0:03ec282c2908 | 62 | return result; |
feabhas | 0:03ec282c2908 | 63 | } |
feabhas | 0:03ec282c2908 | 64 | |
feabhas | 0:03ec282c2908 | 65 | ROM_Code_t ReadROM() { |
feabhas | 0:03ec282c2908 | 66 | ROM_Code_t ROM_Code; |
feabhas | 0:03ec282c2908 | 67 | if (Reset(sensor) != 0) { |
feabhas | 0:03ec282c2908 | 68 | inError(); |
feabhas | 0:03ec282c2908 | 69 | } else { |
feabhas | 0:03ec282c2908 | 70 | |
feabhas | 0:03ec282c2908 | 71 | WriteByte(sensor, READ_ROM); // Read ROM |
feabhas | 0:03ec282c2908 | 72 | for (uint32_t i = 0; i < 8; ++i) { |
feabhas | 0:03ec282c2908 | 73 | ROM_Code.rom[i] = ReadByte(sensor); |
feabhas | 0:03ec282c2908 | 74 | } |
feabhas | 0:03ec282c2908 | 75 | } |
feabhas | 0:03ec282c2908 | 76 | return ROM_Code; |
feabhas | 0:03ec282c2908 | 77 | } |
feabhas | 0:03ec282c2908 | 78 | |
feabhas | 0:03ec282c2908 | 79 | // temperature is store as 7.4 fixed point format (assuming 12 bit conversion) |
Renato | 1:b2ea1e9d90df | 80 | void showTemperature(float *f) { |
feabhas | 0:03ec282c2908 | 81 | DoConversion(); |
feabhas | 0:03ec282c2908 | 82 | uint32_t temp = GetTemperature(); |
Renato | 1:b2ea1e9d90df | 83 | *f = (temp & 0x0F) * 0.0625; // calculate .4 part |
Renato | 1:b2ea1e9d90df | 84 | *f += (temp >> 4); // add 7.0 part to it |
Renato | 1:b2ea1e9d90df | 85 | //return(f); |
Renato | 1:b2ea1e9d90df | 86 | //s.printf("Temp is %2.1fC\n\r", f); // display in 2.1 format |
feabhas | 0:03ec282c2908 | 87 | } |
feabhas | 0:03ec282c2908 | 88 |