/**
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
**/
51单片机之74HC138译码器控制数码管进行100s计数
最新推荐文章于 2024-03-18 14:13:07 发布