tempe

Dependencies:   mbed

Committer:
emelinegen
Date:
Tue Jun 22 12:34:28 2021 +0000
Revision:
2:778929fc1520
Parent:
1:7bf2fdef45c9
temp

Who changed what in which revision?

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