|
內涵51的仿真圖和keil工程
0.png (37.77 KB, 下載次數: 79)
下載附件
2016-5-16 03:16 上傳
51單片機交通燈程序:
- #include<stdio.h>
- #include<REG51.h>
- #include<intrins.h>
- sfr p0= 0x80;
- sfr p1= 0x90;
- sfr p2= 0xA0;
- sfr p3= 0xb0; //這部分內容其實在“#include<reg51.h>”里已經有,但里面定義的必須區分大小寫,在這里,因為我程序采用的是小寫,reg51.h里對各個端口與寄存器的定義都是大寫,所以在編譯連接時,會報錯,所以,在本設計程序里,我只用到了端口,在這里也就只定義了四個,而沒有去改reg51.h里面的內容。其實兩者是一樣的。
- sbit sw = p0^0;
- sbit OE =P0^6;
- sbit LE =P0^7; //74LS373鎖存器控制端定義
- char display[]={ 0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99 }; //p1口的數碼管時間顯示調用,利用74L74BCD碼,8位驅動輸出;
- //函數聲明 begin
- void delay1(int count);
- void delay_long(int number1,int number2);
- void people_car_drive();
- //函數聲明end
- //***********************//延時子程序
- void delay1(int count)
- { int i;
- for(i=count;i>0;i--)
- { ;}
- }
- void delay_long(int number1,int number2)
- {
- int a,b;
- for(a=number1;a>0;a--)
- {
- for(b=number2;b>0;b--)
- { _nop_(); }
- }
-
- }
- //**********************//延時子程序
- void people_car_drive()
- {
- int p_1=2,i,j=9,p_2=6; //****************//行人通行時,延時20秒
- p2=0x09; //南北紅燈亮
- p3=0x24; //東西綠燈亮
-
- while(p_1-->0)
- { LE=1;
- OE=0;
- if(p_1==0){OE=1;} //當十位數減到0時,只顯示個位數
- p1=display[p_1];
- delay1(1000);
- LE=0;
- j=9;
- for(i=10;i>0;i--)
- {
- if(p_1==0&&j==3)break; //減到3時退出循環,讓其黃燈閃爍顯示
- p1=display[j--];
- delay_long(16000,2);
- if(sw==1)return;
- }
-
- }
-
- //*******************************************************************************//
-
- p2=0x12; //南北黃燈閃爍三秒,以提醒行人注意
- p3=0x12;
- p1=display[3];
- delay_long(8000,1);
- p2=0x00;
- p3=0x00;
- delay_long(14000,1);
-
- p2=0x12;
- p3=0x12;
- p1=display[2];
- delay_long(8000,1);
- p2=0x00;
- p3=0x00;
- delay_long(14000,1);
-
- p2=0x12;
- p3=0x12;
- p1=display[1];
- delay_long(8000,1);
- p2=0x00;
- p3=0x00;
- delay_long(14000,1);
- //*****************以下是車輛通行時延時60秒//
-
- p2=0x24; //南北綠燈亮
- p3=0x09; //東西紅燈亮
-
- while(p_2-->0)
- { LE=1;
- OE=0;
- if(p_2==0){OE=1;} //當十位數減到0時,只顯示個位數
- p1=display[p_2];
- delay1(1000);
- LE=0;
- j=9;
- for(i=10;i>0;i--)
- {
- if(p_2==0&&j==3)break; //減到2時退出循環
- p1=display[j--];
- delay_long(16000,2);
- if(sw==1)return;
- }
- }
-
- p2=0x12; //南北黃燈閃爍三秒,以提醒行人注意
- p3=0x12;
- p1=display[3];
- delay_long(8000,1);
- p2=0x00;
- p3=0x00;
- delay_long(14000,1);
-
- p2=0x12;
- p3=0x12;
- p1=display[2];
- delay_long(8000,1);
- p2=0x00;
- p3=0x00;
- delay_long(14000,1);
-
- p2=0x12;
- p3=0x12;
- p1=display[1];
- delay_long(8000,1);
- p2=0x00;
- p3=0x00;
- delay_long(14000,1); //南北黃燈閃爍三秒完畢
- }
-
- void main() //主函數入口處
- {
- p0=0x01;
- p1=0x00;
- p2=0x00;
- p3=0x00; //初始化各端口
- { while(1)
- {
- if(sw==0)
- { people_car_drive();}
- else
- {
- p2=0x00;
- p3=0x00; //關閉所有交通燈
- }
- }
-
- }
- }
復制代碼
|
-
-
51_交通燈設計.rar
2016-5-12 20:16 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
62.51 KB, 下載次數: 9, 下載積分: 黑幣 -5
評分
-
查看全部評分
|