|
本帖最后由 天外飛仙 于 2020-6-8 01:29 编辑
求教各位大佬。我在写51单片机交通灯的时候遇到问题,可能是中断出问题了,运行以后,第一轮状态的灯跟数码管都是正常的(如下图所示)
但是跳变到第二轮状态的时候就定在初始状态,一动不动,
附件里有代码跟protues仿真工程文件
- #include"reg51.h"
- #define uchar unsigned char
- #define uint unsigned int
- unsigned char count1=0; //中断计算器
- unsigned char count2=60; //红灯秒数变量
- unsigned char count3=55; //绿灯秒数变量
- unsigned char count4=5; //黄灯秒数变量
- unsigned char type=1; //状态类型
- 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的显示数~共阳极;
- void delay(unsigned int time);
- timer0() interrupt 1 //T0定时方式1中断函数
- {
- count1++; //中断次数+1,50ms过去
- if(count1==20) //将1S分成20个50ms
- {
- count1=0;
- count2--; //count2减一1表示1S过去,显示器-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) // 南北向亮红灯时候的数码管函数
- {
- 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) // 东西向亮红灯时候的数码管函数
- {
- 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) //南北向亮绿灯时候的数码管函数
- {
- 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) //东西向亮绿灯时候的数码管函数
- {
- 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) //南北向亮黄灯时候的数码管函数
- {
- 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) //东西向亮黄灯时候的数码管函数
- {
- 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;}//南北绿灯,东西红灯,持续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;}//南北黄灯,东西红灯,持续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;}//南北红灯,东西绿灯,持续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;}//南北红灯,东西黄灯,持续5秒
-
- }
- void main()
- {
- TMOD=0x01; //设置T0定时方式1,t=50ms
- TH0=(65536-50000)/256; //计数初值a=2^16-50000us*12/12
- TL0=(65536-50000)%256; //将a的值转化为十六进制分高低位分别赋予TH0跟TL0
- EA=1; //开总中断
- ET0=1; //开T0中断
- TR0=1; //启动T0
- while(1)
- traffic();
- }
- void delay(unsigned int time)
- {
- unsigned int j=0;
- for(;time>0;time--)
- for(j=0;j<125;j++);
- }
复制代码 还有一个问题,如果traffic函数改成这样,编译以后会出现警告,然后在protues上仿真不了,完全没反应,这是怎么回事,看不懂提示啊.....
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;//南北红灯,东西黄灯
}
|
|