本帖最后由 我是寻水鱼 于 2017-11-14 17:02 编辑
在做一个课题,用到tim3 tim4定时器中断,本意是先是定时器3打开,然后进入中断,当条件满足后,进入定时器4中断,然后在定时器4中断内,当另一条件满足后再次进入定时器3中断,这样循环,为啥3就是进不去4,4也进不去3,每个单独使用都没有问题。求解答。。
1.中断配置
[mw_shl_code=c,true]void NVIC_Configuration()
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVIC_InitStructure.NVIC_IRQChannel=TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel=TIM4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}[/mw_shl_code]
2. 定时器3, 4 配置
[mw_shl_code=c,true]void time_init()
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure; //
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
TIM_ClearITPendingBit(TIM3,TIM_IT_Update);//
TIM_TimeBaseInitStructure.TIM_Period = 2000;//
TIM_TimeBaseInitStructure.TIM_Prescaler = 35999;//
TIM_TimeBaseInitStructure.TIM_ClockDivision = 0; //
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;//
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStructure);
TIM_Cmd(TIM3,ENABLE); //
TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE ); //
}[/mw_shl_code]
[mw_shl_code=c,true]void time_init4()
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure; //éùÃ÷ò»¸ö½á11ìå±äá¿£¬óÃà′3õê¼»ˉGPIO
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4,ENABLE);
TIM_ClearITPendingBit(TIM4,TIM_IT_Update);
TIM_TimeBaseInitStructure.TIM_Period = 2000;
TIM_TimeBaseInitStructure.TIM_Prescaler = 35999;
TIM_TimeBaseInitStructure.TIM_ClockDivision = 0;
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM4,&TIM_TimeBaseInitStructure);
TIM_Cmd(TIM4,ENABLE);
TIM_ITConfig(TIM4, TIM_IT_Update, ENABLE ); //ê1Äü»òÕßê§ÄüÖ¸¶¨μÄTIMÖD¶Ï
}[/mw_shl_code]
3.定时器3中断函数
[mw_shl_code=c,true]void TIM3_IRQHandler() //
{
static u16 showw1,showw2;
static u32 i1=0;
static u8 a,b,c,d,e,f,g;
static u8 temp1[20];
TIM_ClearITPendingBit(TIM3,TIM_IT_Update);
i1++;
if(i1%10==0)
showw2=a*100+b*10+c;
if(showw1>=showw2)//
{
TIM_ClearITPendingBit(TIM3,TIM_IT_Update);
TIM_Cmd(TIM3,DISABLE);//关闭定时器3
delay_ms(20);
time_init4();;//定时器4初始化
}
}
else if(i1==1)
{
sprintf((char*)temp1,"%d",showw1);
display_GB2312_string(149,1,temp1);
}
}[/mw_shl_code]
4.定时器4中断函数
[mw_shl_code=c,true]void TIM4_IRQHandler() //
{
static u32 showw1,showw2,showw3,aa,bb,cc;
static u32 i1=0;
static u8 a,b,c,d,e,f,g,p,q;
static u8 temp1[20],temp2[20];
TIM_ClearITPendingBit(TIM4,TIM_IT_Update);
showw1=i1++;
showw2=a*100+b*10+c;
showw3=p*10+q;
if(showw3==2) //
{
if(showw1==(2*showw2))//
{
TIM_ClearITPendingBit(TIM4, TIM_IT_Update);
TIM_Cmd(TIM4,DISABLE);//关闭定时器4
GPIO_SetBits(GPIOB,GPIO_Pin_11); //PB11à-¸ß
delayms(1000);
delay_ms(20);
clear_screen();//½øèë×Ô¶ˉ1yÂ˽çÃæ
zidongmoshixianshi11();
TIM_Cmd(TIM3,ENABLE);//打开定时器3
zidongmoshiyunxing1();
}
else if(showw1>=showw2&&showw1<(2*showw2))/
{
aa=showw1-showw2;
sprintf((char*)temp2,"%2d",aa);
display_GB2312_string(149,1,temp2);
display_GB2312_string(144,7,"2");//
GPIO_SetBits(GPIOB,GPIO_Pin_10);
GPIO_ResetBits(GPIOB,GPIO_Pin_11);
}
else if(showw1>=0&&showw1<showw2)//
{
sprintf((char*)temp1,"%2d",showw1);
display_GB2312_string(149,1,temp1);
display_GB2312_string(144,7,"1");//
GPIO_ResetBits(GPIOB,GPIO_Pin_10); /
}
}[/mw_shl_code]
一周热门 更多>