新手求助,407VET6定时器1PWM输出

2019-07-20 17:20发布

两天前自己配置的定时器1PWM输出,当时能用,同学还看到了的,今天来突然不能用了,大神能不能帮忙看下代码有没有错误
GPIO_InitTypeDef GPIO_InitStructure;
               
        RCC->APB2ENR|=1;//enable the clock of Timer1
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
        RCC_APB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
       
        GPIO_PinAFConfig(GPIOA,GPIO_PinSource8,GPIO_AF_TIM1 );
        GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_TIM1 );
        GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_TIM1 );
        GPIO_PinAFConfig(GPIOA,GPIO_PinSource11,GPIO_AF_TIM1 );
       
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10|GPIO_Pin_11;         
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;        //¸′óÃ1|Äü
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;        //Ëù¶è100MHz
        GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;      //íÆíì¸′óÃêä3ö
        GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;        //éÏà-
        GPIO_Init(GPIOA,&GPIO_InitStructure);         //3õê¼»ˉPA
       
        TIM1->ARR=1000;                        //auto reload
        TIM1->PSC=84;               
       
        TIM1->CCMR1|=6<<4;        //CH1 PWM1 mode
        TIM1->CCMR1|=6<<12;        //CH2 PWM1 mode
        TIM1->CCMR2|=6<<4;        //CH3 PWM1 mode
        TIM1->CCMR2|=6<<12;        //CH4 PWM1 mode
       
        TIM1->CCMR1|=1<<3;        //CH1 preload enable
        TIM1->CCMR1|=1<<11;        //CH2 preload enable
        TIM1->CCMR2|=1<<3;        //CH3 preload enable
        TIM1->CCMR2|=1<<11;        //CH4 preload enable
                       
        TIM1->CCER|=1;                        //enable OC1
        TIM1->CCER|=1<<4;                //enable OC2
        TIM1->CCER|=1<<8;                //enable OC3
        TIM1->CCER|=1<<12;        //enable OC4
       
        TIM1->CCER|=0<<1;         //oc1 active high
        TIM1->CCER|=0<<5;         //oc2 active high
        TIM1->CCER|=0<<9;         //oc3 active high
        TIM1->CCER|=0<<13;         //oc4 active high
       
        TIM1->BDTR|=1<<15;        //enable OC,BDTR mode=1
        TIM1->BDTR|=1<<14;        //enable auto output
        TIM1->EGR|=1;
        TIM1->CR1|=1<<7;                // enable ARPE
        TIM1->CCR1=CCR1;
        TIM1->CCR2=CCR2;
        TIM1->CCR3=CCR3;
        TIM1->CCR4=CCR4;
        TIM1->CR1|=1;                                //enable timer1       

友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
2条回答
HUSTPY
1楼-- · 2019-07-20 22:45
哈哈,找到错误了,是楼主自己白痴了,初始化的是Timer1,对比较器赋值的却是Timer8。
不好意思,误导大家了。代码是正确的。供大家参考。
HUSTPY
2楼-- · 2019-07-21 04:37
还有一个错误就是,GPIOA的时钟应该是AHB1,而我写成了APB1

一周热门 更多>