思路:普通IO口配置上升沿下降沿触发中断,然后配置一个定时器(周期大于等于PWM周期),在IO口中断中读取定时器计数。
定时器不需要配置中断
void TIM3_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
/* TIM2 clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
TIM_TimeBaseStructure.TIM_Prescaler = 72-1; //72M/72=1M
TIM_TimeBaseStructure.TIM_Period = 20000; //20ms overflow
TIM_TimeBaseStructure.TIM_ClockDivision = 0x0; //
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
/*load */
TIM_ARRPreloadConfig(TIM3, ENABLE);
//clear IT bit
TIM_ClearITPendingBit(TIM3,TIM_IT_Update);
/* overflow interrupt*/
//TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);
/* allow*/
TIM_Cmd(TIM3, ENABLE);
}
IO口配置
GPIO_InitTypeDef GPIO_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
/*
PE9 CH1 DJ // PE10 CH2 MD // PE11 CH6 adjust */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9|GPIO_Pin_10|GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //50M
GPIO_Init(GPIOE, &GPIO_InitStructure);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOE,GPIO_PinSource9);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOE,GPIO_PinSource10);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOE,GPIO_PinSource11);
EXTI_InitStructure.EXTI_Line = EXTI_Line9 | EXTI_Line10 |EXTI_Line11;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
IO中断配置
NVIC_InitTypeDef NVIC_InitStructure;
/* Configure one bit for preemption priority */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
/* PB6 PE5 interrupt */
NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //highest priority
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/* PE3 interrupt */
NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
中断处理函数(
counter_reload是20000,
TIM_TimeBaseStructure
.
TIM_Period
=
20000
;
)
void EXTI9_5_IRQHandler(void)
{
static u16 C_tmp;
static u16 PE9_Counter;
static u16 PE9_last_count;
if(EXTI_GetITStatus(EXTI_Line9) != RESET)
{
EXTI_ClearITPendingBit(EXTI_Line9);
C_tmp=TIM_GetCounter(TIM3);
//Delay(1);
if(!(GPIOE->IDR & GPIO_Pin_9))
{ // PE6=0 falling
PE9_Counter=C_tmp;
PE9_Counter=(PE9_Counter-PE9_last_count)>0?
(PE9_Counter-PE9_last_count):(PE9_Counter-PE9_last_count+counter_reload);
if (PE9_Counter <Min_duty || PE9_Counter >Max_duty) //1ms~2ms
return;
}
else
{ //Rising
PE9_last_count=C_tmp;
}
}
}
void EXTI15_10_IRQHandler(void)
{
static u16 C_tmp;
static u16 PE10_Counter;
static u16 PE10_last_count;
static u16 PE11_Counter;
static u16 PE11_last_count;
if(EXTI_GetITStatus(EXTI_Line10) != RESET)
{
EXTI_ClearITPendingBit(EXTI_Line10);
C_tmp=TIM_GetCounter(TIM3);
//Delay(1);
if(!(GPIOE->IDR & GPIO_Pin_10))
{ // PE6=0 falling
PE10_Counter=C_tmp;
PE10_Counter=(PE10_Counter-PE10_last_count)>0?
(PE10_Counter-PE10_last_count):(PE10_Counter-PE10_last_count+counter_reload);
if (PE10_Counter <Min_duty || PE10_Counter >Max_duty) //1ms~2ms
return;
}
else
{ //Rising
PE10_last_count=C_tmp;
}
}
else if(EXTI_GetITStatus(EXTI_Line11) != RESET)
{
EXTI_ClearITPendingBit(EXTI_Line11);
C_tmp=TIM_GetCounter(TIM3);
if(!(GPIOE->IDR & GPIO_Pin_11))
{ // PE6=0 falling
PE11_Counter=C_tmp;
PE11_Counter=(PE11_Counter-PE11_last_count)>0?
(PE11_Counter-PE11_last_count):(PE11_Counter-PE11_last_count+counter_reload);
if (PE11_Counter <Min_duty || PE11_Counter >Max_duty) //1ms~2ms
return;
}
else
{ //Rising
PE11_last_count=C_tmp;
}
}
}