#include <reg52.h>
#define uchar unsigned char
#define uint unsigned int
uchar a=0; //东西显示时间的缓存变量
uchar b=0; //南北显示时间的缓存变量
uchar time=0; //记录循环单位的位置
uchar int_time=0;//记录中断的次数
uchar clock_a_red=30; //东西定时器初值
uchar clock_a_green=25;
uchar clock_a_yellow=5;
uchar clock_b_red=30; //南北南北计时器初值
uchar clock_b_green=25;
uchar clock_b_yellow=5;
uchar code table[]={ //数码管显示编码
0x3f,0x06,0x5b,0x4f,0x66,
0x6d,0x7d,0x07,0x7f,0x6f};
//延时模块
void delayms(uint xms)
{
uint i,j;
for(i=xms;i>0;i--)
for(j=110;j>0;j--);
}
//主函数模块
void main()
{
TMOD=0X01;
TH1=(65535-50000)/256;
TH0=(65535-50000)%256;
EA=1;
ET0=1;
TR0=1;
P2=0xff;
P1=0x00;
while(1)
{
/*数码管显示模块*/
P2=0xfe;
P1=table[a/10];
delayms(1);
P2=0xfd;
P1=table[a%10];
delayms(1);
P2=0xfb;
P1=table[b/10];
delayms(1);
P2=0xf7;
P1=table[b%10];
delayms(1);
}
}
//中断服务模块
void T0_tme() interrupt 1
{
TH1=(65535-50000)/256;
TH0=(65535-50000)%256;
if(20==int_time++)
{
time++;
if(time>=0&&time<25)
{
P0=0xde;
a=clock_a_red--;
b=clock_b_green--;
}
else if(time>=25&&time<30)
{
P0=0xee;
clock_b_green=25;
a=clock_a_red--;
b=clock_b_yellow--;
}
else if(time>=30&&time<55)
{
P0=0xf3;
clock_a_red=30;
clock_b_yellow=5;
a=clock_a_green--;
b=clock_b_red--;
}
else if(time>=55&&time<60)
{
P0=0xf5;
clock_a_green=25;
a=clock_a_yellow--;
b=clock_b_red--;
}
else
{
time=0;
clock_a_yellow=5;
clock_b_red=30;
}
int_time=0;
}
}
|