基于51单片机的交通灯,出现数码管闪烁,还有噪音?
时间:10-02
整理:3721RD
点击:
我在做一个基于51单片机的交通灯,在普中HC6800-ES v2.0开发板上做,我把信号灯接在p1口0-5端口,自己焊接了一个信号灯模块,现在程序运行后正常通行四个子状态以及全面禁行模式都没问题,但是切换南北限行或者切换东西限行时,会出现数码管闪烁,还有一点噪音,比如东西方向原本绿灯,切换东西限行,会先有5秒黄灯,然后红灯,这个时候切换后原本应该是黄灯 ,但是这个时候数码管就会闪烁,时间看不清。并且信号灯显示也有问题,大概5秒后就正常了原本只应该亮两个灯,但是会亮四个,其中两个闪烁5秒左右会熄灭,求大神解答
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
#define zrun_sta1 0xe1
#define zrun_sta2 0xd1
#define zrun_sta3 0xcc
#define zrun_sta4 0xca
#define nbxx_sta1 0xca
#define nbxx_sta2 0xe1
#define dxxx_sta1 0xe1
#define dxxx_sta2 0xcc
#define stop_sta 0xc9
sbit nanbei_r=P1^0;
sbit dongxi_r=P1^3;
sbit wei1=P2^2;
sbit wei2=P2^3;
sbit wei3=P2^4;
sbit key1=P3^0;
sbit key2=P3^1;
sbit key3=P3^2;
sbit key4=P3^3;
uchar code seg7[]={
0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71
};
uchar dxtime,nbtime,count,runmode,sec;
void delayms(uint xms)
{
uint i,j;
for(i=xms;i>0;i--)
for(j=110;j>0;j--);
}
void display(uchar nbtime,uchar dxtime)
{
uchar shi,ge;
shi=nbtime/10;
ge=nbtime%10;
P0=seg7[shi];
wei1=1;
wei2=1;
wei3=1;
delayms(5);
P0=0x00;
P0=seg7[ge];
wei1=0;
wei2=1;
wei3=1;
delayms(5);
P0=0x00;
shi=dxtime/10;
ge=dxtime%10;
P0=seg7[shi];
wei1=1;
wei2=0;
wei3=1;
delayms(5);
P0=0x00;
P0=seg7[ge];
wei1=0;
wei2=0;
wei3=1;
delayms(5);
P0=0x00;
}
void regu_run()
{
if(sec<30)
P1=zrun_sta1;
else if(sec<35)
P1=zrun_sta2;
else if(sec<65)
P1=zrun_sta3;
else if(sec<70)
P1=zrun_sta4;
display(nbtime,dxtime);
}
void nbxx()
{
bit btemp;
btemp=nanbei_r;
if(btemp==1)
{
if(sec<5)
{
P1=nbxx_sta1;
display(nbtime,88);
}
else
{
P1=nbxx_sta2;
display(88,88);
}
}
else
{
P1=nbxx_sta2;
display(88,88);
}
}
void dxxx()
{
bit btemp;
btemp=dongxi_r;
if(btemp==1)
{
if(sec<5)
{
P1=dxxx_sta1;
display(88,dxtime);
}
else
{
P1=dxxx_sta2;
display(88,88);
}
}
else
{
P1=dxxx_sta2;
display(88,88);
}
}
void keyscan()
{
if(key1==0)
{
delayms(2);
if(key1==0)
{
while(!key1); //等待k1键松开
runmode=1;
dxtime=30;
nbtime=35;
sec=0;
TR0=1;
}
}
if(key2==0)
{
delayms(10);
if(key2==0)
{
while(!key2);
TR0=0;
runmode=2;
nbtime=5;
sec=0;
TR0=1;
}
}
if(key3==0)
{
delayms(10);
if(key3==0)
{
while(!key3);
TR0=0;
dxtime=5;
runmode=3;
sec=0;
TR0=1;
}
}
if(key4==0)
{
delayms(2);
if(key4==0)
{
while(!key4);
runmode=4;
sec=0;
TR0=0;
}
}
}
void main()
{
P2=0xFF; //防止出现交叉重影
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
sec=1;
nbtime=35;
dxtime=30;
TR0=0;
while(1)
{
keyscan();
if(runmode==1)
regu_run();
if(runmode==2)
nbxx();
if(runmode==3)
dxxx();
if(runmode==4)
{
TR0=0;
P1=stop_sta;
display(88,88);
}
}
}
void timer0_int()interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
count++;
if(count==20)
{
sec++;
nbtime--;
dxtime--;
if(sec==70)
{
sec=0;
nbtime=35;
dxtime=30;
}
if(sec==30)
{
dxtime=5;
}
if(sec==35)
{
dxtime=35;
nbtime=30;
}
if(sec==65)
{
nbtime=5;
}
count=0;
}
TR0=1;
}
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
#define zrun_sta1 0xe1
#define zrun_sta2 0xd1
#define zrun_sta3 0xcc
#define zrun_sta4 0xca
#define nbxx_sta1 0xca
#define nbxx_sta2 0xe1
#define dxxx_sta1 0xe1
#define dxxx_sta2 0xcc
#define stop_sta 0xc9
sbit nanbei_r=P1^0;
sbit dongxi_r=P1^3;
sbit wei1=P2^2;
sbit wei2=P2^3;
sbit wei3=P2^4;
sbit key1=P3^0;
sbit key2=P3^1;
sbit key3=P3^2;
sbit key4=P3^3;
uchar code seg7[]={
0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71
};
uchar dxtime,nbtime,count,runmode,sec;
void delayms(uint xms)
{
uint i,j;
for(i=xms;i>0;i--)
for(j=110;j>0;j--);
}
void display(uchar nbtime,uchar dxtime)
{
uchar shi,ge;
shi=nbtime/10;
ge=nbtime%10;
P0=seg7[shi];
wei1=1;
wei2=1;
wei3=1;
delayms(5);
P0=0x00;
P0=seg7[ge];
wei1=0;
wei2=1;
wei3=1;
delayms(5);
P0=0x00;
shi=dxtime/10;
ge=dxtime%10;
P0=seg7[shi];
wei1=1;
wei2=0;
wei3=1;
delayms(5);
P0=0x00;
P0=seg7[ge];
wei1=0;
wei2=0;
wei3=1;
delayms(5);
P0=0x00;
}
void regu_run()
{
if(sec<30)
P1=zrun_sta1;
else if(sec<35)
P1=zrun_sta2;
else if(sec<65)
P1=zrun_sta3;
else if(sec<70)
P1=zrun_sta4;
display(nbtime,dxtime);
}
void nbxx()
{
bit btemp;
btemp=nanbei_r;
if(btemp==1)
{
if(sec<5)
{
P1=nbxx_sta1;
display(nbtime,88);
}
else
{
P1=nbxx_sta2;
display(88,88);
}
}
else
{
P1=nbxx_sta2;
display(88,88);
}
}
void dxxx()
{
bit btemp;
btemp=dongxi_r;
if(btemp==1)
{
if(sec<5)
{
P1=dxxx_sta1;
display(88,dxtime);
}
else
{
P1=dxxx_sta2;
display(88,88);
}
}
else
{
P1=dxxx_sta2;
display(88,88);
}
}
void keyscan()
{
if(key1==0)
{
delayms(2);
if(key1==0)
{
while(!key1); //等待k1键松开
runmode=1;
dxtime=30;
nbtime=35;
sec=0;
TR0=1;
}
}
if(key2==0)
{
delayms(10);
if(key2==0)
{
while(!key2);
TR0=0;
runmode=2;
nbtime=5;
sec=0;
TR0=1;
}
}
if(key3==0)
{
delayms(10);
if(key3==0)
{
while(!key3);
TR0=0;
dxtime=5;
runmode=3;
sec=0;
TR0=1;
}
}
if(key4==0)
{
delayms(2);
if(key4==0)
{
while(!key4);
runmode=4;
sec=0;
TR0=0;
}
}
}
void main()
{
P2=0xFF; //防止出现交叉重影
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
sec=1;
nbtime=35;
dxtime=30;
TR0=0;
while(1)
{
keyscan();
if(runmode==1)
regu_run();
if(runmode==2)
nbxx();
if(runmode==3)
dxxx();
if(runmode==4)
{
TR0=0;
P1=stop_sta;
display(88,88);
}
}
}
void timer0_int()interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
count++;
if(count==20)
{
sec++;
nbtime--;
dxtime--;
if(sec==70)
{
sec=0;
nbtime=35;
dxtime=30;
}
if(sec==30)
{
dxtime=5;
}
if(sec==35)
{
dxtime=35;
nbtime=30;
}
if(sec==65)
{
nbtime=5;
}
count=0;
}
TR0=1;
}
问题已经完美解决了
这个简单,肯定是程序的问题了!明日白天再来仔细看和回复你。
不带这样的,黑幕呀~~~~~~~~黑幕!