123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- #include "ds1624.h"
- #include "twi_master.h"
- #include "nrf_delay.h"
- #define DS1634_BASE_ADDRESS 0x90
- #define DS1624_ONESHOT_MODE 0x01
- #define DS1624_CONVERSION_DONE 0x80
- static uint8_t m_device_address;
- const uint8_t command_access_memory = 0x17;
- const uint8_t command_access_config = 0xAC;
- const uint8_t command_read_temp = 0xAA;
- const uint8_t command_start_convert_temp = 0xEE;
- const uint8_t command_stop_convert_temp = 0x22;
- static uint8_t ds1624_config_read(void)
- {
- uint8_t config = 0;
-
- if (twi_master_transfer(m_device_address, (uint8_t*)&command_access_config, 1, TWI_DONT_ISSUE_STOP))
- {
- if (twi_master_transfer(m_device_address | TWI_READ_BIT, &config, 1, TWI_ISSUE_STOP))
- {
-
- }
- else
- {
-
- config = 0;
- }
- }
- return config;
- }
- bool ds1624_init(uint8_t device_address)
- {
- bool transfer_succeeded = true;
- m_device_address = DS1634_BASE_ADDRESS + (uint8_t)(device_address << 1);
- uint8_t config = ds1624_config_read();
- if (config != 0)
- {
-
- if (!(config & DS1624_ONESHOT_MODE))
- {
- uint8_t data_buffer[2];
- data_buffer[0] = command_access_config;
- data_buffer[1] = DS1624_ONESHOT_MODE;
- transfer_succeeded &= twi_master_transfer(m_device_address, data_buffer, 2, TWI_ISSUE_STOP);
- }
- }
- else
- {
- transfer_succeeded = false;
- }
- return transfer_succeeded;
- }
- bool ds1624_start_temp_conversion(void)
- {
- return twi_master_transfer(m_device_address, (uint8_t*)&command_start_convert_temp, 1, TWI_ISSUE_STOP);
- }
- bool ds1624_is_temp_conversion_done(void)
- {
- uint8_t config = ds1624_config_read();
- if (config & DS1624_CONVERSION_DONE)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- bool ds1624_temp_read(int8_t * temperature_in_celcius, int8_t * temperature_fraction)
- {
- bool transfer_succeeded = false;
-
- if (twi_master_transfer(m_device_address, (uint8_t*)&command_read_temp, 1, TWI_DONT_ISSUE_STOP))
- {
- uint8_t data_buffer[2];
-
- if (twi_master_transfer(m_device_address | TWI_READ_BIT, data_buffer, 2, TWI_ISSUE_STOP))
- {
- *temperature_in_celcius = (int8_t)data_buffer[0];
- *temperature_fraction = (int8_t)data_buffer[1];
- transfer_succeeded = true;
- }
- }
- return transfer_succeeded;
- }
|