51单片机之74HC138译码器控制数码管进行100s计数

/** 
	I use a timer in my code to control the time
	I use 138decoder int the hardware 
**/
#include<reg51.h>
#define uint unsigned int
#define uchar unsigned char

sbit LSA=P2^2;
sbit LSB=P2^3;
sbit LSC=P2^4;

uchar code tab_1[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
uchar tab_2[2];
uchar counter=0,time=0,counter_Tube;

void init_timer()
{
	TMOD=0x01;
	TH0=(65535-50000)/256;
	TL0=(65535-50000)%256;
	EA=1;
	ET0=1;
	TR0=1;
}

void timer0() interrupt 1
{
	TH0=(65535-50000)/256;
	TL0=(65535-50000)%256;
	counter++;
	time++;
}

void display()
{
	uint i=0;

	for(i=0;i<2;i++)
	{
		switch(i)
		{
			case 0: LSA=0; LSB=0; LSC=0; break;
			case 1: LSA=1; LSB=0; LSC=0; break;
		}
		P0=tab_2[i];
		if(time<5)
		{	
			time=0;
			P0=0x00;
		}
	}
}

void displayTube()
{
	if(counter==20)
	{
		counter=0;
		counter_Tube++;
		if((counter_Tube==100)||(counter_Tube>100))
		{
			counter_Tube=0;
		}
		tab_2[0]=tab_1[counter_Tube%10];
		tab_2[1]=tab_1[counter_Tube/10];
	   	
	}
}

void main()
{
	init_timer();
	while(1)
	{
		display();
		displayTube();
	}
}				 
/**
	Writer: Terence
	Email:3117807944@qq
	Date: 2020-10-25
	etc: If there is a problem with the code, you can call me.
		 I will solve the problem in the first time
**/
  • 2
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值