【stm32】DAC输出三角波锯齿波及任意波形
导入DAC.ioc(见上节DAC学习笔记)
DAC输出三角波
CubeMX图形化配置
DAC配置
实现0.1ms定时周期
时钟树配置
程序编写
/* USER CODE BEGIN Includes */
#include "lcd.h"
/* USER CODE END Includes */
/* USER CODE BEGIN 2 */lcd_init();lcd_show_str(10, 10, 24, "Demo15_2: DAC1 triggered by TIM6", RED);lcd_show_str(10, 40, 24, "Triangular wave on PA4", RED);lcd_show_str(10, 70, 24, "Wave's interval is 819ms", RED);HAL_DAC_Start(&hdac, DAC_CHANNEL_1);uint32_t DCValue = 0;HAL_DAC_SetValue(&hdac, DAC_CHANNEL_1, DAC_ALIGN_12B_R, DCValue);HAL_TIM_Base_Start(&htim6);/* USER CODE END 2 */
实现效果
采用CubeMonitor监测生成波形
也可用示波器检测波形生成结果
生成噪声波
在此处更改配置
生成锯齿波(任意波形)
使用DMA触发任意波形
若要调整锯齿波的周期,修改TIM6的分频系数和计数周期即可
程序编写
/* USER CODE BEGIN Includes */
#include "lcd.h"
#include "keyled.h"
/* USER CODE END Includes */
/* USER CODE BEGIN 2 */lcd_init();lcd_show_str(10, 10, 24, "Demo15_3: DAC output with DMA", RED);lcd_show_str(10, 40, 24, "Sawtooth wave on PA4", RED);lcd_show_str(10, 70, 24, "Wave's interval is 100ms", RED);uint32_t userWave[1000];userWave[0] = 0;for (uint16_t i = 1; i < 1000; ++i) {userWave[i] = userWave[i - 1] + 4;}HAL_DAC_Start_DMA(&hdac, DAC_CHANNEL_1, userWave, 1000, DAC_ALIGN_12B_R);HAL_TIM_Base_Start(&htim6);/* USER CODE END 2 */
重写回调函数
/* USER CODE BEGIN 4 */
void HAL_DAC_ConvCpltCallbackCh1(DAC_HandleTypeDef *hdac) {LED0_Toggle();
}
/* USER CODE END 4 */