初中生
最后登录1970-1-1
在线时间 小时
注册时间2020-9-15
|
请各位帮我看看我的源码是否可行,我接在示波器上是杂乱的方波,为了计算方便我采集了正弦波码表90个点来凑200hz的频率的,发现不能显示正弦波#include "./dac/bsp_dac.h"
/* -----------------------------------波形数据 ---------------------------------------------------------*/
const uint16_t Sine12bit[90] = {
2191,2333,2474,2613,2748,2881,3009,3133,3252,3364,
3471,3570,3662,3746,3822,3889,3947,3996,4035,4065,
4085,4095,4095,4085,4065,4035,3996,3947,3889,3822,
3746,3662,3570,3471,3364,3252,3133,3009,2881,2748,
2613,2474,2333,2191,2048,1905,1763,1622,1483,1348,
1215,1087,963,844,732,625,526,434,350,274,207,149,
100,61,31,11,1,1,11,31,61,100,149,207,274,350,434,
526,625,732,844,963,1087,1215,1348,1483,1622,1763,
1905,2048
};
uint32_t DualSine12bit[90];
/**
* @brief 使能DAC的时钟,初始化GPIO
*/
static void DAC_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
DAC_InitTypeDef DAC_InitStructure;
/* 使能GPIOA时钟 */
RCC_AHB1PeriphClockCmd(DAC_CH1_GPIO_CLK|DAC_CH2_GPIO_CLK, ENABLE);
/* 使能DAC时钟 */
RCC_APB1PeriphClockCmd(DAC_CLK, ENABLE);
/* DAC的GPIO配置,模拟输入 */
GPIO_InitStructure.GPIO_Pin = DAC_CH1_GPIO_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_Init(DAC_CH1_GPIO_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = DAC_CH2_GPIO_PIN;
GPIO_Init(DAC_CH2_GPIO_PORT, &GPIO_InitStructure);
/* 配置DAC 通道1 */
DAC_InitStructure.DAC_Trigger = DAC_TRIGGER; //使用TIM2作为触发源
DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None; //不使用波形发生器
DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable; //不使用DAC输出缓冲
//三角波振幅(本实验没有用到,可配置成任意值,但本结构体成员不能为空)
DAC_InitStructure.DAC_LFSRUnmask_TriangleAmplitude = DAC_TriangleAmplitude_4095;
DAC_Init(DAC_CH1_CHANNEL, &DAC_InitStructure);
/* 配置DAC 通道2 */
DAC_Init(DAC_CH2_CHANNEL, &DAC_InitStructure);
/* 配置DAC 通道1、2 */
DAC_Cmd(DAC_Channel_1, ENABLE);
DAC_Cmd(DAC_Channel_2, ENABLE);
/* 使能 DAC的DMA请求 */
DAC_DMACmd(DAC_Channel_1, ENABLE);
}
/**
* @brief 配置TIM2
*/
static void DAC_TIM_Config(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
/* 使能TIM2时钟,TIM2CLK 为180M */
RCC_APB1PeriphClockCmd(DAC_TIM_CLK, ENABLE);
/* TIM2基本定时器配置 */
// TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_TimeBaseStructure.TIM_Period = 99; //定时周期 20
TIM_TimeBaseStructure.TIM_Prescaler = 0x63; //预分频,不分频 180M / (0+1) = 180M
TIM_TimeBaseStructure.TIM_ClockDivision = 0x0; //时钟分频系数
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数模式
TIM_TimeBaseInit(DAC_TIM, &TIM_TimeBaseStructure);
/* 配置TIM2触发源 */
TIM_SelectOutputTrigger(DAC_TIM, TIM_TRGOSource_Update);
/* 使能TIM2 */
TIM_Cmd(DAC_TIM, ENABLE);
}
/**
* @brief 配置DMA
*/
static void DAC_DMA_Config(void)
{
DMA_InitTypeDef DMA_InitStructure;
/* DAC1使用DMA1 通道7 数据流5时钟 */
RCC_AHB1PeriphClockCmd(DAC_DMA_CLK, ENABLE);
/* 配置DMA2 */
DMA_InitStructure.DMA_Channel = DAC_CHANNEL;
DMA_InitStructure.DMA_PeripheralBaseAddr = DAC_DHR12RD_Address; //外设数据地址
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&DualSine12bit ; //内存数据地址 DualSine12bit
DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral; //数据传输方向内存至外设
DMA_InitStructure.DMA_BufferSize = 90; //缓存大小为90字节
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //外设数据地址固定
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //内存数据地址自增
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word; //外设数据以字为单位
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Word; //内存数据以字为单位
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; //循环模式
DMA_InitStructure.DMA_Priority = DMA_Priority_High; //高DMA通道优先级
DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
DMA_Init(DAC_DMA_STREAM, &DMA_InitStructure);
/* 使能 DMA_Stream */
DMA_Cmd(DAC_DMA_STREAM, ENABLE);
}
/**
* @brief DAC初始化函数
*/
void DAC_Mode_Init(void)
{
uint32_t Idx = 0;
DAC_Config();
DAC_TIM_Config();
/* 填充正弦波形数据,双通道右对齐*/
for (Idx = 0; Idx < 90; Idx++)
{
DualSine12bit[Idx] = (Sine12bit[Idx] << 16) + (Sine12bit[Idx]);
}
DAC_DMA_Config();
}
|
|