项目:交通灯控制系统---控制led灯/交通灯,控制定时器,控制数码管显示器
单片机源程序如下:
#include<reg52.h>
#define uint unsigned int;
#define uchar unsigned char;
void DigitalTube();
void InterruptT0();
sbit P10 = P1^0;
sbit P11 = P1^1;
sbit P12 = P1^2;
sbit P13 = P1^3;
sbit P14 = P1^4;
sbit P15 = P1^5;
sbit P16 = P1^6;
sbit P17 = P1^7;
sbit P20 = P2^0;
sbit P21 = P2^1;
sbit K22 = P2^2;
sbit K23 = P2^3;
sbit K24 = P2^4;
sbit dula = P2^5;
uint LedBuf[10] = { 0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};
uint Kflag = 1;
uint LedNumNS = 27;
uint LedNumEW = 30;
uint i = 0;
uint j = 0;
uint j1 = 0;
uint k = 0;
uint g = 0;
uint z = 0;
uint LedTime1 = 30;
uint LedTime2 = 27;
uint LedTime3 = 30;
uint LedTime4 = 27;
uint Kf22 = 1;
uint Kf23 = 1;
uint Kf24 = 1;
uint KSta24 = 1;
uint KSta23 = 1;
uint KSta22 = 1;
uchar Kflag22 = 0;
void main()
{
EA = 1;
ET0 = 1;
TR0 = 1;
TMOD = 0x01;
TH0 = 0xFC;
TL0= 0x67;
P1 = 0xEE;
while(1)
{
DigitalTube();
}
}
void InterruptT0() interrupt 1
{
TH0 = 0xFC;
TL0 = 0x67;
i++;
k++;
KSta24 = K24;
if(Kf24 != KSta24)
{
if(Kf24 == 1)
{
Kflag = !Kflag;
}
Kf24 = KSta24;
}
KSta23 = K23;
if(Kf23 != KSta23)
{
if(Kf23 == 1)
{
LedTime1 = LedTime1 +5;
LedTime2 = LedTime2 +5;
LedTime3 = LedTime3 +5;
LedTime4 = LedTime4 +5;
if(LedTime1 >=60)
{
LedTime1 = 10;
LedTime2 = 7;
LedTime3 = 10;
LedTime4 = 7;
}
LedNumEW = LedTime1;
LedNumNS = LedTime2;
}
Kf23= KSta23;
}
KSta22 = K22;
if(Kf22 != KSta22)
{
if(Kf22 == 1)
{
Kflag22 ++;
if(Kflag22>= 3)
{
Kflag22 = 0;
}
switch(Kflag22)
{
case 1:
LedTime1 = 50;
LedTime2 = 47;
LedTime3 = 20;
LedTime4 = 17;
LedNumEW = LedTime1;
LedNumNS = LedTime2;
break;
case 2:
LedTime1 = 20;
LedTime2 = 17;
LedTime3 = 50;
LedTime4 = 47;
LedNumEW = LedTime1;
LedNumNS = LedTime2;
break;
case 0:
LedTime1 = 30;
LedTime2 = 27;
LedTime3 = 30;
LedTime4 = 27;
LedNumEW = LedTime1;
LedNumNS = LedTime2;
break;
}
}
Kf22 = KSta22;
}
if(j >= 200)
{
j = 0;
j1++;
}
if(i >= 1000)
{
i = 0;
LedNumNS--;
LedNumEW--;
switch(g)
{
case 0:
if(LedNumEW == 3)
{
P14 = ! P14;
if(Kf24 == Kflag) P15 = ! P15;
LedNumNS = 3;
}
if(LedNumEW == 0)
{
LedNumNS = LedTime3;
LedNumEW = LedTime4;
P12 = ! P12;
P10 = ! P10;
P13 = ! P13;
if(Kf24 == Kflag)
P15 = ! P15;
g = 1;
}
break;
case 1:
if(LedNumNS == 3)
{
P12 = !P12;
P11 = !P11;
LedNumEW = 3;
}
if(LedNumNS == 0)
{
LedNumEW = LedTime1;
LedNumNS = LedTime2;
if(Kf24 == Kflag) P11 = !P11;
P10 = !P10;
P14 = !P14;
P13 = !P13;
g = 0;
}
break;
}
if(Kf24 != Kflag)
{
if(j1 % 2 == 0)
{
P11 = ! P11;
P15 = ! P15;
}
}
}
}
|