#include //定义了p0、p1等端口
#include //定义了一些函数
#define uint unsigned int //宏定义,方便程序编写
#define uchar unsigned char
#define OFF 0 //定义灯泡开关
#define ON 1
sbit ew_zx_red = P1^0; //东西直行红黄绿
sbit ew_zx_yellow = P1^1;
sbit ew_zx_green = P1^2;
sbit ew_zz_red = P1^3; //东西左转红黄绿
sbit ew_zz_yellow = P1^4;
sbit ew_zz_green = P1^5;
sbit sn_zx_red = P2^0; //南北直行红黄绿
sbit sn_zx_yellow = P2^1;
sbit sn_zx_green = P2^2;
sbit sn_zz_red = P2^3; //南北左转红黄绿
sbit sn_zz_yellow = P2^4;
sbit sn_zz_green = P2^5;
sbit seg_1 = P1^6; //东西数码管位选开关
sbit seg_2 = P1^7;
sbit seg_3 = P2^6; //南北数码管位选开关
sbit seg_4 = P2^7;
int time = 0; //计时器
int count = 0;
uchar SEG_CA[10] = {0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90};
uchar SEG_CC[10] = {0x3f, 0x06, 0x5b, 0x1f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f};
void delay() //延时程序,,,等待
{
int i;
for(i=0; i<10; i++)
;
}
void display_ew(uint num) //东西数字显示
{
P0 =SEG_CA[num/10]; //点亮数字高位
seg_1 = ON;
delay();
seg_1 = OFF;
P0 = SEG_CA[num%10]; //点亮数字低位
seg_2 = ON;
delay();
seg_2 = OFF;
}
void display_sn(uint num) //南北数字显示
{
P0 = SEG_CA[num/10]; //点亮数字高位
seg_3 = ON;
delay();
seg_3 = OFF;
P0 = SEG_CA[num%10]; //点亮数字低位
seg_4 = ON;
delay();
seg_4 = OFF;
}
void display_ew_shan(unsigned int num) //东西3s 倒计时闪烁显示
{
if(count < 2500)
display_ew(num);
else
{
seg_1 = OFF;
seg_2 = OFF;
}
}
void display_sn_shan(unsigned int num) //南北3s倒计时闪烁显示
{
if(count < 2500)
display_sn(num);
else
{
seg_3 = OFF;
seg_4 = OFF;
}
}
void init()
{
P0 = 0x00; //关闭所有端口
P1 = 0x00;
P2 = 0x00;
EA = 0; //打开总中断
ET0 = 1; //打开定时器0中断
TMOD &= 0xf0;
TMOD |= 0xf2; //定时器0工作方式2,自动装载计数值,永远不停的定时, 定时器1 不改变状态
TR0 = 0; //关闭定时器
TL0 = 56; //定时256-56个机器周期,51的机器周期 (1/f)*12 200*1us (一个机器周期为1us)
TH0 = 6;
EA = 1; //开总中断
TR0 = 1; //开始定时
}
void main()
{
init(); //关闭所有器件,初始化了定时器
while(1)
{
/*************************************************************
开始东西直行绿灯亮,东西左转红灯亮27s,显示绿灯倒计时30s
南北直行、左转红灯亮50s,显示红灯倒计时50s
*************************************************************/
while(time%100 < 27)
{
sn_zx_green = ON; //南北直行绿灯亮,显示27s
sn_zx_yellow = OFF;
sn_zx_red = OFF;
display_sn(30 - time);
sn_zz_red = ON; //南北左转红灯亮
sn_zz_green = OFF;
sn_zz_yellow = OFF;
ew_zx_red = ON; //东西直行红灯亮,显示时间50s
ew_zx_yellow = OFF;
ew_zx_green = OFF;
display_ew(50 - time);
ew_zz_red = ON; //东西左转红灯亮,显示时间50s
ew_zz_yellow = OFF;
ew_zx_green = OFF;
}
/*************************************************************
南北直行黄灯亮3s, 倒计时开始闪烁
南北左转,南北方向不变
*************************************************************/
while(time%100 >= 27 && time%100 < 30)
{
sn_zx_yellow = ON; //南北直行黄灯亮 ,显示时间3s
sn_zx_red = OFF;
sn_zx_green = OFF;
display_sn_shan(30 - time);
display_ew(50 - time);
}
/*************************************************************
南北左转绿灯亮17s,直行红灯亮 ,显示绿灯倒计时20s
东西方向不变
*************************************************************/
while(time%100 >= 30 && time%100 < 47)
{
sn_zx_red = ON; //南北直行红灯亮
sn_zx_yellow =OFF;
sn_zx_green = OFF;
sn_zz_green = ON; //南北左转绿灯亮,显示时间17s
sn_zz_red = OFF;
sn_zz_yellow = OFF;
display_sn(50-time);
display_ew(50-time); //东西继续显示倒计时
}
/*************************************************************
南北左转黄灯亮3s, 倒计时开始闪烁 3s
南北直行,南北方向不变
*************************************************************/
while(time%100 >= 47 && time%100 < 50)
{
sn_zz_yellow = ON; //南北左转黄灯亮,显示时间3s
sn_zz_red = OFF;
sn_zz_green = OFF;
display_sn_shan(50-time);
display_ew(50-time); //东西继续显示倒计时
}
/*************************************************************
南北左转红灯亮50s,直行不变,显示倒计时50s
东西直行绿灯亮27,左转不变,显示绿灯倒计时30s
*************************************************************/
while(time%100 >=50 && time%100 < 77)
{
ew_zx_green = ON; //东西直行绿灯亮,显示时间27s
ew_zx_yellow = OFF;
ew_zx_red = OFF;
display_ew(80 - time);
sn_zz_red = ON; //南北左转红灯亮,显示时间50s
sn_zz_yellow = OFF;
sn_zz_green = OFF;
display_sn(100 - time);
}
/*************************************************************
东西直行黄灯亮3s,左转不变,显示黄灯闪烁倒计时3s
南北不变
*************************************************************/
while(time%100 >= 77 && time%100 < 80)
{
ew_zx_yellow = ON; //东西直行黄灯亮,显示时间3s
ew_zx_red = OFF;
ew_zx_green = OFF;
display_ew_shan(80 - time);
display_ew(100 - time);
}
/*************************************************************
东西左转绿灯亮17s,直行不变,显示绿灯倒计时20s
南北不变
*************************************************************/
while(time%100 >=80 && time%100 <97)
{
ew_zx_red = ON; //东西直行红灯亮
ew_zx_yellow = OFF;
ew_zx_green = OFF;
ew_zz_green = ON; //东西左转绿灯亮, 显示17s
ew_zz_red = OFF;
ew_zz_yellow = OFF;
display_ew(100-time);
display_sn(100 - time); //南北继续倒计时
}
/*************************************************************
东西左转黄灯亮17s,直行不变,显示黄灯闪烁倒计时3s
南北不变
*************************************************************/
while(time%100 >= 97 && time<=100)
{
ew_zz_yellow = ON; //东西左转黄灯亮, 显示时间3s
ew_zz_red = OFF;
ew_zz_green = OFF;
display_ew_shan(100 - time);
display_sn(100 - time); //南北继续倒计时
}
}
}
void timer0() interrupt 1
{
count++; //count每过200us就会加1
if(count == 5000)
{
time++; //time每过1s就会加1
count = 0;
}
if(time > 100)
time = 0;
}
阅读(1615) | 评论(0) | 转发(0) |