|
发表于 2023-1-7 11:18:52
|
显示全部楼层
#include<REGX52.H>
#define uint unsigned int
#define uchar unsigned char
sbit key1=P3^2;//数码管外部中断
sbit key2=P3^3;//小灯中断
sbit key3=P2^0;//全暂停中断
sbit led1=P1^0;
bit flag=1;//数码管标志位
bit flag1=1;//灯标志位
uchar a[]={0x05,0x7d,0x46,0x54,0x3c,0x94,0x84,0x5d,0x04,0x14};//段
uchar b[]={0xf7,0xef,0xdf,0xbf};//位
uchar c[]={0x01,0x79,0x42,0x50,0x38,0x90,0x80,0x59,0x00,0x10};//
uchar led[]={0xff,0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
uint time=0;
uint time2,i,j,m,a1,a2,a3,a4;//小数个十百
void display1();//数码管
void math(uint w);
void delayms(uint xms);//延时
void Timer0Init(void);
void main()
{
TMOD=0x01;
TH0=(65536-45872)/256;
TL0=(65536-45872)%256;
EA=1;
ET0=1;
TR0=1;
IT0=1;
EX0=1;//初始化
while(1)
{
for(;;)
{
P1=led[m];
if(key2==0)
{
delayms(10);
if(key2==0)
{
flag1=!flag1;
}
}
if(key3==0)
{
delayms(10);
TR0=!TR0;
while(!key3);
}
display1();//数码管
}
}
}
void T0_time() interrupt 1//定时器
{
TH0=(65536-45872)/256;//50ms
TL0=(65536-45872)%256;
time++;//小数
if(time%2==0)//数码管
{
if(flag==1)
{
a1++;
}
}
if(time%10==0)//灯
{
if(flag1=0)
{
m++;
}
if(m%9==0)
{
m=0;
}
}
}
void delayms(uint xms)//延时
{
uint i,j;
for(i=xms;i>0;i--)
for(j=110;j>0;j--);
}
void anjian() interrupt 0//外部中断0数码管
{
if(flag)
flag=0;
else flag=1;
}
void math(uint w)
{
a1=w/1000;
a2=w%1000/100;
a3=w%100/10;
a4=w%10;
}
void display1()//数码管
{
if(a1>0&&a4!=0)
{
P2=b[0];
P0=a[a1];
P0=0xff;
P2=b[1];
P0=a[a2];
P0=0xff;
P2=b[2];
P0=c[a3];
P0=0xff;
P2=b[3];
P0=a[a4];
P0=0xff;
}
else
if(a1>0&&a4==0)
{
P2=b[1];
P0=a[a1];
P0=0xff;
P2=b[2];
P0=a[a2];
P0=0xff;
P2=b[3];
P0=a[a3];
P0=0xff;
}
else
if(a2>0&&a4!=0)
{
P2=b[1];
P0=a[a2];
P0=0xff;
P2=b[2];
P0=c[a3];
P0=0xff;
P2=b[3];
P0=a[a4];
P0=0xff;
}
else
if(a2>0&&a4==0)
{
P2=b[2];
P0=a[a2];
P0=0xff;
P2=b[3];
P0=a[a |
|