stm32f0 TIM1实现输出PWM波形

//该程序例程是使用的GDstm32f130的芯片,使用TIM1的通道2和通道3输出波形


int main(void)
{     

   u16 led0pwmval=0;
    u8 dir=1;   
    SystemOn_Init();
    InitRFChip();
    PWM_TIM1(899,0);  //?μ?ê?a48MHZ/(999+1)/(1+99)=24KHZ
    while (1)
    {      

      Delay_MS(1);
      if(dir)led0pwmval+=1;
        else led0pwmval-=1;
        if(led0pwmval>800)dir=0;
        if(led0pwmval==0)dir=1; 
        TIM_SetCompare2(TIM1,led0pwmval);      
        TIM_SetCompare3(TIM1,led0pwmval);

    }

}




//IO口初始化
void USART1_GPIO_Configuration(void)
{
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA,ENABLE);
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_2);   //TIM1channel2
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_2);  //TIM1channel3

//PA9(UART Tx) 
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; 
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; 
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; 
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; 
    GPIO_Init(GPIOA, &GPIO_InitStructure); 

//PA10(UART Rx) 
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; 
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; 
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; 
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; 
    GPIO_Init(GPIOA, &GPIO_InitStructure); 

}

//定时器TIM1的初始化
void PWM_TIM1(uint16_t arr,uint16_t psc)
{
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);    //ê±?óê1?ü

   USART1_GPIO_Configuration();

    TIM_DeInit(TIM1);

     TIM_TimeBaseStructure.TIM_Period =arr;                 //éè???ú??ò????üD?ê??t×°è????ˉμ?×??ˉ??×°????′??÷?ü?úμ??μ
     TIM_TimeBaseStructure.TIM_Prescaler =psc;              //éè??ó?à′×÷?aTIMxê±?ó?μ?ê3yêyμ??¤·??μ?μ,10Khzμ???êy?μ?ê
     TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;    //éè??ê±?ó·???:TDTS = Tck_tim
     TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //?òé???êy?£ê?
    //TIM_TimeBaseStructure.TIM_RepetitionCounter=0;    //???′??′??÷
     TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);            //?ù?YTIM_TimeBaseInitStruct?D???¨μ?2?êy3?ê??ˉ

     //TIM1μ?channel2 PWMê?3? 
    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; 
    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //ê?3?±è??ê1?ü
    TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //ê?3?±è????D??a??μ???
    TIM_OC2Init(TIM1, &TIM_OCInitStructure);        //3?ê??ˉ
    TIM_OC2PreloadConfig(TIM1,TIM_OCPreload_Enable);    
 /*    
    TIM_OCInitStructure.TIM_OCNPolarity=TIM_OCNPolarity_High;        //éè???¥21??ê?3???D? 
    TIM_OCInitStructure.TIM_Pulse=30;
    TIM_OCInitStructure.TIM_OutputNState=TIM_OutputNState_Disable;     //ê1?ü?¥21????D?      
    TIM_OCInitStructure.TIM_OCIdleState=TIM_OCIdleState_Reset;        //?à??oóê?3?×′ì?        
    TIM_OCInitStructure.TIM_OCNIdleState=TIM_OCNIdleState_Reset;     //?à??oó?¥21??ê?3?×′ì?
    TIM_OC2Init(TIM1, &TIM_OCInitStructure);        //3?ê??ˉ
    TIM_OC2PreloadConfig(TIM1,TIM_OCPreload_Enable);    

    TIM_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Disable;//??DD?£ê???ê?3?
    TIM_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Disable;//???D?£ê???ê?3?????         
    TIM_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_OFF;      //???¨éè??      
    TIM_BDTRInitStructure.TIM_DeadTime = 0x90;                                         //?à??ê±??       
    TIM_BDTRInitStructure.TIM_Break = TIM_Break_Disable;                 //é23μ1|?üê1?ü     
    TIM_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_High;//é23μê?3???D?       
    TIM_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Disable;//   ×??ˉê?3?ê1?ü   
    TIM_BDTRConfig(TIM1,&TIM_BDTRInitStructure);
  */
      //TIM1μ?channel3  PWMê?3?
    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //TIM1ê?3?±è??′¥·¢?£ê?
    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //ê1?üê?3?±è??×′ì?
    TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //éè??ê?3???D??a??μ???
    TIM_OC3Init(TIM1, &TIM_OCInitStructure);
    TIM_OC3PreloadConfig(TIM1, TIM_OCPreload_Enable);   //ê1?ü?¤×°????′??÷   
    TIM_ARRPreloadConfig(TIM1,ENABLE); 
     TIM_Cmd(TIM1, ENABLE);  //ê1?üTIM1

     TIM_CtrlPWMOutputs(TIM1,ENABLE);

}

你可能感兴趣的:(stm32)