极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 8098|回复: 1

DS18B20的分辨率怎么设置成 9, 10, 11位,默认是12位的

[复制链接]
发表于 2013-12-21 12:18:53 | 显示全部楼层 |阅读模式
先上DallasTemperature.h
  1. #ifndef DallasTemperature_h
  2. #define DallasTemperature_h

  3. #define DALLASTEMPLIBVERSION "3.7.2"

  4. // This library is free software; you can redistribute it and/or
  5. // modify it under the terms of the GNU Lesser General Public
  6. // License as published by the Free Software Foundation; either
  7. // version 2.1 of the License, or (at your option) any later version.

  8. // set to true to include code for new and delete operators
  9. #ifndef REQUIRESNEW
  10. #define REQUIRESNEW false
  11. #endif

  12. // set to true to include code implementing alarm search functions
  13. #ifndef REQUIRESALARMS
  14. #define REQUIRESALARMS true
  15. #endif

  16. #include <inttypes.h>
  17. #include <OneWire.h>

  18. // Model IDs
  19. #define DS18S20MODEL 0x10
  20. #define DS18B20MODEL 0x28
  21. #define DS1822MODEL  0x22

  22. // OneWire commands
  23. #define STARTCONVO      0x44  // Tells device to take a temperature reading and put it on the scratchpad
  24. #define COPYSCRATCH     0x48  // Copy EEPROM
  25. #define READSCRATCH     0xBE  // Read EEPROM
  26. #define WRITESCRATCH    0x4E  // Write to EEPROM
  27. #define RECALLSCRATCH   0xB8  // Reload from last known
  28. #define READPOWERSUPPLY 0xB4  // Determine if device needs parasite power
  29. #define ALARMSEARCH     0xEC  // Query bus for devices with an alarm condition

  30. // Scratchpad locations
  31. #define TEMP_LSB        0
  32. #define TEMP_MSB        1
  33. #define HIGH_ALARM_TEMP 2
  34. #define LOW_ALARM_TEMP  3
  35. #define CONFIGURATION   4
  36. #define INTERNAL_BYTE   5
  37. #define COUNT_REMAIN    6
  38. #define COUNT_PER_C     7
  39. #define SCRATCHPAD_CRC  8

  40. // Device resolution
  41. #define TEMP_9_BIT  0x1F //  9 bit
  42. #define TEMP_10_BIT 0x3F // 10 bit
  43. #define TEMP_11_BIT 0x5F // 11 bit
  44. #define TEMP_12_BIT 0x7F // 12 bit

  45. // Error Codes
  46. #define DEVICE_DISCONNECTED -127

  47. typedef uint8_t DeviceAddress[8];

  48. class DallasTemperature
  49. {
  50.   public:

  51.   DallasTemperature(OneWire*);

  52.   // initalise bus
  53.   void begin(void);

  54.   // returns the number of devices found on the bus
  55.   uint8_t getDeviceCount(void);
  56.   
  57.   // Is a conversion complete on the wire?
  58.   bool isConversionComplete(void);
  59.   
  60.   // returns true if address is valid
  61.   bool validAddress(uint8_t*);

  62.   // finds an address at a given index on the bus
  63.   bool getAddress(uint8_t*, const uint8_t);
  64.   
  65.   // attempt to determine if the device at the given address is connected to the bus
  66.   bool isConnected(uint8_t*);

  67.   // attempt to determine if the device at the given address is connected to the bus
  68.   // also allows for updating the read scratchpad
  69.   bool isConnected(uint8_t*, uint8_t*);

  70.   // read device's scratchpad
  71.   void readScratchPad(uint8_t*, uint8_t*);

  72.   // write device's scratchpad
  73.   void writeScratchPad(uint8_t*, const uint8_t*);

  74.   // read device's power requirements
  75.   bool readPowerSupply(uint8_t*);

  76.   // get global resolution
  77.   uint8_t getResolution();
  78.   
  79.   // set global resolution to 9, 10, 11, or 12 bits
  80.   void setResolution(uint8_t);

  81.   // returns the device resolution, 9-12
  82.   uint8_t getResolution(uint8_t*);

  83.   // set resolution of a device to 9, 10, 11, or 12 bits
  84.   bool setResolution(uint8_t*, uint8_t);
  85.   
  86.   // sets/gets the waitForConversion flag
  87.   void setWaitForConversion(bool);
  88.   bool getWaitForConversion(void);
  89.   
  90.   // sets/gets the checkForConversion flag
  91.   void setCheckForConversion(bool);
  92.   bool getCheckForConversion(void);
  93.   
  94.   // sends command for all devices on the bus to perform a temperature conversion
  95.   void requestTemperatures(void);
  96.    
  97.   // sends command for one device to perform a temperature conversion by address
  98.   bool requestTemperaturesByAddress(uint8_t*);

  99.   // sends command for one device to perform a temperature conversion by index
  100.   bool requestTemperaturesByIndex(uint8_t);

  101.   // returns temperature in degrees C
  102.   float getTempC(uint8_t*);

  103.   // returns temperature in degrees F
  104.   float getTempF(uint8_t*);

  105.   // Get temperature for device index (slow)
  106.   float getTempCByIndex(uint8_t);
  107.   
  108.   // Get temperature for device index (slow)
  109.   float getTempFByIndex(uint8_t);
  110.   
  111.   // returns true if the bus requires parasite power
  112.   bool isParasitePowerMode(void);
  113.   
  114.   bool isConversionAvailable(uint8_t*);

  115.   #if REQUIRESALARMS
  116.   
  117.   typedef void AlarmHandler(uint8_t*);

  118.   // sets the high alarm temperature for a device
  119.   // accepts a char.  valid range is -55C - 125C
  120.   void setHighAlarmTemp(uint8_t*, const char);

  121.   // sets the low alarm temperature for a device
  122.   // accepts a char.  valid range is -55C - 125C
  123.   void setLowAlarmTemp(uint8_t*, const char);

  124.   // returns a signed char with the current high alarm temperature for a device
  125.   // in the range -55C - 125C
  126.   char getHighAlarmTemp(uint8_t*);

  127.   // returns a signed char with the current low alarm temperature for a device
  128.   // in the range -55C - 125C
  129.   char getLowAlarmTemp(uint8_t*);
  130.   
  131.   // resets internal variables used for the alarm search
  132.   void resetAlarmSearch(void);

  133.   // search the wire for devices with active alarms
  134.   bool alarmSearch(uint8_t*);

  135.   // returns true if ia specific device has an alarm
  136.   bool hasAlarm(uint8_t*);

  137.   // returns true if any device is reporting an alarm on the bus
  138.   bool hasAlarm(void);

  139.   // runs the alarm handler for all devices returned by alarmSearch()
  140.   void processAlarms(void);
  141.   
  142.   // sets the alarm handler
  143.   void setAlarmHandler(AlarmHandler *);
  144.   
  145.   // The default alarm handler
  146.   static void defaultAlarmHandler(uint8_t*);

  147.   #endif

  148.   // convert from celcius to farenheit
  149.   static float toFahrenheit(const float);

  150.   // convert from farenheit to celsius
  151.   static float toCelsius(const float);

  152.   #if REQUIRESNEW

  153.   // initalize memory area
  154.   void* operator new (unsigned int);

  155.   // delete memory reference
  156.   void operator delete(void*);
  157.   
  158.   #endif

  159.   private:
  160.   typedef uint8_t ScratchPad[9];
  161.   
  162.   // parasite power on or off
  163.   bool parasite;

  164.   // used to determine the delay amount needed to allow for the
  165.   // temperature conversion to take place
  166.   uint8_t bitResolution;
  167.   
  168.   // used to requestTemperature with or without delay
  169.   bool waitForConversion;
  170.   
  171.   // used to requestTemperature to dynamically check if a conversion is complete
  172.   bool checkForConversion;
  173.   
  174.   // count of devices on the bus
  175.   uint8_t devices;
  176.   
  177.   // Take a pointer to one wire instance
  178.   OneWire* _wire;

  179.   // reads scratchpad and returns the temperature in degrees C
  180.   float calculateTemperature(uint8_t*, uint8_t*);
  181.   
  182.   void        blockTillConversionComplete(uint8_t*,uint8_t*);
  183.   
  184.   #if REQUIRESALARMS

  185.   // required for alarmSearch
  186.   uint8_t alarmSearchAddress[8];
  187.   char alarmSearchJunction;
  188.   uint8_t alarmSearchExhausted;

  189.   // the alarm handler function pointer
  190.   AlarmHandler *_AlarmHandler;

  191.   #endif
  192.   
  193. };
  194. #endif
复制代码



通过注释,发现几个关键:
1、#define TEMP_9_BIT  0x1F //  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编译将直接报错
回复

使用道具 举报

 楼主| 发表于 2013-12-21 12:32:36 | 显示全部楼层
网上一个修改分辨率的程序,顿时凌乱了,菜鸟表示看不懂啊~~~

Code:

  1. // set resolution of all devices to 9, 10, 11, or 12 bits
  2. // if new resolution is out of range, it is constrained.
  3. void DallasTemperature::setResolution(uint8_t newResolution)
  4. {
  5.   bitResolution = constrain(newResolution, 9, 12);
  6.   DeviceAddress deviceAddress;
  7.   for (int i=0; i<devices; i++)
  8.   {
  9.     getAddress(deviceAddress, i);
  10. setResolution(deviceAddress, bitResolution);
  11.   }
  12. }

  13. // set resolution of a device to 9, 10, 11, or 12 bits
  14. // if new resolution is out of range, 9 bits is used.
  15. bool DallasTemperature::setResolution(uint8_t* deviceAddress, uint8_t newResolution)
  16. {
  17.   ScratchPad scratchPad;
  18.   if (isConnected(deviceAddress, scratchPad))
  19.   {
  20.     // DS18S20 has a fixed 9-bit resolution
  21.     if (deviceAddress[0] != DS18S20MODEL)
  22.     {
  23.       switch (newResolution)
  24.       {
  25.         case 12:
  26.           scratchPad[CONFIGURATION] = TEMP_12_BIT;
  27.           break;
  28.         case 11:
  29.           scratchPad[CONFIGURATION] = TEMP_11_BIT;
  30.           break;
  31.         case 10:
  32.           scratchPad[CONFIGURATION] = TEMP_10_BIT;
  33.           break;
  34.         case 9:
  35.         default:
  36.           scratchPad[CONFIGURATION] = TEMP_9_BIT;
  37.           break;
  38.       }
  39.       writeScratchPad(deviceAddress, scratchPad);
  40.     }
  41. return true;  // new value set
  42.   }
  43.   return false;
  44. }
复制代码
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-6 17:48 , Processed in 0.038221 second(s), 17 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表