HFUT_ROSE
HFUT_ROSE
2020-12-05 22:54

C51定时器问题,求助大佬

  • c语言
  • 测试用例
  • 物联网
  • 开发语言

欲实现使用一个一位的数码管,从0开始显示,每隔1s显示数加一,加至16清零继续循环。按下button1间隔时间增长0.5s,按下button2间隔时间缩短0.5s。

/*{0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71}
0~F*/

#include <reg52.h>	
#include <intrins.h>
#define u16 unsigned int 	
#define u8  unsigned char 
#define KEY P1

sbit LSA=P2^2;
sbit LSB=P2^3;
sbit LSC=P2^4;
u8 i=0,keynum;
u8 T=1000;
u8 code smgduan[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};


void delay(u16 j)
{
	while(j--);	
}

void keydown();
	
void Timer0Init()               //用定时器0控制两次扫描的时间间隔
{
	TMOD=0X01;
	TH0=0XFC;
	TL0=0X18;	
	ET0=1;
	EA=1;
	TR0=1;	
  while(!TF0);
  TF0 = 0;
}

void lighting()
{
		if(i<16)
			{
				P0=smgduan[i];
			}	
}	

void main()
{	

	while(1)
	{	
		keydown();
		Timer0Init();
		lighting();
	  if(i==16){i=0;}
	}		
}

void Timer0() interrupt 1
{
	static u16 k;
	TH0=0XFC;	
	TL0=0X18;
	TR0=1;
	k++;
	if(k==T)
	{
		k=0;
		i++;
	}	
}

void keydown()
{
if(KEY!=0x0f)
{
  delay(100);
	if(KEY!=0x0f)
	{
	switch(KEY)
   {
		case(0x77):keynum=0;break;
		case(0xbb):keynum=1;break;
	 }
	}
}
if(keynum==0) {T+=500;}
else if(keynum==1){T-=500;}	
}

 

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

3条回答

为你推荐

换一换