source: trunk/fw_g473rct/Core/Src/dac.c

Last change on this file was 76, checked in by f.jahn, 12 days ago
File size: 3.9 KB
Line 
1/* USER CODE BEGIN Header */
2/**
3 ******************************************************************************
4 * @file dac.c
5 * @brief This file provides code for the configuration
6 * of the DAC instances.
7 ******************************************************************************
8 * @attention
9 *
10 * Copyright (c) 2026 STMicroelectronics.
11 * All rights reserved.
12 *
13 * This software is licensed under terms that can be found in the LICENSE file
14 * in the root directory of this software component.
15 * If no LICENSE file comes with this software, it is provided AS-IS.
16 *
17 ******************************************************************************
18 */
19/* USER CODE END Header */
20/* Includes ------------------------------------------------------------------*/
21#include "dac.h"
22
23/* USER CODE BEGIN 0 */
24#include <stdio.h>
25int getDACValueForVoltage(uint32_t voltage);
26int getVoltageForCurrent(uint32_t current);
27
28/* USER CODE END 0 */
29
30DAC_HandleTypeDef hdac1;
31
32/* DAC1 init function */
33void MX_DAC1_Init(void)
34{
35
36 /* USER CODE BEGIN DAC1_Init 0 */
37
38 /* USER CODE END DAC1_Init 0 */
39
40 DAC_ChannelConfTypeDef sConfig = {0};
41
42 /* USER CODE BEGIN DAC1_Init 1 */
43
44 /* USER CODE END DAC1_Init 1 */
45
46 /** DAC Initialization
47 */
48 hdac1.Instance = DAC1;
49 if (HAL_DAC_Init(&hdac1) != HAL_OK)
50 {
51 Error_Handler();
52 }
53
54 /** DAC channel OUT2 config
55 */
56 sConfig.DAC_HighFrequency = DAC_HIGH_FREQUENCY_INTERFACE_MODE_AUTOMATIC;
57 sConfig.DAC_DMADoubleDataMode = DISABLE;
58 sConfig.DAC_SignedFormat = DISABLE;
59 sConfig.DAC_SampleAndHold = DAC_SAMPLEANDHOLD_DISABLE;
60 sConfig.DAC_Trigger = DAC_TRIGGER_NONE;
61 sConfig.DAC_Trigger2 = DAC_TRIGGER_NONE;
62 sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE;
63 sConfig.DAC_ConnectOnChipPeripheral = DAC_CHIPCONNECT_EXTERNAL;
64 sConfig.DAC_UserTrimming = DAC_TRIMMING_FACTORY;
65 if (HAL_DAC_ConfigChannel(&hdac1, &sConfig, DAC_CHANNEL_2) != HAL_OK)
66 {
67 Error_Handler();
68 }
69 /* USER CODE BEGIN DAC1_Init 2 */
70 HAL_DAC_Start(&hdac1, DAC_CHANNEL_2);
71
72
73 uint32_t current = 2000;
74 uint32_t voltage = getVoltageForCurrent(current);
75 uint16_t dacValue = getDACValueForVoltage(voltage); // entspricht ca. 0,5 V bei VREF = 3,0 V-->3,5A
76
77 printf ("set current %d --> IlimVoltage= %d --> DAC Value= %d\r\n", current,voltage, dacValue );
78
79 HAL_DAC_SetValue(&hdac1,
80 DAC_CHANNEL_2,
81 DAC_ALIGN_12B_R,
82 dacValue);
83 /* USER CODE END DAC1_Init 2 */
84
85}
86
87void HAL_DAC_MspInit(DAC_HandleTypeDef* dacHandle)
88{
89
90 GPIO_InitTypeDef GPIO_InitStruct = {0};
91 if(dacHandle->Instance==DAC1)
92 {
93 /* USER CODE BEGIN DAC1_MspInit 0 */
94
95 /* USER CODE END DAC1_MspInit 0 */
96 /* DAC1 clock enable */
97 __HAL_RCC_DAC1_CLK_ENABLE();
98
99 __HAL_RCC_GPIOA_CLK_ENABLE();
100 /**DAC1 GPIO Configuration
101 PA5 ------> DAC1_OUT2
102 */
103 GPIO_InitStruct.Pin = GPIO_PIN_5;
104 GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
105 GPIO_InitStruct.Pull = GPIO_NOPULL;
106 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
107
108 /* USER CODE BEGIN DAC1_MspInit 1 */
109
110 /* USER CODE END DAC1_MspInit 1 */
111 }
112}
113
114void HAL_DAC_MspDeInit(DAC_HandleTypeDef* dacHandle)
115{
116
117 if(dacHandle->Instance==DAC1)
118 {
119 /* USER CODE BEGIN DAC1_MspDeInit 0 */
120
121 /* USER CODE END DAC1_MspDeInit 0 */
122 /* Peripheral clock disable */
123 __HAL_RCC_DAC1_CLK_DISABLE();
124
125 /**DAC1 GPIO Configuration
126 PA5 ------> DAC1_OUT2
127 */
128 HAL_GPIO_DeInit(GPIOA, GPIO_PIN_5);
129
130 /* USER CODE BEGIN DAC1_MspDeInit 1 */
131
132 /* USER CODE END DAC1_MspDeInit 1 */
133 }
134}
135
136/* USER CODE BEGIN 1 */
137int getVoltageForCurrent(uint32_t current)
138{
139 return ((current + 3754 ) * 1000) / 15015;
140}
141
142int getDACValueForVoltage(uint32_t voltage)
143{
144 const uint32_t vref=3000;
145 const uint32_t dacsteps = 4096;
146
147 return ((uint64_t) dacsteps * voltage) / vref;
148}
149/* USER CODE END 1 */
150
Note: See TracBrowser for help on using the repository browser.