本帖最后由 bsaver 于 2014-6-27 16:10 编辑
大家好,我使用TIM1重映射后 利用OC2在 PE.11 管脚输出PWM,使用 PE.3配置外部中断,发现对PE3输入标准的方波信号(由示波器测试信号输入,保证无干扰),PE3浮空输入 带10K上拉电阻,在中断里面做数据累加,无其他操作指令,发现上电1~2秒内中断次数就能上万,请问这是哪里的问题?源代码如下:
void TIMx_PwmInit(void)
{
TIM_OCInitTypeDef TIM_OCInitStruct;
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
//TIM1 Periph clock enable
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE); //定时器1时钟使能,1分频,72M
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOE, &GPIO_InitStructure);
/* TIM1 Full remapping pins */
GPIO_PinRemapConfig(GPIO_FullRemap_TIM1, ENABLE);
TIM_DeInit(TIM1); //重新将Timer设置为缺省值
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_TimeBaseStructure.TIM_Prescaler = (72 - 1); //psc 设置预分频数 72M/72 = 1000 KHz
TIM_TimeBaseStructure.TIM_Period = PWM_PERIOD; //设置自动装载寄存器 1000 KHz / (period + 1) = 每秒钟中断次数
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //=0x0000 设置时间分割值
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //选择向上计数
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
/* Set the default configuration */
TIM_OCStructInit(&TIM_OCInitStruct);
TIM_OCInitStruct.TIM_OCMode = TIM_OCMode_PWM2; //PWM1模式
TIM_OCInitStruct.TIM_Pulse = PWM1_PULSE; //
TIM_OCInitStruct.TIM_OutputState = TIM_OutputState_Enable; //信号输出到对应的输出引脚
TIM_OCInitStruct.TIM_OCIdleState = TIM_OCIdleState_Set;
TIM_OC2Init(TIM1, &TIM_OCInitStruct);
TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM1, ENABLE);
TIM_CtrlPWMOutputs(TIM1, ENABLE); //使能PWM输出
TIM_Cmd(TIM1, ENABLE); //使能定时器
}
void ExGPIO_Config(void)
{
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
/* Enable GPIOE clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE, ENABLE);
/* Configure PE.03 pin as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(GPIOE, &GPIO_InitStructure);
/* Enable AFIO clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
/* Connect EXTI3 Line to PE.03 pin */
EXTI_ClearITPendingBit(EXTI_Line3);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOE, GPIO_PinSource3);
/* Configure EXTI3 line */
EXTI_InitStructure.EXTI_Line = EXTI_Line3;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; //EXTI_Trigger_Rising_Falling
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
/* Enable and set EXTI3 Interrupt to the lowest priority */
NVIC_InitStructure.NVIC_IRQChannel = EXTI3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
/**
* [url=home.php?mod=space&uid=247401]@brief[/url] This function handles External line 3 interrupt request.
* @param None
* @retval None
*/
void EXTI3_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line3) != RESET)
{
if(numInterrupt) numInterrupt--;
// numInterrupt--;
/* Clear the EXTI line 3 pending bit */
EXTI_ClearITPendingBit(EXTI_Line3);
}
}
|
我也遇到了和你一样的问题