流水灯代码实现
//流水灯左移
//其中调用math.h文件中的pow函数
#include <reg52.h>
#include <math.h>
sbit ADDR0 = P1 ^ 0;
sbit ADDR1 = P1 ^ 1;
sbit ADDR2 = P1 ^ 2;
sbit ADDR3 = P1 ^ 3;
sbit ENLED = P1 ^ 4;
int main(void)
{
int count;
ENLED = 0;
ADDR3 = 1;
ADDR2 = 1;
ADDR1 = 1;
ADDR0 = 0;
count = 0;
while (1)
{
int j;
float fl;
int m;
if (8 == count)
count = 0;
fl = pow(2, count);
m = fl;
P0 = 0xFF ^ m;
for (j = 0; j < 9000; j++)
;
count++;
}
}
//流水灯左移
#include <reg52.h>
sbit ADDR0 = P1 ^ 0;
sbit ADDR1 = P1 ^ 1;
sbit ADDR2 = P1 ^ 2;
sbit ADDR3 = P1 ^ 3;
sbit ENLED = P1 ^ 4;
/*
@func:求取2的m次幂
@ret:返回2的m次幂的整型值
*/
int POW(int m)
{
int i;
int iRet;
iRet = 1;
for (i = 0; i < m; i++)
iRet = iRet * 2;
return iRet;
}
int main(void)
{
int count;
ENLED = 0;
ADDR3 = 1;
ADDR2 = 1;
ADDR1 = 1;
ADDR0 = 0;
count = 0;
while (1)
{
int j;
if (8 == count)
count = 0;
P0 = 0xFF ^ POW(count);
for (j = 0; j < 9000; j++)
;
count++;
}
}
//流水灯右移
#include <reg52.h>
sbit ADDR0 = P1 ^ 0;
sbit ADDR1 = P1 ^ 1;
sbit ADDR2 = P1 ^ 2;
sbit ADDR3 = P1 ^ 3;
sbit ENLED = P1 ^ 4;
int POW(int m)
{
int i;
int iRet;
iRet = 1;
for (i = 0; i < m; i++)
iRet = iRet * 2;
return iRet;
}
int main(void)
{
int count;
ENLED = 0;
ADDR3 = 1;
ADDR2 = 1;
ADDR1 = 1;
ADDR0 = 0;
count = 7;
while (1)
{
int j;
P0 = 0xFF ^ POW(count);
for (j = 0; j < 9000; j++)
;
if (0 == count)
count = 8;
count--;
}
}
//流水灯左移到头接着右移,右移到头再左移
#include <reg52.h>
sbit ADDR0 = P1 ^ 0;
sbit ADDR1 = P1 ^ 1;
sbit ADDR2 = P1 ^ 2;
sbit ADDR3 = P1 ^ 3;
sbit ENLED = P1 ^ 4;
int POW(int m)
{
int i;
int iRet;
iRet = 1;
for (i = 0; i < m; i++)
iRet = iRet * 2;
return iRet;
}
int main(void)
{
int count;
int iflag; //iflag = 0时,从右向左,iflag = 1时,从左向右
ENLED = 0;
ADDR3 = 1;
ADDR2 = 1;
ADDR1 = 1;
ADDR0 = 0;
count = 0;
iflag = 0;
while (1)
{
int j;
if (0 == iflag)
{
if (8 == count)//最后一个灯点亮后,开始点亮倒数第二个灯
{
count = 6;
iflag = 1;
}
}
else
{
if (0 == count)//第一个灯此时未点亮,故只改变方向后进行第一个灯的点亮
iflag = 0;
}
P0 = 0xFF ^ POW(count);
for (j = 0; j < 9000; j++)
;
if (0 == iflag)
count++;
else
count--;
}
}
//流水灯右移到头接着左移,左移到头再右移
#include <reg52.h>
sbit ADDR0 = P1 ^ 0;
sbit ADDR1 = P1 ^ 1;
sbit ADDR2 = P1 ^ 2;
sbit ADDR3 = P1 ^ 3;
sbit ENLED = P1 ^ 4;
int POW(int m)
{
int i;
int iRet;
iRet = 1;
for (i = 0; i < m; i++)
iRet = iRet * 2;
return iRet;
}
int main(void)
{
int count;
int iflag; //iflag = 0时,从右向左,iflag = 1时,从左向右
ENLED = 0;
ADDR3 = 1;
ADDR2 = 1;
ADDR1 = 1;
ADDR0 = 0;
count = 7;
iflag = 1;
while (1)
{
int j;
if (0 == iflag)
{
if (8 == count)//最后一个灯点亮后,开始点亮倒数第二个灯
{
count = 6;
iflag = 1;
}
}
else
{
if (0 == count)//第一个灯此时未点亮,故只改变方向后进行第一个灯的点亮
iflag = 0;
}
P0 = 0xFF ^ POW(count);
for (j = 0; j < 9000; j++)
;
if (0 == iflag)
count++;
else
count--;
}
}
本文来自博客园,作者:EricsT,转载请注明原文链接:https://www.cnblogs.com/EricsT/p/18446164