51单片机定时器与数码管配合显示时为什么会闪烁

以下是乱糟糟的代码,主要把功能集中于smgxs(unsigned int i1,unsigned int i2,unsigned int i3)部分,想实现让数码管通过定时器计数来从2s到0倒计时的功能。利用动态显示的原理可以动态显示一组数,例如显示2.00,但无法换下一组数显示,数码管只固定于2.00,并开始在2.00的后面隐隐闪烁,请问有什么方法能解决这个问题,多谢了。

#include <reg51.h>
#include "shumaguan_xs.h"

#define Tick 20000		//10000x100us=2s
unsigned int 		T100us;	//100us时间常数
unsigned int C100us;	//1`00us计数单元


bit LEDBu;
sbit LED=P1^0;

void T0Int()interrupt 1
{
	C100us--;

if(C100us==0)
	{
		smgxs(0,0,0);
		C100us=Tick;  //100us计数器为0
		LEDBu =!LEDBu; //去翻LED
	}
	

}


void main()
{
	TMOD=0x02;
	T100us=0x9C;
	TH0=T100us;
	TL0=T100us;
	
	IE=0x82;
	
	LEDBu=0;
	LED=0;
	C100us=Tick;
	TR0=1;
	
	
	while(1)
	{
  LED=LEDBu;
	if(C100us*TH0||C100us*TL0<3120000&&C100us*TH0||C100us*TL0>2964000)
	{

{smgxs(2,0,0);}
		
}
else if(C100us*TH0||C100us*TL0<2964000&&C100us*TH0||C100us*TL0>2808000)
	{

		{smgxs(1,9,0);}
	
}
else if(C100us*TH0||C100us*TL0<2808000&&C100us*TH0||C100us*TL0>2652000)
	{

		{smgxs(1,8,0);}
	
}
else if(C100us*TH0||C100us*TL0<2652000&&C100us*TH0||C100us*TL0>2496000)
	{

		smgxs(1,7,0);
	
}
else if(C100us*TH0||C100us*TL0<2496000&&C100us*TH0||C100us*TL0>2340000)
	{
	
		smgxs(1,6,0);
	
}
else if(C100us*TH0||C100us*TL0<2340000&&C100us*TH0||C100us*TL0>2184000)
	{
smgxs(1,5,0);
	
}
else if(C100us*TH0||C100us*TL0<2184000&&C100us*TH0||C100us*TL0>2028000)
	{
smgxs(1,4,0);
	
}
else if(C100us*TH0||C100us*TL0<2028000&&C100us*TH0||C100us*TL0>1872000)
	{
smgxs(1,3,0);
	
}
else if(C100us*TH0||C100us*TL0<1872000&&C100us*TH0||C100us*TL0>1716000)
	{
smgxs(1,2,0);
		
}
else if(C100us*TH0||C100us*TL0<1716000&&C100us*TH0||C100us*TL0>1560000)
	{
smgxs(1,1,0);
		
}
else if(C100us*TH0||C100us*TL0<1560000&&C100us*TH0||C100us*TL0>1404000)
	{
smgxs(1,0,0);
	
}
else if(C100us*TH0||C100us*TL0<1404000&&C100us*TH0||C100us*TL0>1248000)
	{
smgxs(0,9,0);
	
}
else if(C100us*TH0||C100us*TL0<1248000&&C100us*TH0||C100us*TL0>1092000)
	{
smgxs(0,8,0);
		
}
else if(C100us*TH0||C100us*TL0<1092000&&C100us*TH0||C100us*TL0>936000)
	{
smgxs(0,7,0);
	
}
else if(C100us*TH0||C100us*TL0<936000&&C100us*TH0||C100us*TL0>780000)
	{
smgxs(0,6,0);
	
}
else if(C100us*TH0||C100us*TL0<780000&&C100us*TH0||C100us*TL0>624000)
	{
smgxs(0,5,0);

}
else if(C100us*TH0||C100us*TL0<624000&&C100us*TH0||C100us*TL0>468000)
	{
smgxs(0,4,0);
		
}
else if(C100us*TH0||C100us*TL0<468000&&C100us*TH0||C100us*TL0>312000)
	{
smgxs(0,3,0);
	
}
else if(C100us*TH0||C100us*TL0<312000&&C100us*TH0||C100us*TL0>156000)
	{
smgxs(0,2,0);

}
else if(C100us*TH0||C100us*TL0<156000&&C100us*TH0||C100us*TL0>0)
	{
smgxs(0,1,0);
	
}
	}
}

//以下为文件shumaguan_xs.c
#include <reg51.h>

#define LEDLen 6

xdata unsigned char OUTBIT _at_ 0x8002;   // 位控制口
xdata unsigned char OUTSEG _at_ 0x8004;   // 段控制口
xdata unsigned char IN     _at_ 0x8001;   // 键盘读入口

unsigned char LEDBuf[LEDLen];    // 显示缓冲
unsigned char LEDMAP[] = {  // 八段管显示码
  0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f
	/*0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07,
  0x7f, 0x6f, 0x77, 0x7c, 0x39, 0x5e, 0x79, 0x71*/
};
unsigned char table3[]={0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef};	// (带小数点)数码管

void Delay(unsigned char CNT)
{
  unsigned char i;

  while (CNT-- !=0)
    for (i=100; i !=0; i--);
}

void DisplayLED()
{
  unsigned char i;
  unsigned char Pos;
  unsigned char LED;

  Pos = 0x3f;       // 从左边开始显示
  for (i = 0; i < LEDLen; i++) {
    OUTBIT = 0;     // 关所有八段管
    LED = LEDBuf[i];
    OUTSEG = LED;
    OUTBIT = Pos;   // 显示一位八段管
    Delay(1);
    Pos >>= 1;      // 显示下一位
  }
}

code unsigned char KeyTable[] = {   // 键码定义
  0x16, 0x15, 0x14, 0xff,
  0x13, 0x12, 0x11, 0x10,
  0x0d, 0x0c, 0x0b, 0x0a,
  0x0e, 0x03, 0x06, 0x09,
  0x0f, 0x02, 0x05, 0x08,
  0x00, 0x01, 0x04, 0x07
};

unsigned char TestKey()
{
   OUTBIT = 0;            // 输出线置为0
   return (~IN & 0x0f);   // 读入键状态(高四位不用)
}

unsigned char GetKey()
{
  unsigned char Pos;
  unsigned char i;
  unsigned char k;

  i = 6;
  Pos = 0x20;     // 找出键所在列
  do {
    OUTBIT = ~ Pos;
    Pos >>= 1;
    k = ~IN & 0x0f;
  } while ((--i != 0) && (k == 0));

  // 键值 = 列 x 4 + 行
  if (k != 0) {
    i *= 4;
    if (k & 2)
      i += 1;
    else if (k & 4)
      i += 2;
    else if (k & 8)
      i += 3;

    OUTBIT = 0;
    do Delay(10); while (TestKey());  // 等键释放

    return(KeyTable[i]);  // 取出键码
  } else return(0xff);
}
void delayms( unsigned char i)  //延 时   子程序  ( i=1时 约  1MS)
{unsigned char j,k;
	for(j=0;j<110;j++)
		for(k=0;k<i;k++);
}
void smgxs(unsigned int i1,unsigned int i2,unsigned int i3)
{
	unsigned char i;

     //if (TestKey()) 
		for(i=0;i<1;i++)	
		 {
			 OUTSEG =LEDMAP[i3]; 		//取精确到0.01s位  的数值	   
		  OUTBIT=0x01;
		  delayms(1); 		 
		  OUTSEG=0xff;	

			OUTSEG=LEDMAP[i2];		//取精确到0.1s位  的数值
		  OUTBIT=0x02;				  
		  delayms(1);
		  OUTSEG=0xff;
		  		 
			
		 OUTSEG=table3[i1];		//取精确到1s位  的数值
		  OUTBIT=0x04;				  
		  delayms(1);
		 //delay1s();
		 OUTSEG=0xff;
		 }
OUTSEG=0xff;
}

//以下为shumaguan_xs.h
#ifndef SHUMAGUAN_XS_H_
#define	SHUMAGUAN_XS_H_
#include "shumaguan_xs.h"
#define LEDLen 6
void Delay(unsigned char CNT);
void DisplayLED();
void smgxs(unsigned int i1,unsigned int i2,unsigned int i3);


void delayms( unsigned char i);
#endif
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值