简单的单片机花样流水灯C语言源代码
- 格式:docx
- 大小:17.88 KB
- 文档页数:34
花样流水灯C语言程序代码Lesso2.1: 第一个发光管以间隔200ms闪烁(这是最原始的程序没有用延迟子函数)。
#include<reg52.h>sbit D1=P1^0;int a;void main(){while(1){D1=0;a=25000;while(--a);D1=1;a=25000;while(--a);}}Lesson2.2: 8个发光管由上至下间隔1s流动,其中每个管亮500ms,灭500ms,亮时蜂鸣器响,灭时关闭蜂鸣器,一直重复下去。
#include<reg52.h>#include<intrins.h>#define uint unsigned int#define uchar unsigned charvoid delay(uint);uchar temp;sbit beep=P2^3;void main(){temp=0xfe;while(1){P1=temp;beep=0;delay(500);P1=0xff;beep=1;delay(500); temp=_crol_(temp,1);}}void delay(uint z){uint x,y;for(x=z;x>0;x--) for(y=110;y>0;y--);}Lesson2.3:8个发光管来回流动,第个管亮100ms,流动时让蜂鸣器发出“滴滴”声。
#include<reg52.h>#include<intrins.h>#define uint unsigned int#define uchar unsigned charvoid delay(uint);uchar temp;int i,j;sbit beep=P2^3;void main(){temp=0xfe;while(1){for(i=7;i>0;i--){ beep=0;delay(100);beep=1;delay(100);beep=0;delay(100);beep=1;P1=temp;delay(500); temp=_crol_(temp,1);}for(i=7;i>0;i--){beep=0;delay(100);beep=1;delay(100);beep=0;delay(100);beep=1;P1=temp;delay(500); temp=_cror_(temp,1);}}}void delay(uint z){uint x,y;for(x=z;x>0;x--) for(y=110;y>0;y--);}Lesson2.4:用8个发光管演示出8位二进制数累加过程。
五种编程方式实现流水灯的单片机C程序流水灯是一种常见的灯光效果,常用于装饰和展示。
实现流水灯的程序可以使用多种不同的编程方式,包括传统的顺序编程、状态机编程、中断编程、调度器编程和面向对象编程。
下面分别介绍这五种方式实现流水灯的程序。
1.顺序编程方式:顺序编程是最常见的编程方式,也是最直接的方式。
下面是使用顺序编程方式实现流水灯的C程序:```c#include <reg52.h>void delay(unsigned int t)while(t--)for(int i=0; i<50; i++);}void mainunsigned char led = 0x80; // 初始灯光状态while(1)P0 = led; // 输出灯光状态delay(500); // 延时一段时间led >>= 1; // 右移一位,实现流水灯效果if(led == 0) // 到达最右边后重新开始led = 0x80;}}```2.状态机编程方式:状态机编程是一种基于状态的编程方式,通过定义不同的状态和状态转换来实现流水灯效果。
下面是使用状态机编程方式实现流水灯的C程序:```c#include <reg52.h>typedef enumState1,State2,State3,State4,State5} State;void delay(unsigned int t)while(t--)for(int i=0; i<50; i++);}void mainState state = State1; // 初始状态为State1 while(1)switch(state)case State1:P0=0x80;delay(500);state = State2;break;case State2:P0=0x40;delay(500);state = State3;break;case State3:P0=0x20;delay(500);state = State4;break;case State4:P0=0x10;delay(500);state = State5;break;case State5:P0=0x08;delay(500);state = State1;break;}}```3.中断编程方式:中断编程方式是一种基于中断事件的编程方式,通过在特定的中断事件触发时改变灯光状态来实现流水灯效果。
基于单片机心形流水灯C语言源程序精编Document number:WTT-LKK-GBB-08921-EIGG-22986#include<>unsigned int x,y;void delayms(unsigned int z) //延时{unsigned int i,j;for(i=z;i>0;i--)for(j=150;j>0;j--);}void On_all() //开启所有灯{P0=0x00; P1=0x00; P2=0x00; P3=0x00; }void Off_all()//关闭所有灯{P0=0xff; P1=0xff; P2=0xff; P3=0xff; }void ls()//正向流水灯{P0=0x00; delayms(400);P2=0x00; delayms(400);P3=0x00; delayms(400);P1=0x00; delayms(400);P0=0x01; delayms(50);P0=0x02; delayms(50);P0=0x04; delayms(50);P0=0x08; delayms(50);P0=0x10; delayms(50);P0=0x20; delayms(50);P0=0x40; delayms(50);P0=0x80; delayms(50);P0=0x00;P2=0x01; delayms(50);P2=0x04; delayms(50); P2=0x08; delayms(50);P2=0x10; delayms(50); P2=0x20; delayms(50);P2=0x40; delayms(50); P2=0x80; delayms(50);P2=0x00;P3=0x80; delayms(50);P3=0x40; delayms(50); P3=0x20; delayms(50);P3=0x10; delayms(50);P3=0x08; delayms(50); P3=0x04; delayms(50);P3=0x02; delayms(50); P3=0x01; delayms(50); P3=0x00;P1=0x80; delayms(50);P1=0x40; delayms(50);P1=0x20; delayms(50); P1=0x10; delayms(50);P1=0x08; delayms(50); P1=0x04; delayms(50);P1=0x02; delayms(50); P1=0x01; delayms(50); P1=0x00;Off_all();P0=0xfe; delayms(50);P0=0xfd; delayms(50);P0=0xfb; delayms(50); P0=0xf7; delayms(50); P0=0xef; delayms(50); P0=0xdf; delayms(50);P0=0x7f; delayms(50);P0=0xff;P2=0xfe; delayms(50);P2=0xfd; delayms(50);P2=0xfb; delayms(50);P2=0xf7; delayms(50);P2=0xef; delayms(50);P2=0xdf; delayms(50);P2=0xbf; delayms(50);P2=0x7f; delayms(50); P2=0xff;P3=0x7f; delayms(50);P3=0xbf; delayms(50);P3=0xdf; delayms(50);P3=0xef; delayms(50);P3=0xf7; delayms(50);P3=0xfb; delayms(50);P3=0xfd; delayms(50);P3=0xfe; delayms(50);P3=0xff;P1=0x7f; delayms(50);P1=0xbf; delayms(50);P1=0xdf; delayms(50);P1=0xef; delayms(50);P1=0xf7; delayms(50);P1=0xfb; delayms(50);P1=0xfd; delayms(50);P1=0xfe; delayms(50);P1=0xff;P0=0xfe; delayms(50);P0=0xfc; delayms(50);P0=0xf8; delayms(50);P0=0xf0; delayms(50);P0=0xe0; delayms(50);P0=0xc0; delayms(50);P0=0x80; delayms(50);P0=0x00; delayms(50);P2=0xfe; delayms(50);P2=0xfc; delayms(50);P2=0xf8; delayms(50);P2=0xf0; delayms(50);P2=0xe0; delayms(50);P2=0xc0; delayms(50);P2=0x80; delayms(50);P2=0x00; delayms(50);P3=0x7f; delayms(50);P3=0x3f; delayms(50);P3=0x1f; delayms(50);P3=0x0f; delayms(50);P3=0x07; delayms(50);P3=0x03; delayms(50);P3=0x01; delayms(50);P3=0x00; delayms(50);P1=0x7f; delayms(50);P1=0x3f; delayms(50);P1=0x1f; delayms(50);P1=0x0f; delayms(50);P1=0x07; delayms(50);P1=0x03; delayms(50);P1=0x01; delayms(50);P1=0x00; delayms(50);}void fan_ls()//反向流水灯{Off_all(); delayms(300);On_all(); delayms(300); Off_all(); delayms(300); P1=0x00;delayms(400);P3=0x00;delayms(400);P2=0x00;delayms(400);P0=0x00;delayms(400); P1=0x01;delayms(50);P1=0x02;delayms(50);P1=0x04;delayms(50);P1=0x08;delayms(50);P1=0x10;delayms(50);P1=0x20;delayms(50);P1=0x40;delayms(50);P1=0x80;delayms(50);P1=0x00;P3=0x01;delayms(50);P3=0x02;delayms(50);P3=0x04;delayms(50);P3=0x08;delayms(50);P3=0x10;delayms(50);P3=0x20;delayms(50);P3=0x40;delayms(50);P3=0x80;delayms(50);P3=0x00;P2=0x80;delayms(50);P2=0x40;delayms(50);P2=0x20;delayms(50);P2=0x10;delayms(50);P2=0x08;delayms(50);P2=0x04;delayms(50);P2=0x02;delayms(50);P2=0x01;delayms(50);P2=0x00;P0=0x80;delayms(50);P0=0x40;delayms(50);P0=0x20;delayms(50);P0=0x10;delayms(50);P0=0x08;delayms(50);P0=0x04;delayms(50); P0=0x02;delayms(50);P0=0x01;delayms(50);P0=0x00;Off_all();P1=0xfe; delayms(50);P1=0xfd; delayms(50);P1=0xfb; delayms(50); P1=0xf7; delayms(50); P1=0xef; delayms(50); P1=0xdf; delayms(50); P1=0xbf; delayms(50); P1=0x7f; delayms(50);P1=0xff;P3=0xfe; delayms(50);P3=0xfd; delayms(50);P3=0xfb; delayms(50); P3=0xf7; delayms(50); P3=0xef; delayms(50); P3=0xdf; delayms(50); P3=0xbf; delayms(50); P3=0x7f; delayms(50);P3=0xff;P2=0x7f; delayms(50);P2=0xbf; delayms(50); P2=0xdf; delayms(50);P2=0xef; delayms(50); P2=0xf7; delayms(50);P2=0xfb; delayms(50); P2=0xfd; delayms(50); P2=0xfe; delayms(50);P2=0xff;P0=0x7f; delayms(50);P0=0xbf; delayms(50);P0=0xdf; delayms(50);P0=0xef; delayms(50);P0=0xf7; delayms(50);P0=0xfb; delayms(50); P0=0xfd; delayms(50); P0=0xfe; delayms(50);P0=0xff;P1=0xfe; delayms(50);P1=0xfc; delayms(50);P1=0xf8; delayms(50);P1=0xf0; delayms(50);P1=0xe0; delayms(50);P1=0xc0; delayms(50); P1=0x80; delayms(50); P1=0x00; delayms(50);P3=0xfe; delayms(50);P3=0xfc; delayms(50);P3=0xf8; delayms(50);P3=0xf0; delayms(50);P3=0xe0; delayms(50);P3=0xc0; delayms(50); P3=0x80; delayms(50); P3=0x00; delayms(50);P2=0x7f; delayms(50);P2=0x3f; delayms(50);P2=0x1f; delayms(50);P2=0x0f; delayms(50);P2=0x07; delayms(50);P2=0x03; delayms(50);P2=0x01; delayms(50);P2=0x00; delayms(50);P0=0x7f; delayms(50);P0=0x3f; delayms(50);P0=0x1f; delayms(50);P0=0x0f; delayms(50);P0=0x07; delayms(50);P0=0x03; delayms(50);P0=0x01; delayms(50);P0=0x00; delayms(50);Off_all();On_all();Off_all();On_all();Off_all();}void ban_shan()//半边交替闪{Off_all();P1=0x00;P3=0x00;delayms(50);P0=0xff;P2=0xff;d elayms(50);P1=0xff;P3=0xff;delayms(50);P0=0x00;P2=0x00;d elayms(50);P1=0x00;P3=0x00;delayms(50);P0=0xff;P2=0xff;d elayms(50);P1=0xff;P3=0xff;delayms(50);P0=0x00;P2=0x00;d elayms(50);P1=0x00;P3=0x00;delayms(50);P0=0xff;P2=0xff;d elayms(50);P1=0xff;P3=0xff;delayms(50);P0=0x00;P2=0x00;d elayms(50);P1=0x00;P3=0x00;delayms(50);P0=0xff;P2=0xff;d elayms(50);P1=0xff;P3=0xff;delayms(50);P0=0x00;P2=0x00;d elayms(50);P1=0x00;P3=0x00;delayms(50);P0=0xff;P2=0xff;d elayms(50);P1=0xff;P3=0xff;delayms(50);P0=0x00;P2=0x00;d elayms(50);}void shangxia_shan()//上下交替闪{On_all();Off_all();P0=0x00;P1=0x00;delayms(50);P2=0xff;P3=0xff;d elayms(50);P0=0xff;P1=0xff;delayms(50);P2=0x00;P3=0x00;d elayms(50);P0=0x00;P1=0x00;delayms(50);P2=0xff;P3=0xff;d elayms(50);P0=0xff;P1=0xff;delayms(50);P2=0x00;P3=0x00;d elayms(50);P0=0x00;P1=0x00;delayms(50);P2=0xff;P3=0xff;d elayms(50);P0=0xff;P1=0xff;delayms(50);P2=0x00;P3=0x00;d elayms(50);P0=0x00;P1=0x00;delayms(50);P2=0xff;P3=0xff;d elayms(50);P0=0xff;P1=0xff;delayms(50);P2=0x00;P3=0x00;d elayms(50);P0=0x00;P1=0x00;delayms(50);P2=0xff;P3=0xff;d elayms(50);P0=0xff;P1=0xff;delayms(50);P2=0x00;P3=0x00;d elayms(50);}void huayang_shan()//花样闪烁{On_all();delayms(400);P0=0xff;P2=0x00;P3=0x00;P1=0x00;delayms(200);P0=0x00;P2=0xff;P3=0x00;P1=0x00;delayms(200);P0=0x00;P2=0x00;P3=0xff;P1=0x00;delayms(200);P0=0x00;P2=0x00;P3=0x00;P1=0xff;delayms(200);P0=0x00;P2=0x00;P3=0xff;P1=0x00;delayms(200);P0=0x00;P2=0xff;P3=0x00;P1=0x00;delayms(200);P0=0xff;P2=0x00;P3=0x00;P1=0x00;delayms(200);On_all();delayms(400);P0=0x00;P2=0x00;P3=0x00;P1=0xff;delayms(200);P0=0x00;P2=0x00;P3=0xff;P1=0x00;delayms(200);P0=0x00;P2=0xff;P3=0x00;P1=0x00;delayms(200);P0=0xff;P2=0x00;P3=0x00;P1=0x00;delayms(200);P0=0x00;P2=0xff;P3=0x00;P1=0x00;delayms(200);P0=0x00;P2=0x00;P3=0xff;P1=0x00;delayms(200);P0=0x00;P2=0x00;P3=0x00;P1=0xff;delayms(200);On_all();delayms(400);Off_all();delayms(400);P0=0x00;delayms(200);P3=0x00;delayms(200);P1=0x00;delayms(200);P2=0x00;delayms(200);On_all();delayms(400);Off_all();delayms(400);P1=0x00;delayms(200);P2=0x00;delayms(200);P0=0x00;delayms(200);P3=0x00;delayms(200);On_all();delayms(400);Off_all();delayms(50);On_all();delayms(50);Off_all();delayms(50);On_all();delayms(50);Off_all();delayms(50);}void main(){On_all(); delayms(300);Off_all(); delayms(300);On_all(); delayms(300);Off_all(); delayms(300);On_all(); delayms(300);Off_all(); delayms(300);ls(); //正向流水fan_ls(); //反向流水ban_shan(); //半边交替闪shangxia_shan(); //上下交替闪烁huayang_shan();// 花样闪烁}。
简单的51单片机花样流水灯C语言源代码#include<reg51.h> //51系列单片机定义文件#define uchar unsigned char //定义无符号字符#define uint unsigned int //定义无符号整数void Delayms(uint x){ //定义延时函数uint i,j;for(i=x;i>0;i--)for(j=110;j>0;j--);}void main(){uint i;uchar temp;while(1){temp=0x01; //8个流水灯逐个闪动for(i=0;i<8;i++){P0=~temp;Delayms(50);temp<<=1;}temp=0x80; //8个流水灯反向逐个闪动for(i=0;i<8;i++){P0=~temp;Delayms(50);temp>>=1;}temp=0xfe; //8个流水灯依次全部点亮for(i=0;i<8;i++){P0=temp;Delayms(50);temp<<=1;}temp=0x7f; //8个流水灯依次反向全部点亮for(i=0;i<8;i++){P1=temp;Delayms(50);temp>>=1;}}}//此程序的作者向往未来#include<reg52.h>#include<intrins.h>#define uint unsigned int #define uchar unsigned char uchar z=50,e=0x00,f=0xff; uchar code table1[]={0x80,0xc0,0xe0,0xf0,0xf8,0xfc,0xfe,0xff};uchar code table2[]={0x7f,0x3f,0x1f,0x0f,0x07,0x03,0x01,0x00}; uchar code table3[]={0x01,0x03,0x07,0x0f,0x1f,0x3f,0x7f,0xff};uchar code table4[]={0xe7,0xdb,0xbd,0x7e,0xbd,0xdb,0xe7,0xff}; uchar code table5[]={0xe7,0xc3,0x81,0x00,0x81,0xc3,0xe7,0xff};uchar code table6[]={0x7e,0x3c,0x18,0x00, 0x18,0x3c,0x7e,0xff}; void delay(uchar); void lsd1();void lsd2();void lsd3();void lsd4();void lsd5();void lsd6();void lsd7();void lsd8();void lsd9();void lsd10();void lsd11();void lsd12();main(){while(1){lsd1();lsd2();lsd3();lsd4();lsd5();lsd6();lsd7();lsd8();lsd9();lsd10();lsd11();lsd12();}}void delay(uchar x) //延时函数{uint i,j;for(i=x;i>0;i--)for(j=250;j>0;j--);}void lsd1() //lsd1 单个流水灯双程模式1 {uchar a,i,j,k,l,l1,k1,j1,i1;a=0xfe;P0=a;delay(z);for(i=0;i<7;i++) //仅单个灯亮从上往下流{a=_crol_(a,1);P0=a;delay(z);}P0=0xff;a=0xfe;P2=a;delay(z);for(j=0;j<7;j++){a=_crol_(a,1);P2=a;delay(z);}P2=0xff;a=0xfe;P3=a;delay(z);for(k=0;k<7;k++){a=_crol_(a,1);P3=a;delay(z);}P3=0xff;a=0xfe;P1=a;delay(z);for(l=0;l<7;l++){a=_crol_(a,1);P1=a;delay(z);}a=0xbf;P1=a;delay(z);for(l1=0;l1<6;l1++) //l1==6 仅单个灯亮从下往上流{a=_cror_(a,1);P1=a;delay(z);}P1=0xff;a=0x7f;P3=a;delay(z);for(k1=0;k1<7;k1++) {a=_cror_(a,1);P3=a;delay(z);}P3=0xff;a=0x7f;P2=a;delay(z);for(j1=0;j1<7;j1++) {a=_cror_(a,1);P2=a;delay(z);}P2=0xff;a=0x7f;P0=a;delay(z);for(i1=0;i1<7;i1++) {a=_cror_(a,1);P0=a;delay(z);P0=0xff;}void lsd2() //lsd2 两个灯流水双程模式1 {uchar a,i,j,k,l,l1,k1,j1,i1;a=0xfe;P0=a;delay(z);a=a<<1;P0=a;delay(z);for(i=0;i<6;i++){a=_crol_(a,1);P0=a;delay(z);}P0=0x7f;P2=0xfe;delay(z);P0=0xff;a=0xfc;P2=a;delay(z);for(j=0;j<6;j++){a=_crol_(a,1);P2=a;delay(z);P2=0x7f;P3=0xfe;delay(z);P2=0xff;a=0xfc;P3=a;delay(z);for(k=0;k<6;k++) {a=_crol_(a,1);P3=a;delay(z);}P3=0x7f;P1=0xfe;delay(z);P3=0xff;a=0xfc;P1=a;delay(z);for(l=0;l<6;l++) {a=_crol_(a,1);P1=a;delay(z);}P1=0x7f;delay(z);P1=0xff;delay(z);a=0x7f;P1=a;delay(z);a=a>>1;P1=a;delay(z);for(l1=0;l1<6;l1++) {a=_cror_(a,1);P1=a;delay(z);}P1=0xfe;P3=0x7f;delay(z);P1=0xff;a=0x3f;P3=a;delay(z);for(k1=0;k1<6;k1++) {a=_cror_(a,1);P3=a;delay(z);}P3=0xfe;P2=0x7f;delay(z);P3=0xff;P2=a;delay(z);for(j1=0;j1<6;j1++){a=_cror_(a,1);P2=a;delay(z);}P2=0xfe;P0=0x7f;delay(z);P2=0xff;a=0x3f;P0=a;delay(z);for(i1=0;i1<6;i1++){a=_cror_(a,1);P0=a;delay(z);}P0=0xfe;delay(z);P0=0xff;delay(z);}void lsd3() //lsd3 两个灯流水双程模式2 {uchar a,i,j,k,l,l1,k1,j1,i1;P0=a;delay(z);a=a<<1;P0=a;delay(z);for(i=0;i<6;i++) //_crol_与_cror_混合使用{a=_crol_(a,1);P0=a;delay(z);}P0=0x7f;a=0x7f;P2=a;delay(z);P0=f;a=a>>1;P2=a;delay(z);for(j=0;j<6;j++){a=_cror_(a,1);P2=a;delay(z);}P2=0xfe;a=0xfe;P3=a;delay(z);a=a<<1;P3=a;delay(z);for(k=0;k<6;k++) {a=_crol_(a,1);P3=a;delay(z);}P3=0x7f;a=0x7f;P1=a;delay(z);P3=f;a=a>>1;P1=a;delay(z);for(l=0;l<6;l++) {a=_cror_(a,1);P1=a;delay(z);}P1=0xfe;delay(z);P1=f;delay(z);a=0xfe;P1=a;a=a<<1;P1=a;delay(z);for(l1=0;l1<6;l1++) //l1==6 {a=_crol_(a,1);P1=a;delay(z);}P1=0x7f;a=0x7f;P3=a;delay(z);P1=f;a=a>>1;P3=a;for(k1=0;k1<6;k1++){a=_cror_(a,1);P3=a;delay(z);}P3=0xfe;a=0xfe;P2=a;delay(z);P3=f;a=a<<1;P2=a;for(j1=0;j1<6;j1++) {a=_crol_(a,1);P2=a;delay(z);}P2=0x7f;a=0x7f;P0=a;delay(z);P2=f;a=a>>1;P0=a;delay(z);for(i1=0;i1<6;i1++) {a=_cror_(a,1);P0=a;delay(z);}P0=0xfe;delay(z);P0=f;delay(z);}void lsd4() //lsd4{uchar a,i,j,k,l,l1,k1,j1,i1; a=0xfe;delay(z);for(i=0;i<7;i++){a=a<<1; //单个灯依次点亮所有灯,从上往下P0=a;delay(z);}a=0xfe;P2=a;delay(z);for(j=0;j<7;j++){a=a<<1;P2=a;delay(z);}a=0xfe;P3=a;delay(z);for(k=0;k<7;k++){a=a<<1;P3=a;delay(z);}a=0xfe;P1=a;delafor(l=0;l<7;l++){a=a<<1;P1=a;delay(z);}for(l1=0;l1<8;l1++) //l1==8{P1=table1[l1]; //单个灯依次熄灭所有灯,从下往上delay(z);}for(k1=0;k1<8;k1++){P3=table1[k1];delay(z);}for(j1=0;j1<8;j1++){P2=table1[j1];delay(z);}for(i1=0;i1<8;i1++){P0=table1[i1];delay(z);}}void lsd5() //lsd5{uchar a,i,j,k,l,l1,k1,j1,i1;a=0xfe;P0=a;delay(z);for(i=0;i<7;i++){a=a<<1;P0=a;delay(z);}for(j=0;j<8;j++) //单个灯依次点亮所有灯,从下往上{P2=table2[j];delay(z);}a=0xfe;P3=a;delay(z);for(k=0;k<7;k++){a=a<<1;P3=a;delay(z);}for(l=0;l<8;l++){P1=table2[l];delay(z);}for(l1=0;l1<8;l1++) //单个灯依次熄灭所有灯,从上往下{P1=table3[l1];delay(z);}for(k1=0;k1<8;k1++){P3=table1[k1];delay(z);}for(j1=0;j1<8;j1++) //单个灯依次熄灭所有灯,从上往下{P2=table3[j1];delay(z);}for(i1=0;i1<8;i1++){P0=table1[i1];delay(z);}}void lsd6() //每组为单位同亮同灭从左向右再向左{P0=0x00;delay(z);P0=0xff;P2=0x00;delay(z);P2=0xff;P3=0x00;delay(z);P3=0xff;P1=0x00;delay(z);P3=0x00;P1=0xff;delay(z);P2=0x00;P3=0xff;delay(z);P0=0x00;P2=0xff;delay(z);P0=0xff;delay(z);}void lsd7() //lsd7 全亮全灭{uchar i;for(i=0;i<2;i++){P0=0x00;P1=0x00;P2=0x00;P3=0x00;delay(z);P0=0xff;P1=0xff;P2=0xff;P3=0xff;delay(z);}}void lsd8() //每组仅两个灯,从中间往两边再往中间{uchar i;for(i=0;i<8;i++){P0=table4[i];P1=table4[i];P2=table4[i];P3=table4[i];delay(z);}}void lsd9() //每组两个灯引亮所有灯再引灭,从中间带两边再到中间{uchar i;for(i=0;i<8;i++){P0=table5[i];P1=table5[i];P2=table5[i];P3=table5[i];delay(z);}}void lsd10(){uchar a,i,j;a=0xfe;P0=a;P1=a;P2=a;P3=a;delay(z);for(i=0;i<7;i++) //仅单个灯亮从上往下流{a=_crol_(a,1);P0=a;P1=a;P2=a;P3=a;delay(z);}for(j=0;j<7;j++) //仅单个灯亮从上往下流{a=_cror_(a,1);P0=a;P1=a;P2=a;P3=a;delay(z);}P0=f;P1=f;P2=f;P3=f;delay(z);void lsd11() {uchar a,i,j;a=0xfe;P0=a;P1=a;P2=a;P3=a;delay(z);for(i=0;i<7;i++) {a=a<<1;P0=a;P1=a;P2=a;P3=a;delay(z);}for(j=0;j<8;j++) {P0=table1[j];P1=table1[j];P2=table1[j];P3=table1[j]; delay(z);}}void lsd12()uchar a,i,j,k,l;a=0xfe;P0=a;P1=a;delay(z);for(i=0;i<7;i++) {a=a<<1;P0=a;P1=a;delay(z);}a=0x7f;P2=a;P3=a;delay(z);for(j=0;j<7;j++) {a=a>>1;P2=a;P3=a;delay(z);}for(k=0;k<8;k++){P2=table3[k]; P3=table3[k]; delay(z);for(l=0;l<8;l++) {P0=table1[l];P1=table1[l]; delay(z);}while(1);}。
简单的单片机花样流水灯C语言源代码#include<reg51.h> //51系列单片机定义文件#define uchar unsigned char //定义无符号字符#define uint unsigned int //定义无符号整数void Delayms(uint x){ //定义延时函数uint i,j;for(i=x;i>0;i--)for(j=110;j>0;j--);}void main(){uint i;uchar temp;while(1){temp=0x01; //8个流水灯逐个闪动for(i=0;i<8;i++){P0=~temp;Delayms(50);temp<<=1;}temp=0x80; //8个流水灯反向逐个闪动for(i=0;i<8;i++){P0=~temp;Delayms(50);temp>>=1;}temp=0xfe; //8个流水灯依次全部点亮for(i=0;i<8;i++){P0=temp;Delayms(50);temp<<=1;}temp=0x7f; //8个流水灯依次反向全部点亮for(i=0;i<8;i++){P1=temp;Delayms(50);temp>>=1;}}}//此程序的作者向往未来#include<reg52.h>#include<intrins.h>#define uint unsigned int#define uchar unsigned char uchar z=50,e=0x00,f=0xff; uchar code table1[]={0x80,0xc0,0xe0,0xf0,0xf8,0xfc,0xfe,0xff};uchar code table2[]={0x7f,0x3f,0x1f,0x0f,0x07,0x03,0x01,0x00}; uchar code table3[]={0x01,0x03,0x07,0x0f,0x1f,0x3f,0x7f,0xff};uchar code table4[]={0xe7,0xdb,0xbd,0x7e,0xbd,0xdb,0xe7,0xff};uchar code table5[]={ 0xe7,0xc3,0x81,0x00, 0x81,0xc3,0xe7,0xff}; uchar code table6[]={ 0x7e,0x3c,0x18,0x00, 0x18,0x3c,0x7e,0xff}; void delay(uchar); void lsd1();void lsd2();void lsd3();void lsd4();void lsd5();void lsd6();void lsd7();void lsd8();void lsd9();void lsd10();void lsd11();void lsd12();main(){while(1){lsd1();lsd2();lsd3();lsd4();lsd5();lsd6();lsd7();lsd8();lsd9();lsd10();lsd11();lsd12();}}void delay(uchar x) //延时函数{uint i,j;for(i=x;i>0;i--)for(j=250;j>0;j--);}void lsd1() //lsd1 单个流水灯双程模式1{uchar a,i,j,k,l,l1,k1,j1,i1;a=0xfe;P0=a;delay(z);for(i=0;i<7;i++) //仅单个灯亮从上往下流{a=_crol_(a,1);P0=a;delay(z);}P0=0xff;a=0xfe;P2=a;delay(z);for(j=0;j<7;j++){a=_crol_(a,1);P2=a;delay(z);}P2=0xff;a=0xfe;P3=a;delay(z);for(k=0;k<7;k++) {a=_crol_(a,1);P3=a;delay(z);}P3=0xff;a=0xfe;P1=a;delay(z);for(l=0;l<7;l++) {a=_crol_(a,1);P1=a;delay(z);}a=0xbf;P1=a;delay(z);for(l1=0;l1<6;l1++) //l1==6 仅单个灯亮从下往上流{a=_cror_(a,1);P1=a;delay(z);}P1=0xff;a=0x7f;P3=a;delay(z);for(k1=0;k1<7;k1++){a=_cror_(a,1);P3=a;delay(z);}P3=0xff;a=0x7f;P2=a;delay(z);for(j1=0;j1<7;j1++){a=_cror_(a,1);P2=a;delay(z);}P2=0xff;a=0x7f;P0=a;delay(z);for(i1=0;i1<7;i1++){a=_cror_(a,1);P0=a;delay(z);}P0=0xff;}void lsd2() //lsd2 两个灯流水双程模式1{uchar a,i,j,k,l,l1,k1,j1,i1; a=0xfe;P0=a;delay(z);a=a<<1;P0=a;delay(z);for(i=0;i<6;i++){a=_crol_(a,1);P0=a;delay(z);}P0=0x7f;P2=0xfe;delay(z);P0=0xff;a=0xfc;P2=a;delay(z);for(j=0;j<6;j++) {a=_crol_(a,1);P2=a;delay(z);}P2=0x7f;P3=0xfe;delay(z);P2=0xff;a=0xfc;P3=a;delay(z);for(k=0;k<6;k++) {a=_crol_(a,1);P3=a;delay(z);}P3=0x7f;P1=0xfe;delay(z);P3=0xff;a=0xfc;P1=a;delay(z);for(l=0;l<6;l++) {a=_crol_(a,1);P1=a;delay(z);}P1=0x7f;delay(z);P1=0xff;delay(z);a=0x7f;P1=a;delay(z);a=a>>1;P1=a;delay(z);for(l1=0;l1<6;l1++) {a=_cror_(a,1);P1=a;delay(z);}P1=0xfe;P3=0x7f;delay(z);P1=0xff;a=0x3f;P3=a;delay(z);for(k1=0;k1<6;k1++) {a=_cror_(a,1);P3=a;delay(z);}P3=0xfe;P2=0x7f;delay(z);P3=0xff;a=0x3f;P2=a;delay(z);for(j1=0;j1<6;j1++) {a=_cror_(a,1);P2=a;delay(z);}P2=0xfe;P0=0x7f;delay(z);P2=0xff;a=0x3f;P0=a;delay(z);for(i1=0;i1<6;i1++) {a=_cror_(a,1);P0=a;delay(z);}P0=0xfe;delay(z);P0=0xff;delay(z);}void lsd3() //lsd3 两个灯流水双程模式2{uchar a,i,j,k,l,l1,k1,j1,i1;a=0xfe;P0=a;delay(z);a=a<<1;P0=a;delay(z);for(i=0;i<6;i++) //_crol_与_cror_混合使用{a=_crol_(a,1);P0=a;delay(z);}P0=0x7f;a=0x7f;P2=a;delay(z);P0=f;a=a>>1;P2=a;delay(z);for(j=0;j<6;j++) {a=_cror_(a,1);P2=a;delay(z);}P2=0xfe;a=0xfe;P3=a;delay(z);P2=f;a=a<<1;P3=a;delay(z);for(k=0;k<6;k++) {a=_crol_(a,1);P3=a;delay(z);}P3=0x7f;a=0x7f;P1=a;delay(z);P3=f;a=a>>1;P1=a;delay(z);for(l=0;l<6;l++) {a=_cror_(a,1);P1=a;delay(z);}P1=0xfe;delay(z);P1=f;delay(z);P1=a;delay(z);a=a<<1;P1=a;delay(z);for(l1=0;l1<6;l1++) //l1==6 {a=_crol_(a,1);P1=a;delay(z);}P1=0x7f;a=0x7f;P3=a;delay(z);P1=f;a=a>>1;P3=a;for(k1=0;k1<6;k1++){a=_cror_(a,1);delay(z);}P3=0xfe;a=0xfe;P2=a;delay(z);P3=f;a=a<<1;P2=a;delay(z);for(j1=0;j1<6;j1++) {a=_crol_(a,1);P2=a;delay(z);}P2=0x7f;a=0x7f;P0=a;delay(z);P2=f;a=a>>1;P0=a;delay(z);for(i1=0;i1<6;i1++){a=_cror_(a,1);P0=a;delay(z);}P0=0xfe;delay(z);P0=f;delay(z);}void lsd4() //lsd4 {uchar a,i,j,k,l,l1,k1,j1,i1;a=0xfe;P0=a;delay(z);for(i=0;i<7;i++){a=a<<1; //单个灯依次点亮所有灯,从上往下P0=a;delay(z);}a=0xfe;P2=a;delay(z);for(j=0;j<7;j++){a=a<<1;P2=a;delay(z);}a=0xfe;P3=a;delay(z);for(k=0;k<7;k++){a=a<<1;P3=a;delay(z);}a=0xfe;P1=a;delay(z);for(l=0;l<7;l++){a=a<<1;P1=a;delay(z);}for(l1=0;l1<8;l1++) //l1==8{P1=table1[l1]; //单个灯依次熄灭所有灯,从下往上delay(z);}for(k1=0;k1<8;k1++){P3=table1[k1];delay(z);}for(j1=0;j1<8;j1++){P2=table1[j1];delay(z);}for(i1=0;i1<8;i1++){P0=table1[i1];delay(z);}}void lsd5() //lsd5 {uchar a,i,j,k,l,l1,k1,j1,i1;a=0xfe;P0=a;delay(z);for(i=0;i<7;i++){a=a<<1;P0=a;delay(z);}for(j=0;j<8;j++) //单个灯依次点亮所有灯,从下往上{P2=table2[j];delay(z);}a=0xfe;P3=a;delay(z);for(k=0;k<7;k++){a=a<<1;P3=a;delay(z);}for(l=0;l<8;l++){P1=table2[l];delay(z);}for(l1=0;l1<8;l1++) //单个灯依次熄灭所有灯,从上往下{P1=table3[l1];delay(z);}for(k1=0;k1<8;k1++){P3=table1[k1];delay(z);}for(j1=0;j1<8;j1++) //单个灯依次熄灭所有灯,从上往下{P2=table3[j1];delay(z);}for(i1=0;i1<8;i1++){P0=table1[i1];delay(z);}}void lsd6() //每组为单位同亮同灭从左向右再向左{P0=0x00;delay(z);P0=0xff;P2=0x00;delay(z);P2=0xff;P3=0x00;delay(z);P3=0xff;P1=0x00;delay(z);P3=0x00;P1=0xff;delay(z);P2=0x00;P3=0xff;delay(z);P0=0x00;P2=0xff;delay(z);P0=0xff;delay(z);}void lsd7() //lsd7 全亮全灭{uchar i;for(i=0;i<2;i++){P0=0x00;P1=0x00;P2=0x00;P3=0x00;delay(z);P0=0xff;P1=0xff;P2=0xff;P3=0xff;delay(z);}}void lsd8() //每组仅两个灯,从中间往两边再往中间{uchar i;for(i=0;i<8;i++){P0=table4[i];P1=table4[i];P2=table4[i];P3=table4[i];delay(z);}}void lsd9() //每组两个灯引亮所有灯再引灭,从中间带两边再到中间{uchar i;for(i=0;i<8;i++){P0=table5[i];P1=table5[i];P2=table5[i];P3=table5[i];delay(z);}}void lsd10(){uchar a,i,j;a=0xfe;P0=a;P1=a;P2=a;P3=a;delay(z);for(i=0;i<7;i++) //仅单个灯亮从上往下流{a=_crol_(a,1);P0=a;P1=a;P2=a;P3=a;delay(z);}for(j=0;j<7;j++) //仅单个灯亮从上往下流{a=_cror_(a,1);P0=a;P1=a;P2=a;P3=a;delay(z);}P0=f;P1=f;P2=f;P3=f;delay(z);}void lsd11(){uchar a,i,j;a=0xfe;P0=a;P1=a;P2=a;delay(z);for(i=0;i<7;i++){a=a<<1;P0=a;P1=a;P2=a;P3=a;delay(z);}for(j=0;j<8;j++){P0=table1[j]; P1=table1[j];P2=table1[j];P3=table1[j];delay(z);}void lsd12(){uchar a,i,j,k,l;a=0xfe;P0=a;P1=a;delay(z);for(i=0;i<7;i++) {a=a<<1;P0=a;P1=a;delay(z);}a=0x7f;P2=a;P3=a;delay(z);for(j=0;j<7;j++) {a=a>>1;P2=a;P3=a;delay(z);}for(k=0;k<8;k++) {P2=table3[k];P3=table3[k]; delay(z);}for(l=0;l<8;l++) {P0=table1[l];P1=table1[l]; delay(z);}while(1);}。