Emeline Genest / Mbed 2 deprecated DS18B202

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers DS18B20.cpp Source File

DS18B20.cpp

00001 #include "DS18B20.h"
00002 #include "DS1Wire.h"
00003 #include "mbed.h"
00004 #include <stdint.h>
00005 
00006 // Device byte commands over 1-wire serial
00007 enum COMMANDS { READ_ROM = 0x33, CONVERT = 0x44, READ_SCRATCHPAD = 0xBE,  SKIP_ROM = 0xCC };
00008 
00009 // device onboard register layout
00010 typedef struct {
00011     uint8_t    LSB;
00012     uint8_t    MSB;
00013     uint8_t    Th;
00014     uint8_t    Tl;
00015     uint8_t    config;
00016     uint8_t    reserved0xFF;
00017     uint8_t    reserved0xCH;
00018     uint8_t    reserved0x10;
00019     uint8_t    crc;
00020 } ScratchPad_t;
00021 
00022 
00023 DigitalOut conversionInProgress(D1);  // conversion in progress
00024 DigitalOut resetFailure(D2);          // for error reporting
00025 extern DigitalInOut sensor;     // sensor pin
00026 
00027 static void inError() {
00028     while (1) {
00029         resetFailure = !resetFailure;
00030         ThisThread::sleep_for(200ms);
00031         //wait(0.2);
00032     }
00033 }
00034 
00035 void DoConversion() {
00036     if (Reset(sensor) != 0) {
00037         inError();
00038     } else {
00039         conversionInProgress = 1;       // led on
00040         WriteByte(sensor, SKIP_ROM);            // Skip ROM
00041         WriteByte(sensor, CONVERT);             // Convert
00042         while (ReadBit(sensor) == 0) {
00043             // wait for conversion to complete
00044         }
00045         conversionInProgress = 0;       // led off
00046     }
00047 }
00048 
00049 uint32_t GetTemperature() {
00050     uint32_t result = 0;
00051     if (Reset(sensor) != 0) {
00052         inError();
00053     } else {
00054         ScratchPad_t scratchpad;
00055         WriteByte(sensor, SKIP_ROM);    // Skip ROM
00056         WriteByte(sensor, READ_SCRATCHPAD);    // Read Scrachpad
00057         scratchpad.LSB = ReadByte(sensor);
00058         scratchpad.MSB = ReadByte(sensor);
00059         Reset(sensor);    // terminate read as we only want temperature
00060         result = ((scratchpad.MSB << 8) | scratchpad.LSB);
00061     }
00062     return result;
00063 }
00064 
00065 ROM_Code_t ReadROM() {
00066     ROM_Code_t ROM_Code;
00067     if (Reset(sensor) != 0) {
00068         inError();
00069     } else {
00070         
00071         WriteByte(sensor, READ_ROM);    // Read ROM
00072         for (uint32_t i = 0; i < 8; ++i) {
00073             ROM_Code.rom[i] = ReadByte(sensor);
00074         }
00075     }
00076     return ROM_Code;
00077 }
00078 
00079 // temperature is store as 7.4 fixed point format (assuming 12 bit conversion)
00080 float displayTemperature() {
00081     DoConversion();
00082     uint32_t temp = GetTemperature();
00083     float f = (temp & 0x0F) * 0.0625;    // calculate .4 part
00084     f += (temp >> 4);    // add 7.0 part to it
00085     //printf("Temp is %2.1fC\n\r", f);    // display in 2.1 format
00086     printf("Temp is %d C\n\r", (int)(f*10));    // display in 2.1 format
00087     return f;
00088 }
00089