void TIM2_Init()
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE); //GPIOA时钟使能
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE); //TIM2时钟使能
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN; //端口模式
GPIO_InitStructure.GPIO_OType=GPIO_OType_PP; //
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0;
GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_NOPULL; //
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_100MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure); //初始化GPIOA_0
TIM_TimeBaseInitStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseInitStructure.TIM_Period=0xFFFF;
TIM_TimeBaseInitStructure.TIM_Prescaler=0x00;
TIM_TimeBaseInitStructure.TIM_RepetitionCounter=0;
TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitStructure); //定时器模块基本配置
TIM_ITRxExternalClockConfig(TIM2,TIM_TS_ETRF); //外部触发输入ETRF
TIM_ETRClockMode2Config(TIM2,TIM_ExtTRGPSC_OFF,TIM_ExtTRGPolarity_NonInverted,0); //不分频,上升沿
TIM_SetCounter(TIM2,1000);
TIM_Cmd(TIM2,ENABLE);
}
void RUNNING()
{
GPIO_ResetBits(GPIOF,GPIO_Pin_9);
GPIO_SetBits(GPIOF,GPIO_Pin_10);
delay_ms(1000);
GPIO_SetBits(GPIOF,GPIO_Pin_9);
GPIO_ResetBits(GPIOF,GPIO_Pin_10);
delay_ms(1000);
}
int main(void)
{
u32 t=0;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
uart_init(115200);
delay_init(84);
TIM2_Init();
LED_Init();
while(1)
{
RUNNING();
t=TIM_GetCounter(TIM2);
printf("脉冲计数值t=%d\r\n",t);
}
}
|