cancel
Showing results for 
Search instead for 
Did you mean: 

Nucleo-f446re internal dac

harry123
Senior

 

 

/* USER CODE BEGIN Header */
/**
  *******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  *******************************************************************************
  * @attention
  *
  * Copyright (c) 2025 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  *******************************************************************************
  */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */

/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
DAC_HandleTypeDef hdac;

/* USER CODE BEGIN PV */
const uint32_t OFFSET_DAC_VALUE = 4;  // Adjust this based on your measurements (e.g., 1.2mV corresponds to 4 DAC steps)

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_DAC_Init(void);
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
void DAC_Init(void) {
    __HAL_RCC_DAC_CLK_ENABLE();  // Enable DAC clock

    // Configure GPIO pin PA4 (DAC output)
    GPIO_InitTypeDef GPIO_InitStruct = {0};
    GPIO_InitStruct.Pin = GPIO_PIN_4;
    GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

    // Initialize DAC1
    hdac.Instance = DAC;
    HAL_DAC_Init(&hdac);

    // Configure DAC channel 1 (PA4)
    DAC_ChannelConfTypeDef sConfig = {0};
    sConfig.DAC_Trigger = DAC_TRIGGER_NONE;  // No trigger
    sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_DISABLE;  // Enable the output buffer

    HAL_DAC_ConfigChannel(&hdac, &sConfig, DAC_CHANNEL_1);

    // Start DAC output
    HAL_DAC_Start(&hdac, DAC_CHANNEL_1);
}


void Set_DAC_Voltage(uint32_t dac_value)
{
    // Ensure that dac_value is within the valid range (0 to 4095 for 12-bit resolution)
    if (dac_value > 4095) dac_value = 4095;
    HAL_DAC_SetValue(&hdac, DAC_CHANNEL_1, DAC_ALIGN_12B_R, dac_value);
}




void Delay_ms(uint32_t ms) {
    HAL_Delay(ms);  // Delay function using HAL library
}
/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_DAC_Init();
  /* USER CODE BEGIN 2 */
  // Initialize DAC
      DAC_Init();

      while (1) {
             // Set to 0V
             Set_DAC_Voltage(0);
             Delay_ms(2000);  // Wait for 2 seconds

             // Set to 3.3V
             Set_DAC_Voltage(4095);  // 3.3V corresponds to 4095 in 12-bit resolution
             Delay_ms(2000);  // Wait for 2 seconds
         }
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /** Configure the main internal regulator output voltage
  */
  __HAL_RCC_PWR_CLK_ENABLE();
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  RCC_OscInitStruct.PLL.PLLM = 8;
  RCC_OscInitStruct.PLL.PLLN = 180;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  RCC_OscInitStruct.PLL.PLLQ = 2;
  RCC_OscInitStruct.PLL.PLLR = 2;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }

  /** Activate the Over-Drive mode
  */
  if (HAL_PWREx_EnableOverDrive() != HAL_OK)
  {
    Error_Handler();
  }

  /** Initializes the CPU, AHB and APB buses clocks
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
  {
    Error_Handler();
  }
}

/**
  * @brief DAC Initialization Function
  *  None
  * @retval None
  */
static void MX_DAC_Init(void)
{

  /* USER CODE BEGIN DAC_Init 0 */

  /* USER CODE END DAC_Init 0 */

  DAC_ChannelConfTypeDef sConfig = {0};

  /* USER CODE BEGIN DAC_Init 1 */

  /* USER CODE END DAC_Init 1 */

  /** DAC Initialization
  */
  hdac.Instance = DAC;
  if (HAL_DAC_Init(&hdac) != HAL_OK)
  {
    Error_Handler();
  }

  /** DAC channel OUT1 config
  */
  sConfig.DAC_Trigger = DAC_TRIGGER_NONE;
  sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE;
  if (HAL_DAC_ConfigChannel(&hdac, &sConfig, DAC_CHANNEL_1) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN DAC_Init 2 */

  /* USER CODE END DAC_Init 2 */

}

/**
  * @brief GPIO Initialization Function
  *  None
  * @retval None
  */
static void MX_GPIO_Init(void)
{
/* USER CODE BEGIN MX_GPIO_Init_1 */
/* USER CODE END MX_GPIO_Init_1 */

  /* GPIO Ports Clock Enable */
  __HAL_RCC_GPIOA_CLK_ENABLE();

/* USER CODE BEGIN MX_GPIO_Init_2 */
/* USER CODE END MX_GPIO_Init_2 */
}

/* USER CODE BEGIN 4 */

/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */
  __disable_irq();
  while (1)
  {
  }
  /* USER CODE END Error_Handler_Debug */
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  *   file: pointer to the source file name
  *   line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

 

 

Hello

I am using internal dac of f446 to get dc voltage from 0V to 3.3v but when I measure using multimeter im getting 1.2mv for 0v is it because im using internal dac or should i go with external dac like mcp4725 to get exact 0v as it is required for my application.

Like I want to generate dc voltage from 0v to 0.003v with stepsize of 0.0008(12bit resolution).

20 REPLIES 20

@harry123 wrote:

when I measure using multimeter im getting 1.2mv for 0v 


Where, exactly, do you measure that?

Please also show how, exactly, you apply the "0V" - are you sure that "0V" is really 0.000V ?

     while (1) {
             // Set to 0V
             Set_DAC_Voltage(0);
             Delay_ms(2000);  // Wait for 2 seconds

             // Set to 3.3V
             Set_DAC_Voltage(4095);  // 3.3V corresponds to 4095 in 12-bit resolution
             Delay_ms(2000);  // Wait for 2 seconds
         }

Setting digital value to 0 and checking with multimeter at pin PA4

Ozone
Lead III

> ...but when I measure using multimeter im getting 1.2mv for 0v

How about the tolerance specifications for your multimeter ?
And how much of the full-scale value of the multimeter this 1.2mV represent ?

Hl_st
ST Employee

Hello,

did you measured this voltage between VSSA and DAC_OUT pin? For better resolution at such low voltages it would also be advisable to reduce VREF+ up to 1.8V.

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

 

>.......How about the tolerance specifications for your multimeter ?

Fluke 177 multimeter

  • Range: 0.1 mV to 1000 V
  • Accuracy: ±(0.09% of reading + 1 digit)

 

>.......And how much of the full-scale value of the multimeter this 1.2mV represent ?

its 0.001

You didn't really answer the question.

 


@harry123 wrote:

checking with multimeter at pin PA4


Is that directly on the pin of the STM32 chip?

Where, exactly, was your 0V reference for your meter?

What do you get If you measure between that reference point and VSSA ?

  • Range: 0.1 mV to 1000 V
  • Accuracy: ±(0.09% of reading + 1 digit)

The accuracy refers to the full-scale value, which would result in ±0.9V in this case.
I suppose we talk about an auto-ranging digital multimeter here, with much better accuracy than 0.9V for the smaller ranges.

Taking into account a multimeter resolution of 1mV, I wouldn't rack my brain over this 1.2mV.
Especially since the DAC resolution (i.e. 1 LSB) exceeds this 1.2mV value as well.


@harry123 wrote:

Like I want to generate dc voltage from 0v to 0.003v with stepsize of 0.0008(12bit resolution).


That seems a bizarre requirement - perhaps explain what you're trying to achieve here?

http://www.catb.org/esr/faqs/smart-questions.html#goal:~:text=reading%20your%20account.-,Describe%20the%20goal%2C%20not%20the%20step,-If%20you%20are

 

Have you looked at the DAC specifications in the Datasheet? 

mƎALLEm
ST Employee

Hello,

From the datasheet:

mALLEm_1-1741775268463.png

Vout min (buffer ON) = 0,2V

Vout min (buffer OFF) = 0,5mV (a typical value).

So I think 1,2mV is something expected by adding the measurements errors ..

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.