编写测试代码
首先设置DAC输出的数据为12位右对齐,然后指定输出的值0-4096,实际输出的电压为
value/4096x3.3V,最后使能DAC转换,代码如下:
int main(void)
{
/* USER CODE BEGIN 1 */
uint16_t i = 0;
/* USER CODE END 1 */
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_DAC1_Init();
MX_USART1_UART_Init();
/* USER CODE BEGIN 2 */
printf("DAC Test...\r\n");
HAL_DAC_Start(&hdac1, DAC_CHANNEL_2);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
for(i = 0; i < 4096; i++)
{
HAL_DAC_SetValue(&hdac1, DAC_CHANNEL_2, DAC_ALIGN_12B_R, i);
HAL_Delay(2);
}
printf("DAC test finish, test again!\r\n");
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
|