上个月听到我们的电工说起51单片机,自己就心痒痒的,之前在学校听过,没过多久自己就在淘宝买了个学习板玩,附带光盘里有视频教程,现在学到第二课,里面有作业要自己做,是要流水灯来回闪的,自己没参考网上的自己写了个,想不到成功了!!O(∩_∩)O哈哈~贴出源码:
第一题:8个发光管来回流动,间隔500ms。
[sourcecode language="c"]
#include<reg52.h>
#include<intrins.h>
#define uint unsigned int
#define uchar unsigned char
uchar temp;
void zihanshu();
void main()
{
temp=0xfe;
while(1)
{
for(P2=temp;P2>0x7f;P2=temp)
{
temp=_crol_(temp,1);
zihanshu();
}
for(P2=temp;P2<0xfe;P2=temp)
{
temp=_cror_(temp,1);
zihanshu();
}
}
}
void zihanshu()
{
uint x,y;
for(x=100;x>0;x--)
for(y=600;y>0;y--);
}
[/sourcecode]
其实还可以这样:
[sourcecode language="c"]
#include<reg52.h>
#include<intrins.h>
#define uint unsigned int
#define uchar unsigned char
uchar temp;
void zihanshu();
void main()
{
temp=0xfe;
P2=temp;
while(1)
{
while(P2>0x7f)
{
temp=_crol_(temp,1);
P2=temp;
zihanshu();
}
while(P2<0xfe)
{
temp=_cror_(temp,1);
P2=temp;
zihanshu();
}
}
}
void zihanshu()
{
uint x,y;
for(x=100;x>0;x--)
for(y=600;y>0;y--);
}
[/sourcecode]
第二题:8个发光管间隔200ms由上至下,再由下至上,再由上至下,再由下至上,然后全部熄灭再以300ms间隔全部闪烁5次。重复上面过程。
[sourcecode language="c"]
#include<reg52.h>
#include<intrins.h>
#define uint unsigned int
#define uchar unsigned char
uchar temp;
void zihanshu(uint z);
void main()
{
uint b;
temp=0xfe;
P2=temp;
while(1)
{
while(P2>0x7f)
{
temp=_crol_(temp,1);
P2=temp;
zihanshu(40);
}
while(P2<0xfe)
{
temp=_cror_(temp,1);
P2=temp;
zihanshu(40);
}
while(P2>0x7f)
{
temp=_crol_(temp,1);
P2=temp;
zihanshu(40);
}
while(P2<0xfe)
{
temp=_cror_(temp,1);
P2=temp;
zihanshu(40);
}
for(b=5;b>0;b--)
{
zihanshu(58);
P2=0x00;
zihanshu(58);
P2=0xff;
}
}
}
void zihanshu(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=600;y>0;y--);
}
[/sourcecode]
不规范的方希望高手指正!51单片机越玩越有意思,可惜的是自己英文太烂!
