|
本帖最后由 天外飛仙 于 2020-6-8 01:29 編輯
求教各位大佬。我在寫(xiě)51單片機(jī)交通燈的時(shí)候遇到問(wèn)題,可能是中斷出問(wèn)題了,運(yùn)行以后,第一輪狀態(tài)的燈跟數(shù)碼管都是正常的(如下圖所示)
3bcd2d9cdc0368f91b5557ce4a51aad.png (140.23 KB, 下載次數(shù): 44)
下載附件
第一輪狀態(tài)
2020-6-8 01:02 上傳
但是跳變到第二輪狀態(tài)的時(shí)候就定在初始狀態(tài),一動(dòng)不動(dòng),
b41ec759217b6240b44be73e33ae2e2.png (100.4 KB, 下載次數(shù): 33)
下載附件
第二輪狀態(tài)
2020-6-8 01:06 上傳
附件里有代碼跟protues仿真工程文件
- #include"reg51.h"
- #define uchar unsigned char
- #define uint unsigned int
- unsigned char count1=0; //中斷計(jì)算器
- unsigned char count2=60; //紅燈秒數(shù)變量
- unsigned char count3=55; //綠燈秒數(shù)變量
- unsigned char count4=5; //黃燈秒數(shù)變量
- unsigned char type=1; //狀態(tài)類(lèi)型
- sbit dxr=P1^0;
- sbit dxy=P1^1;
- sbit dxg=P1^2;
- sbit nbr=P1^3;
- sbit nby=P1^4;
- sbit nbg=P1^5;
- unsigned char led_mod[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90}; //led的顯示數(shù)~共陽(yáng)極;
- void delay(unsigned int time);
- timer0() interrupt 1 //T0定時(shí)方式1中斷函數(shù)
- {
- count1++; //中斷次數(shù)+1,50ms過(guò)去
- if(count1==20) //將1S分成20個(gè)50ms
- {
- count1=0;
- count2--; //count2減一1表示1S過(guò)去,顯示器-1
- count3--;
- count4--;
- }
- if(count2==0)
- count2=60;
- if(count3==0)
- count3=55;
- if(count4==0)
- count4=5;
- TH0=(65536-50000)/256; //重裝初值;
- TL0=(65536-50000)%256;
- }
- void nbr_display(uint y) // 南北向亮紅燈時(shí)候的數(shù)碼管函數(shù)
- {
- uint shi=y/10;
- uint ge=y%10;
- P2=0x01;
- P0=led_mod[shi];
- delay(1);
- P2=0x02;
- P0=led_mod[ge];
- delay(1);
- }
- void dxr_display(uint y) // 東西向亮紅燈時(shí)候的數(shù)碼管函數(shù)
- {
- uint shi=y/10;
- uint ge=y%10;
- P2=0x04;
- P0=led_mod[shi];
- delay(1);
- P2=0x08;
- P0=led_mod[ge];
- delay(1);
- }
- void nbg_display(uint y) //南北向亮綠燈時(shí)候的數(shù)碼管函數(shù)
- {
- uint shi=y/10;
- uint ge=y%10;
- P2=0x01;
- P0=led_mod[shi];
- delay(1);
- P2=0x02;
- P0=led_mod[ge];
- delay(1);
- }
- void dxg_display(uint y) //東西向亮綠燈時(shí)候的數(shù)碼管函數(shù)
- {
- uint shi=y/10;
- uint ge=y%10;
- P2=0x04;
- P0=led_mod[shi];
- delay(1);
- P2=0x08;
- P0=led_mod[ge];
- delay(1);
- }
- void nby_display(uint y) //南北向亮黃燈時(shí)候的數(shù)碼管函數(shù)
- {
- uint shi=y/10;
- uint ge=y%10;
- P2=0x01;
- P0=led_mod[shi];
- delay(1);
- P2=0x02;
- P0=led_mod[ge];
- delay(1);
- }
- void dxy_display(uint y) //東西向亮黃燈時(shí)候的數(shù)碼管函數(shù)
- {
- uint shi=y/10;
- uint ge=y%10;
- P2=0x04;
- P0=led_mod[shi];
- delay(1);
- P2=0x08;
- P0=led_mod[ge];
- delay(1);
- }
- void traffic()
- {
- if(type==1)
- {nbr=1;nby=1;nbg=0;dxr=0;dxy=1;dxg=1;nbg_display(count3);dxr_display(count2);if(count2==5)type=2;}//南北綠燈,東西紅燈,持續(xù)55秒
- if(type==2)
- {count2=5;count4=5;nbr=1;nby=0;nbg=1;dxr=0;dxy=1;dxg=1;nby_display(count4);dxr_display(count2);if(count2==0)type=3;}//南北黃燈,東西紅燈,持續(xù)5秒
- if(type==3)
- {count2=60;count3=55;nbr=0;nby=1;nbg=1;dxr=1;dxy=1;dxg=0;nbr_display(count2);dxg_display(count3);if(count2==5)type=4;}//南北紅燈,東西綠燈,持續(xù)55秒
- if(type==4)
- {count2=5;count4=5;nbr=0;nby=1;nbg=1;dxr=1;dxy=0;dxg=1;nbr_display(count2);dxy_display(count4);if(count2==0)type=1;}//南北紅燈,東西黃燈,持續(xù)5秒
-
- }
- void main()
- {
- TMOD=0x01; //設(shè)置T0定時(shí)方式1,t=50ms
- TH0=(65536-50000)/256; //計(jì)數(shù)初值a=2^16-50000us*12/12
- TL0=(65536-50000)%256; //將a的值轉(zhuǎn)化為十六進(jìn)制分高低位分別賦予TH0跟TL0
- EA=1; //開(kāi)總中斷
- ET0=1; //開(kāi)T0中斷
- TR0=1; //啟動(dòng)T0
- while(1)
- traffic();
- }
- void delay(unsigned int time)
- {
- unsigned int j=0;
- for(;time>0;time--)
- for(j=0;j<125;j++);
- }
復(fù)制代碼 還有一個(gè)問(wèn)題,如果traffic函數(shù)改成這樣,編譯以后會(huì)出現(xiàn)警告,然后在protues上仿真不了,完全沒(méi)反應(yīng),這是怎么回事,看不懂提示啊.....
e411212bcc4abfe64a5e396d3ce5cf2.png (60.89 KB, 下載次數(shù): 27)
下載附件
2020-6-8 01:11 上傳
switch(type)
{
case1:nbr=1;nby=1;nbg=0;dxr=0;dxy=1;dxg=1;nbg_display(count3);dxr_display(count2);if(count2==5)type=2;break;//南北綠燈55,東西紅燈
case2:count2=5;count4=5;nbr=1;nby=0;nbg=1;dxr=0;dxy=1;dxg=1;nby_display(count4);dxr_display(count2);if(count2==0)type=3;break;//南北黃燈,東西紅燈
case3:count2=60;count3=55;nbr=0;nby=1;nbg=1;dxr=1;dxy=1;dxg=0;nbr_display(count2);dxg_display(count3);if(count2==5)type=4;break;//南北紅燈,東西綠燈
case4:count2=5;count4=5;nbr=0;nby=1;nbg=1;dxr=1;dxy=0;dxg=1;nbr_display(count2);dxy_display(count4);if(count2==0)type=1;break;//南北紅燈,東西黃燈
}
|
-
-
紅綠交通燈.zip
2020-6-8 01:19 上傳
點(diǎn)擊文件名下載附件
21.03 KB, 下載次數(shù): 12
|