请大家看看我输出的正弦波为什么不正确

2019-07-21 02:49发布

小弟想配置三种频率的正弦波,分别是4K,8K,12K,发出波型的频率通过数字“1”“0”来改变。为什么出来的波形会出现高低电平呢?请大神帮帮看一下
u16 SineWave_Value3[64]={0x7FF,0x8C8,0x98E,0xA51,0xB0F,0xBC4,
        0xC71,0xD12,0xDA7,0xE2E,0xEA5,0xF0D,0xF63,0xFA6,0xFD7,0xFF5,
        0xFFE,0xFF5,0xFD7,0xFA6,0xF63,0xF0D,0xEA5,0xE2E,0xDA7,0xD12,
        0xC71,0xBC4,0xB0F,0xA51,0x98E,0x8C8,0x7FF,0x736,0x670,0x5AD,
        0x4EF,0x43A,0x38D,0x2EC,0x257,0x1D0,0x159,0x0F1,0x09B,0x058,
        0x027,0x009,0x000,0x009,0x027,0x058,0x09B,0x0F1,0x159,0x1D0,
  0x257,0x2EC,0x38D,0x43A,0x4EF,0x5AD,0x670,0x736};     
u16 SineWave_Value1[64];



/****************初始化引脚******************/
void SineWave_GPIO_Config(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);  //开时钟
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;       //推挽输出模式
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;   //输出速率
    GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_4 ; //选择引脚
    GPIO_SetBits(GPIOA,GPIO_Pin_4)  ;   //拉高输出
    GPIO_Init(GPIOA, &GPIO_InitStructure);      //初始化
}


/******************DAC初始化ˉ*************************/
void SineWave_DAC_Config( void)
{
    DAC_InitTypeDef            DAC_InitStructure;
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE);//开DAC时钟

  /**************DAC结构初始化*******************/
    DAC_StructInit(&DAC_InitStructure);   
    DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None;//不产生波形
    DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable; //不使能输出缓存
    DAC_InitStructure.DAC_Trigger = DAC_Trigger_T2_TRGO;//DAC触发为定时器2触发
    DAC_Init(DAC_Channel_1, &DAC_InitStructure);//初始化
    DAC_Cmd(DAC_Channel_1, ENABLE);    //使能DAC的通道1
    DAC_DMACmd(DAC_Channel_1, ENABLE); //使能DAC通道1的DMA  
}


/*********定时器2初始化************/
void SineWave_TIM_Config(void)
{
    TIM_TimeBaseInitTypeDef    TIM_TimeBaseStructure;
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);//开时钟
    TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
    TIM_TimeBaseStructure.TIM_Prescaler = 0;     //不预分频
    TIM_TimeBaseStructure.TIM_ClockDivision = 0x0; //不分频<br>  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//向上计数
    TIM_TimeBaseStructure.TIM_Period = 49         ;//设置输出频率
    TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
    TIM_SelectOutputTrigger(TIM2, TIM_TRGOSource_Update);//设置TIME输出触发为更新模式
}

void TIM3_Int_Init(u16 arr,u16 psc)
{
        TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  NVIC_InitTypeDef NVIC_InitStructure;
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);

        TIM_TimeBaseStructure.TIM_Period = arr; //设置自动重装载值
  TIM_TimeBaseStructure.TIM_Prescaler =psc; //设置预分频
  TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Down; //TIM向上计数模式
  TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
  TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE ); //设置更新中断
        //中断优先级NVIC的设置
        NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn; //TIM3中断
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //设置优先级
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //设置优先级
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ被使能
  NVIC_Init(&NVIC_InitStructure);
  TIM_Cmd(TIM3, ENABLE); //使能TIM3
}

//定时器3中断服务程序
void TIM3_IRQHandler(void) //TIM3 ??
{
static        u8 num=0;int t=0;int i=0;static        int c=0;
  if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET) //?? TIM3 ????????
  {
   TIM_ClearITPendingBit(TIM3, TIM_IT_Update ); //?? TIM3 ??????
   num++;
         switch (num)
    {
      case 1:t= 2;  break;
      case 2:t= 1;  break;
      case 3:t= 0;  break;
      case 4:t= 0;  break;
      case 5:t= 0;  break;
      case 6:t= 1;  break;
      case 7:t= 0;  break;
      case 8:t= 1;  break;                //pn???
                        case 9:t= 0;  break;
    }
    if(num==9)
       num=0;               
  }
  if(t==2)
  {  
    TIM2->ARR = 112;       
  }
  if(t==1)
  {
                TIM2->ARR = 140;
  }
        if(t==0)
  {
                TIM2->ARR = 280;
  }
}

/*********DMA配置***********/
void SineWave_DMA_Config(void)
{                  
    DMA_InitTypeDef            DMA_InitStructure;
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA2, ENABLE);//开启DMA2时钟

    DMA_StructInit( &DMA_InitStructure);        //DMA结构体初始化
    DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;//从寄存器读数据
    DMA_InitStructure.DMA_BufferSize = 64;//寄存器大小
    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_Priority = DMA_Priority_VeryHigh;//优先级非常高
    DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;//关闭内存到内存模式
    DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;//循环发送模式
       
                DMA_InitStructure.DMA_PeripheralBaseAddr = DAC_DHR12R1;//外设地址为DAC通道1的地址
                DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)SineWave_Value3;//波形数据表内存地址
                DMA_Init(DMA2_Channel3, &DMA_InitStructure);//初始化
                DMA_Cmd(DMA2_Channel3, ENABLE); //使能DMA通道3      
               
}

主函数
#include "stm32f10x.h"
#include "sign.h"


int main()
{               
        delay_init();  //???????      
        NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);  
        TIM3_Int_Init(499,71);
        SineWave_Data( );                   //生成输出正弦波的波形表
        SineWave_GPIO_Config();             //初始化io
        SineWave_TIM_Config( );            //初始化定时器
        SineWave_DAC_Config();              //配置DAC
        SineWave_DMA_Config();              //配置DMA
TIM_Cmd(TIM2, ENABLE);  
        while(1)
        {
    delay_ms(200);
  }
}       


友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。