Simple cpp wrapper of a ds18b20, onewire 'c' library. Supports multiple sensors.

Dependencies:   mbed

Dependents:   LPC11U68_DS18B20Sensor

Fork of DS18B20Sensor by Steve Spence

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers DS18B20Sensor.cpp Source File

DS18B20Sensor.cpp

Go to the documentation of this file.
00001 /**
00002 * @file DS18B20Sensor.cpp
00003 * @brief Wrapper for the OneWireDrv library by Frederic Blanc.
00004 * @author Steve Spence (Published 3 March 2013 www.mbed.org)
00005 */
00006 #include "DS18B20Sensor.h"
00007 
00008 DS18B20Sensor::DS18B20Sensor(PinName pin) : _oneWirePort(pin)
00009 {
00010     _init = false;
00011     nSensors = 0;
00012 }
00013 
00014 void DS18B20Sensor::getReading(char * text, uint8_t index)
00015 {
00016     uint8_t subzero, cel, cel_frac_bits;
00017     DS18X20_read_meas( &gSensorIDs[index][0], &subzero, &cel, &cel_frac_bits);
00018     DS18B20Sensor::getReading(text, subzero, cel, cel_frac_bits);
00019 }
00020 
00021 void DS18B20Sensor::getReading(char * text, uint8_t subzero, uint8_t cel, uint8_t cel_frac_bits)
00022 {
00023     uint16_t decicelsius;
00024     char s[10];
00025     float temperature;
00026     sprintf(text,"");
00027     sprintf(s,"%s", (subzero)?"-":"+");
00028     strcat(text,s);
00029     decicelsius = DS18X20_temp_to_decicel(subzero, cel, cel_frac_bits);
00030     temperature = decicelsius;
00031     temperature = temperature/10;
00032     sprintf(s,"%4.1f", temperature);
00033     strcat(text,s);
00034 
00035 }
00036 
00037 uint8_t DS18B20Sensor::search(void)
00038 {
00039     search_sensors(&nSensors, &gSensorIDs[0][0]);
00040     _init = true;
00041     return nSensors;
00042 }
00043 
00044 uint8_t DS18B20Sensor::count(void)
00045 {
00046     if (_init==false)
00047         DS18B20Sensor::search();
00048     return nSensors;
00049 }
00050 
00051 uint8_t DS18B20Sensor::startReading(bool includeWait)
00052 {
00053     uint8_t r = DS18X20_start_meas(DS18X20_POWER_EXTERN, 0 );
00054     if ((r == DS18X20_OK) && includeWait)
00055         wait_ms(DS18B20_TCONV_12BIT);
00056     return r;
00057 }
00058 
00059 void DS18B20Sensor::getReading(uint8_t index, uint8_t *subzero, uint8_t *cel, uint8_t *cel_frac_bits)
00060 {
00061     DS18X20_read_meas( &gSensorIDs[index][0], subzero, cel, cel_frac_bits);
00062 }