|
- #include<reg51.H>
- #define uchar unsigned char
- #define uint unsigned int
- uchar code table[]={ //共陰極數(shù)碼管碼表
- 0x3f,0x06,0x5b,0x4f,
- 0x66,0x6d,0x7d,0x07,
- 0x7f,0x6f,0x77,0x7c,
- 0x39,0x5e,0x79,0x71,
- 0xC9,0xFF,0x40};//設(shè)置碼,測(cè)試碼,不計(jì)時(shí)碼
- void delay(uint x);//延時(shí)函數(shù)
- void display(uchar,uchar,uchar,uchar); //數(shù)碼管顯示函數(shù)
- void mkeys(); //鍵盤函數(shù)
- void traffic(); //交通燈函數(shù)
- uchar num,num1,num2, //1南北 2東西
- shi1,ge1,shi2,ge2,
- value1,value2,//南北 綠燈時(shí)間 黃燈時(shí)間
- value3,value4,//東西 綠燈時(shí)間 黃燈時(shí)間
- count1,count2,flag1,flag2; //南北標(biāo)記 東西標(biāo)記
- void main()
- {
- TMOD=0x01;
- TH0=(65536-45872)/256;
- TL0=(65536-45872)%256;
- EA=1; //中斷的總開關(guān)
- ET0=1; //定時(shí)器0的專用中斷開關(guān)
- TR0=1; //啟動(dòng)定時(shí)器開始定時(shí)計(jì)數(shù)的開關(guān)
- /*初狀態(tài)*/
- value1=15; //南北 黃綠燈默認(rèn)值
- value2=5;
- value3=10; //東西 黃綠燈默認(rèn)值
- value4=5;
- num1=value1; //南北數(shù)碼管先綠燈時(shí)間
- num2=value2+value1;//東西紅燈時(shí)間
- shi1=num1/10;
- ge1=num1%10;
- shi2=num2/10;
- ge2=num2%10;
- P1=0x41;//初始狀態(tài):東西紅燈 南北綠燈 20s 15s
- while(1){
- if(num==20) //定時(shí)器1s
- {
- num=0;
- num1--;
- num2--;
- traffic();
- shi1=num1/10;
- ge1=num1%10;
- shi2=num2/10;
- ge2=num2%10;
- }
- {
- mkeys();
- }
- display(shi1,ge1,shi2,ge2);
- }
- }
- void traffic() //紅綠燈主控制程序
- {
- if(num1==0){
- count1++;
- if(count1==1){
- P1=0x42; //東西紅燈 南北黃燈 5s 5s
- num1=value2;
- }
- if(count1==2){
- num1=value3+value4;//東西綠燈 南北紅燈 10s 15s
- P1=0x14;
- }
- if(count1==3){
- P1=0x41;// 東西黃燈 南北紅燈 5s 5s
- num1=value4;
- count1=0;
- }
- }
- if(num2==0){
- count2++;
- if(count2==1){
- //P1=0x14;//東西綠燈 南北紅燈
- num2=value3;
- }
- if(count2==2){
- P1=0x24;//東西黃燈 南北紅燈
- num2=value4;
- }
- if(count2==3){
- num2=value1+value2; //東西紅燈 南北綠燈
- num1=value1;
- count2=0;
- }
-
- }
- }
- void display(uchar shi1,uchar ge1,uchar shi2,uchar ge2) //數(shù)碼管顯示子函數(shù)
- {
- uchar temp;
- temp=P2;
- P2=0xfe; //位選
- P0=table[shi1]; //段選
- delay(5);
-
- P2=0xfd;
- P0=table[ge1];
- delay(5);
-
- P2=0xfb;
- P0=table[shi2];
- delay(5);
-
- P2=0xf7;
- P0=table[ge2];
- delay(5);
- }
- void delay(uint x)//延時(shí)子函數(shù)
- {
- uint i,j;
- for(i=x;i>0;i--)
- for(j=110;j>0;j--);
- }
復(fù)制代碼 |
|