//TIM4 êä3ö
WM
//TIM2¼ÆêyÂö3åêy
//PWMêä3ö3õê¼»ˉ
//arr£o×Ô¶ˉÖØ×°Öμ
//psc£oê±ÖóÔ¤·ÖÆμêy
void TIM4_PWM_Init(u16 arr,u16 psc)
{
//′Ë2¿·ÖDèêÖ¶ˉDT¸ÄIO¿úéèÖÃ
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4,ENABLE); //TIM14ê±Öóê1Äü
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE); //ê1ÄüPORTFê±Öó
GPIO_PinAFConfig(GPIOB,GPIO_PinSource6,GPIO_AF_TIM4); //GPIOB6¸′óÃÎa¶¨ê±Æ÷4
GPIO_PinAFConfig(GPIOB,GPIO_PinSource7,GPIO_AF_TIM4); //GPIOB6¸′óÃÎa¶¨ê±Æ÷4
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7; //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(GPIOB,&GPIO_InitStructure); //3õê¼»ˉPB6|7
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(TIM4,&TIM_TimeBaseStructure);//3õê¼»ˉ¶¨ê±Æ÷4
TIM4->CR1&=0X03FD; //ê1ÄüÖØ×°ÔØARRÖμ
//3õê¼»ˉTIM4 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_High; //êä3ö¼«DÔ:TIMêä3ö±è½Ï¼«DÔμí
TIM_OCInitStructure.TIM_Pulse=arr/2; //±è½ÏÖμ
TIM_OC1Init(TIM4, &TIM_OCInitStructure); //¸ù¾YTÖ¸¶¨μÄ2Îêy3õê¼»ˉíaéèTIM4OCH1í¨μà
TIM_OC1PreloadConfig(TIM4, TIM_OCPreload_Enable); //ê1ÄüTIM14ÔúCCR1éÏμÄԤװÔؼÄ′æÆ÷
//í¨μà13õê¼»ˉ
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_High; //êä3ö¼«DÔ:TIMêä3ö±è½Ï¼«DÔμí
TIM_OCInitStructure.TIM_Pulse=arr/2; //±è½ÏÖμ
TIM_OC2Init(TIM4, &TIM_OCInitStructure); //¸ù¾YTÖ¸¶¨μÄ2Îêy3õê¼»ˉíaéèTIM4OCH1í¨μà
TIM_OC2PreloadConfig(TIM4, TIM_OCPreload_Enable); //ê1ÄüTIM14ÔúCCR1éÏμÄԤװÔؼÄ′æÆ÷
//í¨μà23õê¼»ˉ
TIM_ARRPreloadConfig(TIM4,ENABLE);//ARPEê1Äü
TIM_SelectOutputTrigger(TIM4,TIM_TRGOSource_OC1Ref);//′¥·¢êä3öμ½TRGO(μ½ÆäËû¶¨ê±Æ÷μÄí¨μà)
TIM_Cmd(TIM4, ENABLE); //ê1ÄüTIM14
TIM_CtrlPWMOutputs(TIM4,ENABLE);
}
void TIM2_Slave_Init(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
TIM_DeInit(TIM2);
TIM_TimeBaseStructure.TIM_Prescaler=0; //¶¨ê±Æ÷·ÖÆμ
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up; //ÏòéϼÆêyÄ£ê½
TIM_TimeBaseStructure.TIM_Period=20; //×Ô¶ˉÖØ×°ÔØÖμ,¼Æêyμ½20òç3ö
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM2,&TIM_TimeBaseStructure);//3õê¼»ˉ¶¨ê±Æ÷2
TIM_SelectInputTrigger(TIM2,TIM_TS_ITR3);//Ñ¡ÔñTIM2μÄêäèë′¥·¢Ô′ Äú2¿3£¨TIM4£©
TIM_InternalClockConfig(TIM2); //éèÖÃtim2μÄÄú2¿ê±Öó
TIM2->SMCR|=0X0007; //SMCR¼Ä′æÆ÷éèÖÃ111£¬′óģ꽣¬ía2¿ê±ÖóÄ£ê½1 éÏéyÑØÇy¶ˉ¼Æêy
TIM_ARRPreloadConfig(TIM2, ENABLE); //ARPEê1Äü
TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE); //ê1Äü¸üDÂÖD¶Ï
TIM_Cmd(TIM2,ENABLE);//êÇ·ñ¿aÆôê±Ö󣨿aÆôoóÿ·¢Ëíò»¸öÂö3壬¶¨ê±Æ÷¼óò»£©
//3õê¼»ˉTIM2£¬Ñ¡Ôñ′óģ꽣¬éÏéyÑؼÆêy£¬ê1Äüòç3öÖD¶Ï
NVIC_InitStructure.NVIC_IRQChannel=TIM2_IRQn; //¶¨ê±Æ÷3ÖD¶Ï
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0x01; //ÇàÕ¼óÅÏ輶1
NVIC_InitStructure.NVIC_IRQChannelSubPriority=0x03; //×óóÅÏ輶3
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
//ÅäÖÃÖD¶Ïoˉêy3õê¼»ˉ
}
void TIM2_IRQHandler(void)
{
if(TIM_GetITStatus(TIM2,TIM_IT_Update)==SET) //òç3öÖD¶Ï
{
TIM_Cmd(TIM4, DISABLE); //2»ê1ÄüTIM4
}
}
我也是用的主从 上升沿计数,没有用门极控制模式;我的成功了。你看看哪里不同
一周热门 更多>