Mbed library for LTC2601 / Mikroe DAC 2 Click peripheral.
Revision 0:11d039b90bde, committed 2020-08-25
- Comitter:
- elelthvd
- Date:
- Tue Aug 25 14:26:48 2020 +0800
- Commit message:
- Add LTC2601 library for Mbed OS
Changed in this revision
LTC2601.cpp | Show annotated file Show diff for this revision Revisions of this file |
LTC2601.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r 000000000000 -r 11d039b90bde LTC2601.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LTC2601.cpp Tue Aug 25 14:26:48 2020 +0800 @@ -0,0 +1,48 @@ +#include "LTC2601.h" +#include "mbed.h" + +/* Constructor */ +LTC2601::LTC2601 (SPI *spi, PinName cs, PinName rst) : _spi(spi), _cs(cs), _rst(rst) { + _cs = 1; // deselect + _rst = 1; // deselect + _spi->format(16, 0); // 16-bit word length + _spi->frequency(50*1000*1000); // 50 MHz spi clock + +}; +/* Set DAC voltage 0...5.0V */ +void LTC2601::updateVoltage(float voltage) { + uint16_t dac_value = (voltage/5.0) * 0xFFFF; + updateVoltage(dac_value); +} +/* Set DAC voltage 0...0xFFFF */ +void LTC2601::updateVoltage(uint16_t dac_value) { + writeRegister(WRITE, dac_value); +} +void LTC2601::powerDown() { + writeRegister(POWERDOWN, NULL); +} +/* +* DAC 2 Click only has no MOSI function +* +* Input First SPI write 16-bit: +* 8 bits of don't care +* 4 bits of command word (C3-C0) +* 4 bits if don't care +* +* Input Second SPI write 16-bit: +* 16 bits of data word (D15-D0) +* This data word encodes the desired DAC value between 0.0V and Vref. +* Vref on the Mikroe DAC2 Click equals 5.0V (jumper default). +* +*/ +void LTC2601::writeRegister(CommandName command, uint16_t data) { + _cs = 0; // select; + _spi->write(command); + _spi->write(data); + _cs = 1; // deselect; +} +void LTC2601::hwReset() { + _rst = 1; // select + wait_us(10*1000); // wait some milliseconds (test only) + _rst = 1; // deselect +}
diff -r 000000000000 -r 11d039b90bde LTC2601.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LTC2601.h Tue Aug 25 14:26:48 2020 +0800 @@ -0,0 +1,57 @@ + +#ifndef _LTC2601_H +#define _LTC2601_H +#include "mbed.h" + +class LTC2601 { +public: + /** Commands (Table 1): + * C3 C2 C1 C0 + * 0 0 0 0 Write to Input Register + * 0 0 0 1 Update (Power Up) DAC Register + * 0 0 1 1 Write to and Update (Power Up) + * 0 1 0 0 Power Down (and ignores data word) + * 1 1 1 1 No Operation (ignores data word) + * The first three commands all seem to have the same effect (update and power up). Commands not shown seem ignored. + * + */ + enum CommandName { + WRITE = (0b0000<<4), + UPDATE = (0b0001<<4), + WRITE_UPDATE = (0b0011<<4), + POWERDOWN = (0b0100<<4), + NOOPERATION = (0b1111<<4), + } ; + /* Constructor */ + LTC2601 (SPI *spi, PinName cs, PinName rst); + + /* Set DAC voltage 0...5.0V */ + void updateVoltage(float voltage); + /* Set DAC voltage 0...0xFFFF */ + void updateVoltage(uint16_t dac_value); + void powerDown(); + /* + * DAC 2 Click only has no MOSI function + * + * Input First SPI write 16-bit: + * 8 bits of don't care + * 4 bits of command word (C3-C0) + * 4 bits if don't care + * + * Input Second SPI write 16-bit: + * 16 bits of data word (D15-D0) + * This data word encodes the desired DAC value between 0.0V and Vref. + * Vref on the Mikroe DAC2 Click equals 5.0V (jumper default). + * + */ + void writeRegister(CommandName command, uint16_t data); + void hwReset(); +protected: +private: + SPI *_spi; + DigitalOut _cs; + DigitalOut _rst; +}; + + +#endif // _LTC2601_H \ No newline at end of file