Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
ad5668.h
00001 /** 00002 * @file AD5668.h 00003 * 00004 * @author Jon Buckman 00005 * 00006 * @section LICENSE 00007 * 00008 * Copyright (c) 2017 Jon Buckman 00009 * 00010 * This program is free software: you can redistribute it and/or modify 00011 * it under the terms of the GNU General Public License as published by 00012 * the Free Software Foundation, either version 3 of the License, or 00013 * (at your option) any later version. 00014 * 00015 * This program is distributed in the hope that it will be useful, 00016 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00018 * GNU General Public License for more details. 00019 * 00020 * You should have received a copy of the GNU General Public License 00021 * along with this program. If not, see <http://www.gnu.org/licenses/>. 00022 * 00023 * The above copyright notice and this permission notice shall be included in 00024 * all copies or substantial portions of the Software. 00025 * 00026 * @section DESCRIPTION 00027 * 00028 * AD5668 Digital to Analog Converter from Analog Devices. 00029 * 00030 * Datasheet: 00031 * 00032 * http://www.analog.com/media/en/technical-documentation/data-sheets/AD5668.pdf 00033 * 00034 * Tie LDAC and CLR pins to VDD if not being used. 00035 * 00036 * Example: 00037 * @code 00038 * #include "mbed.h" 00039 * #include "AD5668.h" 00040 * 00041 * Serial pc(USBTX, USBRX); // tx, rx 00042 * 00043 * AD5668 dac(D11, D12, D13, D10); // mosi, miso, sck, cs 00044 * 00045 * int main() { 00046 * while (1) { 00047 * for (int i = 0; i < 65536; i+=10) { 00048 * dac.AD5668_WRITE_INPUT_N_UPDATE_N(AD5668_ADDR_DAC_A, i); 00049 * pc.printf("%d,", i); 00050 * wait(.01); 00051 * } 00052 * } 00053 * } 00054 * @endcode 00055 */ 00056 00057 #ifndef __AD5668_H__ 00058 #define __AD5668_H__ 00059 00060 /** 00061 * Includes 00062 */ 00063 #include "mbed.h" 00064 00065 /** 00066 * Defines 00067 */ 00068 /* AD5668 Input Register */ 00069 #define AD5668_CMD(x) ((0x0000000F & (unsigned int)(x)) << 24) 00070 #define AD5668_ADDR(x) ((0x0000000F & (unsigned int)(x)) << 20) 00071 #define AD5628_DATA_BITS(x) ((0x000FFFFF & (unsigned int)(x)) << 8) 00072 #define AD5648_DATA_BITS(x) ((0x000FFFFF & (unsigned int)(x)) << 6) 00073 #define AD5668_DATA_BITS(x) ((0x000FFFFF & (unsigned int)(x)) << 4) 00074 00075 /* Command Definitions (AD5668_COMMAND(x) options) */ 00076 #define AD5668_CMD_WRITE_INPUT_N 0 // Write to Input Register n. 00077 #define AD5668_CMD_UPDATE_DAC_N 1 // Update DAC Register n. 00078 #define AD5668_CMD_WRITE_INPUT_N_UPDATE_ALL 2 // Write to Input Register n, update all. 00079 #define AD5668_CMD_WRITE_INPUT_N_UPDATE_N 3 // Write to and update DAC Channel n. 00080 #define AD5668_CMD_POWERDOWN 4 // Power down/power up DAC. 00081 #define AD5668_CMD_LOAD_CLEAR_CODE 5 // Load clear code register. 00082 #define AD5668_CMD_LOAD_LDAC_REG 6 // Load LDAC register. 00083 #define AD5668_CMD_RESET 7 // Reset (power-on reset) 00084 #define AD5668_CMD_SET_INT_REF 8 // Set up internal REF register. 00085 00086 /* Address Commands (AD5668_ADDRESS(x) options) */ 00087 #define AD5668_ADDR_DAC_A 0x00 //DAC A 00088 #define AD5668_ADDR_DAC_B 0x01 //DAC B 00089 #define AD5668_ADDR_DAC_C 0x02 //DAC C 00090 #define AD5668_ADDR_DAC_D 0x03 //DAC D 00091 #define AD5668_ADDR_DAC_E 0x04 //DAC E 00092 #define AD5668_ADDR_DAC_F 0x05 //DAC F 00093 #define AD5668_ADDR_DAC_G 0x06 //DAC G 00094 #define AD5668_ADDR_DAC_H 0x07 //DAC H 00095 #define AD5668_ADDR_DAC_ALL 0x0F //All DACs 00096 00097 /* Internal Reference Register */ 00098 #define AD5668_INT_REF_OFF 0 00099 #define AD5668_INT_REF_ON 1 00100 00101 /* Power-Down Modes of Operation */ 00102 #define AD5668_POWER_MODE(x) ((0x03 & (unsigned short) (x)) << 8) 00103 00104 #define AD5668_PWRDN_NONE 0 // Normal operation 00105 #define AD5668_PWRDN_1K 1 // 1 KOhm to GND (Power-down mode) 00106 #define AD5668_PWRDN_100K 2 // 100 KOhm to GND (Power-down mode) 00107 #define AD5668_PWRDN_3STATE 3 // Three-state (Power-down mode) 00108 00109 /* Clear Code Function */ 00110 #define AD5668_CODE_0X0000 0 00111 #define AD5668_CODE_0X8000 1 00112 #define AD5668_CODE_0XFFFF 2 00113 #define AD5668_CODE_NOP 3 00114 00115 /* SPI */ 00116 #define AD5668_SPI_MAX_DATA_RATE 50000000 00117 00118 /** 00119 * AD5668 Digital to Analog Converter from Analog Devices. 00120 */ 00121 class AD5668 { 00122 00123 public: 00124 00125 /** 00126 * Constructor. 00127 * 00128 * @param mosi mbed pin to use for MOSI line of SPI interface. 00129 * @param miso mbed pin to use for MISO line of SPI interface. 00130 * @param sck mbed pin to use for SCK line of SPI interface. 00131 * @param csn mbed pin to use for not chip select line of SPI interface. 00132 */ 00133 AD5668(PinName mosi, PinName miso, PinName sck, PinName csn); 00134 00135 /** 00136 * Set power mode for each channnel. 00137 * 00138 * @param pwrMode power mode of the device. 00139 * @param channel the channel or channels that are being configured. 00140 */ 00141 void AD5668_PowerMode(unsigned char pwrMode, unsigned char channel); 00142 00143 /** 00144 * Reset the device. 00145 * 00146 * @param none. 00147 */ 00148 void AD5668_Reset(void); 00149 00150 /** 00151 * Write a data word to a channel. 00152 * 00153 * @param channel the channel or channels that are being configured. 00154 * @param data the DAC data word. 00155 */ 00156 void AD5668_WriteInput(uint8_t channel, uint16_t data); 00157 00158 /** 00159 * Latch a data word to a channel. 00160 * 00161 * @param channel the channel or channels that are being configured. 00162 * @param data the DAC data word. 00163 */ 00164 void AD5668_UpdateDAC(uint8_t channel, uint16_t data); 00165 00166 /** 00167 * Write a data word to a channel and latch all channels. 00168 * 00169 * @param channel the channel or channels that are being configured. 00170 * @param data the DAC data word. 00171 */ 00172 void AD5668_WriteInputUpdateAll(uint8_t channel, uint16_t data); 00173 00174 /** 00175 * Write a data word to a channel and latch the same channel. 00176 * 00177 * @param channel the channel or channels that are being configured. 00178 * @param data the DAC data word. 00179 */ 00180 void AD5668_WriteInputUpdate(uint8_t channel, uint16_t data); 00181 00182 /** 00183 * Set the internal voltage reference on or off. 00184 * 00185 * @param val set internal voltage reference. 00186 */ 00187 void AD5668_InternalReference(uint8_t val); 00188 00189 /** 00190 * Set the internal voltage reference on or off. 00191 * 00192 * @param val clear mode of the device. 00193 */ 00194 void AD5668_ClearCode(uint8_t val); 00195 00196 private: 00197 00198 /** 00199 * Writes a 32-bit data-word to the Input Register of the device. 00200 * 00201 * @param registerValue value of the register. 00202 */ 00203 void AD5668_SetInputRegister(unsigned int registerValue); 00204 00205 /** 00206 * Write array values to the SPI. 00207 * 00208 * @param reg pointer to the array 00209 */ 00210 void SPI_Write(unsigned char *reg); 00211 00212 SPI spi_; 00213 DigitalOut nCS_; 00214 00215 }; 00216 00217 #endif /* __AD5668_H__ */
Generated on Wed Jul 20 2022 09:43:23 by
