Debian Codegraph @

thirdparty/analogdevicesinc/no-OS/device_drivers/AD5755/ad5755.c

raw symbols SCIP
1 /***************************************************************************//**
2 * @file AD5755.c
3 * @brief Implementation of AD5755 Driver.
4 * @author Istvan Csomortani ([email protected])
5 ********************************************************************************
6 * Copyright 2012(c) Analog Devices, Inc.
7 *
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions are met:
12 * - Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * - Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
17 * distribution.
18 * - Neither the name of Analog Devices, Inc. nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
21 * - The use of this software may or may not infringe the patent rights
22 * of one or more patent holders. This license does not release you
23 * from the requirement that you obtain separate licenses from these
24 * patent holders to use this software.
25 * - Use of the software either in source or binary form, must be run
26 * on or directly connected to an Analog Devices Inc. component.
27 *
28 * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
29 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
30 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
31 * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33 * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
34 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
35 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
36 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 *
39 *******************************************************************************/
40
41 /******************************************************************************/
42 /***************************** Include Files **********************************/
43 /******************************************************************************/
44 #include "AD5755.h" // AD5755 definitions.
45 #include "AD5755_cfg.h" // AD5755_cfg definitions.
46
47 AD5755_type_t this_device;
48
49 /******************************************************************************/
50 /************************ Functions Definitions *******************************/
51 /******************************************************************************/
52
53 /***************************************************************************//**
54 * @brief Initializes the device and powers-up all channels. The device is
55 * initialized with the values held by AD5755_InitialSettings structure.
56 *
57 * @return status - Result of the initialization procedure.
58 * Example: -1 - SPI peripheral was not initialized.
59 * 0 - SPI peripheral is initialized.
60 *******************************************************************************/
61 char AD5755_Init(AD5755_type_t device)
62 {
63 unsigned char status = -1;
64 unsigned char channel = 0;
65 AD5755_Setup *pAD5755_st = &AD5755_st;
66 unsigned short dacControlBuff[4] = {0, 0, 0, 0};
67
68 this_device = device;
69
70 /* GPIO configuration. */
71 AD5755_LDAC_OUT;
72 AD5755_LDAC_LOW;
73 AD5755_RESET_OUT;
74 AD5755_RESET_HIGH;
75 AD5755_CLEAR_OUT;
76 AD5755_CLEAR_LOW;
77 AD5755_POC_OUT;
78 AD5755_POC_LOW;
79
80 status = SPI_Init(0, // Transfer format.
81 8000000, // SPI clock frequency.
82 1, // SPI clock polarity.
83 1); // SPI clock edge.
84 /* Device Setup. */
85 /* Configure the POC bit, STATREAD bit and ShtCcLim bit. */
86 AD5755_SetControlRegisters(AD5755_CREG_MAIN,
87 0,
88 (pAD5755_st->pocBit * AD5755_MAIN_POC) |
89 (pAD5755_st->statReadBit * AD5755_MAIN_STATREAD) |
90 AD5755_MAIN_SHTCCTLIM(pAD5755_st->shtCcLimBit));
91
92 AD5755_Software_Reset();
93 TIME_DelayUs(100);
94 /* DC-to-DC configuration. */
95 AD5755_SetControlRegisters(AD5755_CREG_DC_DC,
96 0,
97 (pAD5755_st->dcDcCompBit * AD5755_DC_DC_COMP) |
98 (AD5755_DC_DC_FREQ(pAD5755_st->dcDcFreqBit)) |
99 (AD5755_DC_DC_PHASE(pAD5755_st->dcDcPhaseBit)) |
100 (AD5755_DC_DC_MAX_V(pAD5755_st->dcDcMaxVBit)));
101 /* Configure the DAC control register on a per channel basis. */
102 for(channel = AD5755_DAC_A; channel <= AD5755_DAC_D; channel++)
103 {
104 if((this_device == ID_AD5755) || (this_device == ID_AD5755_1))
105 {
106 dacControlBuff[channel] = AD5755_DAC_INT_ENABLE |
107 AD5755_DAC_CLR_EN |
108 pAD5755_st->rsetBits[channel] |
109 AD5755_DAC_DC_DC |
110 pAD5755_st->ovrngBits[channel] |
111 AD5755_DAC_R(AD5755_R_0_5_V);
112 }
113 else
114 {
115 dacControlBuff[channel] = AD5755_DAC_INT_ENABLE |
116 AD5755_DAC_CLR_EN |
117 pAD5755_st->rsetBits[channel] |
118 AD5755_DAC_DC_DC |
119 pAD5755_st->ovrngBits[channel] |
120 AD5755_DAC_R(AD5755_R_4_20_MA);
121 }
122 AD5755_SetControlRegisters(AD5755_CREG_DAC,
123 channel,
124 dacControlBuff[channel]);
125 }
126 /* Allow at least 200us before enabling the channel output. */
127 TIME_DelayUs(200);
128 /* Enable the channel output. */
129 for(channel = AD5755_DAC_A; channel <= AD5755_DAC_D; channel++)
130 {
131 /* Write to each DAC data register*/
132 AD5755_SetRegisterValue(AD5755_DREG_WR_DAC,
133 channel,
134 0x0000);
135 AD5755_SetControlRegisters(AD5755_CREG_DAC,
136 channel,
137 dacControlBuff[channel] | AD5755_DAC_OUTEN);
138 }
139 return status;
140 }
141
142 /***************************************************************************//**
143 * @brief Reads the value of a register.
144 *
145 * @param registerAddress - Address of the register.
146 * Example:
147 * AD5755_RD_DATA_REG(x)
148 * AD5755_RD_CTRL_REG(x)
149 * AD5755_RD_GAIN_REG(x)
150 * AD5755_RD_OFFSET_REG(x)
151 * AD5755_RD_CODE_REG(x)
152 * AD5755_RD_SR_CTRL_REG(x)
153 * AD5755_RD_STATUS_REG
154 * AD5755_RD_MAIN_CTRL_REG
155 * AD5755_RD_Dc_DC_CTRL_REG
156 * x = any of AD5755_DAC_A, .. AD5755_DAC_D
157 *
158 * @return regValue - Value of the register.
159 *******************************************************************************/
160 long AD5755_GetRegisterValue(unsigned char registerAddress)
161 {
162 unsigned char buffer[4] = {0, 0, 0, 0};
163 unsigned long command = 0;
164 long regValue = 0;
165 unsigned char crc = 0;
166
167 command = AD5755_ISR_READ |
168 AD5755_ISR_DUT_AD1(AD5755_st.pinAD1state) |
169 AD5755_ISR_DUT_AD0(AD5755_st.pinAD0state) |
170 AD5755_ISR_RD(registerAddress);
171 buffer[0] = (command & 0xFF0000) >> 16;
172 buffer[1] = (command & 0x00FF00) >> 8;
173 buffer[2] = (command & 0x0000FF) >> 0;
174 if(AD5755_st.enablePacketErrorCheck)
175 {
176 buffer[3] = AD5755_CheckCrc(buffer, 3);
177 }
178 SPI_Write(AD5755_SLAVE_ID,
179 buffer,
180 3 + AD5755_st.enablePacketErrorCheck);
181 command = AD5755_ISR_WRITE |
182 AD5755_ISR_DUT_AD1(AD5755_st.pinAD1state) |
183 AD5755_ISR_DUT_AD0(AD5755_st.pinAD0state) |
184 AD5755_ISR_NOP;
185 buffer[0] = (command & 0xFF0000) >> 16;
186 buffer[1] = (command & 0x00FF00) >> 8;
187 buffer[2] = (command & 0x0000FF) >> 0;
188 if(AD5755_st.enablePacketErrorCheck)
189 {
190 buffer[3] = AD5755_CheckCrc(buffer, 3);
191 }
192 SPI_Read(AD5755_SLAVE_ID,
193 buffer,
194 3 + AD5755_st.enablePacketErrorCheck);
195 regValue = ((unsigned short)buffer[1] << 8) + buffer[2];
196 /* Check the CRC. */
197 if(AD5755_st.enablePacketErrorCheck)
198 {
199 crc = AD5755_CheckCrc(&buffer[1], 3);
200 if(crc != AD5755_CRC_CHECK_CODE)
201 {
202 regValue = -1;
203 }
204 }
205 return regValue;
206 }
207
208 /***************************************************************************//**
209 * @brief Writes data into a register.
210 *
211 * @param registerAddress - Address of the register.
212 * Example:
213 * AD5755_DREG_WR_DAC
214 * AD5755_DREG_WR_GAIN
215 * AD5755_DREG_WR_GAIN_ALL
216 * AD5755_DREG_WR_OFFSET
217 * AD5755_DREG_WR_OFFSET_ALL
218 * AD5755_DREG_WR_CLR_CODE
219 * AD5755_DREG_WR_CTRL_REG
220 *
221 * @param channel - Channel option.
222 * Example: AD5755_DAC_A
223 * AD5755_DAC_B
224 * AD5755_DAC_C
225 * AD5755_DAC_D
226 * @param registerValue - Data value to write.
227 *
228 * @return None.
229 *******************************************************************************/
230 unsigned short AD5755_SetRegisterValue(unsigned char registerAddress,
231 unsigned char channel,
232 unsigned short registerValue)
233 {
234 unsigned char buff[4] = {0, 0, 0, 0};
235 unsigned long command = 0;
236 unsigned short statusReg = 0;
237
238 command = AD5755_ISR_WRITE |
239 AD5755_ISR_DUT_AD1(AD5755_st.pinAD1state) |
240 AD5755_ISR_DUT_AD0(AD5755_st.pinAD0state) |
241 AD5755_ISR_DREG(registerAddress) |
242 AD5755_ISR_DAC_AD(channel)|
243 AD5755_ISR_DATA(registerValue);
244 buff[0] = (command & 0xFF0000) >> 16;
245 buff[1] = (command & 0x00FF00) >> 8;
246 buff[2] = (command & 0x0000FF) >> 0;
247 if(AD5755_st.enablePacketErrorCheck)
248 {
249 buff[3] = AD5755_CheckCrc(buff, 3);
250 }
251 if(AD5755_st.statReadBit == 0)
252 {
253 SPI_Write(AD5755_SLAVE_ID,
254 buff,
255 3 + AD5755_st.enablePacketErrorCheck);
256 }
257 else
258 {
259 SPI_Read(AD5755_SLAVE_ID,
260 buff,
261 3 + AD5755_st.enablePacketErrorCheck);
262 statusReg = (buff[1] << 8) + buff[2];
263 }
264
265 return statusReg;
266 }
267
268
269 /***************************************************************************//**
270 * @brief Performs a software reset to the device.
271 *
272 * @return None.
273 *******************************************************************************/
274 void AD5755_Software_Reset(void)
275 {
276 AD5755_SetControlRegisters(AD5755_CREG_SOFT, 0, AD5755_RESET_CODE);
277 }
278
279 /***************************************************************************//**
280 * @brief Enables/Disables watchdog timer and sets the timeout period.
281 *
282 * @param wtdEnable - Watchdog enable option.
283 * Example: 0 - disables watchdog timer
284 * 1 - enabled watchdog timer
285 * @param timeout - Timeout period for the watchdog timer.
286 * Example: AD5755_WD_5MS
287 * AD5755_WD_10MS
288 * AD5755_WD_100MS
289 * AD5755_WD_200MS
290 *
291 * @return None.
292 *******************************************************************************/
293 void AD5755_WatchDogSetup(unsigned char wtdEnable, unsigned char timeout)
294 {
295 unsigned long oldMainCtrlReg = 0;
296 unsigned long newMainCtrlReg = 0;
297
298 oldMainCtrlReg = AD5755_GetRegisterValue(AD5755_RD_MAIN_CTRL_REG);
299 oldMainCtrlReg &= ~(AD5755_MAIN_EWD | AD5755_MAIN_WD(3));
300 newMainCtrlReg = oldMainCtrlReg |
301 (AD5755_MAIN_EWD * wtdEnable) |
302 AD5755_MAIN_WD(timeout);
303 AD5755_SetControlRegisters(AD5755_CREG_MAIN,
304 0,
305 newMainCtrlReg);
306 }
307
308 /***************************************************************************//**
309 * @brief Write a "service pulse" to the AD5755 watchdog timer when enabled.
310 *
311 * @return None.
312 *******************************************************************************/
313 void AD5755_FeedWatchDogTimer(void)
314 {
315 AD5755_SetControlRegisters(AD5755_CREG_SOFT, 0, AD5755_SPI_CODE);
316 }
317
318 /***************************************************************************//**
319 * @brief Configures one of the control registers.
320 *
321 * @param ctrlRegAddress - Control Register Address.
322 * Example:
323 * AD5755_CREG_SLEW
324 * AD5755_CREG_MAIN
325 * AD5755_CREG_DAC
326 * AD5755_CREG_DC_DC
327 * AD5755_CREG_SOFT
328 *
329 * @param channel - Channel option.
330 * Example: AD5755_DAC_A
331 * AD5755_DAC_B
332 * AD5755_DAC_C
333 * AD5755_DAC_D
334 * @param regValue - Value to be written to the selected Control Register.
335 *
336 * @return None.
337 *******************************************************************************/
338 void AD5755_SetControlRegisters(unsigned char ctrlRegAddress,
339 unsigned char channel,
340 unsigned short regValue)
341 {
342 AD5755_SetRegisterValue(AD5755_DREG_WR_CTRL_REG,
343 channel,
344 AD5755_CTRL_CREG(ctrlRegAddress) | regValue);
345 }
346
347 /***************************************************************************//**
348 * @brief Computes the CRC for a data buffer.
349 *
350 * @param data - Data buffer.
351 * @param bytesNumber - Data buffer size in bytes.
352 *
353 * @return The computed CRC.
354 *******************************************************************************/
355 unsigned char AD5755_CheckCrc(unsigned char* data, unsigned char bytesNumber)
356 {
357 unsigned char crc = 0x00;
358 unsigned char byte = 0;
359 unsigned char bit = 0;
360
361 /* Calculates 8-Bit checksum with given polynomial. */
362 for(byte = 0; byte < bytesNumber; byte++)
363 {
364 crc ^= (data[byte]);
365 for(bit = 8; bit > 0; bit--)
366 {
367 if (crc & 0x80)
368 {
369 crc = (crc << 1) ^ AD5755_CRC_POLYNOMIAL;
370 }
371 else
372 {
373 crc = (crc << 1);
374 }
375 }
376 }
377
378 return crc;
379 }
380
381 /***************************************************************************//**
382 * @brief Allows power-up/down of the dc-to-dc converter, DAC and internal
383 * amplifiers for the selected channel.
384 *
385 * @param channel - Channel option.
386 * Example: AD5755_DAC_A
387 * AD5755_DAC_B
388 * AD5755_DAC_C
389 * AD5755_DAC_D
390 * @param pwrStatus - Power mode.
391 * Example: 0 - power-down the channel;
392 * 1 - power-up the channel.
393 *
394 * @return None.
395 *******************************************************************************/
396 void AD5755_SetChannelPower(unsigned char channel, unsigned char pwrStatus)
397 {
398 unsigned long oldDacCtrlReg = 0;
399 unsigned long newDacCtrlReg = 0;
400
401 /* Read the content of the DAC Control Register of the selected channel. */
402 oldDacCtrlReg = AD5755_GetRegisterValue(AD5755_RD_CTRL_REG(channel));
403 oldDacCtrlReg &= ~(AD5755_DAC_INT_ENABLE |
404 AD5755_DAC_DC_DC |
405 AD5755_DAC_OUTEN);
406 newDacCtrlReg = oldDacCtrlReg | (pwrStatus * (AD5755_DAC_INT_ENABLE |
407 AD5755_DAC_DC_DC |
408 AD5755_DAC_OUTEN));
409 AD5755_SetControlRegisters(AD5755_CREG_DAC,
410 AD5755_DAC_A,
411 newDacCtrlReg);
412 }
413
414 /***************************************************************************//**
415 * @brief Sets the range of a channel.
416 *
417 * @param channel - Channel option.
418 * Example: AD5755_DAC_A
419 * AD5755_DAC_B
420 * AD5755_DAC_C
421 * AD5755_DAC_D
422 * @param range - Range option.
423 * Example:
424 * AD5755_R_0_5_V - 0 V to 5 V voltage range (default)
425 * AD5755_R_0_10_V - 0 V to 10 V voltage range
426 * AD5755_R_M5_P5_V - -5 V to +5 V voltage range
427 * AD5755_R_M10_P10_V - -10 V to 10 V voltage range
428 * AD5755_R_4_20_MA - 4 mA to 20 mA current range
429 * AD5755_R_0_20_MA - 0 mA to 20 mA current range
430 * AD5755_R_0_24_MA - 0 mA to 24 mA current range
431 *
432 * @return None.
433 *******************************************************************************/
434 void AD5755_SetChannelRange(unsigned char channel, unsigned char range)
435 {
436 unsigned short outputCode = 0x0000;
437 unsigned long oldDacCtrlReg = 0;
438 unsigned long newDacCtrlReg = 0;
439
440 /* Read the content of the DAC Control Register of the selected channel. */
441 oldDacCtrlReg = AD5755_GetRegisterValue(AD5755_RD_CTRL_REG(channel));
442 /* Clear the bits that will be modified by this function. */
443 oldDacCtrlReg &= ~(AD5755_DAC_INT_ENABLE |
444 AD5755_DAC_OUTEN |
445 AD5755_DAC_DC_DC |
446 AD5755_DAC_R(7));
447 /* Select the output code before changing the range. */
448 if((range == AD5755_R_M5_P5_V) || (range == AD5755_R_M10_P10_V))
449 {
450 outputCode = 0x8000;
451 }
452 /* Set the output code to zero or midscale. */
453 AD5755_SetRegisterValue(AD5755_DREG_WR_DAC, channel, outputCode);
454 /* Set range. */
455 newDacCtrlReg = oldDacCtrlReg |
456 AD5755_DAC_INT_ENABLE |
457 AD5755_DAC_DC_DC |
458 AD5755_DAC_R(range);
459 AD5755_SetControlRegisters(AD5755_CREG_DAC, channel, newDacCtrlReg);
460 /* Set the output code to zero or midscale. */
461 AD5755_SetRegisterValue(AD5755_DREG_WR_DAC, channel, outputCode);
462 TIME_DelayUs(200);
463 /* Enable the output of the channel. */
464 newDacCtrlReg |= AD5755_DAC_OUTEN;
465 AD5755_SetControlRegisters(AD5755_CREG_DAC, channel, newDacCtrlReg);
466 }
467
468 /***************************************************************************//**
469 * @brief Selects if the channel clears when CLEAR pin is activated.
470 *
471 * @param channel - Channel option.
472 * Example: AD5755_DAC_A
473 * AD5755_DAC_B
474 * AD5755_DAC_C
475 * AD5755_DAC_D
476 * @param clearEn - Clear Enable option.
477 * Example:
478 * 1 - channel clears when the part is cleared;
479 * 0 - channel does not clear when the part is cleared.
480 *
481 * @return None.
482 *******************************************************************************/
483 void AD5755_ChannelClearEnable(unsigned char channel, unsigned char clearEn)
484 {
485 unsigned long oldDacCtrlReg = 0;
486 unsigned long newDacCtrlReg = 0;
487
488 /* Read the content of the DAC Control Register of the selected channel. */
489 oldDacCtrlReg = AD5755_GetRegisterValue(AD5755_RD_CTRL_REG(channel));
490 /* Clear the CLR_EN bit. */
491 oldDacCtrlReg &= ~(AD5755_DAC_CLR_EN);
492 newDacCtrlReg |= oldDacCtrlReg | (clearEn * AD5755_DAC_CLR_EN);
493 AD5755_SetControlRegisters(AD5755_CREG_DAC, channel, newDacCtrlReg);
494 }
495
496 /***************************************************************************//**
497 * @brief Configures the Digital Slew Rate Control.
498 *
499 * @param channel - Channel option.
500 * Example: AD5755_DAC_A
501 * AD5755_DAC_B
502 * AD5755_DAC_C
503 * AD5755_DAC_D
504 * @param srEn - Enable/Disable the Slew Rate Control.
505 * Example: 0 - disable feature;
506 * 1 - enable feature.
507 * @param updtFreq - Update Clock Frequency(Hz).
508 * Example: AD5755_SR_CLK_64K
509 * AD5755_SR_CLK_32k
510 * ...
511 * AD5755_SR_CLK_8
512 * AD5755_SR_CLK_4
513 * AD5755_SR_CLK_0_5
514 * @param stepSize - Step Size (LSBs).
515 * Example: AD5755_STEP_1
516 * AD5755_STEP_2
517 * ...
518 * AD5755_STEP_128
519 * AD5755_STEP_256
520 *
521 * @return None.
522 *******************************************************************************/
523 void AD5755_SlewRateCtrl(char channel, char srEn, char updtFreq, char stepSize)
524 {
525 AD5755_SetControlRegisters(AD5755_CREG_SLEW,
526 channel,
527 srEn * (AD5755_SLEW_SREN) |
528 AD5755_SLEW_SR_CLOCK(updtFreq) |
529 AD5755_SLEW_SR_STEP(stepSize));
530 }
531
532 /***************************************************************************//**
533 * @brief Sets the output voltage of a channel.
534 *
535 * @param channel - Channel option.
536 * Example: AD5755_DAC_A
537 * AD5755_DAC_B
538 * AD5755_DAC_C
539 * AD5755_DAC_D
540 * @param voltage - Value to be outputted by the DAC(Volts).
541 *
542 * @return The actual voltage value that can be outputted by the channel.
543 *******************************************************************************/
544 float AD5755_SetVoltage(unsigned char channel, float voltage)
545 {
546 unsigned long offset = 0;
547 unsigned long gain = 0;
548 unsigned long dacVal = 0;
549 unsigned long code = 0;
550 unsigned char range = 0;
551 unsigned char resolution = 0;
552 unsigned long rangeOffset = 0;
553 float vRef = 0;
554 float realVoltage = 0;
555
556 if((this_device == ID_AD5755) || (this_device == ID_AD5755_1))
557 {
558 /* Get the offset, gain and range of the selected channel. */
559 offset = AD5755_GetRegisterValue(AD5755_RD_OFFSET_REG(channel));
560 gain = AD5755_GetRegisterValue(AD5755_RD_GAIN_REG(channel));
561 range = AD5755_GetRegisterValue(AD5755_RD_CTRL_REG(channel)) & 0x7;
562 switch(range)
563 {
564 case AD5755_R_0_5_V :
565 {
566 rangeOffset = 0;
567 vRef = 5.0;
568 resolution = 16;
569 break;
570 }
571 case AD5755_R_0_10_V :
572 {
573 rangeOffset = 0;
574 vRef = 10.0;
575 resolution = 16;
576 break;
577 }
578 case AD5755_R_M5_P5_V :
579 {
580 rangeOffset = 0x8000;
581 vRef = 5.0;
582 resolution = 15;
583 break;
584 }
585 case AD5755_R_M10_P10_V :
586 {
587 rangeOffset = 0x8000;
588 vRef = 10.0;
589 resolution = 15;
590 break;
591 }
592 default :
593 {
594 rangeOffset = 0;
595 vRef = 0;
596 resolution = 0;
597 break;
598 }
599 }
600 /* Compute the binary code from the users voltage value. */
601 code = (long)(voltage * (1l << resolution) / vRef) + rangeOffset;
602 if(code > 0xFFFF)
603 {
604 code = 0xFFFF;
605 }
606 /* Offset and Gain are used to obtain the correct value to be written
607 to the DAC register in order to output the voltage desired by the user.
608 */
609 if((code + (1l << 15) - offset) > 0) // Avoid negative values
610 {
611 dacVal = (code + (1l << 15) - offset) * (1l << 16) / (gain + 1);
612 }
613 else
614 {
615 dacVal = 0;
616 }
617
618 /* Write to the Data Register of the DAC. */
619 AD5755_SetRegisterValue(AD5755_DREG_WR_DAC,
620 channel,
621 dacVal);
622 realVoltage = ((long)(code - rangeOffset) * vRef) / (1l << resolution);
623 }
624 return realVoltage;
625 }
626
627 /***************************************************************************//**
628 * @brief Sets the output current of a channel.
629 *
630 * @param channel - Channel option.
631 * Example: AD5755_DAC_A
632 * AD5755_DAC_B
633 * AD5755_DAC_C
634 * AD5755_DAC_D
635 * @param mACurrent - Value to be outputted by the DAC(milliampere).
636 *
637 * @return The actual current value that can be outputted by the channel.
638 *******************************************************************************/
639 float AD5755_SetCurrent(unsigned char channel, float mACurrent)
640 {
641 long offset = 0;
642 long gain = 0;
643 long range = 0;
644 long dacVal = 0;
645 long code = 0;
646 char rangeOffset = 0;
647 float iRef = 0;
648 float realCurrent = 0;
649
650 /* Get the offset, gain and range of the selected channel. */
651 offset = AD5755_GetRegisterValue(AD5755_RD_OFFSET_REG(channel));
652 gain = AD5755_GetRegisterValue(AD5755_RD_GAIN_REG(channel));
653 range = AD5755_GetRegisterValue(AD5755_RD_CTRL_REG(channel)) & 0x7;
654 switch(range)
655 {
656 case AD5755_R_4_20_MA :
657 {
658 iRef = 16.0; // mA
659 rangeOffset = 4; // mA
660 break;
661 }
662 case AD5755_R_0_20_MA :
663 {
664 iRef = 20.0; // mA
665 rangeOffset = 0; // mA
666 break;
667 }
668 case AD5755_R_0_24_MA :
669 {
670 iRef = 24.0; // mA
671 rangeOffset = 0; // mA
672 break;
673 }
674 default :
675 {
676 iRef = 1;
677 rangeOffset = 0;
678 break;
679 }
680 }
681 /* Compute the binary code from the value(mA) provided by user. */
682 code = (long)((mACurrent - rangeOffset) * (1l << 16) / iRef);
683 if(code > 0xFFFF)
684 {
685 code = 0xFFFF;
686 }
687 /* Offset and Gain are used to obtain the correct value to be written to the
688 DAC register in order to output the current desired by the user. */
689 if((code + (1l << 15) - offset) > 0) // Avoid negative values
690 {
691 dacVal = (code + (1l << 15) - offset) * (1l << 16) / (gain + 1);
692 }
693 else
694 {
695 dacVal = 0;
696 }
697 /* Write to the Data Register of the DAC. */
698 AD5755_SetRegisterValue(AD5755_DREG_WR_DAC,
699 channel,
700 dacVal);
701
702 realCurrent = (code * iRef / (float)(1l << 16)) + rangeOffset;
703
704 return realCurrent;
705 }