/***************************************************************************//** * @brief Sets the range of a channel. * * @param channel - Channel option. * Example: AD5755_DAC_A * AD5755_DAC_B * AD5755_DAC_C * AD5755_DAC_D * @param range - Range option. * Example: * AD5755_R_0_5_V - 0 V to 5 V voltage range (default) * AD5755_R_0_10_V - 0 V to 10 V voltage range * AD5755_R_M5_P5_V - -5 V to +5 V voltage range * AD5755_R_M10_P10_V - -10 V to 10 V voltage range * AD5755_R_4_20_MA - 4 mA to 20 mA current range * AD5755_R_0_20_MA - 0 mA to 20 mA current range * AD5755_R_0_24_MA - 0 mA to 24 mA current range * * @return None. *******************************************************************************/ void AD5755_SetChannelRange(unsigned char channel, unsigned char range) { unsigned short outputCode = 0x0000; unsigned long oldDacCtrlReg = 0; unsigned long newDacCtrlReg = 0; /* Read the content of the DAC Control Register of the selected channel. */ oldDacCtrlReg = AD5755_GetRegisterValue(AD5755_RD_CTRL_REG(channel)); /* Clear the bits that will be modified by this function. */ oldDacCtrlReg &= ~(AD5755_DAC_INT_ENABLE | AD5755_DAC_OUTEN | AD5755_DAC_DC_DC | AD5755_DAC_R(7)); /* Select the output code before changing the range. */ if((range == AD5755_R_M5_P5_V) || (range == AD5755_R_M10_P10_V)) { outputCode = 0x8000; } /* Set the output code to zero or midscale. */ AD5755_SetRegisterValue(AD5755_DREG_WR_DAC, channel, outputCode); /* Set range. */ newDacCtrlReg = oldDacCtrlReg | AD5755_DAC_INT_ENABLE | AD5755_DAC_DC_DC | AD5755_DAC_R(range); AD5755_SetControlRegisters(AD5755_CREG_DAC, channel, newDacCtrlReg); /* Set the output code to zero or midscale. */ AD5755_SetRegisterValue(AD5755_DREG_WR_DAC, channel, outputCode); TIME_DelayUs(200); /* Enable the output of the channel. */ newDacCtrlReg |= AD5755_DAC_OUTEN; AD5755_SetControlRegisters(AD5755_CREG_DAC, channel, newDacCtrlReg); }
/***************************************************************************//** * @brief Selects if the channel clears when CLEAR pin is activated. * * @param channel - Channel option. * Example: AD5755_DAC_A * AD5755_DAC_B * AD5755_DAC_C * AD5755_DAC_D * @param clearEn - Clear Enable option. * Example: * 1 - channel clears when the part is cleared; * 0 - channel does not clear when the part is cleared. * * @return None. *******************************************************************************/ void AD5755_ChannelClearEnable(unsigned char channel, unsigned char clearEn) { unsigned long oldDacCtrlReg = 0; unsigned long newDacCtrlReg = 0; /* Read the content of the DAC Control Register of the selected channel. */ oldDacCtrlReg = AD5755_GetRegisterValue(AD5755_RD_CTRL_REG(channel)); /* Clear the CLR_EN bit. */ oldDacCtrlReg &= ~(AD5755_DAC_CLR_EN); newDacCtrlReg |= oldDacCtrlReg | (clearEn * AD5755_DAC_CLR_EN); AD5755_SetControlRegisters(AD5755_CREG_DAC, channel, newDacCtrlReg); }
/***************************************************************************//** * @brief Sets the range of a channel. * * @param dev - The device structure. * @param channel - Channel option. * Example: AD5755_DAC_A * AD5755_DAC_B * AD5755_DAC_C * AD5755_DAC_D * @param range - Range option. * Example: * AD5755_R_0_5_V - 0 V to 5 V voltage range (default) * AD5755_R_0_10_V - 0 V to 10 V voltage range * AD5755_R_M5_P5_V - -5 V to +5 V voltage range * AD5755_R_M10_P10_V - -10 V to 10 V voltage range * AD5755_R_4_20_MA - 4 mA to 20 mA current range * AD5755_R_0_20_MA - 0 mA to 20 mA current range * AD5755_R_0_24_MA - 0 mA to 24 mA current range * * @return None. *******************************************************************************/ void ad5755_set_channel_range(struct ad5755_dev *dev, uint8_t channel, uint8_t range) { uint16_t output_code = 0x0000; uint32_t old_dac_ctrl_reg = 0; uint32_t new_dac_ctrl_reg = 0; /* Read the content of the DAC Control Register of the selected channel. */ old_dac_ctrl_reg = ad5755_get_register_value(dev, AD5755_RD_CTRL_REG(channel)); /* Clear the bits that will be modified by this function. */ old_dac_ctrl_reg &= ~(AD5755_DAC_INT_ENABLE | AD5755_DAC_OUTEN | AD5755_DAC_DC_DC | AD5755_DAC_R(7)); /* Select the output code before changing the range. */ if((range == AD5755_R_M5_P5_V) || (range == AD5755_R_M10_P10_V)) { output_code = 0x8000; } /* Set the output code to zero or midscale. */ ad5755_set_register_value(dev, AD5755_DREG_WR_DAC, channel, output_code); /* Set range. */ new_dac_ctrl_reg = old_dac_ctrl_reg | AD5755_DAC_INT_ENABLE | AD5755_DAC_DC_DC | AD5755_DAC_R(range); ad5755_set_control_registers(dev, AD5755_CREG_DAC, channel, new_dac_ctrl_reg); /* Set the output code to zero or midscale. */ ad5755_set_register_value(dev, AD5755_DREG_WR_DAC, channel, output_code); mdelay(200); /* Enable the output of the channel. */ new_dac_ctrl_reg |= AD5755_DAC_OUTEN; ad5755_set_control_registers(dev, AD5755_CREG_DAC, channel, new_dac_ctrl_reg); }
/***************************************************************************//** * @brief Allows power-up/down of the dc-to-dc converter, DAC and internal * amplifiers for the selected channel. * * @param channel - Channel option. * Example: AD5755_DAC_A * AD5755_DAC_B * AD5755_DAC_C * AD5755_DAC_D * @param pwrStatus - Power mode. * Example: 0 - power-down the channel; * 1 - power-up the channel. * * @return None. *******************************************************************************/ void AD5755_SetChannelPower(unsigned char channel, unsigned char pwrStatus) { unsigned long oldDacCtrlReg = 0; unsigned long newDacCtrlReg = 0; /* Read the content of the DAC Control Register of the selected channel. */ oldDacCtrlReg = AD5755_GetRegisterValue(AD5755_RD_CTRL_REG(channel)); oldDacCtrlReg &= ~(AD5755_DAC_INT_ENABLE | AD5755_DAC_DC_DC | AD5755_DAC_OUTEN); newDacCtrlReg = oldDacCtrlReg | (pwrStatus * (AD5755_DAC_INT_ENABLE | AD5755_DAC_DC_DC | AD5755_DAC_OUTEN)); AD5755_SetControlRegisters(AD5755_CREG_DAC, AD5755_DAC_A, newDacCtrlReg); }
/***************************************************************************//** * @brief Selects if the channel clears when CLEAR pin is activated. * * @param dev - The device structure. * @param channel - Channel option. * Example: AD5755_DAC_A * AD5755_DAC_B * AD5755_DAC_C * AD5755_DAC_D * @param clear_en - Clear Enable option. * Example: * 1 - channel clears when the part is cleared; * 0 - channel does not clear when the part is cleared. * * @return None. *******************************************************************************/ void ad5755_channel_clear_enable(struct ad5755_dev *dev, uint8_t channel, uint8_t clear_en) { uint32_t old_dac_ctrl_reg = 0; uint32_t new_dac_ctrl_reg = 0; /* Read the content of the DAC Control Register of the selected channel. */ old_dac_ctrl_reg = ad5755_get_register_value(dev, AD5755_RD_CTRL_REG(channel)); /* Clear the CLR_EN bit. */ old_dac_ctrl_reg &= ~(AD5755_DAC_CLR_EN); new_dac_ctrl_reg |= old_dac_ctrl_reg | (clear_en * AD5755_DAC_CLR_EN); ad5755_set_control_registers(dev, AD5755_CREG_DAC, channel, new_dac_ctrl_reg); }
/***************************************************************************//** * @brief Allows power-up/down of the dc-to-dc converter, DAC and internal * amplifiers for the selected channel. * * @param dev - The device structure. * @param channel - Channel option. * Example: AD5755_DAC_A * AD5755_DAC_B * AD5755_DAC_C * AD5755_DAC_D * @param pwr_status - Power mode. * Example: 0 - power-down the channel; * 1 - power-up the channel. * * @return None. *******************************************************************************/ void ad5755_set_channel_power(struct ad5755_dev *dev, uint8_t channel, uint8_t pwr_status) { uint32_t old_dac_ctrl_reg = 0; uint32_t new_dac_ctrl_reg = 0; /* Read the content of the DAC Control Register of the selected channel. */ old_dac_ctrl_reg = ad5755_get_register_value(dev, AD5755_RD_CTRL_REG(channel)); old_dac_ctrl_reg &= ~(AD5755_DAC_INT_ENABLE | AD5755_DAC_DC_DC | AD5755_DAC_OUTEN); new_dac_ctrl_reg = old_dac_ctrl_reg | (pwr_status * (AD5755_DAC_INT_ENABLE | AD5755_DAC_DC_DC | AD5755_DAC_OUTEN)); ad5755_set_control_registers(dev, AD5755_CREG_DAC, AD5755_DAC_A, new_dac_ctrl_reg); }
/***************************************************************************//** * @brief Sets the output current of a channel. * * @param dev - The device structure. * @param channel - Channel option. * Example: AD5755_DAC_A * AD5755_DAC_B * AD5755_DAC_C * AD5755_DAC_D * @param m_acurrent - Value to be outputted by the DAC(milliampere). * * @return The actual current value that can be outputted by the channel. *******************************************************************************/ float ad5755_set_current(struct ad5755_dev *dev, uint8_t channel, float m_acurrent) { int32_t offset = 0; int32_t gain = 0; int32_t range = 0; int32_t dac_val = 0; int32_t code = 0; int8_t range_offset = 0; float i_ref = 0; float real_current = 0; /* Get the offset, gain and range of the selected channel. */ offset = ad5755_get_register_value(dev, AD5755_RD_OFFSET_REG(channel)); gain = ad5755_get_register_value(dev, AD5755_RD_GAIN_REG(channel)); range = ad5755_get_register_value(dev, AD5755_RD_CTRL_REG(channel)) & 0x7; switch(range) { case AD5755_R_4_20_MA : { i_ref = 16.0; // mA range_offset = 4; // mA break; } case AD5755_R_0_20_MA : { i_ref = 20.0; // mA range_offset = 0; // mA break; } case AD5755_R_0_24_MA : { i_ref = 24.0; // mA range_offset = 0; // mA break; } default : { i_ref = 1; range_offset = 0; break; } } /* Compute the binary code from the value(mA) provided by user. */ code = (int32_t)((m_acurrent - range_offset) * (1l << 16) / i_ref); if(code > 0xFFFF) { code = 0xFFFF; } /* Offset and Gain are used to obtain the correct value to be written to the DAC register in order to output the current desired by the user. */ if((code + (1l << 15) - offset) > 0) { // Avoid negative values dac_val = (code + (1l << 15) - offset) * (1l << 16) / (gain + 1); } else { dac_val = 0; } /* Write to the Data Register of the DAC. */ ad5755_set_register_value(dev, AD5755_DREG_WR_DAC, channel, dac_val); real_current = (code * i_ref / (float)(1l << 16)) + range_offset; return real_current; }
/***************************************************************************//** * @brief Sets the output voltage of a channel. * * @param dev - The device structure. * @param channel - Channel option. * Example: AD5755_DAC_A * AD5755_DAC_B * AD5755_DAC_C * AD5755_DAC_D * @param voltage - Value to be outputted by the DAC(Volts). * * @return The actual voltage value that can be outputted by the channel. *******************************************************************************/ float ad5755_set_voltage(struct ad5755_dev *dev, uint8_t channel, float voltage) { uint32_t offset = 0; uint32_t gain = 0; uint32_t dac_val = 0; int32_t code = 0; uint8_t range = 0; uint8_t resolution = 0; uint32_t range_offset = 0; float v_ref = 0; float real_voltage = 0; if((dev->this_device == ID_AD5755) || (dev->this_device == ID_AD5755_1)) { /* Get the offset, gain and range of the selected channel. */ offset = ad5755_get_register_value(dev, AD5755_RD_OFFSET_REG(channel)); gain = ad5755_get_register_value(dev, AD5755_RD_GAIN_REG(channel)); range = ad5755_get_register_value(dev, AD5755_RD_CTRL_REG(channel)) & 0x7; switch(range) { case AD5755_R_0_5_V : { range_offset = 0; v_ref = 5.0; resolution = 16; break; } case AD5755_R_0_10_V : { range_offset = 0; v_ref = 10.0; resolution = 16; break; } case AD5755_R_M5_P5_V : { range_offset = 0x8000; v_ref = 5.0; resolution = 15; break; } case AD5755_R_M10_P10_V : { range_offset = 0x8000; v_ref = 10.0; resolution = 15; break; } default : { range_offset = 0; v_ref = 0; resolution = 0; break; } } /* Compute the binary code from the users voltage value. */ code = (int32_t)(voltage * (1l << resolution) / v_ref) + range_offset; if(code > 0xFFFF) { code = 0xFFFF; } /* Offset and Gain are used to obtain the correct value to be written to the DAC register in order to output the voltage desired by the user. */ if((int32_t)(code + (1l << 15) - offset) > 0) { // Avoid negative values dac_val = (code + (1l << 15) - offset) * (1l << 16) / (gain + 1); } else { dac_val = 0; } /* Write to the Data Register of the DAC. */ ad5755_set_register_value(dev, AD5755_DREG_WR_DAC, channel, dac_val); real_voltage = ((int32_t)(dac_val - range_offset) * v_ref) / (1l << resolution); } return real_voltage; }
/***************************************************************************//** * @brief Sets the output current of a channel. * * @param channel - Channel option. * Example: AD5755_DAC_A * AD5755_DAC_B * AD5755_DAC_C * AD5755_DAC_D * @param mACurrent - Value to be outputted by the DAC(milliampere). * * @return The actual current value that can be outputted by the channel. *******************************************************************************/ float AD5755_SetCurrent(unsigned char channel, float mACurrent) { long offset = 0; long gain = 0; long range = 0; long dacVal = 0; long code = 0; char rangeOffset = 0; float iRef = 0; float realCurrent = 0; /* Get the offset, gain and range of the selected channel. */ offset = AD5755_GetRegisterValue(AD5755_RD_OFFSET_REG(channel)); gain = AD5755_GetRegisterValue(AD5755_RD_GAIN_REG(channel)); range = AD5755_GetRegisterValue(AD5755_RD_CTRL_REG(channel)) & 0x7; switch(range) { case AD5755_R_4_20_MA : { iRef = 16.0; // mA rangeOffset = 4; // mA break; } case AD5755_R_0_20_MA : { iRef = 20.0; // mA rangeOffset = 0; // mA break; } case AD5755_R_0_24_MA : { iRef = 24.0; // mA rangeOffset = 0; // mA break; } default : { iRef = 1; rangeOffset = 0; break; } } /* Compute the binary code from the value(mA) provided by user. */ code = (long)((mACurrent - rangeOffset) * (1l << 16) / iRef); if(code > 0xFFFF) { code = 0xFFFF; } /* Offset and Gain are used to obtain the correct value to be written to the DAC register in order to output the current desired by the user. */ if((code + (1l << 15) - offset) > 0) // Avoid negative values { dacVal = (code + (1l << 15) - offset) * (1l << 16) / (gain + 1); } else { dacVal = 0; } /* Write to the Data Register of the DAC. */ AD5755_SetRegisterValue(AD5755_DREG_WR_DAC, channel, dacVal); realCurrent = (code * iRef / (float)(1l << 16)) + rangeOffset; return realCurrent; }
/***************************************************************************//** * @brief Sets the output voltage of a channel. * * @param channel - Channel option. * Example: AD5755_DAC_A * AD5755_DAC_B * AD5755_DAC_C * AD5755_DAC_D * @param voltage - Value to be outputted by the DAC(Volts). * * @return The actual voltage value that can be outputted by the channel. *******************************************************************************/ float AD5755_SetVoltage(unsigned char channel, float voltage) { unsigned long offset = 0; unsigned long gain = 0; unsigned long dacVal = 0; unsigned long code = 0; unsigned char range = 0; unsigned char resolution = 0; unsigned long rangeOffset = 0; float vRef = 0; float realVoltage = 0; if((this_device == ID_AD5755) || (this_device == ID_AD5755_1)) { /* Get the offset, gain and range of the selected channel. */ offset = AD5755_GetRegisterValue(AD5755_RD_OFFSET_REG(channel)); gain = AD5755_GetRegisterValue(AD5755_RD_GAIN_REG(channel)); range = AD5755_GetRegisterValue(AD5755_RD_CTRL_REG(channel)) & 0x7; switch(range) { case AD5755_R_0_5_V : { rangeOffset = 0; vRef = 5.0; resolution = 16; break; } case AD5755_R_0_10_V : { rangeOffset = 0; vRef = 10.0; resolution = 16; break; } case AD5755_R_M5_P5_V : { rangeOffset = 0x8000; vRef = 5.0; resolution = 15; break; } case AD5755_R_M10_P10_V : { rangeOffset = 0x8000; vRef = 10.0; resolution = 15; break; } default : { rangeOffset = 0; vRef = 0; resolution = 0; break; } } /* Compute the binary code from the users voltage value. */ code = (long)(voltage * (1l << resolution) / vRef) + rangeOffset; if(code > 0xFFFF) { code = 0xFFFF; } /* Offset and Gain are used to obtain the correct value to be written to the DAC register in order to output the voltage desired by the user. */ if((code + (1l << 15) - offset) > 0) // Avoid negative values { dacVal = (code + (1l << 15) - offset) * (1l << 16) / (gain + 1); } else { dacVal = 0; } /* Write to the Data Register of the DAC. */ AD5755_SetRegisterValue(AD5755_DREG_WR_DAC, channel, dacVal); realVoltage = ((long)(code - rangeOffset) * vRef) / (1l << resolution); } return realVoltage; }