定时器0的方式1 定时器1的方式1 数码管和led
版权声明:本文为博主原创文章。未经博主同意不得转载。 https://blog.csdn.net/u011046042/article/details/37881715
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit dula=P2^6;
sbit wela=P2^7;
sbit led1=P1^0;
uchar code table[]=
{
0x3f,0x06,0x5b,0x4f,0x66,
0x6d,0x7d,0x07,0x7f,0x6f,
0x77,0x7c,0x39,0x5e,0x79,0x71
};
void delayms(uint);
void display(uchar,uchar);
uchar num,num1,num2,shi,ge;
void main()
{
TMOD=0x01; //设置定时器0和1为工作方式1(00010001)
TH0=(65536-45872)/256; //装初值
TL0=(65536-45872)%256;
TH1=(65536-45872)/256; //装初值
TL1=(65536-45872)%256;
EA=1;
ET0=1;
ET1=1;
TR0=1;
TR1=1;
while(1)
{
display(shi,ge);
}
}
void display(uchar shi,uchar ge)
{
dula=1;
P0=table[shi];
dula=0;
wela=1;
P0=0xfe;
wela=0;
delayms(5);
dula=1;
P0=table[ge];
dula=0;
wela=1;
P0=0xfd;
wela=0;
delayms(5);
}
void delayms(uint xms)
{
uint i,j;
for(i=xms;i>0;i--)
for(j=110;j>0;j--);
}
void T0_time() interrupt 1
{
TH0=(65536-45872)/256; //重装初值
TL0=(65536-45872)%256;
num1++;
if(num1==4) //假设到了4次,说明200ms的时间到了
{
num1=0; //然后吧num1清0又一次再计4次
led1=~led1; //让发光管状态取反
}
}
void T1_time() interrupt 3
{
TH1=(65536-45872)/256; //重装初值
TL1=(65536-45872)%256;
num2++;
if(num2==200) //假设到了20次,说明1秒时间到
{
num2=0; //清0,再计时
num++;
if(num==60) //到60归0
num=0;
shi=num/10;
ge=num%10;
}
}
#define uchar unsigned char
#define uint unsigned int
sbit dula=P2^6;
sbit wela=P2^7;
sbit led1=P1^0;
uchar code table[]=
{
0x3f,0x06,0x5b,0x4f,0x66,
0x6d,0x7d,0x07,0x7f,0x6f,
0x77,0x7c,0x39,0x5e,0x79,0x71
};
void delayms(uint);
void display(uchar,uchar);
uchar num,num1,num2,shi,ge;
void main()
{
TMOD=0x01; //设置定时器0和1为工作方式1(00010001)
TH0=(65536-45872)/256; //装初值
TL0=(65536-45872)%256;
TH1=(65536-45872)/256; //装初值
TL1=(65536-45872)%256;
EA=1;
ET0=1;
ET1=1;
TR0=1;
TR1=1;
while(1)
{
display(shi,ge);
}
}
void display(uchar shi,uchar ge)
{
dula=1;
P0=table[shi];
dula=0;
wela=1;
P0=0xfe;
wela=0;
delayms(5);
dula=1;
P0=table[ge];
dula=0;
wela=1;
P0=0xfd;
wela=0;
delayms(5);
}
void delayms(uint xms)
{
uint i,j;
for(i=xms;i>0;i--)
for(j=110;j>0;j--);
}
void T0_time() interrupt 1
{
TH0=(65536-45872)/256; //重装初值
TL0=(65536-45872)%256;
num1++;
if(num1==4) //假设到了4次,说明200ms的时间到了
{
num1=0; //然后吧num1清0又一次再计4次
led1=~led1; //让发光管状态取反
}
}
void T1_time() interrupt 3
{
TH1=(65536-45872)/256; //重装初值
TL1=(65536-45872)%256;
num2++;
if(num2==200) //假设到了20次,说明1秒时间到
{
num2=0; //清0,再计时
num++;
if(num==60) //到60归0
num=0;
shi=num/10;
ge=num%10;
}
}