STM32利用DAC+DMA+TIMER 输出正弦波
使用的是STM32F407的板子,程序参考的是STM32F4xx固件库的DAC_SignalsGeneration文件夹下的程序 。官方例程分别使用了DAC的禁止生成波(DAC_WaveGeneration_None)的Escalator Wave梯形波,Sine Wave正弦波和stm32内置的的Noise Wave(DAC_WaveGeneration_Noise),Triangle Wave(DAC_WaveGeneration_Triangle) 。
下面主要说一下输出正弦波的配置 。
正弦电压的数组
const uint16_t aSine12bit[32] = {
2047, 2447, 2831, 3185, 3498, 3750, 3939, 4056, 4095, 4056,
3939, 3750, 3495, 3185, 2831, 2447, 2047, 1647, 1263, 909,
599, 344, 155, 38, 0, 38, 155, 344, 599, 909, 1263, 1647};
把以上数组用matlab画出后,如下图所示:
下面是配置DAC通道2的程序,开DMA,设置TIMER6触发DAC
void DAC_Ch2_SineWaveConfig(void)
{
DMA_InitTypeDef DMA_InitStructure;
DAC_InitTypeDefDAC_InitStructure;
DAC_InitStructure.DAC_Trigger = DAC_Trigger_T6_TRGO;
DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None;
DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable;
DAC_Init(DAC_Channel_2, &DAC_InitStructure);
DMA_DeInit(DMA1_Stream6);
DMA_InitStructure.DMA_Channel = DMA_Channel_7;
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)DAC_DHR12R2_ADDRESS;
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&aSine12bit;
DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral;
DMA_InitStructure.DMA_BufferSize = 32;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
DMA_Init(DMA1_Stream6, &DMA_InitStructure);
DMA_Cmd(DMA1_Stream6, ENABLE);
DAC_Cmd(DAC_Channel_2, ENABLE);
DAC_DMACmd(DAC_Channel_2, ENABLE);
}
下面是定时器6的配置程序
定时器更新频率是84M/(0xFF+1)=328KHZ
void TIM6_Config(void)
{
TIM_TimeBaseInitTypeDefTIM_TimeBaseStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM6, ENABLE);
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_TimeBaseStructure.TIM_Period = 0xFF;
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM6, &TIM_TimeBaseStructure);
TIM_SelectOutputTrigger(TIM6, TIM_TRGOSource_Update);
TIM_Cmd(TIM6, ENABLE);
}
因为我们的正弦波形是由32个点构成,所以输出的正弦波形的频率是328K/32=10.25K
用示波器验证,如下图所示
【STM32利用DAC+DMA+TIMER 输出正弦波】
推荐阅读
- STM32系列第13篇--OLED
- STM32中结构体的学习
- 利用89C51单片机的P1口控制8个发光二极管LED编程
- STM32的中断和事件的区别
- STM32采用HAL库使用usart_DMA问题
- STM32f103 双串口配置和中断
- 多层住宅使用面积测量办法 住宅使用面积的利用
- skg烤箱的价格
- keil MDK开发STM32使用内联函数
- STM32F334使用串口发送接收数据