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

I didn't catch that part, to be honest.

However, when true, one could generate 0...3.0V, and use a voltage divider.
Trying to operate with a digital IC (the STM32) switched at mid-range MHz frequencies in this low DC voltage range seems quite optimistic to me ...


@Ozone wrote:

Trying to operate with a digital IC (the STM32) switched at mid-range MHz frequencies in this low DC voltage range seems quite optimistic to me ...


Strong early contender for the 2025 Understatement Of The Year award right there!!

 

Relying on a hand-held multimeter is possibly also rather optimistic...

 

@harry123 remember also that the Nucleo boards are not optimised for precision analogue ...

>Is that directly on the pin of the STM32 chip?

yes

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

  GND of  CN6 connector on board 

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

 VSSA  in my board AGND and GND of CN6 connector is giving -5v.

 

@harry123 

1,2mV you found is almost expected. This is in mV.

The typical value is around 0,5mV. With some measurement errors you can fall into 1.2mV.

Did you see my previous reply? see the datasheet.

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.

>...... perhaps explain what you're trying to achieve here?

I want to control the signal of SMPS

if I give a signal of 3v dc the output is 25A

the output signal gradually decreases with decrease in voltage so trying to acheive it

So why does that require you to "generate dc voltage from 0v to 0.003v with stepsize of 0.0008" ??

At 1st instance -> 0 to 3v   the output goes to 0A to 25A

2nd instance -> 0 to 0.3v  the output goes to 0A to 2.5A

......

4th instance 0 to 0.003  the output goes to 0A to 0.025A

 

So I wanted to use DAC for this requirement

Thank you


@harry123 wrote:

4th instance 0 to 0.003  the output goes to 0A to 0.025A


If a such accuracy is a MUST, you need to use a dedicated DAC IC with that accuracy by taking care about HW (analog) and PCB.

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.

Is this an existing SMPS, or just something that you're thinking to design?

If the latter, I think it's a poor approach.

I think a better approach would be to have a "range" setting:

  • You have 4 "ranges": 0-25A; 0-2.5A; 0-250mA; 0-25mA
  • Your control voltage is always 0-3V

 

Yes It is an existing smps.

For demagnetising transformer by gradually increasing and decreasing voltage from controller.

 

Can I use internal dac or should I go for external 12 bit dac