对应的电路图如下:
- #include<reg51.h>
- #define uchar unsigned char
- #define uint unsigned int
- //共阴数码管编码
- uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
- // 南北四向数码管位选
- sbit b1 = P2^0;
- sbit b2 = P2^1;
- sbit n1 = P2^2;
- sbit n2 = P2^3;
- sbit d1 = P2^4;
- sbit d2 = P2^5;
- sbit x1 = P2^6;
- sbit x2 = P2^7;
- //南北四向12个LED灯
- sbit br = P3^0;
- sbit by = P3^1;
- sbit bg = P3^2;
- sbit nr = P3^3;
- sbit ny = P3^4;
- sbit ng = P3^5;
- sbit dr = P3^6;
- sbit dy = P3^7;
- sbit dg = P1^0;
- sbit xr = P1^1;
- sbit xy = P1^2;
- sbit xg = P1^3;
- //设置时间
- sbit SET = P1^6;
- sbit ADD = P1^5;
- sbit MINUS = P1^4;
- //蜂鸣器
- sbit buzz = P1^7;
- uchar Flash_Count=5, Operation_Type=1; //闪烁秒数,通行方向控制 1. B 2.N 3.D 4.X
- uchar NBsecond_SET = 30,Nsecond,Bsecond; //南北向通行时间
- uchar DXsecond_SET = 20,Dsecond,Xsecond; //东西向通行时间
- uchar second_i=1,num=0,RunType=1,flg=0;
- uchar key_can; //按键值
- //菜单变量
- uchar menu_1=0,menu_2,key_new=1;
- void DelayMS(uint x)
- {
- uchar i;
- while(x--)
- for(i=0;i<120;i++);
- }
- //初始化定时器
- void init()
- {
- EA = 1; //开总中断
- TMOD = 0X11; //定时器0、工作方式1
- TH0 = 0x3c;
- TL0 = 0xb0; // 50ms
- ET0 = 1; //开定时器0中断
- TR0 = 1; //允许定时器0定时
- }
- //数码管显示 second:显示秒数 tpy: 1. B 2.N 3.D 4.X
- void displaySMG(uchar second, uchar tpy)
- {
- uchar shi,ge;
- shi = second/10;
- ge = second%10;
- if(tpy==1)
- {
- P2=0xff;
- b1 = 0;
- P0 = table[shi];
- DelayMS(5);
- P2=0xff;
- b2 = 0;
- P0 = table[ge];
- DelayMS(5);
- }else if(tpy==2)
- {
- P2=0xff;
- n1 = 0;
- P0 = table[shi];
- DelayMS(5);
- P2=0xff;
- n2 = 0;
- P0 = table[ge];
- DelayMS(5);
- }else if(tpy==3)
- {
- P2=0xff;
- d1 = 0;
- P0 = table[shi];
- DelayMS(5);
- P2=0xff;
- d2 = 0;
- P0 = table[ge];
- DelayMS(5);
- }else if(tpy==4)
- {
- P2=0xff;
- x1 = 0;
- P0 = table[shi];
- DelayMS(5);
- P2=0xff;
- x2 = 0;
- P0 = table[ge];
- DelayMS(5);
- }
- }
- //运行模式1
- void modeONE()
- {
- if(Operation_Type==1)
- {
- Bsecond = NBsecond_SET-num; // 北向绿灯时间
- Nsecond = NBsecond_SET-num; // 南向红灯
- Dsecond = NBsecond_SET + NBsecond_SET-num; // 东向红灯
- Xsecond = 2*NBsecond_SET + DXsecond_SET-num;// 西向红灯
- br = 1;
- nr = 0;
- ny = 1;
- ng = 1;
- dr = 0;
- dy = 1;
- dg = 1;
- xr = 0;
- xy = 1;
- xg = 1;
- if(Bsecond<=Flash_Count)
- {
- if(second_i%10==0)
- by = ~by;
- bg = 1;
- }else
- {
- bg = 0;
- by = 1;
- }
- if(Bsecond==0)
- {
- Operation_Type=2;
- num=0;
- }
- displaySMG(Bsecond,1);
- displaySMG(Nsecond,2);
- displaySMG(Dsecond,3);
- displaySMG(Xsecond,4);
- }else if(Operation_Type==2)
- {
- Bsecond = NBsecond_SET + 2*DXsecond_SET-num;// 北向红灯
- Nsecond = NBsecond_SET - num; // 南向绿灯
- Dsecond = NBsecond_SET - num; // 东向红灯
- Xsecond = NBsecond_SET + DXsecond_SET-num; // 西向红灯
- br = 0;
- by = 1;
- bg = 1;
- nr = 1;
- dr = 0;
- dy = 1;
- dg = 1;
- xr = 0;
- xy = 1;
- xg = 1;
- if(Nsecond<=Flash_Count)
- {
- if(second_i%10==0)
- ny = ~ny;
- ng = 1;
- }else
- {
- ng = 0;
- ny = 1;
- }
- if(Nsecond==0)
- {
- Operation_Type=3;
- num=0;
- }
- displaySMG(Bsecond,1);
- displaySMG(Nsecond,2);
- displaySMG(Dsecond,3);
- displaySMG(Xsecond,4);
- }else if(Operation_Type==3)
- {
- Bsecond = 2*DXsecond_SET-num; // 北向红灯
- Nsecond = 2*DXsecond_SET+NBsecond_SET-num; // 南向红灯
- Dsecond = DXsecond_SET - num; // 东向绿灯
- Xsecond = DXsecond_SET - num; // 西向红灯
- br = 0;
- by = 1;
- bg = 1;
- nr = 0;
- ny = 1;
- ng = 1;
- dr = 1;
- xr = 0;
- xy = 1;
- xg = 1;
- if(Dsecond<=Flash_Count)
- {
- if(second_i%10==0)
- dy = ~dy;
- dg = 1;
- }else
- {
- dg = 0;
- dy = 1;
- }
- if(Dsecond==0)
- {
- Operation_Type=4;
- num=0;
- }
- displaySMG(Bsecond,1);
- displaySMG(Nsecond,2);
- displaySMG(Dsecond,3);
- displaySMG(Xsecond,4);
- }else if(Operation_Type==4)
- {
- Bsecond = DXsecond_SET - num; // 北向红灯
- Nsecond = DXsecond_SET + NBsecond_SET-num; // 南向红灯
- Dsecond = DXsecond_SET + 2*NBsecond_SET-num; // 东向红灯
- Xsecond = DXsecond_SET - num; // 西向绿灯
- br = 0;
- by = 1;
- bg = 1;
- nr = 0;
- ny = 1;
- ng = 1;
- dr = 0;
- dy = 1;
- dg = 1;
- xr = 1;
- if(Xsecond<=Flash_Count)
- {
- if(second_i%10==0)
- xy = ~xy;
- xg = 1;
- }else
- {
- xg = 0;
- xy = 1;
- }
- if(Xsecond==0)
- {
- Operation_Type=1;
- num=0;
- }
- displaySMG(Bsecond,1);
- displaySMG(Nsecond,2);
- displaySMG(Dsecond,3);
- displaySMG(Xsecond,4);
- }
- }
- //运行模式2
- void modeTWO()
- {
- if(Operation_Type==1)
- {
- Bsecond = NBsecond_SET - num; // 北向绿灯时间
- Nsecond = NBsecond_SET - num; // 南向绿灯时间
- Dsecond = NBsecond_SET - num; // 东向红灯
- Xsecond = NBsecond_SET - num; // 西向红灯
- br = 1;
- nr = 1;
- dr = 0;
- dy = 1;
- dg = 1;
- xr = 0;
- xy = 1;
- xg = 1;
- if(Bsecond<=Flash_Count)
- {
- if(second_i%10==0)
- {
- by = ~by;
- ny = ~ny;
- }
- bg = 1;
- ng = 1;
- }else
- {
- bg = 0;
- by = 1;
- ng = 0;
- ny = 1;
- }
- if(Bsecond==0)
- {
- Operation_Type=2;
- num=0;
- }
- displaySMG(Bsecond,1);
- displaySMG(Nsecond,2);
- displaySMG(Dsecond,3);
- displaySMG(Xsecond,4);
- }else if(Operation_Type==2)
- {
- Bsecond = DXsecond_SET - num; // 北向绿灯时间
- Nsecond = DXsecond_SET - num; // 南向绿灯时间
- Dsecond = DXsecond_SET - num; // 东向红灯
- Xsecond = DXsecond_SET - num; // 西向红灯
- br = 0;
- by = 1;
- bg = 1;
- nr = 0;
- ny = 1;
- ng = 1;
- dr = 1;
- xr = 1;
- if(Dsecond<=Flash_Count)
- {
- if(second_i%10==0)
- {
- dy = ~dy;
- xy = ~xy;
- }
- dg = 1;
- xg = 1;
- }else
- {
- dy = 1;
- dg = 0;
- xy = 1;
- xg = 0;
- }
- if(Dsecond==0)
- {
- Operation_Type=1;
- num=0;
- }
- displaySMG(Bsecond,1);
- displaySMG(Nsecond,2);
- displaySMG(Dsecond,3);
- displaySMG(Xsecond,4);
- }
- }
- /*************定时器0中断服务程序***************/
- void time0_int() interrupt 1
- {
- TH0 = 0x3c;
- TL0 = 0xb0; // 50ms
- second_i++;
- if (second_i==20) //1s
- {
- second_i = 1;
- num++;
- }
- if(menu_1 == 0)
- {
- if(RunType == 1)// 模式1
- {
- modeONE();
- }else if(RunType == 2)// 模式2
- {
- modeTWO();
- }
- }
- }
- /********************独立按键程序*****************/
- void key() //独立按键程序
- {
- key_can = 20; //按键值还原
- P1 |= 0x70; //对应的按键IO口输出为1
- if((P1 & 0x70) != 0x70) //按键按下
- {
- DelayMS(1); //按键消抖动
- if(((P1 & 0x70) != 0x70) && (key_new == 1))
- { //确认是按键按下
- key_new = 0;
- num=0;
- flg=1;
- if(SET == 0)
- key_can = 1;
- if(ADD == 0)
- key_can = 2;
- if(MINUS == 0)
- key_can = 3;
- }
- }
- else
- key_new = 1;
- }
- /**********************设置函数************************/
- void key_with()
- {
- if(key_can == 1) //设置键
- {
- menu_1++;
- if(menu_1 == 1) //设置南北向时间
- {
- menu_2 = 1;
- }
- if(menu_1 == 2) //设置东西向时间
- {
- menu_2 = 2;
- }
- if(menu_1 == 3) //设置运行模式
- {
- menu_2 = 3;
- }
- if(menu_1 > 3) //回到正常显示
- {
- menu_1 = 0;
- flg=0;
- }
- }
- if(menu_2 == 1) //设置南北向时间
- {
- if(key_can == 2) //加
- {
- NBsecond_SET+=0x01;
- }
- if(key_can == 3) //减
- {
- if(NBsecond_SET != 0x00)
- NBsecond_SET -- ;
- }
- }
- if(menu_2 == 2) //设置东西向时间
- {
- if(key_can == 2) //加
- {
- DXsecond_SET+=0x01;
- }
- if(key_can == 3) //减
- {
- if(DXsecond_SET != 0x00)
- DXsecond_SET -- ;
- }
- }
- if(menu_2 == 3) //设置模式
- {
- if(key_can == 2) //加
- {
- RunType+=0x01;
- }
- if(key_can == 3) //减
- {
- if(RunType > 1)
- RunType -- ;
- }
- }
- if(flg==1)
- {
- if(menu_1==1)
- {
- displaySMG(NBsecond_SET,1);
- displaySMG(NBsecond_SET,2);
- }else if(menu_1==2)
- {
- displaySMG(DXsecond_SET,3);
- displaySMG(DXsecond_SET,4);
- }else if(menu_1==3)
- {
- displaySMG(RunType,1);
- displaySMG(RunType,2);
- displaySMG(RunType,3);
- displaySMG(RunType,4);
- }
- }
- }
- //主程序
- void main()
- {
- init();
- while(1)
- {
- key(); //按键程序
- if(key_can < 10|flg==1)
- {
- key_with();
- }
- }
- }
复制代码 |