下面51单片机C语言程序怎么改成按钮按一下才执行,再按一下就停止,如此反复???
#include<reg52.h>#include<intrins.h>#defineuintunsignedint#defineucharunsignedcharvoi...
#include<reg52.h>
#include <intrins.h>
#define uint unsigned int
#define uchar unsigned char
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void main()
{
uchar a,i,j;
while(1)
{
for(j=0;j<3;j++)
{
P1=0x55;
delay(5000);
P1=0xaa;
delay(5000);
}
for(j=0;j<3;j++)
{
a=0xfe;
for(i=0;i<8;i++)
{
P1=a;
delay(2000);
a=_crol_(a,1);
}
}
P1=0xff;
for(j=0;j<3;j++)
{
P1=0x7e;
delay(2500);
P1=0xbd;
delay(2500);
P1=0xdb;
delay(2500);
P1=0xe7;
delay(2500);
}
P1=0xff;
for(j=0;j<3;j++)
{
P1=0xe7;
delay(2500);
P1=0xdb;
delay(2500);
P1=0xbd;
delay(2500);
P1=0x7e;
delay(2500);
}
P1=0xff;
for(j=0;j<6;j++)
{
P1=~P1;
delay(5000);
}
P1=0xff;
}
} 展开
#include <intrins.h>
#define uint unsigned int
#define uchar unsigned char
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void main()
{
uchar a,i,j;
while(1)
{
for(j=0;j<3;j++)
{
P1=0x55;
delay(5000);
P1=0xaa;
delay(5000);
}
for(j=0;j<3;j++)
{
a=0xfe;
for(i=0;i<8;i++)
{
P1=a;
delay(2000);
a=_crol_(a,1);
}
}
P1=0xff;
for(j=0;j<3;j++)
{
P1=0x7e;
delay(2500);
P1=0xbd;
delay(2500);
P1=0xdb;
delay(2500);
P1=0xe7;
delay(2500);
}
P1=0xff;
for(j=0;j<3;j++)
{
P1=0xe7;
delay(2500);
P1=0xdb;
delay(2500);
P1=0xbd;
delay(2500);
P1=0x7e;
delay(2500);
}
P1=0xff;
for(j=0;j<6;j++)
{
P1=~P1;
delay(5000);
}
P1=0xff;
}
} 展开
5个回答
展开全部
#include<reg52.h>
#include <intrins.h>
#define uint unsigned int
#define uchar unsigned char
#define ulong unsigend long
sbit aj=P2^0;//按键
uchar Flag=0;
uint js_flag=0;
uchar code tab1[]={3,2,0x55,0xaa};
uchar code tab2[]={3,8,0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f,};
uchar code tab3[]={3,4,0x7e,0xbd,0xdb,0xe7,};
uchar code tab4[]={3,4,0xe7,0xdb,0xbd,0x7e,};
uchar code tab5[]={6,2,0x00,0xff};
void key()
{
static uint ms;
if(aj==0)
{
if(++ms==4)
{
if(aj==0)
{
if(Flag==0)
{
Flag=1;
}
else if(Flag==1)
{
Flag=0;
P1=0xff;
}
}
}
}
else
{
ms=0;
}
}
uchar deng(uint time,uchar code *a)
{
uchar Size;
uchar Num;
uchar i,j;
Num=*a;
a++;
Size=*a;
a++;
P0=0xff;
for(i=0;i<Num;i++)
{
for(j=0;j<Size;j++)
{
js_flag=0;
while(js_flag<=time)
{
key();
if(Flag==0)
{
return 0;
}
P1=a[j];
}
}
}
return 1;
}
void yun(void)
{
if(Flag==1)
{
deng(5000,tab1);
deng(2000,tab2);
deng(2500,tab3);
deng(2500,tab4);
deng(5000,tab5);
}
}
void main()
{
TMOD=0x01;
EA=1;
ET0=1;
TR0=1;
Flag=0;
while(1)
{
key();
yun();
}
}
void t0()interrupt 1
{
js_flag++;
TH0=(65536-1000)/256;
TL0=(65536-1000)%256;
}
#include <intrins.h>
#define uint unsigned int
#define uchar unsigned char
#define ulong unsigend long
sbit aj=P2^0;//按键
uchar Flag=0;
uint js_flag=0;
uchar code tab1[]={3,2,0x55,0xaa};
uchar code tab2[]={3,8,0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f,};
uchar code tab3[]={3,4,0x7e,0xbd,0xdb,0xe7,};
uchar code tab4[]={3,4,0xe7,0xdb,0xbd,0x7e,};
uchar code tab5[]={6,2,0x00,0xff};
void key()
{
static uint ms;
if(aj==0)
{
if(++ms==4)
{
if(aj==0)
{
if(Flag==0)
{
Flag=1;
}
else if(Flag==1)
{
Flag=0;
P1=0xff;
}
}
}
}
else
{
ms=0;
}
}
uchar deng(uint time,uchar code *a)
{
uchar Size;
uchar Num;
uchar i,j;
Num=*a;
a++;
Size=*a;
a++;
P0=0xff;
for(i=0;i<Num;i++)
{
for(j=0;j<Size;j++)
{
js_flag=0;
while(js_flag<=time)
{
key();
if(Flag==0)
{
return 0;
}
P1=a[j];
}
}
}
return 1;
}
void yun(void)
{
if(Flag==1)
{
deng(5000,tab1);
deng(2000,tab2);
deng(2500,tab3);
deng(2500,tab4);
deng(5000,tab5);
}
}
void main()
{
TMOD=0x01;
EA=1;
ET0=1;
TR0=1;
Flag=0;
while(1)
{
key();
yun();
}
}
void t0()interrupt 1
{
js_flag++;
TH0=(65536-1000)/256;
TL0=(65536-1000)%256;
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
当然是使用中断,es 让按键触发后让布尔值赋值1进入while(Btn_Pressed)nop(); 无限空,然后再按让Btn_Pressed = 0;跳出循环 完毕
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2014-11-14
展开全部
这个好像得把延时用定时器才能实现
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
程序发给我
我就可以改了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询