打印
[STM32F1]

求问STM32F103C8T6S使用TIM2 PWM DMA的问题

[复制链接]
1678|3
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
yhummqj|  楼主 | 2016-9-7 13:51 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
目的: 使用DMA读取buffer里面数据,填充TIM2的OC1的CCR1寄存器,输出不同占空比的PWM。现在不知怎么使用DMA,参照stdperiph中TIM1 DMA的例程。无法实现pwm输出。求帮助。

#define TIM2_CCR1_Address    0x40000034

#define TIME2_DMA_ENABLE

uint16_t SRC_Buffer[10] = {200,500,200,500,200,500,200,500,200,500};
void Time2_Initialzation(void)
{
        GPIO_InitTypeDef GPIO_InitStructure;
        TIM_OCInitTypeDef TIM_OCInitStructure;
        TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
        DMA_InitTypeDef DMA_InitStructure;
       

       
        /* TIM2 Configuration ---------------------------------------------------
         TIM2CLK = 72 MHz, Prescaler = 0, TIM2 counter clock = 72 MHz
         TIM2 frequency = TIM2CLK/(TIM2_Period + 1) = 17.57 KHz
        - TIM2 Channel1 & Channel1N duty cycle = TIM2->CCR1 / (TIM2_Period + 1) = 50%
        ----------------------------------------------------------------------- */

        /* Time Base configuration */
        TIM_TimeBaseStructure.TIM_Prescaler = 0;
        TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
        TIM_TimeBaseStructure.TIM_Period = 1023;
        TIM_TimeBaseStructure.TIM_ClockDivision = 0;
        TIM_TimeBaseStructure.TIM_RepetitionCounter = 2;

        TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);

        /* GPIOB Configuration: Channel 1 as alternate function push-pull */
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOA, &GPIO_InitStructure);


        /* Channel 1, 2,3 and 4 Configuration in PWM mode */
        TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
        TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
        TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Disable;;
        TIM_OCInitStructure.TIM_Pulse = 0;
        //TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
        TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
        TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_Low;
        //TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
        TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset;
        TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Reset;

        TIM_OC1Init(TIM2, &TIM_OCInitStructure);

#ifdef TIME2_DMA_ENABLE
        /* DMA1 Channel5 Config */
        DMA_DeInit(DMA1_Channel5);
        //DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)TIM2_CCR1_Address;
        DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&TIM2->CCR1;
        DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)SRC_Buffer;
        DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
        DMA_InitStructure.DMA_BufferSize = 3;
        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_Channel5, &DMA_InitStructure);

        /* DMA1 Channel5 enable */
        DMA_Cmd(DMA1_Channel5, ENABLE);
        //TIM_DMAConfig(TIM2,TIM_DMABase_CCR1,TIM_DMABurstLength_2Byte);
        /* TIM1 Update DMA Request enable */
        TIM_DMACmd(TIM2, TIM_DMA_Update, ENABLE);
        //TIM_DMACmd(TIM2, TIM_DMA_CC1, ENABLE);
        //

#endif
        /* TIM2 counter enable */
        TIM_Cmd(TIM2, ENABLE);

        /* TIM2 Main Output Enable */
        TIM_CtrlPWMOutputs(TIM2, ENABLE);

}

RCC 已配置,请忽略。
沙发
yhummqj|  楼主 | 2016-9-7 13:56 | 只看该作者
楼主 智商着急。

使用特权

评论回复
板凳
yhummqj|  楼主 | 2016-9-8 09:16 | 只看该作者

使用特权

评论回复
地板
yhummqj|  楼主 | 2016-9-8 09:51 | 只看该作者
本帖最后由 yhummqj 于 2016-9-8 09:53 编辑

结贴。参考 https://bbs.21ic.com/forum.php?mo ... highlight=tim%2Bdma 得知,自己想当然了。
以为基本采用例程就以为可以实现。折磨了几天。发现是DMA通道选错。使用TIM2的channel1通道,应该使用DMA_InitStructure.DMA_PeripheralBaseAddr填写TIM2->CCR1这个都没次有错。TIM_DMACmd(TIM2, TIM_DMA_Update, ENABLE) 这条指令是准备在TIM2,update时,更新TIM2->CCR1的值。
此时DMA通道应该选择TIM2_UPDATE,应该不是选择TIM2_CH1.所以将程序中的 DMA1_Channel5 修改成 DMA1_Channel2 就可以了。

使用特权

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

本版积分规则

1

主题

18

帖子

0

粉丝