DS1Wire simple Library

Committer:
feabhas
Date:
Fri Dec 30 19:34:53 2011 +0000
Revision:
0:bfa1afd9ae98

        

Who changed what in which revision?

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