新人闲暇之余所做。。。
-
- #include "reg52.h"
- #define u8 unsigned char
- #define u16 unsigned int
- #define xiaoden P1
- u8 Second,First;
- u16 n;
- sbit bell=P2^0;
- sbit green00=P1^5;
- sbit yellow00=P1^4;
- sbit red00=P1^3;
- sbit green10=P1^2;
- sbit yellow10=P1^1;
- sbit red10=P1^0;
- sbit green01=P3^3;
- sbit yellow01=P3^2;
- sbit red01=P3^1;
- sbit green11=P3^0;
- sbit yellow11=P1^7;
- sbit red11=P1^6;
- u8 count,shi,fen,miao;
- sbit rw=P2^5;
- sbit rs=P2^6;
- sbit lcden=P2^7;
- u8 code table[]=" 2017-11-19 SUN";
- u8 code table1[]=" 30";
- void delay(u16 z)
- {
- u16 x,y;
- for(x=z;x>0;x--)
- for(y=115;y>0;y--);
- }
- void delays(u16 i)
- {
- while(i--);
- }
- void Timer0Init()
- {
- TMOD|=0X01;//选择为定时器0模式,工作方式1,仅用TR0打开启动。
- TH0=0XFC; //给定时器赋初值,定时1ms
- TL0=0X18;
- ET0=1;//打开定时器0中断允许
- EA=1;//打开总中断
- TR0=1;//打开定时器
- }
- void write_com(u8 com) //数据
- {
- rs=0;
- rw=0;
- lcden=0;
- P0=com;
- delay(5);
- lcden=1;
- delay(5);
- lcden=0;
- }
- void write_date(u8 date) //命令
- {
- rs=1;
- rw=0;
- lcden=0;
- P0=date;
- delay(5);
- lcden=1;
- delay(5);
- lcden=0;
- }
- void init()
- {
- u8 num;
- lcden=0;
- fen=2;
- miao=30;
- write_com(0x38);
- write_com(0x01);
- write_com(0x0c);
- write_com(0x06);
-
- write_com(0x00+0x80);
- for(num=0;num<15;num++)
- {
- write_date(table[num]);
- delay(20);
- }
- write_com(0x40+0x80);
- for(num=0;num<12;num++)
- { write_date(table1[num]);
- delay(20); }
- Timer0Init();
- }
- void write_sfm(u8 add,u8 date)
- {
- u8 shi,ge;
- shi=date/10;
- ge=date%10;
- write_com(0x80+0x40+add);
- write_date(0x30+shi);
- write_date(0x30+ge);
- }
- void main()
- {
- init();
- Second = 1;
- First=1;
- Timer0Init();
-
-
- while(1)
- {
- if(Second == 71)
- {
- Second = 1;
- miao=31;
- }
- if(First==142)
- {
- First=1;
- }
- //--宝田路通行,30秒--//
- if(Second < 31)
- {
- if(count==2)
- {
- count=0;
-
- miao--;
- }
- write_sfm(14,miao);
-
- //--宝田路通行--//
- xiaoden= 0xFF; //将所有的灯熄灭
- green11 = 1;
- green01 = 1;
- yellow01=1;
- red01=1;
- green00 = 0;
- green01 = 0;
- red10 = 0;
- red11= 0;
-
- if((First==52))
- {
- n=100;
- while(n--)
- {
- bell=~bell;
- delays(100);
- }
-
-
- }
- if((First==54))
- { n=100;
- while(n--)
- {
- bell=~bell;
- delays(100);
- }
- }
- if((First==56))
- { n=100;
- while(n--)
- {
- bell=~bell;
- delays(100);
- }
-
- }
- if((First==58))
- {
- n=100;
- while(n--)
- {
- bell=~bell;
- delays(100);
- }
-
- }
- if((First==60))
- {
- n=100;
- while(n--)
- {
- bell=~bell;
- delays(100);
- }
-
- }
- }
- //--黄灯等待切换状态,5秒--//
- else if(Second < 36)
- {
- if(miao==0)
- {
- miao=5;
- }
- if(count==2)
- {
- count=0;
-
- miao--;}
- write_sfm(14,miao);
- //--黄灯阶段--//
- xiaoden= 0xFF; //将所有的灯熄灭
- green11 = 1;
- green01 = 1;
- yellow01=1;
- red01=1;
- yellow00= 0;
- yellow01= 0;
- red10 = 0;
- red11= 0;
- }
- //--前进路通行--//
- else if(Second < 66)
- {
- if(miao==0)
- {
- miao=30;
- }
- if(count==2)
- {
- count=0;
- miao--;
- }
- write_sfm(14,miao);
-
- //--绿灯阶段--//
- xiaoden= 0xFF; //将所有的灯熄灭
- green11 = 1;
- green01 = 1;
- yellow01=1;
- red01=1;
- red00 = 0;
- red01= 0;
- green10 = 0;
- green11 = 0;
-
-
- if((First==122))
- {
- n=100;
- while(n--)
- {
- bell=~bell;
- delays(100);
- }
-
- }
- if((First==124))
- {
- n=100;
- while(n--)
- {
- bell=~bell;
- delays(100);
- }
-
- }
- if((First==126))
- {
- n=100;
- while(n--)
- {
- bell=~bell;
- delays(100);
- }
- }
- if((First==128))
- {
- n=100;
- while(n--)
- {
- bell=~bell;
- delays(100);
- }
-
- }
- if((First==130))
- {
- n=100;
- while(n--)
- {
- bell=~bell;
- delays(100);
- }
-
- }
-
- }
- //--黄灯等待切换状态,5秒--//
- else
- {
- if(miao==0)
- {
- miao=5;
- }
- if(count==2)
- {
- count=0;
- miao--; }
- write_sfm(14,miao);
- //--黄灯阶段--//
- xiaoden= 0xFF; //将所有的灯熄灭
- green11 = 1;
- green01 = 1;
- yellow01=1;
- red01=1;
- red00 = 0;
- red01 = 0;
- yellow10 = 0;
- yellow11 = 0;
-
- } }
- }
- void Timer0() interrupt 1
- {
- static u16 i,j;
- TH0=0XFC; //给定时器赋初值,定时1ms
- TL0=0X18;
- i++;
- j++;
-
- if(j==500)
- {
- j=0;
- First++;
- count++;
- }
-
- if(i==1000)
- {
- i=0;
- Second ++;
- }
- }
复制代码
|