一、DAC简介
DAC(Digital-to-Analog Converter),即数字/模拟转换模块,故名思议,它的作用就是把输入的数字编码,转换成对应的模拟电压输出,它的功能与 ADC 相反。在常见的数字信号系统中,大部分传感器信号被化成电压信号,而 ADC 把电压模拟信号转换成易于计算机存储、处理的数字编码,由计算机处理完成后,再由 DAC 输出电压模拟信号,该电压模拟信号常用来驱动某些执行器件,使人类易于感知。如音频信号的采集及还原就是这样一个过程。
STM32 具有片上 DAC 外设,它的分辨率可配置为 8 位或 12 位的数字输入信号,具有两个 DAC 输出通道,这两个通道互不影响,每个通道都可以使用 DMA 功能,都具有出错检测能力,可外部触发。
二、DAC通道选择
在 STM32 中具有 2 个这样的 DAC 部件,每个 DAC 有 1 个对应的输出通道连接到特定的引脚,即:PA4-通道 1
,PA5-通道 2
,为避免干扰,使用 DAC 功能时,DAC 通道引脚需要被配置成模拟输入功能(AIN)。
三、新建工程
1. 打开 STM32CubeMX 软件,点击“新建工程”
2. 选择 MCU 和封装
3. 配置时钟
RCC 设置,选择 HSE(外部高速时钟) 为 Crystal/Ceramic Resonator(晶振/陶瓷谐振器)
选择 Clock Configuration,配置系统时钟 SYSCLK 为 72MHz
修改 HCLK 的值为 72 后,输入回车,软件会自动修改所有配置
4. 配置调试模式
非常重要的一步,否则会造成第一次烧录程序后续无法识别调试器
SYS 设置,选择 Debug 为 Serial Wire
四、DAC1
4.1 参数配置
在 Analog
中选择 DAC
设置,并选择 OUT1 Configuration
通道1
或者在右边图找到
PA4
引脚,选择 DAC_OUT1
具体配置参数如下。
- OUT1/2 Configuration:
对应两个输出通道。 - External Trigger:
外部中断EXTI9 触发
就是使用外部中断来触发DAC。 - Output Buffer:
使能DAC输出缓存。
DAC 集成了 2 个输出缓存,可以用来减少输出阻抗,无需外部运放即可直接驱动外部负载。每个 DAC 通道输出缓存可以通过设置 DAC_CR 寄存器的 BOFFx 位来使能或者关闭。
如果带载能力还不行,后面就接一个电压跟随器,选择运放一定要选择电流大的型号。
使能输出缓冲后,DAC 输出的最小电压为 0.2V,最大电压为 VREF+-0.2,而未使能输出缓冲则输出可达到0V。
- Trigger:
选择DAC的触发方式
Timer 2/4/5/6/7/8 Trigger Out event
定时器触发,利用这种方式可以输出特定的波形。在这里我们选择定时器2。
Software trigger
软件触发,在本模式下,向 DAC_SWTRIGR 寄存器写入配置即可触发信号进行转换。 - Wave generation mode:Disable(不使用波形发生器)。
4.2 配置DMA
点击 DMA Settings
添加 DAC_CH1 对应 DMA2 的通道3。DMA模式选择循环模式,方向选为内存到外设。
- Priority:
当发生多个 DMA 通道请求时,就意味着有先后响应处理的顺序问题,这个就由仲裁器也管理。仲裁器管理 DMA 通道请求分为两个阶段。第一阶段属于软件阶段,可以在 DMA_CCRx 寄存器中设置,有 4 个等级:非常高、高、中和低四个优先级。第二阶段属于硬件阶段,如果两个或以上的 DMA 通道请求设置的优先级一样,则他们优先级取决于通 道编号,编号越低优先权越高,比如通道 0 高于通道 1。在大容量产品和互联型产品中,DMA1 控制器拥有高于 DMA2 控制器的优先级。 - Mode:
Normal
表示单次传输,传输一次后终止传输。
Circular
表示循环传输,传输完成后又重新开始继续传输,不断循环永不停止。 - Increment Address:
Peripheral
表示外设地址自增。
Memory
表示内存地址自增。 - Data Width:
Byte
一个字节。
Half Word
半个字,等于两字节。
Word
一个字,等于四字节。
五、TIM2通用定时器
5.1 参数配置
在 Timers
中选择 TIM2
设置,时钟源 Clock Source
选择内部时钟 Internal Clock
在 Parameter Settings
进行具体参数配置。
- Prescaler(时钟预分频数):0
则驱动计数器的时钟 CK_CNT = CK_INT(即72MHz)/(0+1) = 72MHz
即不分频 - Counter Mode(计数模式):Up(向上计数模式)
- Counter Period(自动重装载值):20-1
- auto-reload-preload(自动重装载):Disable(不使能)
- TRGO Parameters(触发输出):Update Event(更新事件)
在定时器的定时时间到达的时候输出一个信号(如:定时器更新产生TRGO信号来触发ADC的同步转换)
5.2 生成代码
输入项目名和项目路径
选择应用的 IDE 开发环境 MDK-ARM V5
每个外设生成独立的
’.c/.h’
文件
不勾:所有初始化代码都生成在 main.c
勾选:初始化代码生成在对应的外设文件。 如 GPIO 初始化代码生成在 gpio.c 中。
点击 GENERATE CODE 生成代码
六、库函数
/* IO operation functions *****************************************************/
HAL_StatusTypeDef HAL_DAC_Start(DAC_HandleTypeDef* hdac, uint32_t Channel); //开启DAC输出
HAL_StatusTypeDef HAL_DAC_Stop(DAC_HandleTypeDef* hdac, uint32_t Channel); //关闭DAC输出
HAL_StatusTypeDef HAL_DAC_Start_DMA(DAC_HandleTypeDef* hdac, uint32_t Channel, uint32_t* pData, uint32_t Length, uint32_t Alignment); //需要函数中不断开启 //开启DAC的DMA输出
HAL_StatusTypeDef HAL_DAC_Stop_DMA(DAC_HandleTypeDef* hdac, uint32_t Channel); //关闭DAC的DMA输出
HAL_StatusTypeDef HAL_DAC_SetValue(DAC_HandleTypeDef* hdac, uint32_t Channel, uint32_t Alignment, uint32_t Data); //设置DAC输出值
uint32_t HAL_DAC_GetValue(DAC_HandleTypeDef* hdac, uint32_t Channel); //获取DAC输出值
/**
* @brief Starts the TIM Base generation.
* @param htim TIM Base handle
* @retval HAL status
*/
HAL_StatusTypeDef HAL_TIM_Base_Start(TIM_HandleTypeDef *htim);
七、生成模拟音频波形数据表
7.1 安装Adobe Audition
链接:https://pan.baidu.com/s/1iGMV7FPn-87KI9FnFUDx9A 提取码:4ahu
7.2 生成Wav文件
用 Adobe Audition
打开一个音频文件,选择一段区域。右键,存储选区为。
设置参数,然后导出
- 格式:Wave PCM
- 采样类型:8000 Hz,单声道,8位
- 去除包含标记和其他元数据,这样生成的文件更小
7.3 关于采样类型选择
看看采样频率和位深需要定时器频率的关系如下表格:
根据上面 五、TIM2通用定时器
的参数配置,定时器频率:
TIM2的定时器频率:f = Tclk/[(psc+1) * (cnt+1)] = 72MHz/[1*20] = 3.6MHz。
- 定时器时钟Tclk:72MHz
- 预分频器psc:0
- 自动重装载值cnt:19
所以我们采样频率选择 8000 Hz,位深选择 8位
7.4 安装DataToHex
链接:https://pan.baidu.com/s/1VZwq4zs5b3Mutn5p8rL2Uw 提取码:9d0h
7.5 生成十六进制数组
如:
/*----The Length of This Array is 1134 Byte.----*/
unsigned char Array[]={
0x52,0x49,0x46,0x46,0x66,0x04,0x00,0x00,0x57,0x41,0x56,0x45,0x66,0x6D,0x74,0x20,
0x10,0x00,0x00,0x00,0x01,0x00,0x01,0x00,0x40,0x1F,0x00,0x00,0x40,0x1F,0x00,0x00,
0x01,0x00,0x08,0x00,0x64,0x61,0x74,0x61,0x41,0x04,0x00,0x00,0x58,0x6D,0x7F,0x8F,
0xA5,0xB0,0xB9,0xBF,0xC0,0xB8,0xAF,0xA2,0x93,0x83,0x75,0x6B,0x63,0x61,0x63,0x6A,
0x72,0x7F,0x87,0x90,0x9A,0x9C,0x99,0x9A,0x97,0x8D,0x88,0x7E,0x73,0x69,0x60,0x58,
0x4B,0x47,0x43,0x46,0x4F,0x5B,0x6B,0x7D,0x8E,0x9F,0xAE,0xB5,0xBD,0xBB,0xB4,0xB0,
0x9E,0x92,0x85,0x74,0x6F,0x68,0x64,0x68,0x6E,0x74,0x80,0x8A,0x90,0x98,0x9D,0x9A,
0x97,0x91,0x8A,0x85,0x7A,0x75,0x70,0x66,0x62,0x5C,0x54,0x4F,0x4A,0x49,0x55,0x5A,
0x6B,0x80,0x89,0x9E,0xAD,0xB4,0xBE,0xBB,0xB4,0xAC,0x9F,0x8D,0x83,0x73,0x6B,0x68,
0x64,0x69,0x70,0x76,0x83,0x8D,0x93,0x9A,0x9D,0x99,0x97,0x8F,0x86,0x83,0x7A,0x71,
0x6F,0x65,0x61,0x5E,0x56,0x54,0x4E,0x48,0x53,0x5D,0x63,0x7D,0x8A,0x95,0xAD,0xB0,
0xB8,0xBE,0xB3,0xAB,0xA4,0x8E,0x83,0x77,0x6A,0x68,0x65,0x68,0x70,0x78,0x83,0x8F,
0x96,0x9B,0x9E,0x9B,0x96,0x8F,0x87,0x7E,0x7B,0x74,0x6D,0x71,0x67,0x67,0x64,0x5D,
0x57,0x53,0x49,0x52,0x5B,0x61,0x7C,0x87,0x95,0xA9,0xB3,0xB7,0xBD,0xB1,0xA9,0xA2,
0x8D,0x80,0x78,0x69,0x66,0x68,0x6A,0x74,0x7D,0x87,0x92,0x98,0x9A,0x9D,0x98,0x93,
0x8B,0x83,0x7B,0x79,0x74,0x6F,0x72,0x6E,0x6B,0x6A,0x62,0x5A,0x55,0x4B,0x4F,0x59,
0x5C,0x73,0x84,0x8E,0xA9,0xAE,0xB5,0xBB,0xB4,0xA8,0xA4,0x8D,0x7F,0x78,0x67,0x69,
0x6A,0x6F,0x78,0x85,0x8B,0x95,0x9D,0x9A,0x9B,0x94,0x88,0x83,0x7D,0x78,0x78,0x76,
0x74,0x78,0x73,0x74,0x6F,0x62,0x5B,0x4F,0x47,0x4D,0x57,0x5F,0x7E,0x87,0x9A,0xAA,
0xAE,0xAE,0xAE,0xA2,0x95,0x90,0x7C,0x79,0x74,0x71,0x79,0x81,0x87,0x94,0x97,0x9A,
0x9C,0x98,0x90,0x8B,0x81,0x79,0x78,0x76,0x79,0x79,0x7D,0x7B,0x7B,0x78,0x71,0x66,
0x58,0x4C,0x40,0x41,0x4E,0x58,0x74,0x89,0x98,0xAB,0xB4,0xB3,0xB1,0xA9,0x95,0x8F,
0x7F,0x77,0x74,0x74,0x7A,0x83,0x8B,0x96,0x9A,0x9D,0x9D,0x97,0x8E,0x83,0x7E,0x76,
0x74,0x76,0x76,0x79,0x7E,0x7C,0x7C,0x7B,0x73,0x68,0x59,0x4A,0x41,0x3C,0x4B,0x57,
0x6E,0x89,0x97,0xA9,0xB7,0xB6,0xB0,0xAE,0x97,0x8E,0x81,0x76,0x73,0x77,0x79,0x84,
0x8F,0x95,0x9E,0x9E,0x9C,0x97,0x90,0x83,0x80,0x76,0x73,0x75,0x77,0x79,0x7F,0x80,
0x7E,0x7D,0x76,0x69,0x5A,0x4D,0x40,0x3B,0x47,0x52,0x66,0x82,0x91,0xA4,0xB2,0xB4,
0xB3,0xAE,0x9C,0x91,0x84,0x78,0x76,0x75,0x7B,0x84,0x8C,0x95,0x9B,0x9E,0x9C,0x99,
0x90,0x86,0x83,0x77,0x74,0x76,0x74,0x78,0x7C,0x7C,0x7D,0x7A,0x73,0x69,0x5D,0x4E,
0x47,0x41,0x49,0x56,0x65,0x7D,0x8E,0xA0,0xAB,0xB2,0xAE,0xAC,0x9E,0x93,0x88,0x7F,
0x7B,0x7A,0x7D,0x84,0x8B,0x92,0x9A,0x9A,0x9B,0x99,0x90,0x89,0x87,0x7A,0x78,0x78,
0x73,0x79,0x79,0x78,0x78,0x74,0x6A,0x62,0x57,0x4E,0x47,0x47,0x50,0x5C,0x6F,0x82,
0x91,0xA0,0xA9,0xAB,0xAE,0xA5,0x9F,0x96,0x8A,0x86,0x82,0x7F,0x83,0x87,0x8A,0x93,
0x96,0x97,0x96,0x94,0x8C,0x88,0x83,0x7C,0x78,0x76,0x72,0x73,0x73,0x6E,0x6E,0x66,
0x5E,0x58,0x52,0x4C,0x54,0x57,0x64,0x75,0x7F,0x8F,0x9C,0xA0,0xA6,0xA8,0xA1,0xA1,
0x98,0x90,0x8C,0x87,0x84,0x89,0x87,0x8B,0x90,0x91,0x92,0x92,0x8F,0x8B,0x89,0x82,
0x7F,0x7B,0x76,0x73,0x72,0x6C,0x69,0x63,0x5C,0x57,0x53,0x4F,0x56,0x59,0x62,0x71,
0x7B,0x88,0x96,0x9A,0xA1,0xA6,0xA3,0xA3,0x9F,0x98,0x95,0x90,0x8B,0x8D,0x8A,0x8C,
0x8E,0x8D,0x8E,0x8E,0x8B,0x8A,0x87,0x81,0x7F,0x7B,0x75,0x72,0x6D,0x68,0x62,0x5D,
0x58,0x53,0x51,0x52,0x58,0x60,0x6A,0x74,0x80,0x8B,0x95,0x9C,0xA1,0xA3,0xA4,0xA1,
0x9F,0x9C,0x97,0x95,0x91,0x90,0x8E,0x8F,0x8E,0x8E,0x8C,0x8C,0x88,0x87,0x83,0x7E,
0x7B,0x76,0x71,0x6B,0x67,0x5F,0x5B,0x55,0x53,0x51,0x54,0x57,0x5F,0x69,0x72,0x7F,
0x89,0x91,0x9B,0xA1,0xA3,0xA7,0xA5,0xA3,0xA1,0x9D,0x99,0x96,0x92,0x90,0x8F,0x8D,
0x8C,0x8B,0x89,0x86,0x86,0x7F,0x7C,0x77,0x71,0x6B,0x66,0x5E,0x5A,0x56,0x52,0x52,
0x54,0x57,0x5F,0x67,0x6F,0x7B,0x83,0x8C,0x96,0x9C,0xA0,0xA6,0xA6,0xA6,0xA6,0xA1,
0x9E,0x9C,0x96,0x94,0x92,0x8F,0x8C,0x8B,0x86,0x85,0x81,0x7D,0x7B,0x76,0x70,0x6D,
0x66,0x60,0x5D,0x58,0x56,0x55,0x56,0x58,0x5F,0x64,0x6D,0x77,0x7F,0x88,0x91,0x97,
0x9E,0xA2,0xA5,0xA8,0xA7,0xA5,0xA3,0x9F,0x9B,0x98,0x93,0x91,0x8E,0x8B,0x88,0x83,
0x80,0x7B,0x78,0x72,0x6E,0x68,0x64,0x5E,0x5A,0x56,0x53,0x53,0x55,0x58,0x5E,0x66,
0x70,0x78,0x83,0x8C,0x94,0x9B,0xA0,0xA5,0xA8,0xA9,0xA9,0xA6,0xA5,0xA2,0x9E,0x9B,
0x97,0x92,0x8F,0x8B,0x85,0x80,0x7B,0x76,0x70,0x6B,0x65,0x60,0x5B,0x57,0x52,0x52,
0x51,0x53,0x58,0x5D,0x66,0x6E,0x79,0x82,0x8C,0x95,0x9C,0xA2,0xA7,0xA9,0xAC,0xAC,
0xAA,0xA8,0xA6,0xA1,0x9D,0x99,0x93,0x8F,0x89,0x83,0x7F,0x78,0x72,0x6C,0x65,0x5F,
0x5A,0x54,0x51,0x4E,0x4E,0x50,0x54,0x5A,0x61,0x6A,0x74,0x7D,0x88,0x92,0x9A,0xA2,
0xA8,0xAC,0xAF,0xAF,0xAF,0xAD,0xAA,0xA5,0xA0,0x9C,0x96,0x91,0x8A,0x83,0x7D,0x78,
0x70,0x6A,0x62,0x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84,0x88,0x8B,0x8E,0x91,0x93,0x95,0x94,0x95,0x93,0x92,0x90,0x8E,0x8B,0x87,
0x84,0x81,0x7D,0x7A,0x78,0x76,0x73,0x73,0x71,0x70,0x70,0x71,0x73,0x00,
};
八、修改main函数
添加模拟音频波形数据表 Array[]
添加 HAL_TIM_Base_Start()
函数,启动定时器。
添加 HAL_DAC_Start_DMA()
函数,启动 DAC 的 DMA 输出。
unsigned char Array[]={
0xFF,0xF3,0x28,0xC4,0x00,0x0C,0xC8,0x4E,0xA8,0xFF,0x58,0x10,0x00,0x80,0x2A,0x42,
0x52,0x34,0x03,0xF9,0xF5,0xE1,0xB6,0x06,0x17,0x39,0xFD,0xAB,0xB2,0x04,0xD6,0xAA,
0x52,0x0A,0x9C,0x32,0xF4,0x37,0xF6,0x7C,0x22,0xEC,0x59,0xD4,0xF2,0x1C,0xBB,0x30,
0xB7,0xA4,0xD2,0x7F,0xFD,0xBF,0xEB,0xFB,0x3E,0x51,0x63,0xFF,0xFF,0xD5,0x94,0x0C,
0x2A,0xC9,0xBA,0xE6,0xA2,0x98,0xAA,0x91,0xFF,0xF3,0x28,0xC4,0x07,0x0C,0x98,0x6A,
0xBC,0x01,0x98,0x68,0x00,0xA8,0x91,0x70,0x14,0x62,0x23,0xCB,0x87,0x02,0x8A,0x66,
0xC3,0x4F,0xA4,0x01,0x11,0x7A,0x29,0xE5,0x58,0x48,0xCD,0xFC,0xB8,0xAB,0x7D,0x2E,
0x6A,0x7E,0x97,0x89,0xFF,0x86,0xCA,0x06,0x19,0xD7,0xD4,0x24,0xE1,0x80,0xE3,0x3D,
0x55,0xFF,0xFE,0xF1,0x25,0xC0,0x53,0x36,0xD4,0xDF,0x51,0x04,0x62,0x63,0x2A,0x56,
0xFF,0xF3,0x28,0xC4,0x0F,0x0F,0x21,0xC2,0xB8,0x01,0xD9,0x10,0x01,0x9B,0x0E,0xD4,
0x8D,0x95,0x47,0x80,0x1D,0xE9,0x76,0x59,0x47,0x2B,0xD2,0x9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AA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,
0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,
0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,
0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,
0xFF,0xF3,0x28,0xC4,0xC4,0x00,0x00,0x03,0x48,0x00,0x00,0x00,0x00,0xAA,0xAA,0xAA,
0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,
0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,
0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,
0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,
};
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_DAC_Init();
MX_TIM2_Init();
/* USER CODE BEGIN 2 */
HAL_TIM_Base_Start(&htim2);
HAL_DAC_Start_DMA(&hdac, DAC_CHANNEL_1, (uint32_t *)Array, 1134, DAC_ALIGN_12B_R);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
九、实验现象
好像有点不太像。。。
十、注意事项
用户代码要加在 USER CODE BEGIN N
和 USER CODE END N
之间,否则下次使用 STM32CubeMX 重新生成代码后,会被删除。
• 由 Leung 写于 2021 年 3 月 19 日
• 参考:STM32CubeMX系列教程8:数模转换(DAC)
使用STM32F103的DAC功能实现音频输出
STM32CubeIDE Audio播放音频,PWM + TIM