新手上路
- 积分
- 20
- 金钱
- 20
- 注册时间
- 2017-7-16
- 在线时间
- 15 小时
|
TIM_ICInitTypeDef TIM4_ICInitStructure;
void TIM4_Cap_Init(u16 arr,u16 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); //ʹÄÜTIM4ʱÖÓ
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); //ʹÄÜGPIOBʱÖÓ
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; //PB6 Çå³ý֮ǰÉèÖÃ
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //PB6 ÊäÈë
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_ResetBits(GPIOB,GPIO_Pin_6); //PB6 ÏÂÀ­
//³õʼ»¯¶¨Ê±Æ÷4 TIM4
TIM_TimeBaseStructure.TIM_Period = arr; //É趨¼ÆÊýÆ÷×Ô¶¯ÖØ×°Öµ
TIM_TimeBaseStructure.TIM_Prescaler =psc; //Ô¤·ÖƵÆ÷
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //ÉèÖÃʱÖÓ·Ö¸î:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIMÏòÉϼÆÊýģʽ
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); //¸ù¾ÝTIM_TimeBaseInitStructÖÐÖ¸¶¨µÄ²ÎÊý³õʼ»¯TIMxµÄʱ¼ä»ùÊýµ¥Î»
//³õʼ»¯TIM2ÊäÈ벶»ñ²ÎÊý
TIM4_ICInitStructure.TIM_Channel = TIM_Channel_1; //CC1S=01 Ñ¡ÔñÊäÈë¶Ë IC1Ó³Éäµ½TI1ÉÏ
TIM4_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling; //ÉÏÉýÑز¶»ñ
TIM4_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //Ó³Éäµ½TI1ÉÏ
TIM4_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //ÅäÖÃÊäÈë·ÖƵ,²»·ÖƵ
TIM4_ICInitStructure.TIM_ICFilter = 0x00;//IC1F=0000 ÅäÖÃÊäÈëÂ˲¨Æ÷ ²»Â˲¨
TIM_ICInit(TIM4, &TIM4_ICInitStructure);
//ÖжϷÖ×é³õʼ»¯
NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn; //TIM2ÖжÏ
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; //ÏÈÕ¼ÓÅÏȼ¶2¼¶
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //´ÓÓÅÏȼ¶0¼¶
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQͨµÀ±»Ê¹ÄÜ
NVIC_Init(&NVIC_InitStructure); //¸ù¾ÝNVIC_InitStructÖÐÖ¸¶¨µÄ²ÎÊý³õʼ»¯ÍâÉèNVIC¼Ä´æÆ÷
TIM_ITConfig(TIM4,TIM_IT_Update|TIM_IT_CC1,ENABLE);//ÔÊÐí¸üÐÂÖÐ¶Ï ,ÔÊÐíCC1IE²¶»ñÖжÏ
TIM_Cmd(TIM4,ENABLE ); //ʹÄܶ¨Ê±Æ÷2
}
u8 TIM4CH1_CAPTURE_STA=0; //ÊäÈ벶»ñ״̬
u16 TIM4CH1_CAPTURE_VAL; //ÊäÈ벶»ñÖµ
//¶¨Ê±Æ÷5ÖжϷþÎñ³ÌÐò
void TIM4_IRQHandler(void)
{
if((TIM4CH1_CAPTURE_STA&0X80)==0)//»¹Î´³É¹¦²¶»ñ
{
if (TIM_GetITStatus(TIM4, TIM_IT_Update) != RESET)//·¢ÉúÖжÏ
{
if(TIM4CH1_CAPTURE_STA&0X40)//ÒѾ­²¶»ñµ½¸ßµçƽÁË
{
if((TIM4CH1_CAPTURE_STA&0X3F)==0X3F)//¸ßµçƽ̫³¤ÁË
{
TIM4CH1_CAPTURE_STA|=0X80;//±ê¼Ç³É¹¦²¶»ñÁËÒ»´Î
TIM4CH1_CAPTURE_VAL=0XFFFF;
}else TIM4CH1_CAPTURE_STA++;
}
}
if (TIM_GetITStatus(TIM4, TIM_IT_CC1) != RESET)//²¶»ñ1·¢Éú²¶»ñʼþ
{
if(TIM4CH1_CAPTURE_STA&0X40) //²¶»ñµ½Ò»¸öϽµÑØ
{
TIM4CH1_CAPTURE_STA|=0X80; //±ê¼Ç³É¹¦²¶»ñµ½Ò»´ÎÉÏÉýÑØ
TIM4CH1_CAPTURE_VAL=TIM_GetCapture1(TIM4);
TIM_OC1PolarityConfig(TIM4,TIM_ICPolarity_Rising); //CC1P=0 ÉèÖÃΪÉÏÉýÑز¶»ñ
}
else //»¹Î´¿ªÊ¼,µÚÒ»´Î²¶»ñÉÏÉýÑØ
{
TIM4CH1_CAPTURE_STA=0; //Çå¿Õ
TIM4CH1_CAPTURE_VAL=0;
TIM_SetCounter(TIM4,0);
TIM4CH1_CAPTURE_STA|=0X40; //±ê¼Ç²¶»ñµ½ÁËÉÏÉýÑØ
}
}
}
TIM_ClearITPendingBit(TIM4, TIM_IT_CC1|TIM_IT_Update); //Çå³ýÖжϱê־λ
}
|
|