void buhuo_init() //timer1初始化 我用的是f103rct6 pa12 做输入检测口 另外用tim2 输出一个pwm
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
/* 配置A B两相输入管脚模式*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); /*使能LED灯使用的GPIO时钟*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode =GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
TIM_TimeBaseStructure.TIM_Period=999;
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
TIM_ETRClockMode2Config(TIM1,TIM_ExtTRGPSC_OFF,TIM_ExtTRGPolarity_NonInverted,6);
TIM1->DIER|=1<<0; //允许更新中断
TIM1->DIER|=1<<6; //允许触发中断
TIM_SetCounter(TIM1,0);
TIM_Cmd(TIM1,ENABLE);
}
//下面这个是中断 预想的功能是 让timer1 做脉冲计数 比如我period设置为399 当检测到400个脉冲时溢出进入中断,然后关闭timer2。 因为我只需要400个脉冲
void TIM1_UP_IRQHandler(void)
{
if(TIM1->SR&0X0001)//溢出中断
{
printf("
产生溢出中断!
");
TIM_ARRPreloadConfig(TIM2, DISABLE);
TIM_Cmd(TIM2, DISABLE);
}
printf("
捕获值:%d
",TIM_GetCounter(TIM1));
TIM_ClearITPendingBit(TIM1, TIM_FLAG_Update);
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA| RCC_APB2Periph_AFIO, ENABLE); /*使能LED灯使用的GPIO时钟*/
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode =GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* GPIO Configuration */
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = 999;
TIM_TimeBaseStructure.TIM_Prescaler = 71;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 499;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init(TIM2, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM2, ENABLE);
/* TIM3 enable counter */
TIM_Cmd(TIM2, ENABLE);
}
---------------------------------。TIM2 测外部脉冲个数 有这么难吗。对了,中断里判断两种进中断的可能,一种是计数器溢出,一种是捕获/比较中断源 ,问题能出在这?
void TIM2_IRQHandler(void)
{
TIM_ClearITPendingBit(TIM2, TIM_IT_CC1|TIM_IT_Update);
if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
{
/////////////////////发生溢出 ** 处理////////////////////////////////////
TIM_Cmd(TIM1,DISABLE);
TIM_CtrlPWMOutputs(TIM1,DISABLE);
//////////////////////发生溢出 ** 处理///////////////////////////////////
}
if (TIM_GetITStatus(TIM2, TIM_IT_CC1) != RESET);//捕获1发生捕获事件
printf(" 进入中断!!!");
printf(" 捕获的值:%d ",TIM_GetCounter(TIM1));
}
一周热门 更多>