代码如下,就是根据例程改的,哪里出了问题 求大神指教
void TIM4_Cap_Init(u16 arr,u16 psc)
{
//**************************************êäèë2¶»ñ·½°¸********************************
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); //ê1ÄüTIM4ê±Öó
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); //ê1ÄüGPIOBê±Öó
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6| GPIO_Pin_7| GPIO_Pin_8; //Çå3y֮ǰéèÖÃ
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //êäèë
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_ResetBits(GPIOB,GPIO_Pin_6); //ÏÂà-
GPIO_ResetBits(GPIOB,GPIO_Pin_7); //ÏÂà-
GPIO_ResetBits(GPIOB,GPIO_Pin_8); //ÏÂà-
//3õê¼»ˉ¶¨ê±Æ÷4 TIM4
TIM_TimeBaseStructure.TIM_Period = arr; //é趨¼ÆêyÆ÷×Ô¶ˉÖØ×°Öμ
TIM_TimeBaseStructure.TIM_Prescaler =psc; //Ô¤·ÖÆμÆ÷
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //éèÖÃê±Öó·Ö¸î:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIMÏòéϼÆêyÄ£ê½
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); //¸ù¾YTIM_TimeBaseInitStructÖDÖ¸¶¨μÄ2Îêy3õê¼»ˉTIMxμÄê±¼ä»ùêyμ¥λ
//3õê¼»ˉTIM5êäèë2¶»ñ2Îêy
TIM4_ICInitStructure.TIM_Channel = TIM_Channel_1; //CC1S=01 Ñ¡Ôñêäèë¶Ë IC1ó3éäμ½TI1éÏ
TIM4_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; //éÏéyÑØ2¶»ñ
TIM4_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //ó3éäμ½TI1éÏ
TIM4_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //ÅäÖÃêäèë·ÖÆμ,2»·ÖÆμ
TIM4_ICInitStructure.TIM_ICFilter = 0x00;//IC1F=0000 ÅäÖÃêäèëÂË2¨Æ÷ 2»ÂË2¨
TIM_ICInit(TIM4, &TIM4_ICInitStructure);
TIM4_ICInitStructure.TIM_Channel = TIM_Channel_2; //CC1S=01 Ñ¡Ôñêäèë¶Ë IC1ó3éäμ½TI1éÏ
TIM4_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; //éÏéyÑØ2¶»ñ
TIM4_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //ó3éäμ½TI1éÏ
TIM4_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //ÅäÖÃêäèë·ÖÆμ,2»·ÖÆμ
TIM4_ICInitStructure.TIM_ICFilter = 0x00;//IC1F=0000 ÅäÖÃêäèëÂË2¨Æ÷ 2»ÂË2¨
TIM_ICInit(TIM4, &TIM4_ICInitStructure);
TIM4_ICInitStructure.TIM_Channel = TIM_Channel_3; //CC1S=01 Ñ¡Ôñêäèë¶Ë IC1ó3éäμ½TI1éÏ
TIM4_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; //éÏéyÑØ2¶»ñ
TIM4_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //ó3éäμ½TI1éÏ
TIM4_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //ÅäÖÃêäèë·ÖÆμ,2»·ÖÆμ
TIM4_ICInitStructure.TIM_ICFilter = 0x00;//IC1F=0000 ÅäÖÃêäèëÂË2¨Æ÷ 2»ÂË2¨
TIM_ICInit(TIM4, &TIM4_ICInitStructure);
//ÖD¶Ï·Ö×é3õê¼»ˉ
NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn; //TIM4ÖD¶Ï
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; //ÏèÕ¼óÅÏ輶2¼¶
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //′óóÅÏ輶0¼¶
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQí¨μà±»ê1Äü
NVIC_Init(&NVIC_InitStructure); //¸ù¾YNVIC_InitStructÖDÖ¸¶¨μÄ2Îêy3õê¼»ˉíaéèNVIC¼Ä′æÆ÷
TIM_ITConfig(TIM4,TIM_IT_Update|TIM_IT_CC1,ENABLE);//ÔêDí¸üDÂÖD¶Ï ,ÔêDíCC1IE2¶»ñÖD¶Ï
TIM_ITConfig(TIM4,TIM_IT_CC2,ENABLE);//ÔêDí¸üDÂÖD¶Ï ,ÔêDíCC1IE2¶»ñÖD¶Ï
TIM_ITConfig(TIM4,TIM_IT_CC3,ENABLE);//ÔêDí¸üDÂÖD¶Ï ,ÔêDíCC1IE2¶»ñÖD¶Ï
TIM_SetCounter(TIM4, 0);
TIM_Cmd(TIM4,ENABLE ); //ê1Äü¶¨ê±Æ÷5
}
//****************************************pwmêä3ö************************************************************
void TIM4_IRQHandler(void)
{
if (TIM_GetITStatus(TIM4, TIM_IT_CC1) != RESET)//2¶»ñ1·¢éú2¶»ñê¼t
colour_count++;
if (TIM_GetITStatus(TIM4, TIM_IT_CC2) != RESET)//2¶»ñ2·¢éú2¶»ñê¼t
code_disk1++;
if (TIM_GetITStatus(TIM4, TIM_IT_CC3) != RESET)//2¶»ñ3·¢éú2¶»ñê¼t
code_disk2++;
TIM_ClearITPendingBit(TIM4, TIM_IT_CC1|TIM_IT_Update); //Çå3yÖD¶Ï±ê־λ
TIM_ClearITPendingBit(TIM4, TIM_IT_CC2|TIM_IT_Update); //Çå3yÖD¶Ï±ê־λ
TIM_ClearITPendingBit(TIM4, TIM_IT_CC3|TIM_IT_Update); //Çå3yÖD¶Ï±ê־λ
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>