This is the device firmware for the controlBoard in the DIY 3D Printable Raspberry Pi Raman Spectrometer. For more information please visit: http://hackaday.io/project/1279

Dependencies:   mbed

Committer:
flatcat
Date:
Fri Aug 15 10:38:50 2014 +0000
Revision:
0:14942e263231
http://hackaday.io/project/1279

Who changed what in which revision?

UserRevisionLine numberNew contents of line
flatcat 0:14942e263231 1 /*
flatcat 0:14942e263231 2 * DS18B20. Maxim DS18B20 One-Wire Thermometer.
flatcat 0:14942e263231 3 * Uses the OneWireCRC library.
flatcat 0:14942e263231 4 *
flatcat 0:14942e263231 5 * Copyright (C) <2010> Petras Saduikis <petras@petras.co.uk>
flatcat 0:14942e263231 6 *
flatcat 0:14942e263231 7 * This file is part of OneWireThermometer.
flatcat 0:14942e263231 8 *
flatcat 0:14942e263231 9 * OneWireThermometer is free software: you can redistribute it and/or modify
flatcat 0:14942e263231 10 * it under the terms of the GNU General Public License as published by
flatcat 0:14942e263231 11 * the Free Software Foundation, either version 3 of the License, or
flatcat 0:14942e263231 12 * (at your option) any later version.
flatcat 0:14942e263231 13 *
flatcat 0:14942e263231 14 * OneWireThermometer is distributed in the hope that it will be useful,
flatcat 0:14942e263231 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
flatcat 0:14942e263231 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
flatcat 0:14942e263231 17 * GNU General Public License for more details.
flatcat 0:14942e263231 18 *
flatcat 0:14942e263231 19 * You should have received a copy of the GNU General Public License
flatcat 0:14942e263231 20 * along with OneWireThermometer. If not, see <http://www.gnu.org/licenses/>.
flatcat 0:14942e263231 21 */
flatcat 0:14942e263231 22
flatcat 0:14942e263231 23 #include "DS18B20.h"
flatcat 0:14942e263231 24 //#include "DebugTrace.h"
flatcat 0:14942e263231 25
flatcat 0:14942e263231 26 //DebugTrace pc_ds18B20(OFF, TO_SERIAL);
flatcat 0:14942e263231 27
flatcat 0:14942e263231 28 DS18B20::DS18B20( PinName pin) :
flatcat 0:14942e263231 29 OneWireThermometer(pin, DS18B20_ID)
flatcat 0:14942e263231 30 {
flatcat 0:14942e263231 31 }
flatcat 0:14942e263231 32
flatcat 0:14942e263231 33 void DS18B20::setResolution(eResolution resln)
flatcat 0:14942e263231 34 {
flatcat 0:14942e263231 35 // as the write to the configuration register involves a write to the
flatcat 0:14942e263231 36 // high and low alarm bytes, need to read these registers first
flatcat 0:14942e263231 37 // and copy them back on the write
flatcat 0:14942e263231 38
flatcat 0:14942e263231 39 BYTE read_data[THERMOM_SCRATCHPAD_SIZE];
flatcat 0:14942e263231 40 BYTE write_data[ALARM_CONFIG_SIZE];
flatcat 0:14942e263231 41
flatcat 0:14942e263231 42 if (readAndValidateData(read_data))
flatcat 0:14942e263231 43 {
flatcat 0:14942e263231 44 // copy alarm and config data to write data
flatcat 0:14942e263231 45 for (int k = 2; k < 5; k++)
flatcat 0:14942e263231 46 {
flatcat 0:14942e263231 47 write_data[k - 2] = read_data[k];
flatcat 0:14942e263231 48 }
flatcat 0:14942e263231 49 int config = write_data[2];
flatcat 0:14942e263231 50 config &= 0x9F;
flatcat 0:14942e263231 51 config ^= (resln << 5);
flatcat 0:14942e263231 52 write_data[2] = config;
flatcat 0:14942e263231 53
flatcat 0:14942e263231 54 resetAndAddress();
flatcat 0:14942e263231 55 oneWire.writeByte(WRITESCRATCH);
flatcat 0:14942e263231 56 for (int k = 0; k < 3; k++)
flatcat 0:14942e263231 57 {
flatcat 0:14942e263231 58 oneWire.writeByte(write_data[k]);
flatcat 0:14942e263231 59 }
flatcat 0:14942e263231 60
flatcat 0:14942e263231 61 // remember it so we can use the correct delay in reading the temperature
flatcat 0:14942e263231 62 // for parasitic power
flatcat 0:14942e263231 63 resolution = resln;
flatcat 0:14942e263231 64 }
flatcat 0:14942e263231 65 }
flatcat 0:14942e263231 66
flatcat 0:14942e263231 67 float DS18B20::calculateTemperature(BYTE* data)
flatcat 0:14942e263231 68 {
flatcat 0:14942e263231 69 bool signBit = false;
flatcat 0:14942e263231 70 if (data[TEMPERATURE_MSB] & 0x80) signBit = true;
flatcat 0:14942e263231 71
flatcat 0:14942e263231 72 int read_temp = (data[TEMPERATURE_MSB] << 8) + data[TEMPERATURE_LSB];
flatcat 0:14942e263231 73 if (signBit)
flatcat 0:14942e263231 74 {
flatcat 0:14942e263231 75 read_temp = (read_temp ^ 0xFFFF) + 1; // two's complement
flatcat 0:14942e263231 76 read_temp *= -1;
flatcat 0:14942e263231 77 }
flatcat 0:14942e263231 78
flatcat 0:14942e263231 79 int resolution = (data[CONFIG_REG_BYTE] & 0x60) >> 5; // mask off bits 6,5 and move to 1,0
flatcat 0:14942e263231 80 switch (resolution)
flatcat 0:14942e263231 81 {
flatcat 0:14942e263231 82 case nineBit: // 0.5 deg C increments
flatcat 0:14942e263231 83 read_temp &= 0xFFF8; // bits 2,1,0 are undefined
flatcat 0:14942e263231 84 //pc_ds18B20.traceOut("9 bit resolution ...\r\n");
flatcat 0:14942e263231 85 break;
flatcat 0:14942e263231 86 case tenBit: // 0.25 deg C increments
flatcat 0:14942e263231 87 read_temp &= 0xFFFC; // bits 1,0 are undefined
flatcat 0:14942e263231 88 //pc_ds18B20.traceOut("10 bit resolution ...\r\n");
flatcat 0:14942e263231 89 break;
flatcat 0:14942e263231 90 case elevenBit: // 0.125 deg C increments
flatcat 0:14942e263231 91 read_temp &= 0xFFFE; // bit 0 is undefined
flatcat 0:14942e263231 92 //pc_ds18B20.traceOut("11 bit resolution ...\r\n");
flatcat 0:14942e263231 93 break;
flatcat 0:14942e263231 94 case twelveBit: // 0.0625 deg C increments
flatcat 0:14942e263231 95 //pc_ds18B20.traceOut("12 bit resolution ...\r\n");
flatcat 0:14942e263231 96 break;
flatcat 0:14942e263231 97 }
flatcat 0:14942e263231 98 float realTemp = (float)read_temp/16 ;
flatcat 0:14942e263231 99
flatcat 0:14942e263231 100 //pc_ds18B20.traceOut("TEMP_READ/REAL TEMP: %f \r\n", realTemp);
flatcat 0:14942e263231 101
flatcat 0:14942e263231 102 return realTemp;
flatcat 0:14942e263231 103 }