Skip to main content
Lead
June 28, 2021
Solved

DAC not working - Get no current/voltage from it.

Hi!

I have a STM32F373VBT and when I enable the DAC output, then I get only 0.06V as output, no matter what DAC value I send to the DAC.. I don't understand why i get only fixed 0.06V as output for both of these DAC outputs.

First if all, the DAC outputs are going to drive a OP-amp. This OP-amp will transform 0-3.3V to 0-20mA output by help of 24V voltage source.

0693W00000BcDDXQA3.pngI have connected the DAC pins here to the OP-amp.

0693W00000BcDDwQAN.png0693W00000BcDDiQAN.png 

And there are no solder bridge between the MCU's pinns.

Not even them are attached to the GND. I have checked with a DMM.

0693W00000BcDDGQA3.jpg 

I have placed the DAC-control code here.

static DAC_HandleTypeDef* hanalog1;
static DAC_HandleTypeDef* hanalog2;
static uint16_t dac_values[3] = {0};
 
void STM32_PLC_Start_Analog_Output(DAC_HandleTypeDef* hdac1, DAC_HandleTypeDef* hdac2, TIM_HandleTypeDef* htim6) {
	HAL_TIM_Base_Start(htim6); /* The Analog Outputs (DAC) are triggered by TIM6 */
	if(HAL_DAC_Start(hdac1, DAC1_CHANNEL_1) != HAL_OK)
		Error_Handler();
	if(HAL_DAC_Start(hdac1, DAC1_CHANNEL_2) != HAL_OK)
		Error_Handler();
	if(HAL_DAC_Start(hdac2, DAC2_CHANNEL_1) != HAL_OK)
		Error_Handler();
	hanalog1 = hdac1;
	hanalog2 = hdac2;
}
 
void STM32_PLC_Analog_Output_Set(uint8_t i, uint16_t output) {
	switch(i){
	case 0:
		HAL_DAC_SetValue(hanalog1, DAC1_CHANNEL_1, DAC_ALIGN_12B_R, output);
		dac_values[0] = output;
		break;
	case 1:
		HAL_DAC_SetValue(hanalog1, DAC1_CHANNEL_2, DAC_ALIGN_12B_R, output);
		dac_values[1] = output;
		break;
	case 2:
		HAL_DAC_SetValue(hanalog2, DAC2_CHANNEL_1, DAC_ALIGN_12B_R, output);
		dac_values[2] = output;
		break;
	}
}
 
uint16_t STM32_PLC_Analog_Output_Get(uint8_t i) {
	return dac_values[i];
}

And I control the DAC like this.

STM32_PLC_Start_Analog_Output(&hdac1, &hdac2, &htim6); // This runs only once
 .
 .
 .
	 STM32_PLC_Analog_Output_Set(0, d0);
	 STM32_PLC_Analog_Output_Set(1, d1);
	 STM32_PLC_Analog_Output_Set(2, d2);
	 d0 += 10;
	 d1 += 20;
	 d2 += 30;
	 if(d0 >= 4095)
		 d0 = 0;
	 if(d1 >= 4095)
		 d1 = 0;
	 if(d2 >= 4095)
		 d2 = 0;

TIM6, which is the trigger for DAC, is decleared.

static void MX_TIM6_Init(void)
{
 
 /* USER CODE BEGIN TIM6_Init 0 */
 
 /* USER CODE END TIM6_Init 0 */
 
 TIM_MasterConfigTypeDef sMasterConfig = {0};
 
 /* USER CODE BEGIN TIM6_Init 1 */
 
 /* USER CODE END TIM6_Init 1 */
 htim6.Instance = TIM6;
 htim6.Init.Prescaler = 18;
 htim6.Init.CounterMode = TIM_COUNTERMODE_UP;
 htim6.Init.Period = 0xffff;
 htim6.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
 if (HAL_TIM_Base_Init(&htim6) != HAL_OK)
 {
 Error_Handler();
 }
 sMasterConfig.MasterOutputTrigger = TIM_TRGO_UPDATE;
 sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
 if (HAL_TIMEx_MasterConfigSynchronization(&htim6, &sMasterConfig) != HAL_OK)
 {
 Error_Handler();
 }
 /* USER CODE BEGIN TIM6_Init 2 */
 
 /* USER CODE END TIM6_Init 2 */
 
}

and also the DAC's itself.

static void MX_DAC1_Init(void)
{
 
 /* USER CODE BEGIN DAC1_Init 0 */
 
 /* USER CODE END DAC1_Init 0 */
 
 DAC_ChannelConfTypeDef sConfig = {0};
 
 /* USER CODE BEGIN DAC1_Init 1 */
 
 /* USER CODE END DAC1_Init 1 */
 /** DAC Initialization
 */
 hdac1.Instance = DAC1;
 if (HAL_DAC_Init(&hdac1) != HAL_OK)
 {
 Error_Handler();
 }
 /** DAC channel OUT1 config
 */
 sConfig.DAC_Trigger = DAC_TRIGGER_T6_TRGO;
 sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE;
 if (HAL_DAC_ConfigChannel(&hdac1, &sConfig, DAC_CHANNEL_1) != HAL_OK)
 {
 Error_Handler();
 }
 /** DAC channel OUT2 config
 */
 if (HAL_DAC_ConfigChannel(&hdac1, &sConfig, DAC_CHANNEL_2) != HAL_OK)
 {
 Error_Handler();
 }
 /* USER CODE BEGIN DAC1_Init 2 */
 
 /* USER CODE END DAC1_Init 2 */
 
}
 
/**
 * @brief DAC2 Initialization Function
 * @param None
 * @retval None
 */
static void MX_DAC2_Init(void)
{
 
 /* USER CODE BEGIN DAC2_Init 0 */
 
 /* USER CODE END DAC2_Init 0 */
 
 DAC_ChannelConfTypeDef sConfig = {0};
 
 /* USER CODE BEGIN DAC2_Init 1 */
 
 /* USER CODE END DAC2_Init 1 */
 /** DAC Initialization
 */
 hdac2.Instance = DAC2;
 if (HAL_DAC_Init(&hdac2) != HAL_OK)
 {
 Error_Handler();
 }
 /** DAC channel OUT1 config
 */
 sConfig.DAC_Trigger = DAC_TRIGGER_T6_TRGO;
 sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE;
 if (HAL_DAC_ConfigChannel(&hdac2, &sConfig, DAC_CHANNEL_1) != HAL_OK)
 {
 Error_Handler();
 }
 /* USER CODE BEGIN DAC2_Init 2 */
 
 /* USER CODE END DAC2_Init 2 */
 
}

 Here is my Main.c file

https://pastebin.com/fYRwyjQt

If I disable these, then DAC works. It must be a bug or something?

I can send you the project if you want. Just tell me how.

/* USER CODE END SysInit */
 
 /* Initialize all configured peripherals */
 MX_GPIO_Init();
 MX_DAC1_Init();
 MX_DAC2_Init();
 //MX_RTC_Init();
 MX_SPI2_Init();
 //MX_TIM2_Init();
 //MX_TIM5_Init();
 MX_SPI1_Init();
 //MX_TIM4_Init();
 //MX_SDADC1_Init();
 //MX_SDADC2_Init();
 //MX_SDADC3_Init();
 //MX_TIM19_Init();
 //MX_CAN_Init();
 //MX_USART1_UART_Init();
 MX_TIM6_Init();
 //MX_TIM12_Init();
 //MX_TIM13_Init();
 //MX_TIM16_Init();
 //MX_USB_DEVICE_Init();
 //MX_FATFS_Init();
 //MX_TIM14_Init();
 /* USER CODE BEGIN 2 */

This topic has been closed for replies.
waclawek.jan
waclawek.janBest answer
Super User
June 28, 2021

> It is a weak voltage for 4095 value for the DAC. Only 2.20V.

The DAC is high impedance output unless its output buffer is switched on, but that then does not go rail-to-rail.

2.2V is suspiciously low, though, if loaded only by the opamp. I presume you are talking about a DC value, given the relatively large capacitor as load.

JW

10 replies

waclawek.jan
Super User
June 28, 2021

Read out and check/post DAC and relevant GPIO registers content.

JW

DMårtAuthor
Lead
June 28, 2021

What do you mean?

STM32MP151AAC3 custom board with STM32-OS as operating system: https://github.com/DanielMartensson/STM32-ComputerSTM32MP257FAK3 custom board with STM64-OS as operating system: https://github.com/DanielMartensson/STM64-Computer
waclawek.jan
Super User
June 28, 2021

Peripheral modules such as DAC and GPIO work according to what is written to their registers, as is described in respective chapters in Reference Manual to your STM32 model.

So read them out - in debugger; or in program, outputting them in a suitable way, e.g. through UART into a terminal - to find out what is written into them and check whether they are set according to the requirements.

JW

waclawek.jan
waclawek.janBest answer
Super User
June 28, 2021

> It is a weak voltage for 4095 value for the DAC. Only 2.20V.

The DAC is high impedance output unless its output buffer is switched on, but that then does not go rail-to-rail.

2.2V is suspiciously low, though, if loaded only by the opamp. I presume you are talking about a DC value, given the relatively large capacitor as load.

JW

DMårtAuthor
Lead
June 28, 2021

The opamp is not powered by anything yet. It just there. So the current from the DAC goes directly to the non-inverting inputs of the opamp.

Yes. I'm talking about DC voltage. Measured at the pins of the MCU.

STM32MP151AAC3 custom board with STM32-OS as operating system: https://github.com/DanielMartensson/STM32-ComputerSTM32MP257FAK3 custom board with STM64-OS as operating system: https://github.com/DanielMartensson/STM64-Computer