ibutton read with modified ds18b20 libraries
Due to the lack of programs for ibutton interfacing with mbed, i built this one (still needs a lot of development), it's in a very basic state, but i'm working on it...
ds1990.cpp@0:4f399e4b64a9, 2013-08-23 (annotated)
- Committer:
- Renato
- Date:
- Fri Aug 23 08:36:59 2013 +0000
- Revision:
- 0:4f399e4b64a9
Function to read ibutton serial & family
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Renato | 0:4f399e4b64a9 | 1 | #include "ds1990.h" |
Renato | 0:4f399e4b64a9 | 2 | #include "DS1Wire.h" |
Renato | 0:4f399e4b64a9 | 3 | #include "mbed.h" |
Renato | 0:4f399e4b64a9 | 4 | #include <stdint.h> |
Renato | 0:4f399e4b64a9 | 5 | |
Renato | 0:4f399e4b64a9 | 6 | // Device byte commands over 1-wire serial |
Renato | 0:4f399e4b64a9 | 7 | enum COMMANDS { READ_ROM = 0x33, CONVERT = 0x44, READ_SCRATCHPAD = 0xBE, SKIP_ROM = 0xCC }; |
Renato | 0:4f399e4b64a9 | 8 | |
Renato | 0:4f399e4b64a9 | 9 | |
Renato | 0:4f399e4b64a9 | 10 | // device onboard register layout |
Renato | 0:4f399e4b64a9 | 11 | typedef struct { |
Renato | 0:4f399e4b64a9 | 12 | uint8_t LSB; |
Renato | 0:4f399e4b64a9 | 13 | uint8_t MSB; |
Renato | 0:4f399e4b64a9 | 14 | uint8_t Th; |
Renato | 0:4f399e4b64a9 | 15 | uint8_t Tl; |
Renato | 0:4f399e4b64a9 | 16 | uint8_t config; |
Renato | 0:4f399e4b64a9 | 17 | uint8_t reserved0xFF; |
Renato | 0:4f399e4b64a9 | 18 | uint8_t reserved0xCH; |
Renato | 0:4f399e4b64a9 | 19 | uint8_t reserved0x10; |
Renato | 0:4f399e4b64a9 | 20 | uint8_t CRC; |
Renato | 0:4f399e4b64a9 | 21 | } ScratchPad_t; |
Renato | 0:4f399e4b64a9 | 22 | |
Renato | 0:4f399e4b64a9 | 23 | |
Renato | 0:4f399e4b64a9 | 24 | DigitalOut conversionInProgress(LED4); // conversion in progress |
Renato | 0:4f399e4b64a9 | 25 | DigitalOut resetFailure(LED1); // for error reporting |
Renato | 0:4f399e4b64a9 | 26 | extern DigitalInOut sensor; // sensor pin |
Renato | 0:4f399e4b64a9 | 27 | |
Renato | 0:4f399e4b64a9 | 28 | static void inError() { |
Renato | 0:4f399e4b64a9 | 29 | while (1) { |
Renato | 0:4f399e4b64a9 | 30 | resetFailure = !resetFailure; |
Renato | 0:4f399e4b64a9 | 31 | wait(0.2); |
Renato | 0:4f399e4b64a9 | 32 | } |
Renato | 0:4f399e4b64a9 | 33 | } |
Renato | 0:4f399e4b64a9 | 34 | |
Renato | 0:4f399e4b64a9 | 35 | void DoConversion() { |
Renato | 0:4f399e4b64a9 | 36 | if (Reset(sensor) != 0) { |
Renato | 0:4f399e4b64a9 | 37 | inError(); |
Renato | 0:4f399e4b64a9 | 38 | } else { |
Renato | 0:4f399e4b64a9 | 39 | conversionInProgress = 1; // led on |
Renato | 0:4f399e4b64a9 | 40 | WriteByte(sensor, SKIP_ROM); // Skip ROM |
Renato | 0:4f399e4b64a9 | 41 | WriteByte(sensor, CONVERT); // Convert |
Renato | 0:4f399e4b64a9 | 42 | while (ReadBit(sensor) == 0) { |
Renato | 0:4f399e4b64a9 | 43 | // wait for conversion to complete |
Renato | 0:4f399e4b64a9 | 44 | } |
Renato | 0:4f399e4b64a9 | 45 | conversionInProgress = 0; // led off |
Renato | 0:4f399e4b64a9 | 46 | } |
Renato | 0:4f399e4b64a9 | 47 | } |
Renato | 0:4f399e4b64a9 | 48 | |
Renato | 0:4f399e4b64a9 | 49 | uint32_t GetTemperature() { |
Renato | 0:4f399e4b64a9 | 50 | uint32_t result = 0; |
Renato | 0:4f399e4b64a9 | 51 | if (Reset(sensor) != 0) { |
Renato | 0:4f399e4b64a9 | 52 | inError(); |
Renato | 0:4f399e4b64a9 | 53 | } else { |
Renato | 0:4f399e4b64a9 | 54 | ScratchPad_t scratchpad; |
Renato | 0:4f399e4b64a9 | 55 | WriteByte(sensor, SKIP_ROM); // Skip ROM |
Renato | 0:4f399e4b64a9 | 56 | WriteByte(sensor, READ_SCRATCHPAD); // Read Scrachpad |
Renato | 0:4f399e4b64a9 | 57 | scratchpad.LSB = ReadByte(sensor); |
Renato | 0:4f399e4b64a9 | 58 | scratchpad.MSB = ReadByte(sensor); |
Renato | 0:4f399e4b64a9 | 59 | Reset(sensor); // terminate read as we only want temperature |
Renato | 0:4f399e4b64a9 | 60 | result = ((scratchpad.MSB << 8) | scratchpad.LSB); |
Renato | 0:4f399e4b64a9 | 61 | } |
Renato | 0:4f399e4b64a9 | 62 | return result; |
Renato | 0:4f399e4b64a9 | 63 | } |
Renato | 0:4f399e4b64a9 | 64 | |
Renato | 0:4f399e4b64a9 | 65 | ROM_Code_t ReadROM() { |
Renato | 0:4f399e4b64a9 | 66 | ROM_Code_t ROM_Code; |
Renato | 0:4f399e4b64a9 | 67 | if (Reset(sensor) != 0) { |
Renato | 0:4f399e4b64a9 | 68 | inError(); |
Renato | 0:4f399e4b64a9 | 69 | } else { |
Renato | 0:4f399e4b64a9 | 70 | |
Renato | 0:4f399e4b64a9 | 71 | WriteByte(sensor, READ_ROM); // Read ROM |
Renato | 0:4f399e4b64a9 | 72 | for (uint32_t i = 0; i < 8; ++i) { |
Renato | 0:4f399e4b64a9 | 73 | ROM_Code.rom[i] = ReadByte(sensor); |
Renato | 0:4f399e4b64a9 | 74 | } |
Renato | 0:4f399e4b64a9 | 75 | } |
Renato | 0:4f399e4b64a9 | 76 | return ROM_Code; |
Renato | 0:4f399e4b64a9 | 77 | } |
Renato | 0:4f399e4b64a9 | 78 | |
Renato | 0:4f399e4b64a9 | 79 | // temperature is store as 7.4 fixed point format (assuming 12 bit conversion) |
Renato | 0:4f399e4b64a9 | 80 | void showTemperature(float *f) { |
Renato | 0:4f399e4b64a9 | 81 | DoConversion(); |
Renato | 0:4f399e4b64a9 | 82 | uint32_t temp = GetTemperature(); |
Renato | 0:4f399e4b64a9 | 83 | *f = (temp & 0x0F) * 0.0625; // calculate .4 part |
Renato | 0:4f399e4b64a9 | 84 | *f += (temp >> 4); // add 7.0 part to it |
Renato | 0:4f399e4b64a9 | 85 | //return(f); |
Renato | 0:4f399e4b64a9 | 86 | //s.printf("Temp is %2.1fC\n\r", f); // display in 2.1 format |
Renato | 0:4f399e4b64a9 | 87 | } |