Mạch đèn giao thông ngã tư 89c51
Code:
//////////////////**************BY VDM***********///////////////////
/////////////DAI HOC SU PHAM KY THUAT HUNG YEN////
///////////////WWW.SPKTHY.COM////////////
///////////SVTH: VUONG DUY MINH/////////
#include <REG51.H>
sbit tang=P1^1;
sbit giam=P1^2;
sbit ok=P1^3;
sbit menu=P1^0;
sbit LED2=P2^0;
sbit LED1=P2^1;
sbit LED4=P2^2;
sbit LED3=P2^3;
sbit LED6=P2^4;
sbit LED5=P2^5;
sbit LED8=P2^6;
sbit LED7=P2^7;
unsigned char thoi_gian0,thoi_gian1,thoi_gian2,thoi_gian3;
unsigned char Data,Display,time,index,mode,bat_nhay,read_time,alarm,kt,vdm1,vdm2,i;
unsigned int a[10] = {0xC0,0xF9,0xA4,0XB0,0x99,0x92,0x82,0xF8,0x80,0x90 };
int n,m,h,x;
////////////////////ham tre/////
void delay(int t){
while(t--);
}
void timer_0(void) interrupt 1 // goi chuong trinh ngat ngaoi 0
{x=x++;
if(x==4000)
{
x=0;
thoi_gian0--;
thoi_gian1--;
thoi_gian2--;
thoi_gian3--;
}
}
//////////////////
void hien_thi0()
{
unsigned char k,chuc,donvi;
for(k=0;k<2;k++)
{
donvi=thoi_gian0%10;
chuc=thoi_gian0/10;
}
P0=a[donvi];
LED1=1;
delay(500);
LED1=0;
P0=a[chuc];
LED2=1;
delay(500);
LED2=0;
}
void hien_thi1()
{
unsigned char k,chuc,donvi;
for(k=0;k<2;k++)
{
donvi=thoi_gian1%10;
chuc=thoi_gian1/10;
}
P0=a[donvi];
LED3=1;
delay(500);
LED3=0;
P0=a[chuc];
LED4=1;
delay(500);
LED4=0;
}
void hien_thi2()
{
unsigned char k,chuc,donvi;
for(k=0;k<2;k++)
{
donvi=thoi_gian2%10;
chuc=thoi_gian2/10;
}
P0=a[donvi];
LED5=1;
delay(500);
LED5=0;
P0=a[chuc];
LED6=1;
delay(500);
LED6=0;
}
void hien_thi3()
{
unsigned char k,chuc,donvi;
for(k=0;k<2;k++)
{
donvi=thoi_gian3%10;
chuc=thoi_gian3/10;
}
P0=a[donvi];
LED7=1;
delay(500);
LED7=0;
P0=a[chuc];
LED8=1;
delay(500);
LED8=0;
}
void hien_thi00()
{
unsigned char k,chuc,donvi;
for(k=0;k<2;k++)
{
donvi=vdm1%10;
chuc=vdm1/10;
}
P0=a[donvi];
LED1=1;
delay(500);
LED1=0;
P0=a[chuc];
LED2=1;
delay(500);
LED2=0;
}
void hien_thi11()
{
unsigned char k,chuc,donvi;
for(k=0;k<2;k++)
{
donvi=vdm2%10;
chuc=vdm2/10;
}
P0=a[donvi];
LED3=1;
delay(500);
LED3=0;
P0=a[chuc];
LED4=1;
delay(500);
LED4=0;
}
void cai_dat()
{
P1=0XFF;
vdm1=thoi_gian0;
vdm2=thoi_gian1;
while(1)
{
if(tang==0)
{
vdm1++;
vdm2++; while(tang==0);
}
if(giam==0)
{
vdm1--;
vdm2--;for(i==0;i<10;i++){if(tang==1)i=12; else i=3;}
}
if(ok==0) break;
hien_thi00();
hien_thi11();
}
}
void main() ///////////chuong trinh chinh
{
EA=1;
TR0=0;
TMOD=0X02; //CHON TIMER CHE DO TIMER 2 TU LAP LAI
TH0=0x54;
TR0=1;TF1=0;
ET0=1; ET1=1;
TR1=0;
vdm1=vdm2=0;
time=index=mode=0;
P1=0Xff;
P2=0Xff;
P3=0xff;
cai_dat();
a=vdm1;
b=vdm2;
while(1){
thoi_gian0=15;a
thoi_gian1=18;a+3
thoi_gian2=46;a+31;
thoi_gian3=64;a+49;
do
{
P3=0X5E;P1=0X9B;
hien_thi0();
hien_thi1();
hien_thi2();
hien_thi3();
}
while(thoi_gian0!=0);
thoi_gian0=3;
do
{
P3=0X5D;P1=0X9B;
hien_thi0();
hien_thi1();
hien_thi2();
hien_thi3();
}
while(thoi_gian0!=0);
thoi_gian1=25;
thoi_gian0=61;
do
{
P3=0X73;P1=0X9B;
hien_thi0();
hien_thi1();
hien_thi2();
hien_thi3();
}
while(thoi_gian1!=0);
thoi_gian1=3;
do
{
P1=0X9B;P3=0X6B;
hien_thi0();
hien_thi1();
hien_thi2();
hien_thi3();
}
while(thoi_gian1!=0);
//////////
thoi_gian0=46;
thoi_gian1=64;
thoi_gian2=15;
thoi_gian3=18;
do
{
P1=0X5E;P3=0X9B;
hien_thi0();
hien_thi1();
hien_thi2();
hien_thi3();
}
while(thoi_gian2!=0);
thoi_gian2=3;
do
{
P1=0X5D;P3=0X9B;
hien_thi0();
hien_thi1();
hien_thi2();
hien_thi3();
}
while(thoi_gian2!=0);
thoi_gian3=25;
thoi_gian2=61;
do
{
P1=0X73;P3=0X9B;
hien_thi0();
hien_thi1();
hien_thi2();
hien_thi3();
}
while(thoi_gian3!=0);
thoi_gian3=3;
do
{
P1=0X6B;P3=0X9B;
hien_thi0();
hien_thi1();
hien_thi2();
hien_thi3();
}
while(thoi_gian3!=0);
}
}
By VDM
( Full Code)
Khi nhấn nút "Tải Về Máy", bạn sẽ vào trang quảng cáo, vui lòng chờ 5 giây. Nút hiện ra ở góc phải phía trên, nhấn vào để đến trang download