📅  最后修改于: 2023-12-03 15:14:35.698000             🧑  作者: Mango
DAC是数字转模拟转换器(Digital-to-Analog Converter)的缩写,是将数字信号转换为模拟信号的重要设备。在实际开发中,我们有时需要使用DAC进行模拟信号的输出。下面我们来看几个关于DAC的示例问题。
我们可以使用DAC输出正弦波的离散值,然后用低通滤波器对其进行平滑处理,得到连续的正弦波信号。下面是一个基于STM32的DAC输出正弦波的示例代码:
#include "stm32f10x.h"
float sine_table[256] = {
0.000000, 0.024541, 0.049068, 0.073565, 0.098017, 0.122411, ..., 0.998795
}
int main()
{
GPIO_InitTypeDef GPIO_InitStructure;
DAC_InitTypeDef DAC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOA, &GPIO_InitStructure);
DAC_InitStructure.DAC_Trigger = DAC_Trigger_None;
DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None;
DAC_InitStructure.DAC_LFSRUnmask_TriangleAmplitude = DAC_TriangleAmplitude_4095;
DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable;
DAC_Init(DAC_Channel_1, &DAC_InitStructure);
DAC_Init(DAC_Channel_2, &DAC_InitStructure);
DAC_Cmd(DAC_Channel_1, ENABLE);
DAC_Cmd(DAC_Channel_2, ENABLE);
while (1) {
for (int i = 0; i < 256; i++) {
DAC_SetChannel1Data(DAC_Align_12b_R, (uint16_t)(4095 * sine_table[i]));
DAC_SetChannel2Data(DAC_Align_12b_R, (uint16_t)(4095 * sine_table[i]));
for (int j = 0; j < 10000; j++);
}
}
}
使用DMA可以大大降低CPU的使用率,提高系统的效率。下面是一个基于STM32的DMA输出DAC信号的示例代码:
#include "stm32f10x.h"
uint16_t sine_table[256] = {
2048, 2098, 2149, 2200, 2250, 2300, ..., 1997
};
void DMA1_Channel3_IRQHandler()
{
if (DMA_GetITStatus(DMA1_FLAG_TC3)) {
DMA_Cmd(DMA1_Channel3, DISABLE);
DMA_ClearITPendingBit(DMA1_IT_TC3);
DAC_Cmd(DAC_Channel_1, DISABLE);
DAC_SetChannel1Data(DAC_Align_12b_R, 0);
}
}
int main()
{
GPIO_InitTypeDef GPIO_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
DAC_InitTypeDef DAC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOA, &GPIO_InitStructure);
DMA_DeInit(DMA1_Channel3);
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&DAC->DHR12R1;
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)sine_table;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
DMA_InitStructure.DMA_BufferSize = 256;
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_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel3, &DMA_InitStructure);
DAC_InitStructure.DAC_Trigger = DAC_Trigger_None;
DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None;
DAC_InitStructure.DAC_LFSRUnmask_TriangleAmplitude = DAC_TriangleAmplitude_4095;
DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable;
DAC_Init(DAC_Channel_1, &DAC_InitStructure);
DMA_ITConfig(DMA1_Channel3, DMA_IT_TC, ENABLE);
DAC_DMACmd(DAC_Channel_1, ENABLE);
DMA_Cmd(DMA1_Channel3, ENABLE);
DAC_Cmd(DAC_Channel_1, ENABLE);
while (1) {}
}
使用中断可以实现同步更新多个DAC通道的输出。下面是一个基于STM32的DAC中断更新输出的示例代码:
#include "stm32f10x.h"
float sine_table[256] = {
0.000000, 0.024541, 0.049068, 0.073565, 0.098017, 0.122411, ..., 0.998795
}
void TIM6_DAC_IRQHandler()
{
DAC_SetChannel1Data(DAC_Align_12b_R, (uint16_t)(4095 * sine_table[TIM_GetCounter(TIM6)]));
DAC_SetChannel2Data(DAC_Align_12b_R, (uint16_t)(4095 * sine_table[TIM_GetCounter(TIM6)]));
TIM_ClearITPendingBit(TIM6, TIM_IT_Update);
}
int main()
{
GPIO_InitTypeDef GPIO_InitStructure;
DAC_InitTypeDef DAC_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM6, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOA, &GPIO_InitStructure);
DAC_InitStructure.DAC_Trigger = DAC_Trigger_T6_TRGO;
DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None;
DAC_InitStructure.DAC_LFSRUnmask_TriangleAmplitude = DAC_TriangleAmplitude_4095;
DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable;
DAC_Init(DAC_Channel_1, &DAC_InitStructure);
DAC_Init(DAC_Channel_2, &DAC_InitStructure);
TIM_TimeBaseInitStruct.TIM_Period = 255;
TIM_TimeBaseInitStruct.TIM_Prescaler = 0;
TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM6, &TIM_TimeBaseInitStruct);
TIM_SelectOutputTrigger(TIM6, TIM_TRGOSource_Update);
NVIC_EnableIRQ(TIM6_DAC_IRQn);
TIM_Cmd(TIM6, ENABLE);
DAC_Cmd(DAC_Channel_1, ENABLE);
DAC_Cmd(DAC_Channel_2, ENABLE);
while (1) {}
}
以上就是关于DAC示例问题的介绍,希望对大家有所帮助!