Post Go back to editing

STM32 connect AD5761 but output 0V?

#include <stdbool.h>
#include <stdint.h>

#include "bsp_ad5761r.h"
#include "stm32f10x.h"
#include "stm32f10x_conf.h"
#include "spi.h"

#define false 0  
#define true  1 

#define AD5761R_RESET_PIN       GPIO_Pin_9  	//PB9
#define AD5761R_CLEAR_PIN       GPIO_Pin_8  	//PB8

//低电平有效数字接口同步输入引脚。这是输入数据的帧同步信号。
//当SYNC为低电平时,使能输入移位寄存器,然后数据在后续时钟的下降沿输入移位寄存器。输入移位寄存器在SYNC的上升沿更新。 
#define AD5761R_SYNC_PIN        GPIO_Pin_4  	//PA4
//低电平有效加载DAC逻辑输入引脚。用于更新DAC寄存器和模拟输出。
//当永久接为低电平时,DAC输出会在SYNC上升沿更新。LDAC引脚不能悬空。
#define AD5761R_LDAC_PIN        GPIO_Pin_10 	//PB10

#define AD5761R_RCC_GPIOClockCmd() RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB,ENABLE)
#define PIN_OUT(g,p,v)			(v) ? GPIO_SetBits(g, p) : GPIO_ResetBits(g, p)
#define PIN_IN(g,p)				GPIO_ReadInputDataBit(g, p)


#define AD5761R_CLEAR(x)        PIN_OUT(GPIOB, AD5761R_CLEAR_PIN, x)
#define AD5761R_RESET(x)        PIN_OUT(GPIOB, AD5761R_RESET_PIN, x)
#define AD5761R_SYNC(x)   		PIN_OUT(GPIOA, AD5761R_SYNC_PIN, x)
#define AD5761R_LDAC(x)         PIN_OUT(GPIOB, AD5761R_LDAC_PIN, x)


void SPI1_init(void)
{
	SPI_InitTypeDef  SPI_InitStructure;
	GPIO_InitTypeDef GPIO_InitStructure;	

	RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA, ENABLE ); 
	RCC_APB2PeriphClockCmd ( RCC_APB2Periph_SPI1, ENABLE ); // SPI_Periph clock enable--72MHZ

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    GPIO_SetBits(GPIOA, GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7);//pull up

	// SPI1 configuration
	SPI_InitStructure.SPI_Direction 				= SPI_Direction_2Lines_FullDuplex;
	SPI_InitStructure.SPI_Mode 						= SPI_Mode_Master;
	SPI_InitStructure.SPI_DataSize 					= SPI_DataSize_8b;
	SPI_InitStructure.SPI_CPOL 							= SPI_CPOL_High;		
	SPI_InitStructure.SPI_CPHA 							= SPI_CPHA_1Edge;		
	SPI_InitStructure.SPI_NSS 							= SPI_NSS_Soft;		
	SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256; 
	SPI_InitStructure.SPI_FirstBit 					= SPI_FirstBit_MSB;
	SPI_InitStructure.SPI_CRCPolynomial 		= 7;
	SPI_Init(SPI1 , &SPI_InitStructure);
	SPI_Cmd(SPI1 , ENABLE);
}


uint8_t SPI1_ReadWriteByte(uint8_t TxData)
{
	uint8_t retry = 0;
	while (SPI_I2S_GetFlagStatus(SPI1 , SPI_I2S_FLAG_TXE) == RESET)
	{
		 retry++;
         if(retry > 200)return 0;
  	}
	SPI_I2S_SendData(SPI1,TxData);							
	retry=0; 
	while (SPI_I2S_GetFlagStatus(SPI1 , SPI_I2S_FLAG_RXNE) == RESET)
 	{
 		retry++;
        if(retry > 200)return 0;
  	}
	return SPI_I2S_ReceiveData(SPI1);							      		    									
}


void SPI1_SetSpeed(uint8_t SPI_BaudRatePrescaler)
{
    assert_param(IS_SPI_BAUDRATE_PRESCALER(SPI_BaudRatePrescaler));
    SPI1->CR1&=0XFFC7;
    SPI1->CR1|=SPI_BaudRatePrescaler;   
    SPI_Cmd(SPI1,ENABLE); 
} 

//72M
#define delay_us(x) { unsigned int _dcnt; \
_dcnt=(x*160); \
while(_dcnt-- > 0) \
{ continue; }\
}

void delay_ms(u16 time)
{    
   u16 i=0;  
   while(time--)
   {
      i=4000;  
      while(i--) ;    
   }
}

/******************************************************************************/
/************************ Functions Definitions *******************************/
/******************************************************************************/

static uint8_t gpio_set_value( uint8_t gpio_pin,uint8_t value);
static int32_t  spi_write_and_read( uint8_t spi_ss, uint8_t *data,uint8_t number);
static void AD5761R_GPIO_Init(void);

//static ad5761r_dev * device_glb;
/**
 * SPI write to device.
 * @param dev - The device structure.
 * @param reg_addr_cmd - The input shift register command.
 * 						 Accepted values: CMD_NOP
 * 										  CMD_WR_TO_INPUT_REG
 * 										  CMD_UPDATE_DAC_REG_FROM_INPUT_REG
 * 										  CMD_WR_UPDATE_DAC_REG
 * 										  CMD_WR_CTRL_REG
 * 										  CMD_SW_DATA_RESET
 * 										  CMD_DIS_DAISY_CHAIN
 * 										  CMD_RD_INPUT_REG
 * 										  CMD_RD_DAC_REG
 * 										  CMD_RD_CTRL_REG
 * 										  CMD_SW_FULL_RESET
 * @param reg_data - The transmitted data.
 * @return 0 in case of success, negative error code otherwise.
 */
int32_t ad5761r_write(ad5761r_dev *dev,
											uint8_t reg_addr_cmd,
											uint16_t reg_data)
{
	uint8_t data[3];
	int32_t ret;

	data[0] = reg_addr_cmd;
	data[1] = (reg_data & 0xFF00) >> 8;
	data[2] = (reg_data & 0x00FF) >> 0;

	AD5761R_SYNC(0);
	ret = spi_write_and_read(dev->spi_ss, data, 3);
	AD5761R_SYNC(1);

	return ret;
}

/**
 *  SPI read from device.
 * @param dev - The device structure.
 * @param reg_addr_cmd - The input shift register command.
 * 						 Accepted values: CMD_NOP
 * 										  CMD_WR_TO_INPUT_REG
 * 										  CMD_UPDATE_DAC_REG_FROM_INPUT_REG
 * 										  CMD_WR_UPDATE_DAC_REG
 * 										  CMD_WR_CTRL_REG
 * 										  CMD_SW_DATA_RESET
 * 										  CMD_DIS_DAISY_CHAIN
 * 										  CMD_RD_INPUT_REG
 * 										  CMD_RD_DAC_REG
 * 										  CMD_RD_CTRL_REG
 * 										  CMD_SW_FULL_RESET
 * @param - The received data.
 * @return 0 in case of success, negative error code otherwise.
 */
int32_t ad5761r_read(ad5761r_dev *dev,
					 uint8_t reg_addr_cmd,
					 uint16_t *reg_data)
{
    uint8_t data[3];
    int32_t ret = 0;
    
    data[0] = reg_addr_cmd;
	data[1] = 0;
	data[2] = 0;

	AD5761R_SYNC(0);

	ret = spi_write_and_read(dev->spi_ss, data, 3);
	*reg_data = (data[1] << 8) | data[2];

	AD5761R_SYNC(1);

	return ret;
}

/**
 * Readback the register data.
 * Note: Readback operation is not enabled if daisy-chain mode is disabled.
 * @param dev - The device structure.
 * @param reg - The register to be read.
 * 				Accepted values: AD5761R_REG_INPUT
 * 								 AD5761R_REG_DAC
 * 								 AD5761R_REG_CTRL
 * @param reg_data - The register data.
 * @return 0 in case of success, negative error code otherwise.
 */
int32_t ad5761r_register_readback(ad5761r_dev *dev,
								  ad5761r_reg reg,
								  uint16_t *reg_data)
{
	uint8_t reg_addr;
	int32_t ret;

	if (!dev->daisy_chain_en) {
		
		return -1;
	}

	switch (reg) {
	case AD5761R_REG_INPUT:
		reg_addr = CMD_RD_INPUT_REG;
		break;
	case AD5761R_REG_DAC:
		reg_addr = CMD_RD_DAC_REG;
		break;
	case AD5761R_REG_CTRL:
		reg_addr = CMD_RD_CTRL_REG;
		break;
	default:
		return -1;
	}

	/* During the first command, the last 16 bits are don?t care bits. */
	ret = ad5761r_read(dev, reg_addr, reg_data);

	/* During the next command, the register contents are shifted out of the
	   SDO pin with the MSB shifted out first. */
	ret |= ad5761r_read(dev, reg_addr, reg_data);

	return ret;
}

/**
 * Configure the part based on the settings stored in the device structure.
 * @param dev - The device structure.
 * @return 0 in case of success, negative error code otherwise.
 */
int32_t ad5761r_config(ad5761r_dev *dev)
{
	uint16_t reg_data;

	reg_data = AD5761R_CTRL_CV(dev->cv) |
			   (dev->ovr_en ? AD5761R_CTRL_OVR : 0) |
			   (dev->b2c_range_en? AD5761R_CTRL_B2C : 0) |
			   (dev->exc_temp_sd_en ? AD5761R_CTRL_ETS : 0) |
			   (dev->int_ref_en ? 0 : AD5761R_CTRL_IRO) |
			   AD5761R_CTRL_PV(dev->pv) |
			   AD5761R_CTRL_RA(dev->ra);
			   

	return ad5761r_write(dev, CMD_WR_CTRL_REG, reg_data);
}

/**
 * Enable/disable daisy-chain mode.
 * @param dev - The device structure.
 * @param en_dis - Set true in order to enable the daisy-chain mode.
 * 				   Accepted values: true
 * 									false
 * @return 0 in case of success, negative error code otherwise.
 */
int32_t ad5761r_set_daisy_chain_en_dis(ad5761r_dev *dev,
									   Bool en_dis)
{
	dev->daisy_chain_en = en_dis;
	
	return ad5761r_write(dev, CMD_DIS_DAISY_CHAIN,
			AD5761R_DIS_DAISY_CHAIN_DDC(!en_dis));
}

/**
 * Get the status of the daisy-chain mode.
 * @param dev - The device structure.
 * @param en_dis - The status of the daisy-chain mode (enabled, disabled).
 * @return 0 in case of success, negative error code otherwise.
 */
int32_t ad5761r_get_daisy_chain_en_dis(ad5761r_dev *dev,
									   Bool *en_dis)
{
	*en_dis = dev->daisy_chain_en;

	return 0;
}

/**
 * Set the output_range.
 * @param dev - The device structure.
 * @param out_range - The output range.
 * 					  Accepted values: AD5761R_RANGE_M_10V_TO_P_10V,
 * 									   AD5761R_RANGE_0_V_TO_P_10V
 * 									   AD5761R_RANGE_M_5V_TO_P_5V
 * 									   AD5761R_RANGE_0V_TO_P_5V
 * 									   AD5761R_RANGE_M_2V5_TO_P_7V5
 * 									   AD5761R_RANGE_M_3V_TO_P_3V
 * 									   AD5761R_RANGE_0V_TO_P_16V
 * 									   AD5761R_RANGE_0V_TO_P_20V
 * @return 0 in case of success, negative error code otherwise.
 */
int32_t ad5761r_set_output_range(ad5761r_dev *dev,
								 ad5761r_range out_range)
{
	dev->ra = out_range;

	return ad5761r_config(dev);
}

/**
 * Get the output_range.
 * @param dev - The device structure.
 * @return 0 in case of success, negative error code otherwise.
 */
int32_t ad5761r_get_output_range(ad5761r_dev *dev,
								 ad5761r_range *out_range)
{
	*out_range = dev->ra;

	return 0;
}

/**
 * Set the power up voltage.
 * @param dev - The device structure.
 * @param pv - The power up voltage.
 * 			   Accepted values: AD5761R_SCALE_ZERO
 *								AD5761R_SCALE_HALF
 *								AD5761R_SCALE_FULL
 * @return 0 in case of success, negative error code otherwise.
 */
int32_t ad5761r_set_power_up_voltage(ad5761r_dev *dev,
									 ad5761r_scale pv)
{
	dev->pv = pv;

	return ad5761r_config(dev);
}

/**
 * Get the power up voltage.
 * @param dev - The device structure.
 * @param pv - The power up voltage.
 * @return 0 in case of success, negative error code otherwise.
 */
int32_t ad5761r_get_power_up_voltage(ad5761r_dev *dev,
									 ad5761r_scale *pv)
{
	*pv = dev->pv;

	return 0;
}

/**
 * Set the clear voltage.
 * @param dev - The device structure.
 * @param cv - The clear voltage.
 * 			   Accepted values: AD5761R_SCALE_ZERO
 *								AD5761R_SCALE_HALF
 *								AD5761R_SCALE_FULL
 * @return 0 in case of success, negative error code otherwise.
 */
int32_t ad5761r_set_clear_voltage(ad5761r_dev *dev,
								  ad5761r_scale cv)
{
	dev->cv = cv;

	return ad5761r_config(dev);
}

/**
 * Get the clear voltage.
 * @param dev - The device structure.
 * @param cv - The clear voltage.
 * @return 0 in case of success, negative error code otherwise.
 */
int32_t ad5761r_get_clear_voltage(ad5761r_dev *dev,
								  ad5761r_scale *cv)
{
	*cv = dev->cv;

	return 0;
}

/**
 * Enable/disable internal reference.
 * @param dev - The device structure.
 * @param en_dis - Set true in order to enable the internal reference.
 * 				   Accepted values: true
 * 									false
 * @return 0 in case of success, negative error code otherwise.
 */
int32_t ad5761r_set_internal_reference_en_dis(ad5761r_dev *dev,
											  Bool en_dis)
{
	dev->int_ref_en = en_dis;

	return ad5761r_config(dev);
}

/**
 * Get the status of the internal reference.
 * @param dev - The device structure.
 * @param en_dis - The status of the internal reference (enabled, disabled).
 * @return 0 in case of success, negative error code otherwise.
 */
int32_t ad5761r_get_internal_reference_en_dis(ad5761r_dev *dev,
											  Bool *en_dis)
{
	*en_dis = dev->int_ref_en;

	return 0;
}

/**
 * Enable/disable ETS (exceed temperature shutdown) function.
 * @param dev - The device structure.
 * @param en_dis - Set true in order to enable the ETS function.
 * 				   Accepted values: true
 * 									false
 * @return 0 in case of success, negative error code otherwise.
 */
int32_t ad5761r_set_exceed_temp_shutdown_en_dis(ad5761r_dev *dev,
												Bool en_dis)
{
	dev->exc_temp_sd_en = en_dis;

	return ad5761r_config(dev);
}

/**
 * Get the status of the ETS (exceed temperature shutdown) function.
 * @param dev - The device structure.
 * @param en_dis - The status of the ETS function (enabled, disabled).
 * @return 0 in case of success, negative error code otherwise.
 */
int32_t ad5761r_get_exceed_temp_shutdown_en_dis(ad5761r_dev *dev,
												Bool *en_dis)
{
	*en_dis = dev->exc_temp_sd_en;

	return 0;
}

/**
 * Enable/disable the twos complement bipolar output range.
 * @param dev - The device structure.
 * @param en_dis - Set true in order to enable the twos complement bipolar
 * 				   output range.
 * 				   Accepted values: true
 * 									false
 * @return 0 in case of success, negative error code otherwise.
 */
int32_t ad5761r_set_2c_bipolar_range_en_dis(ad5761r_dev *dev,
											Bool en_dis)
{
	dev->b2c_range_en = en_dis;

	return ad5761r_config(dev);
}

/**
 * Get the status of the twos complement bipolar output range.
 * @param dev - The device structure.
 * @param en_dis - The status of the twos complement bipolar output range
 * 				   (enabled, disabled).
 * @return 0 in case of success, negative error code otherwise.
 */
int32_t ad5761r_get_2c_bipolar_range_en_dis(ad5761r_dev *dev,
											Bool *en_dis)
{
	*en_dis = dev->b2c_range_en;

	return 0;
}

/**
 * Enable/disable the 5% overrange.
 * @param dev - The device structure.
 * @param en_dis - Set true in order to enable the 5% overrange.
 * 				   Accepted values: true
 * 									false
 * @return 0 in case of success, negative error code otherwise.
 */
int32_t ad5761r_set_overrange_en_dis(ad5761r_dev *dev,
									 Bool en_dis)
{
	dev->ovr_en = en_dis;

	return ad5761r_config(dev);
}

/**
 * Get the status of the 5% overrange.
 * @param dev - The device structure.
 * @param en_dis - The status of the twos 5% overrange (enabled, disabled).
 * @return 0 in case of success, negative error code otherwise.
 */
int32_t ad5761r_get_overrange_en_dis(ad5761r_dev *dev,
									 Bool *en_dis)
{
	*en_dis = dev->ovr_en;

	return 0;
}

/**
 * Get the short-circuit condition.
 * Note: The condition is reset at every control register write.
 * @param dev - The device structure.
 * @param en_dis - The status of the short-circuit condition (detected,
 * 				   not detected).
 * @return 0 in case of success, negative error code otherwise.
 */
int32_t ad5761r_get_short_circuit_condition(ad5761r_dev *dev,
											Bool *sc)
{
	uint16_t reg_data;
	int32_t ret;

	ret = ad5761r_read(dev, CMD_RD_CTRL_REG, &reg_data);
	*sc = ((reg_data & AD5761R_CTRL_SC) >> 12);

	return ret;
}

/**
 * Get the brownout condition.
 * Note: The condition is reset at every control register write.
 * @param dev - The device structure.
 * @param en_dis - The status of the brownout condition (detected,
 * 				   not detected).
 * @return 0 in case of success, negative error code otherwise.
 */
int32_t ad5761r_get_brownout_condition(ad5761r_dev *dev,
									   Bool *bo)
{
	uint16_t reg_data;
	int32_t ret;

	ret = ad5761r_read(dev, CMD_RD_CTRL_REG, &reg_data);
	*bo = ((reg_data & AD5761R_CTRL_BO) >> 11);

	return ret;
}

/**
 * Set the reset pin value.
 * @param dev - The device structure.
 * @param value - The pin value.
 * 				  Accepted values: GPIO_LOW
 * 				  				   GPIO_HIGH
 * @return 0 in case of success, negative error code otherwise.
 */
int32_t ad5761r_set_reset_pin(ad5761r_dev *dev,
							  uint8_t value)
{
	//if (dev->gpio_reset >= 0) {
		dev->gpio_reset_value = value;
		return gpio_set_value(dev->gpio_reset, dev->gpio_reset_value);
	//} else
	//	return -1;
}

/**
 * Get the reset pin value.
 * @param dev - The device structure.
 * @param value - The pin value.
 * @return 0 in case of success, negative error code otherwise.
 */
int32_t ad5761r_get_reset_pin(ad5761r_dev *dev,
							  uint8_t *value)
{
	//if (dev->gpio_reset >= 0) {
		*value = dev->gpio_reset_value;
		return 0;
	//} else
	//	return -1;
}

/**
 * Set the clr pin value.
 * @param dev - The device structure.
 * @param value - The pin value.
 * 				  Accepted values: GPIO_LOW
 * 				  				   GPIO_HIGH
 * @return 0 in case of success, negative error code otherwise.
 */
int32_t ad5761r_set_clr_pin(ad5761r_dev *dev,
							uint8_t value)
{
	//if (dev->gpio_clr >= 0) {
		dev->gpio_clr_value = value;
		return gpio_set_value(dev->gpio_clr, dev->gpio_clr_value);
	//} else
	//	return -1;
}

/**
 * Get the clr pin value.
 * @param dev - The device structure.
 * @param value - The pin value.
 * @return 0 in case of success, negative error code otherwise.
 */
int32_t ad5761r_get_clr_pin(ad5761r_dev *dev,
							uint8_t *value)
{
	//if (dev->gpio_clr >= 0) {
		*value = dev->gpio_clr_value;
		return 0;
	//} else
	//	return -1;
}

/**
 * Set the ldac pin value.
 * @param dev - The device structure.
 * @param value - The pin value.
 * 				  Accepted values: GPIO_LOW
 * 				  				   GPIO_HIGH
 * @return 0 in case of success, negative error code otherwise.
 */
int32_t ad5761r_set_ldac_pin(ad5761r_dev *dev,
							 uint8_t value)
{
	//if (dev->gpio_ldac >= 0) {
		dev->gpio_ldac_value = value;
		return gpio_set_value(dev->gpio_ldac, dev->gpio_ldac_value);
	//} else
	//	return -1;
}

/**
 * Get the ldac pin value.
 * @param dev - The device structure.
 * @param value - The pin value.
 * @return 0 in case of success, negative error code otherwise.
 */
int32_t ad5761r_get_ldac_pin(ad5761r_dev *dev,
							 uint8_t *value)
{
	//if (dev->gpio_ldac >= 0) {
		*value = dev->gpio_ldac_value;
		return 0;
	//} else
	//	return -1;
}

/**
 * Write to input register.
 * @param dev - The device structure.
 * @param dac_data - The DAC data.
 * @return 0 in case of success, negative error code otherwise.
 */
int32_t ad5761r_write_input_register(ad5761r_dev *dev,
									 uint16_t dac_data)
{
	uint16_t reg_data;


		reg_data = AD5761R_DATA(dac_data);


	return ad5761r_write(dev, CMD_WR_TO_INPUT_REG, reg_data);
}

/**
 * Update DAC register.
 * @param dev - The device structure.
 * @return 0 in case of success, negative error code otherwise.
 */
int32_t ad5761r_update_dac_register(ad5761r_dev *dev)
{
	return ad5761r_write(dev, CMD_UPDATE_DAC_REG, 0);
}

/**
 * Write to input register and update DAC register.
 * @param dev - The device structure.
 * @param dac_data - The register data.
 * @return 0 in case of success, negative error code otherwise.
 */
int32_t ad5761r_write_update_dac_register(ad5761r_dev *dev,
										  uint16_t dac_data)
{
	uint16_t reg_data;

  	AD5761R_LDAC(1);
	reg_data = AD5761R_DATA(dac_data);

	ad5761r_write(dev, CMD_WR_UPDATE_DAC_REG, reg_data);
	
	return 0;
}

/**
 * Software data reset.
 * @param dev - The device structure.
 * @return 0 in case of success, negative error code otherwise.
 */
int32_t ad5761r_software_data_reset(ad5761r_dev *dev)
{
	return ad5761r_write(dev, CMD_SW_DATA_RESET, 0);
}

/**
 * Software full reset.
 * @param dev - The device structure.
 * @return 0 in case of success, negative error code otherwise.
 */
int32_t ad5761r_software_full_reset(ad5761r_dev *dev)
{
	return ad5761r_write(dev, CMD_SW_FULL_RESET, 0);
}

/**
 * Initialize the device.
 * @param device - The device structure.
 * @param init_param - The structure that contains the device initial
 * 					   parameters.
 * @return 0 in case of success, negative error code otherwise.
 */
int32_t ad5761r_init(void)
{

	return 0;
}



/**
 * SCK空闲时刻为高电平 技术边沿被采样 =>CPOL = 1 CPHA = 0 
 * SCLK下降沿输入,SYNC上升沿锁存
 */
static int32_t  spi_write_and_read( uint8_t spi_ss, uint8_t *data,uint8_t number)   
{
	for(uint8_t i=0; i<number; i++){

		*(data+i) = SPI1_ReadWriteByte(*(data+i));
	}

    return 0;
}

static void AD5761R_GPIO_Init(void)
{

	GPIO_InitTypeDef  GPIO_InitStructure;
	AD5761R_RCC_GPIOClockCmd();

	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Speed =GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Pin = AD5761R_SYNC_PIN;	
	GPIO_Init(GPIOA,&GPIO_InitStructure);
	AD5761R_SYNC(1);

	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Speed =GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Pin = AD5761R_CLEAR_PIN|AD5761R_RESET_PIN|AD5761R_LDAC_PIN;	
	GPIO_Init(GPIOB,&GPIO_InitStructure);

	AD5761R_CLEAR(1);	
	AD5761R_RESET(1);
	AD5761R_LDAC(0);
	SPI1_init();
	SPI1_SetSpeed(SPI_BaudRatePrescaler_256);

}

static uint8_t gpio_set_value( uint8_t gpio_pin,uint8_t value)   //没有用到
{
	 return 0;
}

/***********AD5761R的初始化*******************/
void AD5761R_Init(void)
{
	AD5761R_GPIO_Init();   
	AD5761R_RESET(0);
  	ad5761r_dev dev;
	dev.ra  = AD5761R_RANGE_0V_TO_P_5V;   //output volt range
	dev.int_ref_en = true;                //open ref volt
	dev.cv = AD5761R_SCALE_ZERO;  //clear voltage
	dev.b2c_range_en = false;
	dev.pv = AD5761R_SCALE_ZERO;  //power up voltage
	dev.ovr_en = false;
	dev.daisy_chain_en = true;
	dev.exc_temp_sd_en = false;
	AD5761R_RESET(1);
	//ad5761r_software_full_reset(&dev);
	ad5761r_config(&dev);
	ad5761r_write_update_dac_register(0,0);
}


/*************set AD5761R output volt   0-NV ************************/
void AD5761R_SetVoltage(float volt)
{
	uint16_t reg_val;
	if(volt > 5.0){
		volt = 5.0;
	}else if(volt < 0){
		volt = 0.0;
	}
	
	double dval = volt;
	//dval = volt * 6553.6;=>volt * 65536/10
	dval = volt * 65536/5;
    reg_val = dval;
	ad5761r_write_update_dac_register(0,reg_val);
}




亲爱的先生:

 我使用stm32f103c8t6控件AD5761r,  原理图和代码请参见附件。

 现在,Ad5761使用外部2.5V参考电压,双通道电源分别为0V和5V,

ad5761电源为5​​V,使用SPI的3.3V电源直接与STM32连接。

原理图有问题吗?

我调试了软件,ad5761r模拟输出0V电压,

我不知道出什么问题,请给我一些建议!谢谢!

最良好的问候!

我的邮件ID:mkkk999@163.com