Mbed library for LTC2601 / Mikroe DAC 2 Click peripheral.
LTC2601.cpp@0:11d039b90bde, 2020-08-25 (annotated)
- Committer:
- elelthvd
- Date:
- Tue Aug 25 14:26:48 2020 +0800
- Revision:
- 0:11d039b90bde
Add LTC2601 library for Mbed OS
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
elelthvd |
0:11d039b90bde | 1 | #include "LTC2601.h" |
elelthvd |
0:11d039b90bde | 2 | #include "mbed.h" |
elelthvd |
0:11d039b90bde | 3 | |
elelthvd |
0:11d039b90bde | 4 | /* Constructor */ |
elelthvd |
0:11d039b90bde | 5 | LTC2601::LTC2601 (SPI *spi, PinName cs, PinName rst) : _spi(spi), _cs(cs), _rst(rst) { |
elelthvd |
0:11d039b90bde | 6 | _cs = 1; // deselect |
elelthvd |
0:11d039b90bde | 7 | _rst = 1; // deselect |
elelthvd |
0:11d039b90bde | 8 | _spi->format(16, 0); // 16-bit word length |
elelthvd |
0:11d039b90bde | 9 | _spi->frequency(50*1000*1000); // 50 MHz spi clock |
elelthvd |
0:11d039b90bde | 10 | |
elelthvd |
0:11d039b90bde | 11 | }; |
elelthvd |
0:11d039b90bde | 12 | /* Set DAC voltage 0...5.0V */ |
elelthvd |
0:11d039b90bde | 13 | void LTC2601::updateVoltage(float voltage) { |
elelthvd |
0:11d039b90bde | 14 | uint16_t dac_value = (voltage/5.0) * 0xFFFF; |
elelthvd |
0:11d039b90bde | 15 | updateVoltage(dac_value); |
elelthvd |
0:11d039b90bde | 16 | } |
elelthvd |
0:11d039b90bde | 17 | /* Set DAC voltage 0...0xFFFF */ |
elelthvd |
0:11d039b90bde | 18 | void LTC2601::updateVoltage(uint16_t dac_value) { |
elelthvd |
0:11d039b90bde | 19 | writeRegister(WRITE, dac_value); |
elelthvd |
0:11d039b90bde | 20 | } |
elelthvd |
0:11d039b90bde | 21 | void LTC2601::powerDown() { |
elelthvd |
0:11d039b90bde | 22 | writeRegister(POWERDOWN, NULL); |
elelthvd |
0:11d039b90bde | 23 | } |
elelthvd |
0:11d039b90bde | 24 | /* |
elelthvd |
0:11d039b90bde | 25 | * DAC 2 Click only has no MOSI function |
elelthvd |
0:11d039b90bde | 26 | * |
elelthvd |
0:11d039b90bde | 27 | * Input First SPI write 16-bit: |
elelthvd |
0:11d039b90bde | 28 | * 8 bits of don't care |
elelthvd |
0:11d039b90bde | 29 | * 4 bits of command word (C3-C0) |
elelthvd |
0:11d039b90bde | 30 | * 4 bits if don't care |
elelthvd |
0:11d039b90bde | 31 | * |
elelthvd |
0:11d039b90bde | 32 | * Input Second SPI write 16-bit: |
elelthvd |
0:11d039b90bde | 33 | * 16 bits of data word (D15-D0) |
elelthvd |
0:11d039b90bde | 34 | * This data word encodes the desired DAC value between 0.0V and Vref. |
elelthvd |
0:11d039b90bde | 35 | * Vref on the Mikroe DAC2 Click equals 5.0V (jumper default). |
elelthvd |
0:11d039b90bde | 36 | * |
elelthvd |
0:11d039b90bde | 37 | */ |
elelthvd |
0:11d039b90bde | 38 | void LTC2601::writeRegister(CommandName command, uint16_t data) { |
elelthvd |
0:11d039b90bde | 39 | _cs = 0; // select; |
elelthvd |
0:11d039b90bde | 40 | _spi->write(command); |
elelthvd |
0:11d039b90bde | 41 | _spi->write(data); |
elelthvd |
0:11d039b90bde | 42 | _cs = 1; // deselect; |
elelthvd |
0:11d039b90bde | 43 | } |
elelthvd |
0:11d039b90bde | 44 | void LTC2601::hwReset() { |
elelthvd |
0:11d039b90bde | 45 | _rst = 1; // select |
elelthvd |
0:11d039b90bde | 46 | wait_us(10*1000); // wait some milliseconds (test only) |
elelthvd |
0:11d039b90bde | 47 | _rst = 1; // deselect |
elelthvd |
0:11d039b90bde | 48 | } |