【stm32】hal库学习笔记-DAC数模转换(超详细!)
DAC功能概述
DAC:将数字信号转换为模拟信号
并行式
分辨率 采样速率
DAC驱动函数
Cube图形化配置
导入TFT_LCD ioc
设置DAC通道
更改ADC配置
优先级设置
更改TIM3配置
按键配置
移入按键和触摸屏驱动文件
程序编写
/* 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_1: DAC1 by soft trigger", RED);
lcd_show_str(10, 40, 24, "Connect PA4 & PA5", RED);
lcd_show_str(10, 70, 24, "DAC1 output to PA4", RED);
lcd_show_str(10,100, 24, "ADC1-IN5 acquire PA5", RED);
lcd_show_str(10,130, 24, "KeyUp = DAC1 output ++", RED);
lcd_show_str(10,160, 24, "KeyDown = DAC1 output --", RED);
lcd_show_str(10,220, 24, "DAC Output =", RED);
lcd_show_str(10,280, 24, "ADC Input =", RED);
lcd_show_str(10,340, 24, "Voltage(mV) = ", RED);
HAL_DAC_Start(&hdac, DAC_CHANNEL_1);
uint32_t DacOutValue = 1000;
HAL_DAC_SetValue(&hdac, DAC_CHANNEL_1, DAC_ALIGN_12B_R, DacOutValue);
lcd_show_num(50, 250, DacOutValue, 4, 24, RED);
HAL_ADC_Start_IT(&hadc1);
HAL_TIM_Base_Start(&htim3);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
KEYS curKey = ScanPressedKey(KEY_WAIT_ALWAYS);
if (curKey == KEY_UP) {
DacOutValue += 50;
} else if (curKey == KEY_DOWN) {
DacOutValue -= 50;
}
HAL_DAC_SetValue(&hdac, DAC_CHANNEL_1, DAC_ALIGN_12B_R, DacOutValue);
lcd_show_num(50, 250, DacOutValue, 4, 24, RED);
HAL_Delay(300);
/* USER CODE END WHILE */
/* USER CODE BEGIN 4 */
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc) {
uint32_t val = HAL_ADC_GetValue(hadc);
lcd_show_num(50, 310, val, 4, 24, RED);
uint32_t Volt = val * 3300 >> 12;
lcd_show_num(50, 370, Volt, 4, 24, RED);
}
/* USER CODE END 4 */