ds1624.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /* Copyright (c) 2012 Nordic Semiconductor. All Rights Reserved.
  2. *
  3. * The information contained herein is property of Nordic Semiconductor ASA.
  4. * Terms and conditions of usage are described in detail in NORDIC
  5. * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
  6. *
  7. * Licensees are granted free, non-transferable use of the information. NO
  8. * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
  9. * the file.
  10. *
  11. */
  12. #include "ds1624.h"
  13. #include "twi_master.h"
  14. #include "nrf_delay.h"
  15. /*lint ++flb "Enter library region" */
  16. #define DS1634_BASE_ADDRESS 0x90 //!< 4 MSBs of the DS1624 TWI address
  17. #define DS1624_ONESHOT_MODE 0x01 //!< Bit in configuration register for 1-shot mode
  18. #define DS1624_CONVERSION_DONE 0x80 //!< Bit in configuration register to indicate completed temperature conversion
  19. static uint8_t m_device_address; //!< Device address in bits [7:1]
  20. const uint8_t command_access_memory = 0x17; //!< Reads or writes to 256-byte EEPROM memory
  21. const uint8_t command_access_config = 0xAC; //!< Reads or writes configuration data to configuration register
  22. const uint8_t command_read_temp = 0xAA; //!< Reads last converted temperature value from temperature register
  23. const uint8_t command_start_convert_temp = 0xEE; //!< Initiates temperature conversion.
  24. const uint8_t command_stop_convert_temp = 0x22; //!< Halts temperature conversion.
  25. /**
  26. * @brief Function for reading the current configuration of the sensor.
  27. *
  28. * @return uint8_t Zero if communication with the sensor failed. Contents (always non-zero) of configuration register (@ref DS1624_ONESHOT_MODE and @ref DS1624_CONVERSION_DONE) if communication succeeded.
  29. */
  30. static uint8_t ds1624_config_read(void)
  31. {
  32. uint8_t config = 0;
  33. // Write: command protocol
  34. if (twi_master_transfer(m_device_address, (uint8_t*)&command_access_config, 1, TWI_DONT_ISSUE_STOP))
  35. {
  36. if (twi_master_transfer(m_device_address | TWI_READ_BIT, &config, 1, TWI_ISSUE_STOP)) // Read: current configuration
  37. {
  38. // Read succeeded, configuration stored to variable "config"
  39. }
  40. else
  41. {
  42. // Read failed
  43. config = 0;
  44. }
  45. }
  46. return config;
  47. }
  48. bool ds1624_init(uint8_t device_address)
  49. {
  50. bool transfer_succeeded = true;
  51. m_device_address = DS1634_BASE_ADDRESS + (uint8_t)(device_address << 1);
  52. uint8_t config = ds1624_config_read();
  53. if (config != 0)
  54. {
  55. // Configure DS1624 for 1SHOT mode if not done so already.
  56. if (!(config & DS1624_ONESHOT_MODE))
  57. {
  58. uint8_t data_buffer[2];
  59. data_buffer[0] = command_access_config;
  60. data_buffer[1] = DS1624_ONESHOT_MODE;
  61. transfer_succeeded &= twi_master_transfer(m_device_address, data_buffer, 2, TWI_ISSUE_STOP);
  62. }
  63. }
  64. else
  65. {
  66. transfer_succeeded = false;
  67. }
  68. return transfer_succeeded;
  69. }
  70. bool ds1624_start_temp_conversion(void)
  71. {
  72. return twi_master_transfer(m_device_address, (uint8_t*)&command_start_convert_temp, 1, TWI_ISSUE_STOP);
  73. }
  74. bool ds1624_is_temp_conversion_done(void)
  75. {
  76. uint8_t config = ds1624_config_read();
  77. if (config & DS1624_CONVERSION_DONE)
  78. {
  79. return true;
  80. }
  81. else
  82. {
  83. return false;
  84. }
  85. }
  86. bool ds1624_temp_read(int8_t * temperature_in_celcius, int8_t * temperature_fraction)
  87. {
  88. bool transfer_succeeded = false;
  89. // Write: Begin read temperature command
  90. if (twi_master_transfer(m_device_address, (uint8_t*)&command_read_temp, 1, TWI_DONT_ISSUE_STOP))
  91. {
  92. uint8_t data_buffer[2];
  93. // Read: 2 temperature bytes to data_buffer
  94. if (twi_master_transfer(m_device_address | TWI_READ_BIT, data_buffer, 2, TWI_ISSUE_STOP))
  95. {
  96. *temperature_in_celcius = (int8_t)data_buffer[0];
  97. *temperature_fraction = (int8_t)data_buffer[1];
  98. transfer_succeeded = true;
  99. }
  100. }
  101. return transfer_succeeded;
  102. }
  103. /*lint --flb "Leave library region" */