野火电子论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 12806|回复: 2

通过DMA输出正弦波,用TIM2中断。输出不了正弦波。

[复制链接]
发表于 2016-11-29 22:03:56 | 显示全部楼层 |阅读模式
请大神指导。
main函数为:

#include "stm32f10x.h"
#include "bsp_dac.h"
#include "bsp_usart1.h"

int main(void)
{
    USART1_Init();
    printf(" \r\n ----DAC实验--- \r\n");
    DAC_Mode_Init();
//    printf(" \r\n ----DAC实验--- \r\n");   
    while(1)
    {
        
    }
}

usart1.c

#include "bsp_usart1.h"

static void NVIC_Configuration(void)
{
    NVIC_InitTypeDef    NVIC_InitStructure;
   
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);
   
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
    NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
   
    NVIC_Init(&NVIC_InitStructure);
   
   
}

static void USART1_Config(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    USART_InitTypeDef USART_InitStructure;
   
    /*config GPIOA clock*/
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
    /*USART1 GPIO config*/
    /*Configure USART1 Tx(PA.09) as alternate function push-pull*/
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
    /*COnfigure USART1 Rx(PA.10) as input floating*/
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
    /*Config USART1 clock*/
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
    USART_InitStructure.USART_BaudRate = 115200;
    USART_InitStructure.USART_WordLength = USART_WordLength_8b;
    USART_InitStructure.USART_StopBits = USART_StopBits_1;
    USART_InitStructure.USART_Parity = USART_Parity_No;
    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
    USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
    USART_Init(USART1, &USART_InitStructure);
    /*Enable usart receive interrupt*/
    USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
   
    USART_Cmd(USART1, ENABLE);
   
    /*Config usart interrupt*/
    NVIC_Configuration();
}

int fputc(int ch, FILE *f)
{
    USART_SendData(USART1,(uint8_t)ch);
   
    while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
   
    return ch;
}
int fgetc(FILE *f)
{
    while(USART_GetITStatus(USART1, USART_IT_RXNE) == RESET);
   
    return (int)USART_ReceiveData(USART1);
}

void ScanDate(void)
{
//    uint8_t ch;
    if(USART_GetITStatus(USART1, USART_FLAG_RXNE) == RESET)
    {
//       ch = USART_ReceiveData(USART1);
        printf("ÕâÊÇ´®¿ÚÖжÏ");
    }
    USART_ClearITPendingBit(USART1, USART_IT_RXNE);
}


void USART1_Init(void)
{
    USART1_Config();
}

DAC.c

#include "bsp_dac.h"
#define DAC_DHR12RD_Address      0x40007420

const uint16_t Sine12bit[32] = {
2448,2832,3186,3496,3751,3940,4057,4095,4057,3940,
3751,3496,3186,2832,2448,2048,1648,1264,910,600,345,
156,39,0,39,156,345,600,910,1264,1648,2048
};

uint32_t DualSine12bit[32];


static void DAC_Config(void)
{
  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_T2_TRGO;      
  DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None;
  DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Disable;
  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);

  DAC_DMACmd(DAC_Channel_1, ENABLE);
}

static void DAC_TIM_Config(void)
{

TIM_TimeBaseInitTypeDef    TIM_TimeBaseStructure;

  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);

// TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
  TIM_TimeBaseStructure.TIM_Period = 19;               
  TIM_TimeBaseStructure.TIM_Prescaler = 0x0;              
  TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;         
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  
  TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);

  TIM_SelectOutputTrigger(TIM2, TIM_TRGOSource_Update);

  TIM_Cmd(TIM2, ENABLE);
}

static void DAC_DMA_Config(void)
{
DMA_InitTypeDef  DMA_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA2, ENABLE);


  DMA_InitStructure.DMA_PeripheralBaseAddr = DAC_DHR12RD_Address;     
  DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&DualSine12bit;   
  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;           
  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_Word;
  DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Word;     
  DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;            
  DMA_InitStructure.DMA_Priority = DMA_Priority_High;           
  DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;              
  DMA_Init(DMA2_Channel3, &DMA_InitStructure);

  DMA_Cmd(DMA2_Channel3, ENABLE);
}


void DAC_Mode_Init(void)
{
uint32_t Idx = 0;  
DAC_Config();
DAC_TIM_Config();
DAC_DMA_Config();


  for (Idx = 0; Idx < 32; Idx++)
  {
    DualSine12bit[Idx] = (Sine12bit[Idx] << 16) + (Sine12bit[Idx]);
  }
}

为什么正弦波输出不了。如果将printf(" \r\n ----DAC实验--- \r\n"); 注释掉,就能正常工作。不知道什么原因。用的是stm32f103vzt

回复

使用道具 举报

发表于 2016-11-29 23:05:20 | 显示全部楼层
这是我之前写的一个代码,频率可调的,希望能给你参考帮助!

实验22 DAC输出正弦波.rar

4.44 MB, 下载次数: 44

回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-11-30 09:19:50 | 显示全部楼层
谢谢,我想知道我的程序为什么出现这种情况。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

联系站长|手机版|野火电子官网|野火淘宝店铺|野火电子论坛 ( 粤ICP备14069197号 ) 大学生ARM嵌入式2群

GMT+8, 2025-1-13 17:09 , Processed in 0.029373 second(s), 26 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表