示例#1
0
/***************************************************************************//**
 * @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;
}
示例#2
0
文件: ad5755.c 项目: 363546178/no-OS
/***************************************************************************//**
 * @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;
}
示例#3
0
/***************************************************************************//**
 * @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;
}
示例#4
0
文件: ad5755.c 项目: 363546178/no-OS
/***************************************************************************//**
 * @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;
}