TIM2_PWM_Init(300,84-1);
while(1)
{
if(dir)led0pwmval+=1;//dir==1 led0pwmvalμYÔö
else led0pwmval-=1; //dir==0 led0pwmvalμY¼õ
if(led0pwmval>300)dir=0;//led0pwmvalμ½′ï300o󣬷½ÏòÎaμY¼õ
if(led0pwmval==0)dir=1; //led0pwmvalμY¼õμ½0o󣬷½Ïò¸ÄÎaμYÔö
TIM_SetCompare1(TIM2,led0pwmval); //DT¸Ä±è½ÏÖμ
delay_ms(10);
// LED0=0;
// OSTimeDlyHMSM(0,0,0,200,OS_OPT_TIME_HMSM_STRICT,&err); //Ñóê±200ms
// LED0=1;
// OSTimeDlyHMSM(0,0,0,300,OS_OPT_TIME_HMSM_STRICT,&err); //Ñóê±500ms
}
void TIM2_PWM_Init(u32 arr,u32 psc)
{
//′Ë2¿·ÖDèêÖ¶ˉDT¸ÄIO¿úéèÖÃ
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE); //TIM14ê±Öóê1Äü
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE); //ê1ÄüPORTFê±Öó
GPIO_PinAFConfig(GPIOC,GPIO_PinSource1,GPIO_AF_TIM2); //GPIOF9¸′óÃÎa¶¨ê±Æ÷14
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; //GPIOF9
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(GPIOC,&GPIO_InitStructure); //3õê¼»ˉPF9
TIM_TimeBaseStructure.TIM_Prescaler=psc; //¶¨ê±Æ÷·ÖÆμ
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up; //ÏòéϼÆêyÄ£ê½
TIM_TimeBaseStructure.TIM_Period=arr; //×Ô¶ˉÖØ×°ÔØÖμ
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM2,&TIM_TimeBaseStructure);//3õê¼»ˉ¶¨ê±Æ÷14
//3õê¼»ˉTIM14 Channel1 PWMÄ£ê½
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //Ñ¡Ôñ¶¨ê±Æ÷Ä£ê½:TIMÂö3å¿í¶èμ÷ÖÆÄ£ê½2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //±è½Ïêä3öê1Äü
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //êä3ö¼«DÔ:TIMêä3ö±è½Ï¼«DÔμí
// TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init(TIM2, &TIM_OCInitStructure); //¸ù¾YTÖ¸¶¨μÄ2Îêy3õê¼»ˉíaéèTIM1 4OC1
TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Enable); //ê1ÄüTIM14ÔúCCR1éÏμÄԤװÔؼÄ′æÆ÷
TIM_ARRPreloadConfig(TIM2,ENABLE);//ARPEê1Äü
TIM_Cmd(TIM2, ENABLE); //ê1ÄüTIM14
}
LED灯亮度未出现变化
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>