Renato Loureiro / Mbed 2 deprecated Astromed

Dependencies:   OLED160G1 mbed uOLED

Fork of DS18B20GSM by Renato Loureiro

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 
00010 // device onboard register layout
00011 typedef struct {
00012     uint8_t    LSB;
00013     uint8_t    MSB;
00014     uint8_t    Th;
00015     uint8_t    Tl;
00016     uint8_t    config;
00017     uint8_t    reserved0xFF;
00018     uint8_t    reserved0xCH;
00019     uint8_t    reserved0x10;
00020     uint8_t    CRC;
00021 } ScratchPad_t;
00022 
00023 
00024 DigitalOut conversionInProgress(LED4);  // conversion in progress
00025 DigitalOut resetFailure(LED1);          // for error reporting
00026 extern DigitalInOut sensor;     // sensor pin
00027 
00028 static void inError() {
00029     while (1) {
00030         resetFailure = !resetFailure;
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 void showTemperature(float *f) {
00081     DoConversion();
00082     uint32_t temp = GetTemperature();
00083     *f = (temp & 0x0F) * 0.0625;    // calculate .4 part
00084     *f += (temp >> 4);    // add 7.0 part to it
00085     //return(f);
00086     //s.printf("Temp is %2.1fC\n\r", f);    // display in 2.1 format
00087 }
00088