本帖最后由 tedeum 于 2013-5-14 16:13 编辑
网上找到一段代码,不是很清楚,请教各位大侠:
void pwm_in_int(void)
{
EXTI_InitTypeDef EXTI_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
/* Configure PB pin as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_8|GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOC, &GPIO_InitStructure);
//初始化TIM2用于计时
TIM_TimeBaseStructure.TIM_Period = 0xFFFF;//20000;
TIM_TimeBaseStructure.TIM_Prescaler = 71;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_Cmd(TIM2, ENABLE);
/* Connect EXTI0 Line to PA.00 pin */
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource3);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource4);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource5);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource8);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource9);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOC, GPIO_PinSource13);
/* Configure EXTI line */
EXTI_InitStructure.EXTI_Line = EXTI_Line3;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
EXTI_InitStructure.EXTI_Line = EXTI_Line4;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
EXTI_InitStructure.EXTI_Line = EXTI_Line5;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
EXTI_InitStructure.EXTI_Line = EXTI_Line8;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
EXTI_InitStructure.EXTI_Line = EXTI_Line9;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
EXTI_InitStructure.EXTI_Line = EXTI_Line13;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
/* Enable and set EXTI Interrupt to the lowest priority */
NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0F;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0F;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = EXTI3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0F;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0F;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = EXTI4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0F;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0F;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0F;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0F;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void EXTI3_IRQHandler(void)
{
if(GPIOB->IDR & GPIO_Pin_3)
{
//高启动定时器,清标准字
PPM_star[1]=TIM_GetCounter(TIM2);
}
else
{
PPM_end[1]=TIM_GetCounter(TIM2)-PPM_star[1];
if(PPM_end[1]>900 && PPM_end[1]<2100)
{PPMINF[1]=PPM_end[1]-INF_DAT[PPM_mid1];}
}
EXTI_ClearITPendingBit(EXTI_Line3);
}
void EXTI4_IRQHandler(void)
{
if(GPIOB->IDR & GPIO_Pin_4)
{
//高启动定时器,清标准字
PPM_star[2]=TIM_GetCounter(TIM2);
}
else
{
PPM_end[2]=TIM_GetCounter(TIM2)-PPM_star[2];
if(PPM_end[2]>900 && PPM_end[2]<2100)
{PPMINF[2]=PPM_end[2]-INF_DAT[PPM_mid2];}
}
EXTI_ClearITPendingBit(EXTI_Line4);
}
void EXTI9_5_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line5) != RESET)
{
//高开始计时
//低停止计时
if(GPIOB->IDR & GPIO_Pin_5)
{
//高启动定时器,清标准字
PPM_star[3]=TIM_GetCounter(TIM2);
}
else
{
PPM_end[3]=TIM_GetCounter(TIM2)-PPM_star[3];
if(PPM_end[3]>900 && PPM_end[3]<2100)
{PPMINF[3]=PPM_end[3]-INF_DAT[PPM_mid3];}
}
EXTI_ClearITPendingBit(EXTI_Line5);
}
if(EXTI_GetITStatus(EXTI_Line8) != RESET)
{
//高开始计时
//低停止计时
if(GPIOB->IDR & GPIO_Pin_8)
{
//高启动定时器,清标准字
PPM_star[4]=TIM_GetCounter(TIM2);
}
else
{
PPM_end[4]=TIM_GetCounter(TIM2)-PPM_star[4];
if(PPM_end[4]>900 && PPM_end[4]<2100)
{PPMINF[4]=PPM_end[4]-INF_DAT[PPM_mid4];}
}
EXTI_ClearITPendingBit(EXTI_Line8);
}
if(EXTI_GetITStatus(EXTI_Line9) != RESET)
{
//高开始计时
//低停止计时
if(GPIOB->IDR & GPIO_Pin_9)
{
//高启动定时器,清标准字
PPM_star[5]=TIM_GetCounter(TIM2);
}
else
{
PPM_end[5]=TIM_GetCounter(TIM2)-PPM_star[5];
if(PPM_end[5]>900 && PPM_end[5]<2100)
{PPMINF[5]=PPM_end[5]-INF_DAT[PPM_mid5];}
}
EXTI_ClearITPendingBit(EXTI_Line9);
}
}
//外部中断
void EXTI15_10_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line13) != RESET)
{
//高开始计时
//低停止计时
if(GPIOC->IDR & GPIO_Pin_13)
{
//高启动定时器,清标准字
PPM_star[6]=TIM_GetCounter(TIM2);
}
else
{
PPM_end[6]=TIM_GetCounter(TIM2)-PPM_star[6];
if(PPM_end[6]>900 && PPM_end[6]<2100)
{PPMINF[6]=PPM_end[6]-INF_DAT[PPM_mid6];}
}
EXTI_ClearITPendingBit(EXTI_Line13);
}
}
|