stm32f103 tim3用dma修改占空比,第一个脉冲出错

2019-08-17 09:21发布

主控stm32f103rbt6  tim3用dma修改占空比,第一个脉冲是TIM_OCInitStruct.TIM_Pulse 初始化时的占空比,第二个占空比才是dma传输的占空比

代码如下
GPIO_InitTypeDef            GPIO_InitStructure;
    DMA_InitTypeDef             DMA_InitStructure;
    TIM_TimeBaseInitTypeDef     TIM_TimeBaseInitStruct;
    TIM_OCInitTypeDef           TIM_OCInitStruct;

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);

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


    /* DMA1 Channel5 Config */
    DMA_DeInit(DMA1_Channel3);

    DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)(&TIM3->CCR1);
    DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)ccr_buf;
    DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
    DMA_InitStructure.DMA_BufferSize = 10;
    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_Channel3, &DMA_InitStructure);

//    /* DMA1 Channel5 enable */
    DMA_Cmd(DMA1_Channel3, ENABLE);
    TIM_Cmd(TIM3, DISABLE);
    TIM_TimeBaseInitStruct.TIM_Prescaler = 7200 - 1;
    TIM_TimeBaseInitStruct.TIM_Period = 100 - 1;
    TIM_TimeBaseInitStruct.TIM_ClockDivision = 0;
    TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseInitStruct.TIM_RepetitionCounter = 0;
    TIM_TimeBaseInit(TIM3, &TIM_TimeBaseInitStruct);     
//   

//    TIM_DMAConfig(TIM3, TIM_DMABase_CCR1, TIM_DMABurstLength_10Bytes);


    TIM_OCInitStruct.TIM_OCMode = TIM_OCMode_PWM1;
    TIM_OCInitStruct.TIM_OCPolarity = TIM_OCPolarity_Low;
    TIM_OCInitStruct.TIM_OutputState = TIM_OutputState_Enable;
    /* TIM_Pulse =  TIM_Period + 12ÅêÇ100%*/
//    TIM_OCInitStruct.TIM_Pulse = 0;
    TIM_OC1Init(TIM3, &TIM_OCInitStruct);

    /* ê1Äü»áê13õê¼»ˉÕ¼¿Õ±è3ö2′Î */
    TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Disable);


    TIM_Cmd(TIM3, ENABLE);
    TIM_DMACmd(TIM3, TIM_DMA_Update, ENABLE);

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