DS18B20的分辨率怎么设置成 9, 10, 11位,默认是12位的
先上DallasTemperature.h#ifndef DallasTemperature_h
#define DallasTemperature_h
#define DALLASTEMPLIBVERSION "3.7.2"
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
// set to true to include code for new and delete operators
#ifndef REQUIRESNEW
#define REQUIRESNEW false
#endif
// set to true to include code implementing alarm search functions
#ifndef REQUIRESALARMS
#define REQUIRESALARMS true
#endif
#include <inttypes.h>
#include <OneWire.h>
// Model IDs
#define DS18S20MODEL 0x10
#define DS18B20MODEL 0x28
#define DS1822MODEL0x22
// OneWire commands
#define STARTCONVO 0x44// Tells device to take a temperature reading and put it on the scratchpad
#define COPYSCRATCH 0x48// Copy EEPROM
#define READSCRATCH 0xBE// Read EEPROM
#define WRITESCRATCH 0x4E// Write to EEPROM
#define RECALLSCRATCH 0xB8// Reload from last known
#define READPOWERSUPPLY 0xB4// Determine if device needs parasite power
#define ALARMSEARCH 0xEC// Query bus for devices with an alarm condition
// Scratchpad locations
#define TEMP_LSB 0
#define TEMP_MSB 1
#define HIGH_ALARM_TEMP 2
#define LOW_ALARM_TEMP3
#define CONFIGURATION 4
#define INTERNAL_BYTE 5
#define COUNT_REMAIN 6
#define COUNT_PER_C 7
#define SCRATCHPAD_CRC8
// Device resolution
#define TEMP_9_BIT0x1F //9 bit
#define TEMP_10_BIT 0x3F // 10 bit
#define TEMP_11_BIT 0x5F // 11 bit
#define TEMP_12_BIT 0x7F // 12 bit
// Error Codes
#define DEVICE_DISCONNECTED -127
typedef uint8_t DeviceAddress;
class DallasTemperature
{
public:
DallasTemperature(OneWire*);
// initalise bus
void begin(void);
// returns the number of devices found on the bus
uint8_t getDeviceCount(void);
// Is a conversion complete on the wire?
bool isConversionComplete(void);
// returns true if address is valid
bool validAddress(uint8_t*);
// finds an address at a given index on the bus
bool getAddress(uint8_t*, const uint8_t);
// attempt to determine if the device at the given address is connected to the bus
bool isConnected(uint8_t*);
// attempt to determine if the device at the given address is connected to the bus
// also allows for updating the read scratchpad
bool isConnected(uint8_t*, uint8_t*);
// read device's scratchpad
void readScratchPad(uint8_t*, uint8_t*);
// write device's scratchpad
void writeScratchPad(uint8_t*, const uint8_t*);
// read device's power requirements
bool readPowerSupply(uint8_t*);
// get global resolution
uint8_t getResolution();
// set global resolution to 9, 10, 11, or 12 bits
void setResolution(uint8_t);
// returns the device resolution, 9-12
uint8_t getResolution(uint8_t*);
// set resolution of a device to 9, 10, 11, or 12 bits
bool setResolution(uint8_t*, uint8_t);
// sets/gets the waitForConversion flag
void setWaitForConversion(bool);
bool getWaitForConversion(void);
// sets/gets the checkForConversion flag
void setCheckForConversion(bool);
bool getCheckForConversion(void);
// sends command for all devices on the bus to perform a temperature conversion
void requestTemperatures(void);
// sends command for one device to perform a temperature conversion by address
bool requestTemperaturesByAddress(uint8_t*);
// sends command for one device to perform a temperature conversion by index
bool requestTemperaturesByIndex(uint8_t);
// returns temperature in degrees C
float getTempC(uint8_t*);
// returns temperature in degrees F
float getTempF(uint8_t*);
// Get temperature for device index (slow)
float getTempCByIndex(uint8_t);
// Get temperature for device index (slow)
float getTempFByIndex(uint8_t);
// returns true if the bus requires parasite power
bool isParasitePowerMode(void);
bool isConversionAvailable(uint8_t*);
#if REQUIRESALARMS
typedef void AlarmHandler(uint8_t*);
// sets the high alarm temperature for a device
// accepts a char.valid range is -55C - 125C
void setHighAlarmTemp(uint8_t*, const char);
// sets the low alarm temperature for a device
// accepts a char.valid range is -55C - 125C
void setLowAlarmTemp(uint8_t*, const char);
// returns a signed char with the current high alarm temperature for a device
// in the range -55C - 125C
char getHighAlarmTemp(uint8_t*);
// returns a signed char with the current low alarm temperature for a device
// in the range -55C - 125C
char getLowAlarmTemp(uint8_t*);
// resets internal variables used for the alarm search
void resetAlarmSearch(void);
// search the wire for devices with active alarms
bool alarmSearch(uint8_t*);
// returns true if ia specific device has an alarm
bool hasAlarm(uint8_t*);
// returns true if any device is reporting an alarm on the bus
bool hasAlarm(void);
// runs the alarm handler for all devices returned by alarmSearch()
void processAlarms(void);
// sets the alarm handler
void setAlarmHandler(AlarmHandler *);
// The default alarm handler
static void defaultAlarmHandler(uint8_t*);
#endif
// convert from celcius to farenheit
static float toFahrenheit(const float);
// convert from farenheit to celsius
static float toCelsius(const float);
#if REQUIRESNEW
// initalize memory area
void* operator new (unsigned int);
// delete memory reference
void operator delete(void*);
#endif
private:
typedef uint8_t ScratchPad;
// parasite power on or off
bool parasite;
// used to determine the delay amount needed to allow for the
// temperature conversion to take place
uint8_t bitResolution;
// used to requestTemperature with or without delay
bool waitForConversion;
// used to requestTemperature to dynamically check if a conversion is complete
bool checkForConversion;
// count of devices on the bus
uint8_t devices;
// Take a pointer to one wire instance
OneWire* _wire;
// reads scratchpad and returns the temperature in degrees C
float calculateTemperature(uint8_t*, uint8_t*);
void blockTillConversionComplete(uint8_t*,uint8_t*);
#if REQUIRESALARMS
// required for alarmSearch
uint8_t alarmSearchAddress;
char alarmSearchJunction;
uint8_t alarmSearchExhausted;
// the alarm handler function pointer
AlarmHandler *_AlarmHandler;
#endif
};
#endif
通过注释,发现几个关键:
1、#define TEMP_9_BIT0x1F //9 bit是把什么地方改成0x1F 呢?
2、
// get global resolution
//得到全局分辨率
uint8_t getResolution();
//目测这里是吧得到的分辨率值赋值给 uint8_t
// set global resolution to 9, 10, 11, or 12 bits
//设置全局分辨率为9,10,11,或12位
void setResolution(uint8_t);
//如果将将这个uint8_t换成0x1F编译将直接报错
3、
// returns the device resolution, 9-12
//返回设备的分辨率,9-12
uint8_t getResolution(uint8_t*);
// set resolution of a device to 9, 10, 11, or 12 bits
//一个装置的分辨率9,10,11,或12位
bool setResolution(uint8_t*, uint8_t);
//如果将这个uint8_t换成0x1F编译将直接报错
网上一个修改分辨率的程序,顿时凌乱了,菜鸟表示看不懂啊~~~
Code:
// set resolution of all devices to 9, 10, 11, or 12 bits
// if new resolution is out of range, it is constrained.
void DallasTemperature::setResolution(uint8_t newResolution)
{
bitResolution = constrain(newResolution, 9, 12);
DeviceAddress deviceAddress;
for (int i=0; i<devices; i++)
{
getAddress(deviceAddress, i);
setResolution(deviceAddress, bitResolution);
}
}
// set resolution of a device to 9, 10, 11, or 12 bits
// if new resolution is out of range, 9 bits is used.
bool DallasTemperature::setResolution(uint8_t* deviceAddress, uint8_t newResolution)
{
ScratchPad scratchPad;
if (isConnected(deviceAddress, scratchPad))
{
// DS18S20 has a fixed 9-bit resolution
if (deviceAddress != DS18S20MODEL)
{
switch (newResolution)
{
case 12:
scratchPad = TEMP_12_BIT;
break;
case 11:
scratchPad = TEMP_11_BIT;
break;
case 10:
scratchPad = TEMP_10_BIT;
break;
case 9:
default:
scratchPad = TEMP_9_BIT;
break;
}
writeScratchPad(deviceAddress, scratchPad);
}
return true;// new value set
}
return false;
}
页:
[1]