51单片机用定时器实现流水灯左右移动?
我的程序是这个样子,就是不知道在怎么样返回?(就是不知道怎么样取反?)#include<reg51.h>unsignedchara=0xfe;bitflag=0;bitk...
我的程序是这个样子,就是不知道在怎么样返回?(就是不知道怎么样取反?)
#include<reg51.h>
unsigned char a=0xfe;
bit flag=0;
bit k=0;
unsigned char n=0;
void main()
{
EA=1;
ET1=1;
TR1=1;
TH1=0x3c;
TL1=0xb0;
TMOD=0x10;
while(1)
{
if(flag==1)
{
P0=a;
a<<=1;
a|=1;
flag=0;
}
}
}
void tm()interrupt 3
{
n++;
TH1=0x3c;
TL1=0xb0;
if(n==20)
{
flag=1;
n=0;
}
} 展开
#include<reg51.h>
unsigned char a=0xfe;
bit flag=0;
bit k=0;
unsigned char n=0;
void main()
{
EA=1;
ET1=1;
TR1=1;
TH1=0x3c;
TL1=0xb0;
TMOD=0x10;
while(1)
{
if(flag==1)
{
P0=a;
a<<=1;
a|=1;
flag=0;
}
}
}
void tm()interrupt 3
{
n++;
TH1=0x3c;
TL1=0xb0;
if(n==20)
{
flag=1;
n=0;
}
} 展开
3个回答
展开全部
#include<reg51.h>
#include<intrins.h>
unsigned char a = 0xfe;
unsigned char n = 0, k = 0;
bit flag;
void main()
{
TMOD = 0x10;
TH1 = 0x3c;
TL1 = 0xb0;
TR1=1;
ET1=1;
EA=1;
P0 = a;
while(1);
}
void tm()interrupt 3
{
TH1 = 0x3c;
TL1 = 0xb0;
n++; n %= 10;
if(n == 0) {
if(flag) a = _cror_(a, 1);
else a = _crol_(a, 1);
k++; k %= 7;
if (!k) flag= !flag;
P0 = a;
}
}
#include<intrins.h>
unsigned char a = 0xfe;
unsigned char n = 0, k = 0;
bit flag;
void main()
{
TMOD = 0x10;
TH1 = 0x3c;
TL1 = 0xb0;
TR1=1;
ET1=1;
EA=1;
P0 = a;
while(1);
}
void tm()interrupt 3
{
TH1 = 0x3c;
TL1 = 0xb0;
n++; n %= 10;
if(n == 0) {
if(flag) a = _cror_(a, 1);
else a = _crol_(a, 1);
k++; k %= 7;
if (!k) flag= !flag;
P0 = a;
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include<reg51.h>
unsigned char a=0xfe;
bit flag=0;
bit k=0;
unsigned char n=0;
void main()
{
EA=1;
ET1=1;
TR1=1;
TH1=0x3c;
TL1=0xb0;
TMOD=0x10;
while(1)
{
if(flag==1)
{
P0=a;
flag=0;
if (k==0)
{
a<<=1;
a|=1;
if (a==0xff)
{
a=0xfe;
k=1;
}
}
else
{
a>>=1;
a|=0x80;
if (a==0)
{
a=0xfe;
k=0;
}
}
}
}
}
void tm()interrupt 3
{
n++;
TH1=0x3c;
TL1=0xb0;
if(n==20)
{
flag=1;
n=0;
}
}
unsigned char a=0xfe;
bit flag=0;
bit k=0;
unsigned char n=0;
void main()
{
EA=1;
ET1=1;
TR1=1;
TH1=0x3c;
TL1=0xb0;
TMOD=0x10;
while(1)
{
if(flag==1)
{
P0=a;
flag=0;
if (k==0)
{
a<<=1;
a|=1;
if (a==0xff)
{
a=0xfe;
k=1;
}
}
else
{
a>>=1;
a|=0x80;
if (a==0)
{
a=0xfe;
k=0;
}
}
}
}
}
void tm()interrupt 3
{
n++;
TH1=0x3c;
TL1=0xb0;
if(n==20)
{
flag=1;
n=0;
}
}
追问
不行啊!还是左移,不能右移?
追答
#include
unsigned char a=0xfe;
bit flag=0;
bit k=0;
unsigned char n=0;
void main()
{
EA=1;
ET1=1;
TR1=1;
TH1=0x3c;
TL1=0xb0;
TMOD=0x10;
while(1)
{
if(flag==1)
{
P0=a;
flag=0;
if (k==0)
{
a>=1;
a|=0x80;
if (a==0xff)//这个地方搞错了,不好意思。
{
a=0xfe;
k=0;
}
}
}
}
}
void tm()interrupt 3
{
n++;
TH1=0x3c;
TL1=0xb0;
if(n==20)
{
flag=1;
n=0;
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include<reg52.h>
void main()
{
unsigned char cnt = 0;
unsigned char s = 0;
unsigned char z = 0;
unsigned char dir = 0;
TMOD = 0x01;
TH0 = 0xB8;
TL0 = 0x00;
TR0 = 1;
while(1)
{
if(TF0 == 1)
{
TF0 = 0;
TH0 = 0xB8;
TL0 = 0x00;
cnt++;
if(cnt >= 50)
{
cnt = 0;
if(dir == 0)
{
P2 = ~ (0x01 << s);
s++;
if(s >= 7)
{
dir = 1;
s = 0;
}
}
else
{
P2 = ~ (0x80 >> s);
s++;
if(s >= 7)
{
dir = 0;
s = 0;
}
}
}
}
}
}
void main()
{
unsigned char cnt = 0;
unsigned char s = 0;
unsigned char z = 0;
unsigned char dir = 0;
TMOD = 0x01;
TH0 = 0xB8;
TL0 = 0x00;
TR0 = 1;
while(1)
{
if(TF0 == 1)
{
TF0 = 0;
TH0 = 0xB8;
TL0 = 0x00;
cnt++;
if(cnt >= 50)
{
cnt = 0;
if(dir == 0)
{
P2 = ~ (0x01 << s);
s++;
if(s >= 7)
{
dir = 1;
s = 0;
}
}
else
{
P2 = ~ (0x80 >> s);
s++;
if(s >= 7)
{
dir = 0;
s = 0;
}
}
}
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询