ADT7310 temperature sensor driver
Revision 0:63996d24973b, committed 2016-11-07
- Comitter:
- adisuciu
- Date:
- Mon Nov 07 15:46:14 2016 +0000
- Commit message:
- Initial revision
Changed in this revision
ADT7310.cpp | Show annotated file Show diff for this revision Revisions of this file |
ADT7310.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r 000000000000 -r 63996d24973b ADT7310.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ADT7310.cpp Mon Nov 07 15:46:14 2016 +0000 @@ -0,0 +1,75 @@ +#include "ADT7310.h" + + +ADT7310::ADT7310(PinName CS, PinName MOSI, PinName MISO, PinName SCK) :/* miso(MISO),*/ adt7310(MOSI, MISO, SCK), cs(CS) +{ + cs = true; + adt7310.format(8, SPI_MODE); +} + +void ADT7310::reset() +{ + cs = false; + adt7310.write(0xff); + adt7310.write(0xff); + adt7310.write(0xff); + adt7310.write(0xff); + cs = true; +} +uint8_t ADT7310::read_status() +{ + uint8_t spi_data[2] = {ADT7310_READ | (ADT7310_STATUS << 3), ADT7310_DUMMY}; + spi_read(spi_data, 2); + return spi_data[1]; +} + +void ADT7310::start_single_conversion() +{ + uint8_t spi_data[2] = {ADT7310_READ | (ADT7310_CONFIG << 3), ADT7310_DUMMY}; + spi_read(spi_data, 2); + spi_data[0] = ADT7310_WRITE | (ADT7310_CONFIG << 3); + spi_data[1] = (spi_data[1] & (~(0x60))) | (0x20); // Oneshot conversion + spi_write(spi_data, 2); +} +void ADT7310::write_config(uint8_t data) +{ + uint8_t spi_data[2] = {ADT7310_WRITE | (ADT7310_CONFIG << 3), data}; + spi_write(spi_data, 2); +} + +uint8_t ADT7310::read_config() +{ + uint8_t spi_data[2] = {ADT7310_READ | (ADT7310_CONFIG << 3), ADT7310_DUMMY}; + spi_read(spi_data, 2); + return spi_data[1]; +} +uint16_t ADT7310::read_temp() +{ + uint8_t spi_data[3] = {ADT7310_READ | (ADT7310_TEMP << 3), ADT7310_DUMMY, ADT7310_DUMMY }; + spi_read(spi_data, 3); + return ((static_cast<uint16_t>(spi_data[1]) << 8) | spi_data[2]); +} +void ADT7310::write_temp_setpoint(uint8_t setpoint, uint16_t data) +{ + uint8_t spi_data[3] = {ADT7310_WRITE | (setpoint << 3), ((data & 0xff00) >> 8), data & 0xff }; + spi_write(spi_data, 3); +} + +void ADT7310::spi_write(uint8_t *data, uint8_t size) +{ + cs = false; + uint8_t i; + for(i = 0; i < size; i++) + adt7310.write(data[i]); + cs = true; + +} +void ADT7310::spi_read(uint8_t *data, uint8_t size) +{ + cs = false; + uint8_t i; + for(i = 0; i < size; i++) + data[i] = adt7310.write(data[i]); + + cs = true; +}
diff -r 000000000000 -r 63996d24973b ADT7310.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ADT7310.h Mon Nov 07 15:46:14 2016 +0000 @@ -0,0 +1,87 @@ + +#ifndef ADT7310_H +#define ADT7310_H + +#include "mbed.h" + +/** + * @brief Analog Devices ADT7310 temperature sensor + */ +class ADT7310 +{ +public: + + +#define ADT7310_READ (1<<6) +#define ADT7310_WRITE (0) +#define ADT7310_DUMMY (0) +#define ADT7310_STATUS (0) +#define ADT7310_CONFIG (1) +#define ADT7310_TEMP (2) +#define ADT7310_ID (3) +#define ADT7310_TCRIT (4) +#define ADT7310_THYST (5) +#define ADT7310_THIGH (6) +#define ADT7310_TLOW (7) + + + /** + * @brief ADT7310 class + * @param CS - chipselect pin + * @param MOSI - MOSI pin + * @param MISO - MISO pin + * @param SCK - Clock pin + */ + ADT7310(PinName CS = SPI_CS, PinName MOSI = SPI_MOSI, PinName MISO = SPI_MISO, PinName SCK = SPI_SCK); + + /** + * @brief resets the ADT7310 + */ + void reset(); + + /** + * @brief reads status register of the temperature sensor + * @return value of the status register + */ + uint8_t read_status(); + + /** + * @brief writes configuration register of the temperature sensor + * @param data - data to be written + */ + void write_config(uint8_t data); + + /** + * @brief issues a conversion to the temperature sensor + */ + void start_single_conversion(); + + /** + * @brief reads configuration register + * @return configuration register value + */ + uint8_t read_config(); + + /** + * @brief reads the temperature + * @return temperature + */ + uint16_t read_temp(); + + /** + * @brief writes temperature setpoints + * @param setpoint - setpoint register + * @param data - data to be written to the setpoint register + */ + void write_temp_setpoint(uint8_t setpoint, uint16_t data); + + void spi_write(uint8_t *data, uint8_t size); + void spi_read(uint8_t *data, uint8_t size); + //DigitalIn miso; + DigitalOut cs; + SPI adt7310; +private: + const uint8_t SPI_MODE = 0x03; +}; + +#endif