单片机交通灯课程设计数码管时间走太快了,不知道是不是代码问题(附代码)? 5
#include<reg52.h>#include<intrins.h>#defineuintunsignedint#defineucharunsignedcharuch...
#include<reg52.h>
#include<intrins.h>
#define uint unsigned int
#define uchar unsigned char
uchar temp,aa,nn;
sbit rn=P2^0;
sbit nb=P2^1;
sbit gn=P2^2;
sbit rw=P2^3;
sbit dx=P2^4;
sbit gw=P2^5;
sbit shi=P3^6;
sbit ge=P3^7; /*对要使用的单片机各个口进行定义*/
bit flag=0;
uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71}; /*将数字0123456789的16进制数定义成数组用以显示*/
void delay(uint z);
void disp(uint n);
void nb_flash(void);
void dx_flash(void);
void xint0();
void timer0();
void LED_ON();
void xint1();
void nanbei();
void dongxi(); /*将所用的子函数定义*/
void main()
{
uint num;
num=0;
aa=0;
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
TR0=1;
EX0=1;
IT0=0;
EX1=1;
IT1=1; /*中断初始化*/
while(1){
nanbei();
nb_flash();
dongxi();
}
}
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void timer0() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
aa++;
if(aa==9)
{
aa=0;
flag=1;
}
}
void disp(uint n) /*显示子函数*/
{
while(!flag)
{
ge=0;
P0=table[n%10];
delay(50);
shi=1;
ge=1;
shi=0;
P0=table[n/10];
delay(50);
shi=1;ge=1;
}
if(flag==1)
flag=0;
}
void nanbei() /*南北方向的红绿灯和数码管显示子函数*/
{
uint num;
num=30;
disp(num);
while(num<=30&&num>15)
{
num--;
P2=0xf3;
disp(num);
}
while(num<=15&&num>4) /*南北方向及转弯的红绿灯和数码管显示子函数*/
{
num--;
P2=0xb3;
disp(num);
}
}
void nb_flash(void) /*南北方向的黄灯和数码管显示子函数*/
{
uint a;
for(a=3;a>0;a--)
{
P2=0xf5;
nb=1;
delay(100);
nb=0;
disp(a);
}
}
void dongxi() /*东西方向的红绿灯和数码管显示子函数*/
{
uint num;
num=30;
while(num<=30&&num>15)
{
P2=0xde;
disp(num);
num--;
}
while(num<=15&&num>3) /*东西方向及转弯的红绿灯和数码管显示子函数*/
{
num--;
P2=0x5e;
disp(num);
}
while(num<=3&&num>0) /*东西方向的黄灯和数码管显示子函数*/
{
P2=0xee;
dx=1;
delay(100);
dx=0;
disp(num);
num--;
}
}
void xint() interrupt 2 /*中断定义*/
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
LED_ON();
}
void LED_ON()
{
uint b;
rn=0;
rw=0;
gn=1;
gw=1;
nb=1;
dx=1;
for(b=5;b>0;b--)
{
ge=0;
shi=1;
P0=table[b];
delay(1000);
ge=1;
shi=1;
}
return;
} 展开
#include<intrins.h>
#define uint unsigned int
#define uchar unsigned char
uchar temp,aa,nn;
sbit rn=P2^0;
sbit nb=P2^1;
sbit gn=P2^2;
sbit rw=P2^3;
sbit dx=P2^4;
sbit gw=P2^5;
sbit shi=P3^6;
sbit ge=P3^7; /*对要使用的单片机各个口进行定义*/
bit flag=0;
uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71}; /*将数字0123456789的16进制数定义成数组用以显示*/
void delay(uint z);
void disp(uint n);
void nb_flash(void);
void dx_flash(void);
void xint0();
void timer0();
void LED_ON();
void xint1();
void nanbei();
void dongxi(); /*将所用的子函数定义*/
void main()
{
uint num;
num=0;
aa=0;
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
TR0=1;
EX0=1;
IT0=0;
EX1=1;
IT1=1; /*中断初始化*/
while(1){
nanbei();
nb_flash();
dongxi();
}
}
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void timer0() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
aa++;
if(aa==9)
{
aa=0;
flag=1;
}
}
void disp(uint n) /*显示子函数*/
{
while(!flag)
{
ge=0;
P0=table[n%10];
delay(50);
shi=1;
ge=1;
shi=0;
P0=table[n/10];
delay(50);
shi=1;ge=1;
}
if(flag==1)
flag=0;
}
void nanbei() /*南北方向的红绿灯和数码管显示子函数*/
{
uint num;
num=30;
disp(num);
while(num<=30&&num>15)
{
num--;
P2=0xf3;
disp(num);
}
while(num<=15&&num>4) /*南北方向及转弯的红绿灯和数码管显示子函数*/
{
num--;
P2=0xb3;
disp(num);
}
}
void nb_flash(void) /*南北方向的黄灯和数码管显示子函数*/
{
uint a;
for(a=3;a>0;a--)
{
P2=0xf5;
nb=1;
delay(100);
nb=0;
disp(a);
}
}
void dongxi() /*东西方向的红绿灯和数码管显示子函数*/
{
uint num;
num=30;
while(num<=30&&num>15)
{
P2=0xde;
disp(num);
num--;
}
while(num<=15&&num>3) /*东西方向及转弯的红绿灯和数码管显示子函数*/
{
num--;
P2=0x5e;
disp(num);
}
while(num<=3&&num>0) /*东西方向的黄灯和数码管显示子函数*/
{
P2=0xee;
dx=1;
delay(100);
dx=0;
disp(num);
num--;
}
}
void xint() interrupt 2 /*中断定义*/
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
LED_ON();
}
void LED_ON()
{
uint b;
rn=0;
rw=0;
gn=1;
gw=1;
nb=1;
dx=1;
for(b=5;b>0;b--)
{
ge=0;
shi=1;
P0=table[b];
delay(1000);
ge=1;
shi=1;
}
return;
} 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询