float CAPTURE_PWM_TIME_UP=0;
float CAPTURE_PWM_TIME_ALL=0;
float CAPTURE_PWM_TIME_LAST=0;
/*----------------------------- TIMER14 500hz -----------------------------------*/
void Timer15_pwm_capture(void)
{
TIMER_BaseInitPara TIM_TimeBaseStructure;
NVIC_InitPara NVIC_InitStructure;
GPIO_InitPara GPIO_InitStructure;
TIMER_ICInitPara TIMER_ICInitParaStruct;
RCC_AHBPeriphClock_Enable(RCC_AHBPERIPH_GPIOA , ENABLE);//GPIO时钟使能
RCC_APB2PeriphClock_Enable(RCC_APB2PERIPH_CFG, ENABLE); //映射时钟
RCC_APB2PeriphClock_Enable(RCC_APB2PERIPH_TIMER15, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_PIN_3; //输入捕获
GPIO_InitStructure.GPIO_Mode = GPIO_MODE_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_SPEED_50MHZ;
GPIO_InitStructure.GPIO_OType = GPIO_OTYPE_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PUPD_PULLDOWN;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA,GPIO_PINSOURCE3,GPIO_AF_0);//配置映射为 TIM15_CH2
TIMER_DeInit(TIMER15);
TIM_TimeBaseStructure.TIMER_Period = 30000-1; //720-10k 2.5k
TIM_TimeBaseStructure.TIMER_Prescaler = 1-1;
TIM_TimeBaseStructure.TIMER_ClockDivision = TIMER_CDIV_DIV1;
TIM_TimeBaseStructure.TIMER_CounterMode = TIMER_COUNTER_UP;//向上计数
TIMER_BaseInit(TIMER15, &TIM_TimeBaseStructure); //使能定时器
NVIC_InitStructure.NVIC_IRQ = TIMER15_IRQn; //中断响应优先级
NVIC_InitStructure.NVIC_IRQPreemptPriority=0;
NVIC_InitStructure.NVIC_IRQSubPriority = 0;
NVIC_InitStructure.NVIC_IRQEnable = ENABLE;
NVIC_Init(&NVIC_InitStructure);
//这里配置了一个通道,另一个通道会自动配置
TIMER_ICInitParaStruct.TIMER_CH = TIMER_CH_2;//捕获通道 TIMER_CH_2
TIMER_ICInitParaStruct.TIMER_ICPolarity =TIMER_IC_POLARITY_RISING;//捕获极性上升沿捕获
TIMER_ICInitParaStruct.TIMER_ICSelection =TIMER_IC_SELECTION_DIRECTTI;//映射关系
TIMER_ICInitParaStruct.TIMER_ICPrescaler =TIMER_IC_PSC_DIV1;//分频系数
TIMER_ICInitParaStruct.TIMER_ICFilter =0x00;//滤波器
**TIMER_PWMCaptureConfig(TIMER15,&TIMER_ICInitParaStruct);**//第一种是配置 TIMER_ICInit(),现在这里配置的是TIMER_PWMCaptureConfig();
TIMER_SelectInputTrigger(TIMER15,TIMER_TS_TI2FP2);
TIMER_SelectSlaveMode(TIMER15,TIMER_SLAVE_MODE_RESET);
TIMER_SelectMasterSlaveMode(TIMER15,TIMER_MASTER_SLAVE_MODE_ENABLE);
TIMER_ClearIntBitState(TIMER15, TIMER_INT_UPDATE);//清除定时器中断标志
TIMER_ClearIntBitState(TIMER15, TIMER_INT_CH2);
TIMER_INTConfig(TIMER15,TIMER_INT_CH2, ENABLE);//使能定时器中断
TIMER_SetCounter(TIMER15,0x0000);//设置计数器的值
TIMER_Enable(TIMER15, ENABLE);//使能定时器
}
void TIM15_IRQHandler(void)
{
CAPTURE_PWM_TIME_UP = (float)(TIMER15->CHCC1);//上升沿计数值,读取捕获值会自动清空中断标志
CAPTURE_PWM_TIME_ALL =(float)(TIMER15->CHCC2);//总pwm计数值,读取捕获值会自动清空中断标志
}
|