-
TMR3输入捕获
配置了TMR3定时器输入捕获,但是全速运行会出错,代码如下:TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure; TIM_ICInitTypeDef TIM5_ICInitStructure; NVIC_InitTypeDef NVIC_InitStructure; /* 开启定时器5时钟 */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5,ENABLE); //使能TIM5时钟 TIM_TimeBaseInitStructure.TIM_Period = 0xffff; //设置定时器自动重装载值 TIM_TimeBaseInitStructure.TIM_Prescaler = 71; //以1MHz的频率计数,一次是1μs(微妙) TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1; //设置时钟分割:TDTS = Tck_tim TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式 TIM_TimeBaseInit(TIM5,&TIM_TimeBaseInitStructure);//初始化TIM5的时间基数单位 TIM5_ICInitStructure.TIM_Channel = TIM_Channel_2; //选择输入端,IC1映射到TI1上 TIM5_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; //上升沿捕获 TIM5_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //映射到TI1上 TIM5_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //配置输入分频,不分频 TIM5_ICInitStructure.TIM_ICFilter = 0x00; //IC1F=0000,配置滤波器,不滤波 TIM_ICInit(TIM5, &TIM5_ICInitStructure); //初始化TIM5输入捕获通道1 //中断分组初始化 NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4); NVIC_InitStructure.NVIC_IRQChannel=TIM3_IRQn; //打开TIM5的全局中断 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1; //抢占优先级为0 NVIC_InitStructure.NVIC_IRQChannelSubPriority=0; //响应优先级为1 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能 NVIC_Init(&NVIC_InitStructure); TIM_Cmd(TIM3,ENABLE); //使能或者失能TIM5外设 TIM_ITConfig(TIM3, TIM_IT_Update|TIM_IT_CC2, ENABLE ); //使能或者失能指定的TIM中断
1650浏览量 3回复量 关注量 -
求助! sos
我写的这个函数,是想要完成 [font=等线][size=12pt]按下一次对应按键时,LED 以 5Hz 频率闪烁 (代表进门)[/size][/font] [font=等线][size=12pt],双击对应按键时,LED 以 2Hz 频率闪烁(代表出门),每个门[/size][/font] [font=等线][size=12pt]最多可以进入三次,超出进入上限时 LED 常亮 5s;这个任务,但是我发现我·按下按键后第一次能进门,第二次时就进不去了,试了好久也没改出来。求大佬帮帮[/size][/font] #include
void Timer01Init() { TMOD = 0x11; //令T0,T1的M1,M0分别为0,1.启用16定时器0,1,启用TL,TH TL0 = 0xD8; //1ms,11.0592Hz TH0 = 0xF0; TL1 = 0xD8; TH1 = 0xF0; TF0 = 0; //溢出中断标志位,为1时向cpu请求中断; TF1 = 0; TR0 = 1; //先禁止定时器计数,按下按键才允许计数;1允许 TR1 = 1; EA = 1; //总中断允许控制位; ET0 = 1; //溢出中断允许位; ET1 = 1; PT0 = 1; //优先级; } #include #include "Timer01Init.h" #define red P1_0 //#define yellow P1_1 //#define blue P1_3 unsigned char A , C , LEDtime , GateNumr , GateNumy , GateNumb , sure , Gatered , Gateyellow , Gateblue ; //B; unsigned char Mode ; unsigned int counter ; void main() { ///////////////////////red Timer01Init(); while(1) { if(P3_1 == 0) //这里有问题 :我不加 && Gatered == 1这个函数可以执行,我加上之后我没想明白为啥会运行不了。 //5Hz,进门 { if(GateNumr < 3) //门只能进入三次 { if(counter >= 100) { counter = 0; red = !red; LEDtime++; } if(LEDtime >= 6) //P1_2亮一个周期LEDtime会加两次,所以LEDtime不为3; { // LCD12864_show_string(1,0,"red"); LEDtime = 0; GateNumr++; TR1 = 0; A = 0; B = 0; C = 0; } }else //进门超出三次常亮5秒 { counter = 0; while(counter <= 5000) //不是5秒? { red = 0 ; } red = 1; GateNumr = 0; counter = 0; TR1 = 0; } } if(C == 2) //2Hz, 出门 { if(GateNumr <= 3) { // LCD12864_show_string(0,1,"KeyPress(P3_1)"); if(counter >= 250) { counter = 0; red = !red; LEDtime++; } if(LEDtime >= 6) //P1_2亮一个周期LEDtime会加两次,所以LEDtime不为3; { LEDtime = 0; GateNumr--; TR1 = 0; A = 0; B = 0; C = 0; } } } Gatered = 0; } } 1870浏览量 0回复量 关注量