一、相关函数
HAL_StatusTypeDef HAL_DAC_SetValue(DAC_HandleTypeDef* hdac, uint32_t Channel, uint32_t Alignment, uint32_t Data)//设置DAC输出电压值
HAL_StatusTypeDef HAL_DAC_Start(DAC_HandleTypeDef* hdac, uint32_t Channel)//使能DAC通道
uint32_t HAL_DAC_GetValue(DAC_HandleTypeDef* hdac, uint32_t Channel)//获取相应通道的输出值
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);
二、输出电压
比较简单,直接上代码
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);
}
}
三、定时器触发DAC+DMA输出任意波形
(1)配置TIM6,话不多说,10ms触发一次,即100HZ。 如下图:
(2)配置DAC,比较简单,不多说,注意使能DMA:
(3)编程实现
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);
}
}
四、定时器触发DAC+DMA输出三波形
(1)CubeMX配置
最大三角波幅(Maximum Triangle Amplitude)为4095,即为3.3V(参考电压为3.3V)
(2)编程实现
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 }
全部0条评论
快来发表一下你的评论吧 !