CubeMX配置DAC雙通道輸出電壓和任意波形
一、相關(guān)函數(shù)
HAL_StatusTypeDef HAL_DAC_SetValue(DAC_HandleTypeDef* hdac, uint32_t Channel, uint32_t Alignment, uint32_t Data)//設(shè)置DAC輸出電壓值
HAL_StatusTypeDef HAL_DAC_Start(DAC_HandleTypeDef* hdac, uint32_t Channel)//使能DAC通道
uint32_t HAL_DAC_GetValue(DAC_HandleTypeDef* hdac, uint32_t Channel)//獲取相應(yīng)通道的輸出值
HAL_StatusTypeDef HAL_DAC_Stop(DAC_HandleTypeDef* hdac, uint32_t Channel);
HAL_StatusTypeDef HAL_DAC_Start_DMA(DAC_HandleTypeDef* hdac, uint32_t Channel, uint32_t* pData, uint32_t Length, uint32_t Alignment);
HAL_StatusTypeDef HAL_DAC_Stop_DMA(DAC_HandleTypeDef* hdac, uint32_t Channel);
二、輸出電壓
比較簡(jiǎn)單,直接上代碼
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_DAC_Init();
MX_UART4_Init();
HAL_DAC_SetValue(&hdac,DAC_CHANNEL_1,DAC_ALIGN_8B_R,128);
HAL_DAC_SetValue(&hdac,DAC_CHANNEL_2,DAC_ALIGN_8B_R,255);
HAL_DAC_Start(&hdac,DAC_CHANNEL_1);
HAL_DAC_Start(&hdac,DAC_CHANNEL_2);
while (1)
{
printf("DAC-Channel 1 : %d\\r\\n",HAL_DAC_GetValue(&hdac,DAC_CHANNEL_1));
printf("DAC-Channel 2 : %d\\r\\n\\n",HAL_DAC_GetValue(&hdac,DAC_CHANNEL_2));
HAL_Delay(1000);
}
}
三、定時(shí)器觸發(fā)DAC+DMA輸出任意波形
(1)配置TIM6,話不多說(shuō),10ms觸發(fā)一次,即100HZ。 如下圖:
(2)配置DAC,比較簡(jiǎn)單,不多說(shuō),注意使能DMA:
(3)編程實(shí)現(xiàn)
int main(void)
{
const uint16_t CH_value[32] = { 2448,2832,3186,3496,3751,3940,4057,4095,
4057,3940,3751,3496,3186,2832,2448,2048,
1648,1264,910,600,345,156,39,0,39,156,345,
600,910,1264,1648,2048};
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_DMA_Init();
MX_DAC_Init();
MX_UART4_Init();
MX_TIM6_Init();
HAL_TIM_Base_Start(&htim6);
HAL_DAC_Start_DMA(&hdac,DAC_CHANNEL_1,(uint32_t *)CH_value,32,DAC_ALIGN_12B_R);
while (1)
{
printf("DAC->Channel1 : %d\\r\\n",HAL_DAC_GetValue(&hdac,DAC_CHANNEL_1));
HAL_Delay(10);
}
}
四、定時(shí)器觸發(fā)DAC+DMA輸出三波形
(1)CubeMX配置
最大三角波幅(Maximum Triangle Amplitude)為4095,即為3.3V(參考電壓為3.3V)
(2)編程實(shí)現(xiàn)
1 void MX_DAC_Init(void)
2 {
3 DAC_ChannelConfTypeDef sConfig = {0};
4
5 /** DAC Initialization
6 */
7 hdac.Instance = DAC;
8 if (HAL_DAC_Init(&hdac) != HAL_OK)
9 {
10 Error_Handler();
11 }
12 /** DAC channel OUT1 config
13 */
14 sConfig.DAC_Trigger = DAC_TRIGGER_T6_TRGO;
15 sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE;
16 if (HAL_DAC_ConfigChannel(&hdac, &sConfig, DAC_CHANNEL_1) != HAL_OK)
17 {
18 Error_Handler();
19 }
20 /** DAC channel OUT2 config
21 */
22 if (HAL_DAC_ConfigChannel(&hdac, &sConfig, DAC_CHANNEL_2) != HAL_OK)
23 {
24 Error_Handler();
25 }
26 /** Configure Triangle wave generation on DAC OUT2
27 */
28 if (HAL_DACEx_TriangleWaveGenerate(&hdac, DAC_CHANNEL_2, DAC_TRIANGLEAMPLITUDE_4095) != HAL_OK)
29 {
30 Error_Handler();
31 }
32
33 }
34
35
36
37 int main(void)
38 {
39 /* USER CODE BEGIN 1 */
40 const uint16_t CH_value[32] = { 2448,2832,3186,3496,3751,3940,4057,4095,
41 4057,3940,3751,3496,3186,2832,2448,2048,
42 1648,1264,910,600,345,156,39,0,39,156,345,
43 600,910,1264,1648,2048};
44 /* USER CODE END 1 */
45
46
47 /* MCU Configuration--------------------------------------------------------*/
48
49 /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
50 HAL_Init();
51
52 /* USER CODE BEGIN Init */
53
54 /* USER CODE END Init */
55
56 /* Configure the system clock */
57 SystemClock_Config();
58
59 /* USER CODE BEGIN SysInit */
60
61 /* USER CODE END SysInit */
62
63 /* Initialize all configured peripherals */
64 MX_GPIO_Init();
65 MX_DMA_Init();
66 MX_DAC_Init();
67 MX_UART4_Init();
68 MX_TIM6_Init();
69 /* USER CODE BEGIN 2 */
70 HAL_TIM_Base_Start(&htim6);
71 HAL_DAC_Start_DMA(&hdac,DAC_CHANNEL_1,(uint32_t *)CH_value,32,DAC_ALIGN_12B_R);
72 HAL_DAC_Start(&hdac,DAC_CHANNEL_2);
73 /* USER CODE END 2 */
74
75 /* Infinite loop */
76 /* USER CODE BEGIN WHILE */
77 while (1)
78 {
79 /* USER CODE END WHILE */
80 printf("DAC Channel 1 ------> %d\\r\\n",HAL_DAC_GetValue(&hdac,DAC_CHANNEL_1));
81 printf("DAC Channel 2 ------> %d\\r\\n\\n",HAL_DAC_GetValue(&hdac,DAC_CHANNEL_2));
82 HAL_Delay(10);
83 /* USER CODE BEGIN 3 */
84 }
85 /* USER CODE END 3 */
86 }
-
dac
+關(guān)注
關(guān)注
43文章
2304瀏覽量
191274 -
輸出電壓
+關(guān)注
關(guān)注
2文章
1122瀏覽量
38195 -
函數(shù)
+關(guān)注
關(guān)注
3文章
4343瀏覽量
62806 -
dma
+關(guān)注
關(guān)注
3文章
566瀏覽量
100751 -
任意波形
+關(guān)注
關(guān)注
0文章
8瀏覽量
7591
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論