DS18B20.cpp 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. * DS18B20.cpp
  3. *
  4. * Created on: Feb 28, 2018
  5. * Author: sebastian
  6. */
  7. #define READ_SCRATCH 0xBE
  8. #define WRITE_SCRATCH 0x4E
  9. #define READ_ROM 0x33
  10. #define SKIP_ROM 0xCC
  11. #define CONVERT_T 0x44
  12. #include "DS18B20.h"
  13. /*
  14. * | -- 8 BIT CRC -- | -- 48 BIT SERIAL NUMBER -- | -- 8 BIT FAMILY CODE (28h) -- |
  15. */
  16. uint8_t ROM[8] = {0,0,0,0,0,0,0,0};
  17. double deviceID;
  18. /**
  19. * initialize the Temperatur Sensor
  20. * This lib is written for only ONE device on the 1-Wire Bus
  21. */
  22. void DS18B20_init(void){
  23. if(OW_reset()){
  24. logger_error("Unable to reset 1-Wire Bus, no Device present %s\n", strerror(errno));
  25. return;
  26. }
  27. DS18B20_readRom();
  28. deviceID = (double (ROM[6]) | ROM[5] << 8 | ROM[5] << 16 | ROM[5] << 24 | ROM[5] << 32 | ROM[5] << 40);
  29. if(ROM[7] == 0x28){
  30. logger(V_HAL, "Found Temperatur Sensor on 1-Wire Bus\n");
  31. logger(V_HAL, "Device ID: %12X", deviceID);
  32. }
  33. }
  34. void DS18B20_readRom(void){
  35. OW_writeByte(READ_ROM);
  36. uint8_t i;
  37. for(i = 8; i >= 0; i--){
  38. ROM[i] = OW_readByte();
  39. }
  40. }
  41. uint16_t DS18B20_readTemp (void){
  42. OW_writeByte(CONVERT_T);
  43. while(!OW_readBit()){
  44. //wait to finish conversion
  45. //TODO don't wait forever
  46. }
  47. OW_writeByte(READ_SCRATCH);
  48. uint8_t scratchPad[9];
  49. uint8_t i;
  50. for(i = 0; i < 9; i++){
  51. scratchPad[i] = OW_readByte();
  52. }
  53. }