交通燈的仿真原理圖如下(proteus仿真工程文件可到本帖附件中下載)
0.png (21.73 KB, 下載次數(shù): 23)
下載附件
2018-5-14 03:32 上傳
0.png (11.1 KB, 下載次數(shù): 16)
下載附件
2018-5-14 03:33 上傳
單片機(jī)源程序如下:
- #include"reg51.h"
- #define uint unsigned int
- #define uchar unsigned char
- sbit dula=P2^1;
- sbit wela=P2^2;
- sbit p1_0=P1^0;
- uchar code dat[]={
- 0xc0,0xf9,0xa4,0xb0,
- 0x99,0x92,0x82,0xf8,
- 0x80,0x90,0x88,0x83,
- 0xc6,0xa1,0x86,0x8e};
- uchar code tem[]={0x01,0x02,0x04,0x08,0x10,0x20};
- uint i;
- uchar num,st;
- void delay(uint);
- void init();
- void main()
- {
- init();
- dula=1;
- P0=0xc0;
- dula=0;
- while(1)
- {
- if(i==1000)
- {
- i=0;
- num++;
- if(st==0)
- {
- if(num==10)
- {
- num=0;
- st=1;
- P2=0x40;
- }
- }
- if(st==1)
- {
- if(num==2)
- {
- num=0;
- st=2;
- P2=0x30;
- }
- }
- if(st==2)
- {
- if(num==10)
- {
- num=0;
- st=3;
- P2=0x40;
- }
- }
- if(st==3)
- {
- if(num==2)
- {
- num=0;
- st=0;
- P2=0x88;
- }
- }
- dula=1;
- P0=dat[num];
- dula=0;
- delay(5);
- }
- }
- }
- //程序初始化
- void init()
- {
- EA=1;//開總中斷允許
- EX0=0;//外部中斷0允許
- IT0=0;//外部中斷0為低電平觸發(fā)方式
- TMOD=0X01;//設(shè)定T0為16位定時(shí)器
- ET0=1;//定時(shí)器T0中斷允許
- TR0=1;//啟動(dòng)定時(shí)器T0
- TH0=0XFC;
- TL0=0X66;
- i=0;
- num=0;
- st=0;
- p1_0=1;
- wela=1;
- P0=0x01;
- wela=0;
- P0=0xff;
- P2=0x88;
- }
- //延時(shí)約為Z毫秒
- void delay(uint z)
- {
- uint x,y;
- for(x=z;x>0;x--)
- for(y=113;y>0;y--);
- }
- //外部中斷0
- void int0()interrupt 0
- {
- P2=0x28;
- st=0;
- i=0;
- ……………………
- …………限于本文篇幅 余下代碼請(qǐng)從51黑下載附件…………
復(fù)制代碼
所有資料51hei提供下載:
單片機(jī).zip
(87.5 KB, 下載次數(shù): 15)
2018-5-13 13:32 上傳
點(diǎn)擊文件名下載附件
交通燈的仿真 下載積分: 黑幣 -5
|