void TIM14_PWM_Init(u32 arr,u32 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM14,ENABLE); //TIM14ʱÖÓʹÄÜ
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE); //ʹÄÜPORTFʱÖÓ
GPIO_PinAFConfig(GPIOF,GPIO_PinSource9,GPIO_AF_TIM14); //GPIOF9¸´ÓÃΪ¶¨Ê±Æ÷14
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //GPIOF9
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //¸´Óù¦ÄÜ
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; //ËÙ¶È100MHz
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //ÍÆÍ츴ÓÃÊä³ö
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //ÉÏÀ
GPIO_Init(GPIOF,&GPIO_InitStructure); //³õʼ»¯PF9
TIM_TimeBaseStructure.TIM_Prescaler=psc; //¶¨Ê±Æ÷·ÖƵ
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up; //ÏòÉϼÆÊýģʽ
TIM_TimeBaseStructure.TIM_Period=arr; //×Ô¶¯ÖØ×°ÔØÖµ
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM14,&TIM_TimeBaseStructure);//³õʼ»¯¶¨Ê±Æ÷14
//³õʼ»¯TIM14 Channel1 PWMģʽ
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //Ñ¡Ôñ¶¨Ê±Æ÷ģʽ:TIMÂö³å¿í¶Èµ÷ÖÆģʽ2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //±È½ÏÊä³öʹÄÜ
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //Êä³ö¼«ÐÔ:TIMÊä³ö±È½Ï¼«ÐÔµÍ
TIM_OC1Init(TIM14, &TIM_OCInitStructure); //¸ù¾ÝTÖ¸¶¨µÄ²ÎÊý³õʼ»¯ÍâÉèTIM1 4OC1
TIM_OC1PreloadConfig(TIM14, TIM_OCPreload_Enable); //ʹÄÜTIM14ÔÚCCR1ÉϵÄԤװÔؼĴæÆ÷
TIM_ARRPreloadConfig(TIM14,ENABLE);//ARPEʹÄÜ
TIM_CtrlPWMOutputs(TIM14,ENABLE);
TIM_Cmd(TIM14, ENABLE); //ʹÄÜTIM14
}
TIM14_PWM_Init(1000-1,0);
TIM_SetCompare1(TIM14,500); |