Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
ds18b20/DS18B20.cpp@3:0ef2ced1f02d, 2014-04-12 (annotated)
- Committer:
- redplam
- Date:
- Sat Apr 12 20:07:15 2014 +0000
- Revision:
- 3:0ef2ced1f02d
- Parent:
- 1:3aec489c7366
myproj
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
redplam | 1:3aec489c7366 | 1 | #include "DS18B20.h" |
redplam | 1:3aec489c7366 | 2 | #include "DS1Wire.h" |
redplam | 1:3aec489c7366 | 3 | #include "mbed.h" |
redplam | 1:3aec489c7366 | 4 | #include <stdint.h> |
redplam | 1:3aec489c7366 | 5 | |
redplam | 1:3aec489c7366 | 6 | // Device byte commands over 1-wire serial |
redplam | 1:3aec489c7366 | 7 | enum COMMANDS { READ_ROM = 0x33, CONVERT = 0x44, READ_SCRATCHPAD = 0xBE, SKIP_ROM = 0xCC }; |
redplam | 1:3aec489c7366 | 8 | |
redplam | 1:3aec489c7366 | 9 | // device onboard register layout |
redplam | 1:3aec489c7366 | 10 | typedef struct { |
redplam | 1:3aec489c7366 | 11 | uint8_t LSB; |
redplam | 1:3aec489c7366 | 12 | uint8_t MSB; |
redplam | 1:3aec489c7366 | 13 | uint8_t Th; |
redplam | 1:3aec489c7366 | 14 | uint8_t Tl; |
redplam | 1:3aec489c7366 | 15 | uint8_t config; |
redplam | 1:3aec489c7366 | 16 | uint8_t reserved0xFF; |
redplam | 1:3aec489c7366 | 17 | uint8_t reserved0xCH; |
redplam | 1:3aec489c7366 | 18 | uint8_t reserved0x10; |
redplam | 1:3aec489c7366 | 19 | uint8_t CRC; |
redplam | 1:3aec489c7366 | 20 | } ScratchPad_t; |
redplam | 1:3aec489c7366 | 21 | |
redplam | 1:3aec489c7366 | 22 | |
redplam | 1:3aec489c7366 | 23 | DigitalOut conversionInProgress(LED4); // conversion in progress |
redplam | 1:3aec489c7366 | 24 | DigitalOut resetFailure(LED1); // for error reporting |
redplam | 1:3aec489c7366 | 25 | extern DigitalInOut sensor; // sensor pin |
redplam | 1:3aec489c7366 | 26 | |
redplam | 1:3aec489c7366 | 27 | static void inError() { |
redplam | 1:3aec489c7366 | 28 | while (1) { |
redplam | 1:3aec489c7366 | 29 | resetFailure = !resetFailure; |
redplam | 1:3aec489c7366 | 30 | wait(0.2); |
redplam | 1:3aec489c7366 | 31 | } |
redplam | 1:3aec489c7366 | 32 | } |
redplam | 1:3aec489c7366 | 33 | |
redplam | 1:3aec489c7366 | 34 | void DoConversion() { |
redplam | 1:3aec489c7366 | 35 | if (Reset(sensor) != 0) { |
redplam | 1:3aec489c7366 | 36 | inError(); |
redplam | 1:3aec489c7366 | 37 | } else { |
redplam | 1:3aec489c7366 | 38 | conversionInProgress = 1; // led on |
redplam | 1:3aec489c7366 | 39 | WriteByte(sensor, SKIP_ROM); // Skip ROM |
redplam | 1:3aec489c7366 | 40 | WriteByte(sensor, CONVERT); // Convert |
redplam | 1:3aec489c7366 | 41 | while (ReadBit(sensor) == 0) { |
redplam | 1:3aec489c7366 | 42 | // wait for conversion to complete |
redplam | 1:3aec489c7366 | 43 | } |
redplam | 1:3aec489c7366 | 44 | conversionInProgress = 0; // led off |
redplam | 1:3aec489c7366 | 45 | } |
redplam | 1:3aec489c7366 | 46 | } |
redplam | 1:3aec489c7366 | 47 | |
redplam | 1:3aec489c7366 | 48 | uint32_t GetTemperature() { |
redplam | 1:3aec489c7366 | 49 | uint32_t result = 0; |
redplam | 1:3aec489c7366 | 50 | if (Reset(sensor) != 0) { |
redplam | 1:3aec489c7366 | 51 | inError(); |
redplam | 1:3aec489c7366 | 52 | } else { |
redplam | 1:3aec489c7366 | 53 | ScratchPad_t scratchpad; |
redplam | 1:3aec489c7366 | 54 | WriteByte(sensor, SKIP_ROM); // Skip ROM |
redplam | 1:3aec489c7366 | 55 | WriteByte(sensor, READ_SCRATCHPAD); // Read Scrachpad |
redplam | 1:3aec489c7366 | 56 | scratchpad.LSB = ReadByte(sensor); |
redplam | 1:3aec489c7366 | 57 | scratchpad.MSB = ReadByte(sensor); |
redplam | 1:3aec489c7366 | 58 | Reset(sensor); // terminate read as we only want temperature |
redplam | 1:3aec489c7366 | 59 | result = ((scratchpad.MSB << 8) | scratchpad.LSB); |
redplam | 1:3aec489c7366 | 60 | } |
redplam | 1:3aec489c7366 | 61 | return result; |
redplam | 1:3aec489c7366 | 62 | } |
redplam | 1:3aec489c7366 | 63 | float mytemp(void) { |
redplam | 1:3aec489c7366 | 64 | DoConversion(); |
redplam | 1:3aec489c7366 | 65 | uint32_t temp = GetTemperature(); |
redplam | 1:3aec489c7366 | 66 | float f = (temp & 0x0F) * 0.0625; // calculate .4 part |
redplam | 1:3aec489c7366 | 67 | f += (temp >> 4); // add 7.0 part to it |
redplam | 1:3aec489c7366 | 68 | return f; |
redplam | 1:3aec489c7366 | 69 | //s.printf("Temp is %2.1fC\n\r", f); // display in 2.1 format |
redplam | 1:3aec489c7366 | 70 | } |
redplam | 1:3aec489c7366 | 71 |