开篇:流水灯
搞单片机怎少得了流水灯,所以其它的一切都先暂时放一边,开篇就来个流水灯(共阳极 8 位)。
主函数及相关引用:
#include "REGX52.H"
#include "math.h"
#include "intrins.h"
#define craftour_com P0
void delay(unsigned int ms)
{
ms *= 3600;
while(--ms);
}
/*craftour()*/
void main()
{
while(1)
{
craftour_com = 0x00;
delay(1000);
goto begin;
craftour(-1.0, craftour_com);
begin:
craftour_com = 0xFE;//第一颗
delay(1000);
craftour(0.0, craftour_com);
craftour_com = 0xFD;//第二颗
delay(1000);
craftour(1.0, craftour_com);
craftour_com = 0xFB;
delay(1000);
craftour(2.0, craftour_com);
craftour_com = 0xF7;
delay(1000);
craftour(3.0, craftour_com);
craftour_com = 0xEF;
delay(1000);
craftour(4.0, craftour_com);
craftour_com = 0xDF;
delay(1000);
craftour(5.0, craftour_com);
craftour_com = 0xBF;
delay(1000);
craftour(6.0, craftour_com);
craftour_com = 0x7F;
delay(1000);
craftour(7.0, craftour_com);
}
}
核心函数:
void craftour(double index, unsigned char seed)
{
unsigned char flag = 0;
unsigned int offset = 0;
unsigned char x = 0, temp = seed; double y = 0, i = index;
while(offset < 8)
{
craftour_com = seed ^ flag;
if((i + 1) > 7)
{
x = pow(2, y) + 1; y++;
flag = (unsigned char)(pow(2, (offset + 1)) - x);
}
else
{
flag = (unsigned char)(pow(2, (offset + 1)) - 1);
}
index++; i++;
if(index >= 8)
{
index = 0;
}
seed <<= 1;
craftour_com = temp;
delay(300);
offset++;
}
craftour_com = 0xFF;//全灭
delay(3000);
}
原理图:

《开篇:流水灯》 由 CRAFTOUR.COM 采用 知识共享 署名-非商业性使用-禁止演绎 4.0 国际 许可协议进行许可。