#include <REGX51.H>
#define xanh1 P2_0
#define vang1 P2_1
#define do1 P2_2
#define xanh2 P3_0
#define vang2 P3_1
#define do2 P3_2
#define xanh3 P3_3
#define vang3 P3_4
#define do3 P3_5
#define ct1 P2_4
#define ct2 P2_5
unsigned char a[10] = { 0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90} ;
//ham tao thoi gian tre quet led
void delay(unsigned int t)
{ unsigned int i,j;
for(i=0;i<t;i++)
{for(j=0;j<10;j++);
}
}
//ham quet led
void quet_led(unsigned int thu1,unsigned int thu2,unsigned int thu3)
{//so thu nhat//
P1=0x04;
P0=a[thu1];
delay(1);
//so thu2
P1=0x01;
P0=a[thu2];
delay(1);
// so thu 3
P1=0x02;
P0=a[thu3];
delay(1);
}
//ham hien thi so tren led 7
void hien_thi(unsigned int b)
{ unsigned int x,y,z,lap;
//tach so b thanh cac so hang tram ,hang chuc,hang don vi//
x=b/100;
y=(b-100*x)/10;
z=b-100*x-10*y;
for(lap=0;lap<500;lap++)//so lan lap hien thi
{ quet_led(x,y,z);
delay (5);
}
}
//ham cho huong 1
void huong1(unsigned int h1)
{ if(h1>130)//bat den xanh trong 57s
{ xanh1=0;vang1=1;do1=1;}
else if(h1>127) //bat den vang trong 3s
{xanh1=1;vang1=0;do1=1;}
else if(h1>=0)//bat den do trong 127s
{ xanh1=1;vang1=1;do1=0;}
}
//ham cho huong 2
void huong2(unsigned int h2)
{ if(h2>127)//bat den do trong 60s
{ xanh2=1;vang2=1;do2=0;}
else if(h2>67)//bat den xanh trong 60s
{xanh2=0;vang2=1;do2=1;}
else if(h2>62)//bat den vang trong 5s
{ xanh2=1;vang2=0;do2=1;}
else if(h2>=0)//bat den do trong 62s
{ xanh2=1;vang2=1;do2=0;}
}
//ham cho huong 3
void huong3(h3)
{ if(h3>62)//bat den do trong 125s
{xanh3=1;vang3=1;do3=0;}
else if(h3>5)//bat den xanh trong 57s
{xanh3=0;vang3=1;do3=1;}
else if(h3>=0)//bat den vang trong 5s
{xanh3=1;vang3=0;do3=1;}
}
//chuong trinh chinh
void main()
{unsigned int h=187;
unsigned int t2=247;//day la chu ky den cua huong 2
ct1=ct2=1;
while(1)
{ if(ct1==0)//hien thi thoi gian cua huong 2
{ if(t2>187) hien_thi(t2-187);
else if(t2>127) hien_thi(t2-127);
else if(t2>122) hien_thi(t2-122);
else if(h>=0) hien_thi(t2);
}
else if(ct2==0) //thoi gian huong 3
{
if(h>62) hien_thi(h-62);
else if(h>5) hien_thi(h-5);
else if(h>=0) hien_thi(h);
}
else //khi khong an nut 2 hoac nut 3 thi mac dinh huong 1.
{
if(h>130) hien_thi(h-130);
else if(h>127) hien_thi(h-127);
else if(h>=0) hien_thi(h);
}
huong1(h);
huong2(h);
huong3(h);
h--; if( h == 0) h = 187;//quay lai chu ky dem cua huong 1 va huong 3.
t2--; if(t2==0) t2=187;
}
}
#define xanh1 P2_0
#define vang1 P2_1
#define do1 P2_2
#define xanh2 P3_0
#define vang2 P3_1
#define do2 P3_2
#define xanh3 P3_3
#define vang3 P3_4
#define do3 P3_5
#define ct1 P2_4
#define ct2 P2_5
unsigned char a[10] = { 0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90} ;
//ham tao thoi gian tre quet led
void delay(unsigned int t)
{ unsigned int i,j;
for(i=0;i<t;i++)
{for(j=0;j<10;j++);
}
}
//ham quet led
void quet_led(unsigned int thu1,unsigned int thu2,unsigned int thu3)
{//so thu nhat//
P1=0x04;
P0=a[thu1];
delay(1);
//so thu2
P1=0x01;
P0=a[thu2];
delay(1);
// so thu 3
P1=0x02;
P0=a[thu3];
delay(1);
}
//ham hien thi so tren led 7
void hien_thi(unsigned int b)
{ unsigned int x,y,z,lap;
//tach so b thanh cac so hang tram ,hang chuc,hang don vi//
x=b/100;
y=(b-100*x)/10;
z=b-100*x-10*y;
for(lap=0;lap<500;lap++)//so lan lap hien thi
{ quet_led(x,y,z);
delay (5);
}
}
//ham cho huong 1
void huong1(unsigned int h1)
{ if(h1>130)//bat den xanh trong 57s
{ xanh1=0;vang1=1;do1=1;}
else if(h1>127) //bat den vang trong 3s
{xanh1=1;vang1=0;do1=1;}
else if(h1>=0)//bat den do trong 127s
{ xanh1=1;vang1=1;do1=0;}
}
//ham cho huong 2
void huong2(unsigned int h2)
{ if(h2>127)//bat den do trong 60s
{ xanh2=1;vang2=1;do2=0;}
else if(h2>67)//bat den xanh trong 60s
{xanh2=0;vang2=1;do2=1;}
else if(h2>62)//bat den vang trong 5s
{ xanh2=1;vang2=0;do2=1;}
else if(h2>=0)//bat den do trong 62s
{ xanh2=1;vang2=1;do2=0;}
}
//ham cho huong 3
void huong3(h3)
{ if(h3>62)//bat den do trong 125s
{xanh3=1;vang3=1;do3=0;}
else if(h3>5)//bat den xanh trong 57s
{xanh3=0;vang3=1;do3=1;}
else if(h3>=0)//bat den vang trong 5s
{xanh3=1;vang3=0;do3=1;}
}
//chuong trinh chinh
void main()
{unsigned int h=187;
unsigned int t2=247;//day la chu ky den cua huong 2
ct1=ct2=1;
while(1)
{ if(ct1==0)//hien thi thoi gian cua huong 2
{ if(t2>187) hien_thi(t2-187);
else if(t2>127) hien_thi(t2-127);
else if(t2>122) hien_thi(t2-122);
else if(h>=0) hien_thi(t2);
}
else if(ct2==0) //thoi gian huong 3
{
if(h>62) hien_thi(h-62);
else if(h>5) hien_thi(h-5);
else if(h>=0) hien_thi(h);
}
else //khi khong an nut 2 hoac nut 3 thi mac dinh huong 1.
{
if(h>130) hien_thi(h-130);
else if(h>127) hien_thi(h-127);
else if(h>=0) hien_thi(h);
}
huong1(h);
huong2(h);
huong3(h);
h--; if( h == 0) h = 187;//quay lai chu ky dem cua huong 1 va huong 3.
t2--; if(t2==0) t2=187;
}
}