/*
呼吸灯加交通灯
J9跳线帽不能拔掉
*/
#include <reg52.h>
sbit Red = P1^0;
sbit Yellow = P1^1;
sbit Green = P1^2;
sbit DU = P2^6; //数码管段选
sbit WE = P2^7; //数码管位选
unsigned char const discode[] ={ 0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F,0x6F,0x40,0x00/*-*/};
unsigned char const positon[2]={ 0xfe,0xfd};
unsigned char disbuff[2] ={ 0,0};
unsigned char pwm_left_val = 0;//
unsigned char pwm_t;//周期
unsigned char posit=0;
unsigned int Dbuf;//
bit flag = 0;
//毫秒级延时
void delay(unsigned int z)
{
unsigned int x,y;
for(x = z; x > 0; x--)
for(y = 114; y > 0 ; y--);
}
void Display(void) //扫描数码管
{
DU = 0;
if(posit==0)
{P0=(discode[disbuff[posit]]);}
else
{P0=discode[disbuff[posit]];}
DU = 1;
DU = 0;
WE = 0;
P0=positon[posit];
WE=1;
WE=0;
if(++posit>=2)
posit=0;
}
//定时器0中断
void timer0() interrupt 1
{
pwm_t++;
if(pwm_t == 255)
pwm_t = P1 = 0;
if(pwm_left_val == pwm_t)
P1 = 0XFF;
}
void timer1() interrupt 3 //T1中断用来扫描数码管和计2MS启动模块
{
TH1=0xf8; //2ms
TL1=0xCC;
if(flag == 0)
{
disbuff[0]=Dbuf % 100 / 10;
disbuff[1]=Dbuf % 10;
}
Display();
}
void main()
{
EA = 1;//总中断允许
while(1)
{
unsigned char i, delay_sec;
TMOD |= 0x02;//T0 8位自动重装模块
TH0 = 225;
TL0 = 225;//11.0592M晶振下占空比最大比值是256,输出100HZ
TR0 = 1;//启动定时器0
ET0 = 1;//允许定时器0中?
for(i=0; i < 4; i++)
{
while(pwm_left_val != 255)
{
++pwm_left_val;
delay(1);
}
while(pwm_left_val != 0)
{
--pwm_left_val;
delay(1);
}
}
ET0 = 0;
TR0 = 0;
P1 = 0XFF;
delay(2000);
TMOD |= 0x10;//T1 16位定时
TH1=0xf8; //2ms
TL1=0xCC;
ET1=1; //允许T1中断
TR1=1; //开启定时器1
while(1)
{
delay_sec = 10;
Red = 0;
do{
Dbuf = delay_sec;
delay(1000);
}while(--delay_sec);
Red = 1;
delay_sec = 10;//10sec
Green = 0;
do{
if(delay_sec <= 3)//绿灯闪烁
{
Green = 0;
Dbuf = delay_sec;
delay(500);
flag = 1;
disbuff[0] = 11;
disbuff[1] = 11;
Green = 1;
delay(500);
flag = 0;
}
else
{
Dbuf = delay_sec;
delay(1000);
}
}while(--delay_sec);
Green = 1;
delay_sec = 3;//
Yellow = 0;
do{
Dbuf = delay_sec;
delay(1000);
}while(--delay_sec);
Yellow = 1;
}
}
}
|