打印

GD32F470 DAC+DMA+TIMER

[复制链接]
178|1
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
t714664420|  楼主 | 2023-3-1 10:21 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
自己写的程序想要实现timer控制dac输出正弦波 ,用到dma。出现了问题,无法输出波形,输出结果为杂波,有没有大神帮忙看一下,万分感谢!
static void sin_dac_gpio_config(void)
{
        /* enable the clock of peripherals */
       
         rcu_periph_clock_enable(SIN_DAC_CLK);       
        rcu_periph_clock_enable(SIN_DAC_CLK);
        rcu_periph_clock_enable(RCU_CRC);
       
        /* once enabled the DAC, the corresponding GPIO pin is connected to the DAC converter automatically */
         gpio_output_options_set(SIN_GPIO_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, SIN_PIN);//推挽输出
   gpio_mode_set(SIN_GPIO_PORT, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, SIN_PIN);
       
}

/*DAC通道输出配置,定时器6触发,不开启输出缓存*/
static void sin_adc_channel_config(void)
{
        dac_deinit();
        dac_trigger_disable(SIN_DAC);
        dac_wave_mode_config(SIN_DAC, DAC_WAVE_DISABLE);
        dac_output_buffer_disable(SIN_DAC);
        dac_trigger_source_config(SIN_DAC,DAC_TRIGGER_T6_TRGO);
        dac_interrupt_disable(SIN_DAC);
        dac_dma_enable(SIN_DAC);
        dac_trigger_enable(SIN_DAC);
        dac_enable(SIN_DAC);
       
       

      
       
       
}
//DMA配置
static void sin_dma_config(void)
{
        dma_single_data_parameter_struct dma_init_struct;
        /* enable DMA CLK */
        rcu_periph_clock_enable(SIN_DMA_CLK);
        /* deinitialize DMA channel3(USART0 tx) */
        dma_deinit(SIN_DMA, SIN_DMA_CHANNEL);
       
       
        dma_init_struct.direction = DMA_MEMORY_TO_PERIPH;
//        dma_init_struct.memory_addr = ;
//         dma_init_struct.memory0_addr = (uint32_t)Sine;       
        dma_init_struct.memory_inc = DMA_MEMORY_INCREASE_ENABLE;
        dma_init_struct.periph_memory_width = DMA_MEMORY_WIDTH_16BIT;
//        dma_init_struct.number = POINT_NUM2;
//        dma_init_struct.periph_addr = ((uint32_t)(&DAC0_R12DH));;
        dma_init_struct.periph_inc = DMA_PERIPH_INCREASE_DISABLE;
        dma_init_struct.priority = DMA_PRIORITY_ULTRA_HIGH;

dma_single_data_mode_init(SIN_DMA, SIN_DMA_CHANNEL, dma_init_struct);
       
       
       
       


}


void sin_dma_function_config(void)
{       
        //DMAx的通道y的存储器基地址配置
        dma_memory_address_config(SIN_DMA,SIN_DMA_CHANNEL,DMA_MEMORY_1,(uint32_t)Sine);  //(uint32_t)Sine
        //配置DMAx通道y还有多少数据要传输
        dma_transfer_number_config(SIN_DMA,SIN_DMA_CHANNEL,ARRAYNUM(Sine));
        //DMAx的通道y的外设基地址配置
        dma_periph_address_config(SIN_DMA,SIN_DMA_CHANNEL,DAC0_R12DH_DAC0_DOADDRESS);
       
        //DMA循环模式开启
        dma_circulation_enable(SIN_DMA, SIN_DMA_CHANNEL);
        //DMA的通道使能
        dma_channel_enable(SIN_DMA, SIN_DMA_CHANNEL);
       
       

}

/*TIMx触发配置*/
void sin_timx_trigger_function_config(void)
{
        timer_parameter_struct timer_initpara;

        rcu_periph_clock_enable(SIN_TIM_CLK);
        timer_deinit(SIN_TIM);

        /* TIMER0 configuration */
        timer_initpara.prescaler         = 999;//预分频
        timer_initpara.counterdirection  = TIMER_COUNTER_UP;//向上计数
        timer_initpara.period            = 49;//定时周期
        timer_init(SIN_TIM,&timer_initpara);
       


        //定时器主输出触发源选择
        timer_master_output_trigger_source_select(SIN_TIM,TIMER_TRI_OUT_SRC_UPDATE);
        timer_update_event_enable(SIN_TIM);
       
       
       
       
       
       
}

/*操作函数*/
void sin_app(void)
{
       
        sin_dma_config();
        sin_dma_function_config();
        sin_dac_gpio_config();
        sin_adc_channel_config();
        sin_timx_trigger_function_config();
        timer_enable(SIN_TIM);

       
}

使用特权

评论回复
沙发
t714664420|  楼主 | 2023-3-1 10:23 | 只看该作者
/*DAC引脚配置*/
static void sin_dac_gpio_config(void)
{
        /* enable the clock of peripherals */
       
         rcu_periph_clock_enable(SIN_DAC_CLK);       
        rcu_periph_clock_enable(SIN_DAC_CLK);
        rcu_periph_clock_enable(RCU_CRC);
       
        /* once enabled the DAC, the corresponding GPIO pin is connected to the DAC converter automatically */
         gpio_output_options_set(SIN_GPIO_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, SIN_PIN);//推挽输出
   gpio_mode_set(SIN_GPIO_PORT, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, SIN_PIN);
       
}

/*DAC通道输出配置,定时器6触发,不开启输出缓存*/
static void sin_adc_channel_config(void)
{
        dac_deinit();
        dac_trigger_disable(SIN_DAC);
        dac_wave_mode_config(SIN_DAC, DAC_WAVE_DISABLE);
        dac_output_buffer_disable(SIN_DAC);
        dac_trigger_source_config(SIN_DAC,DAC_TRIGGER_T6_TRGO);
        //dac_interrupt_disable(SIN_DAC);
        dac_dma_enable(SIN_DAC);
        dac_trigger_enable(SIN_DAC);
        dac_enable(SIN_DAC);
       
       

      
       
       
}
//DMA配置
static void sin_dma_config(void)
{
        dma_single_data_parameter_struct dma_init_struct;
        /* enable DMA CLK */
        rcu_periph_clock_enable(SIN_DMA_CLK);
        /* deinitialize DMA channel3(USART0 tx) */
        dma_deinit(SIN_DMA, SIN_DMA_CHANNEL);
       
       
        dma_init_struct.direction = DMA_MEMORY_TO_PERIPH;
//        dma_init_struct.memory_addr = ;
//         dma_init_struct.memory0_addr = (uint32_t)Sine;       
        dma_init_struct.memory_inc = DMA_MEMORY_INCREASE_ENABLE;
        dma_init_struct.periph_memory_width = DMA_MEMORY_WIDTH_16BIT;
//        dma_init_struct.number = POINT_NUM2;
//        dma_init_struct.periph_addr = ((uint32_t)(&DAC0_R12DH));;
        dma_init_struct.periph_inc = DMA_PERIPH_INCREASE_DISABLE;
        dma_init_struct.priority = DMA_PRIORITY_ULTRA_HIGH;

dma_single_data_mode_init(SIN_DMA, SIN_DMA_CHANNEL, dma_init_struct);
       
       
       
       


}


void sin_dma_function_config(void)
{       
        //DMAx的通道y的存储器基地址配置
        dma_memory_address_config(SIN_DMA,SIN_DMA_CHANNEL,DMA_MEMORY_1,(uint32_t)Sine);  //(uint32_t)Sine
        //配置DMAx通道y还有多少数据要传输
        dma_transfer_number_config(SIN_DMA,SIN_DMA_CHANNEL,ARRAYNUM(Sine));
        //DMAx的通道y的外设基地址配置
        dma_periph_address_config(SIN_DMA,SIN_DMA_CHANNEL,DAC0_R12DH_DAC0_DOADDRESS);
       
        //DMA循环模式开启
        dma_circulation_enable(SIN_DMA, SIN_DMA_CHANNEL);
        //DMA的通道使能
        dma_channel_enable(SIN_DMA, SIN_DMA_CHANNEL);
       
       

}

/*TIMx触发配置*/
void sin_timx_trigger_function_config(void)
{
        timer_parameter_struct timer_initpara;

        rcu_periph_clock_enable(SIN_TIM_CLK);
        timer_deinit(SIN_TIM);

        /* TIMER0 configuration */
        timer_initpara.prescaler         = 999;//预分频
        timer_initpara.counterdirection  = TIMER_COUNTER_UP;//向上计数
        timer_initpara.period            = 49;//定时周期
        timer_init(SIN_TIM,&timer_initpara);
       


        //定时器主输出触发源选择
        timer_master_output_trigger_source_select(SIN_TIM,TIMER_TRI_OUT_SRC_UPDATE);
        timer_update_event_enable(SIN_TIM);
       
       
       
       
       
       
}

/*操作函数*/
void sin_app(void)
{
       
        sin_dma_config();
        sin_dma_function_config();
        sin_dac_gpio_config();
        sin_adc_channel_config();
        sin_timx_trigger_function_config();
        timer_enable(SIN_TIM);

       
}

改动之后仍然不好使

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

1

主题

5

帖子

0

粉丝