21ic问答首页 - 求助!
求助!
啦啦啦呼呀2024-02-19
我写的这个函数,是想要完成 按下一次对应按键时,LED 以 5Hz 频率闪烁 (代表进门)
,双击对应按键时,LED 以 2Hz 频率闪烁(代表出门),每个门
最多可以进入三次,超出进入上限时 LED 常亮 5s;这个任务,但是我发现我·按下按键后第一次能进门,第二次时就进不去了,试了好久也没改出来。求大佬帮帮
#include <REGX52.H>
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 <REGX52.H>
#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;
}
}
,双击对应按键时,LED 以 2Hz 频率闪烁(代表出门),每个门
最多可以进入三次,超出进入上限时 LED 常亮 5s;这个任务,但是我发现我·按下按键后第一次能进门,第二次时就进不去了,试了好久也没改出来。求大佬帮帮
#include <REGX52.H>
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 <REGX52.H>
#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;
}
}
您需要登录后才可以回复 登录 | 注册