TIM3做定时器控制LED1闪烁,TIM1做PWM控制LED0,可以独立共做,但是一起的话只有LED1工作,这是为什么?怎么改?

2019-08-18 20:24发布

问题如上述:


#include "led.h"
#include "delay.h"
#include "sys.h"
#include "key.h"
#include "usart.h"
#include "exti.h"
#include "timer.h"

int main(void)
{       
         u32 i=0;
         NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
         delay_init();                     //Ñóê±oˉêy3õê¼»ˉ          
         LED_Init();                          //3õê¼»ˉóëLEDᬽóμÄó2¼t½ó¿ú
         KEY_Init();
         key_exti_init();
         uart2_init(115200);
         TIMx_Int_Init(5000-1,7199);
         TIMx_PWM_Init(800-1,0);
         while(1)
         {
                i++;
                delay_ms(5);
                TIM_SetCompare1(TIM1,i);
                if(i>300)

}}












#include "timer.h"
#include "led.h"
#include "usart.h"

//í¨óö¨ê±Æ÷ÖD¶Ï3õê¼»ˉ
//Õaàïê±ÖóÑ¡ÔñÎaAPB1μÄ2±¶£¬¶øAPB1Îa36M
//arr£o×Ô¶ˉÖØ×°Öμ¡£
//psc£oê±ÖóÔ¤·ÖÆμêy
//Õaàïê1óÃμÄêǶ¨ê±Æ÷3!
void TIMx_Int_Init(u16 arr,u16 psc)
{
  TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
        NVIC_InitTypeDef NVIC_InitStructure;

        RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //TIM3ê±Öóê1Äü

        //¶Ôó|IO¿úμÄ3õê¼»ˉ
       
        TIM_TimeBaseStructure.TIM_Period = arr; //éèÖÃÔúÏÂò»¸ö¸üDÂê¼t×°èë»î¶ˉμÄ×Ô¶ˉÖØ×°ÔؼÄ′æÆ÷ÖüÆúμÄÖμ
        TIM_TimeBaseStructure.TIM_Prescaler =psc; //éèÖÃóÃà′×÷ÎaTIMxê±ÖóÆμÂê3yêyμÄÔ¤·ÖÆμÖμ
        TIM_TimeBaseStructure.TIM_ClockDivision = 0; //éèÖÃê±Öó·Ö¸î:TDTS = Tck_tim
        TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //TIMÏòéϼÆêyÄ£ê½
        TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //¸ù¾YTIM_TimeBaseInitStructÖDÖ¸¶¨μÄ2Îêy3õê¼»ˉTIMxμÄê±¼ä»ùêyμ¥λ

        TIM_ITConfig(  //ê1Äü»òÕßê§ÄüÖ¸¶¨μÄTIMÖD¶Ï
                TIM3, //TIMx
                TIM_IT_Update  |  //TIM ÖD¶ÏÔ′,¸üDÂÖD¶Ï
                TIM_IT_Trigger,   //TIM ′¥·¢ÖD¶ÏÔ′ £¬′¥·¢ÖD¶Ï
                ENABLE  //ê1Äü
                );
        NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;  //TIMxÖD¶Ï
        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;  //ÏèÕ¼óÅÏ輶0¼¶
        NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;  //′óóÅÏ輶3¼¶
        NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQí¨μà±»ê1Äü
        NVIC_Init(&NVIC_InitStructure);  //¸ù¾YNVIC_InitStructÖDÖ¸¶¨μÄ2Îêy3õê¼»ˉíaéèNVIC¼Ä′æÆ÷

        TIM_Cmd(TIM3, ENABLE);  //ê1ÄüTIMxíaéè                                         
}
void TIM3_IRQHandler(void)   //TIMxÖD¶Ï
{
        if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET) //¼ì2éÖ¸¶¨μÄTIMÖD¶Ï·¢éúóë·ñ:TIM ÖD¶ÏÔ′
        {
                TIM_ClearITPendingBit(TIM3, TIM_IT_Update);  //Çå3yTIMxμÄÖD¶Ï′y′|àíλ:TIM ÖD¶ÏÔ′
                        //ìí¼óÖD¶ÏoóÖ′DDμÄê¼t
                LED1=~LED1;
        }
}



//PWMêä3ö3õê¼»ˉ
//arr£o×Ô¶ˉÖØ×°Öμ
//psc£oê±ÖóÔ¤·ÖÆμêy

void TIMx_PWM_Init(u16 arr,u16 psc)
{  
        GPIO_InitTypeDef GPIO_InitStructure;
        TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
        TIM_OCInitTypeDef  TIM_OCInitStructure;
       
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);                                                                                                        //¶¨ê±Æ÷ê1Äü
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);  //ê1ÄüGPIOíaéèoíAFIO¸′óÃ1|ÄüÄ£¿éê±Öóê1Äü
       
//        GPIO_PinRemapConfig(GPIO_PartialRemap_TIMx, ENABLE); //Timerx2¿·ÖÖØó3éä  TIM3_CH2->B5                                                                                //óÃóúTIM3μÄCH2êä3öμ&#196WMí¨1y¸ÃLEDÏÔê¾

  //éèÖøÃòy½ÅÎa¸′óÃêä3ö1|Äü,êä3öTIMx CHxμ&#196WMÂö3å2¨DÎ
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; //TIM_CHx
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;  //¸′óÃíÆíìêä3ö
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOA, &GPIO_InitStructure);
        //GPIO_WriteBit(GPIOx, GPIO_Pin_x,Bit_SET); // Px.xéÏà-       

        TIM_TimeBaseStructure.TIM_Period = arr; //éèÖÃÔúÏÂò»¸ö¸üDÂê¼t×°èë»î¶ˉμÄ×Ô¶ˉÖØ×°ÔؼÄ′æÆ÷ÖüÆúμÄÖμ
        TIM_TimeBaseStructure.TIM_Prescaler =psc; //éèÖÃóÃà′×÷ÎaTIMxê±ÖóÆμÂê3yêyμÄÔ¤·ÖÆμÖμ
        TIM_TimeBaseStructure.TIM_ClockDivision = 0; //éèÖÃê±Öó·Ö¸î:TDTS = Tck_tim
        TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //TIMÏòéϼÆêyÄ£ê½
        TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); //¸ù¾YTIM_TimeBaseInitStructÖDÖ¸¶¨μÄ2Îêy3õê¼»ˉTIMxμÄê±¼ä»ùêyμ¥λ
       
        TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //&#209;&#161;&#212;&#241;&#182;¨ê±&#198;÷&#196;£ê&#189;:TIM&#194;&#246;3&#229;&#191;í&#182;èμ÷&#214;&#198;&#196;£ê&#189;(&#196;£ê&#189;1£ocnt<ccrê±£&#172;óDD§μ&#231;&#198;&#189;£&#187;&#196;£ê&#189;2£ocnt>ccrê±£&#172;óDD§μ&#231;&#198;&#189;£&#187;oí&#188;&#171;D&#212;&#190;&#246;&#182;¨)
        TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //±è&#189;&#207;ê&#228;3&#246;ê1&#196;ü
        TIM_OCInitStructure.TIM_Pulse = 0; //éè&#214;&#195;′y×°è&#235;2&#182;&#187;&#241;±è&#189;&#207;&#188;&#196;′&#230;&#198;÷μ&#196;&#194;&#246;3&#229;&#214;μ
        TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //ê&#228;3&#246;&#188;&#171;D&#212;:TIMê&#228;3&#246;±è&#189;&#207;&#188;&#171;D&#212;&#184;&#223;
        TIM_OC1Init(TIM1, &TIM_OCInitStructure);  //&#184;ù&#190;YTIM_OCInitStruct&#214;D&#214;&#184;&#182;¨μ&#196;2&#206;êy3&#245;ê&#188;&#187;ˉíaéèTIMx,OCx&#182;&#212;ó|chx
       
        TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);  //ê1&#196;üTIMx&#212;úCCR2é&#207;μ&#196;&#212;¤×°&#212;&#216;&#188;&#196;′&#230;&#198;÷
        TIM_CtrlPWMOutputs(TIM1,ENABLE);
        TIM_ARRPreloadConfig(TIM1, ENABLE); //ê1&#196;üTIMx&#212;úARRé&#207;μ&#196;&#212;¤×°&#212;&#216;&#188;&#196;′&#230;&#198;÷
        TIM_Cmd(TIM1, ENABLE);  //ê1&#196;üTIMxíaéè
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。