#include "timer.h"
#include "led.h"
#include "usart.h"
#include "delay.h"
int n=0,Flag;
u32 Rising1,Falling,Rising2;
u32 Freq=0;
TIM_ICInitTypeDef TIM5_ICInitStructure;
//¶¨ê±Æ÷5í¨μà1êäèë2¶»ñÅäÖÃ
//arr£o×Ô¶ˉÖØ×°Öμ(TIM2,TIM5êÇ32λμÄ!!)
//psc£oê±ÖóÔ¤·ÖÆμêy
void TIM5_CH1_Cap_Init(u32 arr,u16 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5,ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); //ê1ÄüPORTAê±Öó
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; //GPIOA0
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_DOWN; //ÏÂà-
GPIO_Init(GPIOA,&GPIO_InitStructure); //3õê¼»ˉPA0
GPIO_PinAFConfig(GPIOA,GPIO_PinSource0,GPIO_AF_TIM5); //PA0¸′óÃ붨ê±Æ÷5
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(TIM5,&TIM_TimeBaseStructure);
//3õê¼»ˉTIM5êäèë2¶»ñ2Îêy
TIM5_ICInitStructure.TIM_Channel = TIM_Channel_1; //CC1S=01 Ñ¡Ôñêäèë¶Ë IC1ó3éäμ½TI1éÏ
TIM5_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; //éÏéyÑØ2¶»ñ
TIM5_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //ó3éäμ½TI1éÏ
TIM5_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //ÅäÖÃêäèë·ÖÆμ,2»·ÖÆμ
TIM5_ICInitStructure.TIM_ICFilter = 0x00;//IC1F=0000 ÅäÖÃêäèëÂË2¨Æ÷ 2»ÂË2¨
TIM_ICInit(TIM5, &TIM5_ICInitStructure);
TIM_ITConfig(TIM5,TIM_IT_Update|TIM_IT_CC1,ENABLE);//ÔêDí¸üDÂÖD¶Ï ,ÔêDíCC1IE2¶»ñÖD¶Ï
//TIM_Cmd(TIM5,ENABLE ); //ê1Äü¶¨ê±Æ÷5
TIM_ClearITPendingBit(TIM5, TIM_IT_CC1|TIM_IT_Update); //Çå3yÖD¶Ï±ê־λ
NVIC_InitStructure.NVIC_IRQChannel = TIM5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2;//ÇàÕ¼óÅÏ輶3
NVIC_InitStructure.NVIC_IRQChannelSubPriority =0; //×óóÅÏ輶3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQí¨μàê1Äü
NVIC_Init(&NVIC_InitStructure); //¸ù¾YÖ¸¶¨μÄ2Îêy3õê¼»ˉVIC¼Ä′æÆ÷¡¢
TIM_Cmd(TIM5,ENABLE ); //ê1Äü¶¨ê±Æ÷5
delay_ms(10);
}
void TIM5_IRQHandler(void)
{
if(TIM_GetITStatus(TIM5, TIM_IT_CC1) != RESET)
{
n++;
if(n == 1)
{
TIM_OC1PolarityConfig(TIM5,TIM_ICPolarity_Falling);
Rising1 = TIM_GetCapture1(TIM5);
}
if(n == 2)
{
Falling = TIM_GetCapture1(TIM5);
TIM_OC1PolarityConfig(TIM5,TIM_ICPolarity_Rising);
}
if(n == 3)
{
Rising2 = TIM_GetCapture1(TIM5);
Flag = 1;
n = 0;
TIM_ITConfig(TIM5,TIM_IT_CC1,DISABLE);
}
TIM_ClearITPendingBit(TIM5, TIM_IT_CC1);
}
}
一周热门 更多>