花样流水灯C语言源程序
五种编程方式实现流水灯的单片机C程序

五种编程方式实现流水灯的单片机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.中断编程方式:中断编程方式是一种基于中断事件的编程方式,通过在特定的中断事件触发时改变灯光状态来实现流水灯效果。
C51单片机实现呼吸灯和花样流水灯程序

C51单⽚机实现呼吸灯和花样流⽔灯程序1.⽤C51单⽚机实现花样流⽔灯,代码如下:#include <reg52.h>#define LED_A P1 //led灯所⽤的接⼝,是哪个⼝就写P⼏void delayms(){unsigned char x = 0; unsigned char i;unsigned char y = 0;while(y < 0.001) //定义led灯的切换的延时时间,越⼩越快。
{ //(y < 0.001);(x<100);(i<100)都可以修改x = 0;while(x<100){i = 0;while(i<100){i++;}x++;}y++;}}#define LED_NUM 8 //定义led灯的数量,可快速实现不同⽤途的修改;void main(){unsigned char k;unsigned char j;unsigned char LED_ALL[] = {0XFE,0XFD,0XFB,0XF7,0XEF,0XDF,0XBF,0X7F}; //led灯亮灭的⼗六进制数组;unsigned char LED_ALL_2[] = {0XFC,0XF3,0XCF,0X3F}; while(1) { for(k=0;k<3;k++) //第⼀个for实现奇偶灯交叉闪烁三次,想闪烁⼏次就修改(K<3)中的数值; { LED_A = 0xAA; delayms(); LED_A = 0x55; delayms(); } for(k=3;k>0;k--) //实现⼀个流⽔灯左右三次(从左到右再从右到左算⼀次); { for(j=0;j<LED_NUM;j++) { LED_A = LED_ALL[j]; delayms(); } for(j=6;j>0;j--) { LED_A = LED_ALL[j]; delayms(); } } for(k=0;k<3;k++) //实现前四个灯亮后四个灯灭,交叉闪烁3次,修改次数同上; { LED_A = 0xf0; delayms(); LED_A = 0xf; delayms(); } for(k=3;k>0;k--) //实现两个灯依次流⽔3次; { for(j=0;j<4;j++) { LED_A = LED_ALL_2[j]; delayms(); } for(j=2;j>0;j--) { LED_A = LED_ALL_2[j]; delayms(); } } }}2.实现第⼀个灯呼吸,由暗变亮,再由亮变暗,程序如下;#include <reg52.h>sbit LED1 = P3^0;void delay(unsigned int a) //定义⼀个延时函数{ while(--a);}void main(){ unsigned int t,circle=800; //定义变量,circle=800为led灯呼吸的间隔长短,数值越⼩,间隔越短。
花样流水灯程序

}
{
time=0;
miao++;
miao=miao%10;//0-9
display();//数码管显示函数
}
}
void int_T0()//定时器T0初始化
{
EA=1;//打开总中断
ET0=1;//允许T1中断
TMOD=TMOD&0Xf0|0X01;//配置模式为定时模式(C/T=0),16位自动重装
TH0=(65536-500)/256;//50ms
TL0=(65536-500)%256;//从15536开始计数,到65536溢出,一共计50000个脉冲
TR0=1;//允许计数
}
void fuwu_int_T0(void) interrupt 1//定时器0的优先级比定时器1的优先级高
{//它先要执行完定时器0的服务程序,才能再去执行定时器1的服务程序
{
P0=a;
a=a>>1;
a=a|0x01;
delay_ms((num*100)+1);
}
for(i=0;i<2;i++)//8个led闪烁两次
{
P0=0Xff;//led全部消灭
delay_ms((num*100)+1);
P0=0x00;//led灯全部点亮
delay_ms((num*100)+1);
{
if(pwm1==1)
{
pwm1=1;
flag=0;
}
pwm1--;
}
}
if(num1>64)
{
num1=0;
}
if(num1<=pwm1)
多种流水灯C语言程序设计

/*****************************以下程序均采用12M晶振*****************************************************/#include<reg51.h>#define uint unsigned int#define uchar unsigned char#define ulong unsigned longsbit p2=P2^0;void main(){TMOD=0x01;TR0=1;ET0=1;EA=1;while(1){;}}timer0() interrupt 1{TH0=0x00;TL0=0x06;p2=!p2;}定时流水灯//#include<reg51.h>#define uint unsigned int#define uchar unsigned char#define ulong unsigned longbit ldelay=0;void main(){uchar led[9]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f,0xff}; uchar leda;TMOD=0x00;TR0=1;ET0=1;EA=1;while(1)if(ldelay){ldelay=0;P2=led[leda];leda++;if(leda==9)leda=0;}}}timer2() interrupt 1{static uchar t;TF0=0;t++;if(t==10){t=0;ldelay=1;}}变速流水灯//#include<reg51.h>#define uint unsigned int#define uchar unsigned char#define ulong unsigned longbit ldelay=0;uchar speed=100;void main(){uchar led[9]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f,0xff}; uchar leda;TMOD=0x00;TR0=1;ET0=1;EA=1;while(1)if(ldelay){ldelay=0;P2=led[leda];leda++;if(leda==9){leda=0;speed-=10;if(speed==0)speed=100;}}}}timer2() interrupt 1{static uchar t;TF0=0;t++;if(t==speed){t=0;ldelay=1;}}//每2两圈改变一次亮度#include<reg51.h>#define uint unsigned int#define uchar unsigned char#define ulong unsigned longbit ldelay=0;uchar speeda=30;uint k=0;void main(){uchar led[9]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f,0xff}; uchar leda;uchar speed[10]={5,80,70,60,50,40,30,20,10,5}; uint i;uint k;TMOD=0x00;TR0=1;ET0=1;EA=1;while(1){if(ldelay){ldelay=0;P2=led[leda];leda++;if(leda==9){leda=0;k++;if(k==2){k=0;speeda=speed[i];i++;if(i==9)i=0;}}}}}timer2() interrupt 1{static uchar t;TF0=0;t++;if(t==speeda){t=0;ldelay=1;}}//在上面基础上调灯的亮度(调占空比)#include<reg51.h>#define uint unsigned int#define uchar unsigned char#define ulong unsigned longbit ldelay=0;uchar speeda=30;uint k=0;uchar scale;void main(){uchar led[9]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f,0xff}; uchar leda;uchar speed[10]={5,80,70,60,50,40,30,20,10,5};uint i;uint k;TMOD=0x00;TR0=1;ET0=1;EA=1;while(1){if(ldelay==1){ldelay=0;P2=led[leda];leda++;scale++;if(scale==10)scale=0;if(leda==9){leda=0;k++;if(k==2){k=0;speeda=speed[i];i++;if(i==9)i=0;}}}}}timer2() interrupt 1 {static uchar t;TH0=0x1c;TL0=0xbf;TF0=0;t++;if(t==speeda){t=0;ldelay=1;}if(scale==10)P2=0; if(scale==0)P2=1; }。
基于51单片机心形流水灯C语言源程序

#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=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=0x02; 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=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=0xbf; delayms(50); P0=0x7f; delayms(50); P0=0xff;P2=0xfe; 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=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=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=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=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=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);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);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();// 花样闪烁}。
基于单片机心形流水灯C语言源程序精编

基于单片机心形流水灯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();// 花样闪烁}。
单片机流水灯c语言实现

//------------------------------------------------
#include <reg51.h>
#define uchar unsigned char
#define uint unsigned int
uchar code DSY_CODE []=
red_a=1;yellow_a=1;green_a=0;
red_b=0;yellow_b=1;green_b=1;
delayMs(2000);
Operation_Type=2;
break;
case 2://东西向黄灯开始闪烁,绿灯关闭
delayMs(300);
yellow_a=!yellow_a; green_a=1;
while(x--)
{
for(i=0;i<120;i++);
}
}
//---------------------------------------------
//交通灯切换子程序
void Traffic_Light()
{
switch (Operation_Type)
{
case 1://东西向绿灯亮与南北向红灯亮
{
uchar i;
P2=0x01;//P2端口初始化
while(1)//死循环
{
for(i=0;i<7;i++)//i自加1,当i=7时执行第二个for语句
{
P2=_crol_(P2,1);//P2端口循环左移1位
delayMs(150);
}
for(i=0;i<7;i++)
花样流水灯(汇编)

花样流水灯程序(汇编)汇编起始命令统一为:ORG 0000HLJMP MAINORG 0100H延时程序统一为:DELAY: MOV R7, #10 (延时500ms)DL Y2: MOV R6, #200DL Y1: MOV R5, #125DJNZ R5, $DJNZ R6, DL Y1DJNZ R7, DL Y2RET 注:不可少1.普通流水灯MAIN: MOV A, #0FEHLOOP0: MOV R1,#10HLOOP1: MOV P1,ALCALL DELAYRL ADJNZ R1,LOOP1LCALL DELAY 注:每循环一次延时长一些,以便观察LCALL DELAYSJMP LOOP02.L1亮到L8,L8灭到L1MAIN: MOV A, #0FEH LOOP0:MOV R1, #15LOOP1: MOV P1, ALCALL DELAYRL ADEC R1CJNE R1, #07H,LOOP1RR ALOOP2: RR AMOV P1, ALCALL DELAYDJNZ R1, LOOP2LCALL DELAYLCALL DELAYSJMP LOOP0 MAIN: MOV A, #0FEH LOOP0: MOV R1, #08MOV R2, #08 LOOP1: MOV P1, ALCALL DELAYRL ADEC R1DJNZ R1, LOOP1RR ALOOP2: RR AMOV P1, ALCALL DELAYDJNZ R2, LOOP2LCALL DELAYLCALL DELAYSJMP LOOP03.L1到L8逐个亮灭MAIN: MOV A, #0FEHLOOP0: MOV R1, #0FEHMOV R2, #15LOOP1: MOV P1, ALCALL DELAYRL AANL A, R1DEC R2CJNE R2, #07H,LOOP1 注:以上是让灯从1亮到8MOV R1, #80HLOOP2: ORL A, R1MOV P1, ALCALL DELAYRR ADJNZ R2, LOOP2RL ALCALL DELAYLCALL DELAYSJMP LOOP04.13572468亮MAIN: MOV A,#0FEHLOOP0: MOV R1,#08HLOOP1: MOV P1, ALCALL DELAYRL ARL ADEC R1CJNE R1,#04H ,LOOP1MOV A, #0FDH LOOP2: MOV P1, ALCALL DELAYRL ARL ADJNZ R1, LOOP2RR ASJMP LOOP0 5.逐个闪烁MAIN: MOV A, #0FEH LOOP0: MOV R1,#10H LOOP1: MOV P1,ALCALL DELAYMOV P1, #0FFHLCALL DELAYRL ADJNZ R1,LOOP1LCALL DELAYLCALL DELAYSJMP LOOP06.流水灯四个四个地亮花样IMAIN: MOV A, #0F0H LOOP0: MOV R1, #09H LOOP1: MOV P1, ALCALL DELAYRL A花样IIMAIN: MOV A, #0F0HLOOP0: MOV R1, #040H LOOP1: MOV P1, ALCALL DELAYSWAP ADJNZ R1, LOOP1LCALL DELAYDEC R1CJNE R1,#05H,LOOP1 LOOP2: MOV P1, ALCALL DELAYRR ADJNZ R1,LOOP2RL A LCALL DELAY LCALL DELAY SJMP LOOP0 LCALL DELAY SJMP LOOP07.全亮全灭MAIN:LOOP0: MOV R1, #04H LOOP1: MOV P1, #00HLCALL DELAYMOV P1, #0FFHLCALL DELAYDJNZ R1, LOOP1LCALL DELAYLCALL DELAYSJMP LOOP08.两个两个地亮MAIN: MOV A, #0FCH LOOP0: MOV R1, #08H LOOP1: MOV P1, ALCALL DELAYRL ARL ADEC R1CJNE R1,#04H,LOOP1RR ARR ALOOP2: MOV P1, ALCALL DELAYRR ARR ADJNZ R1, LOOP2RL ARL ALCALL DELAYLCALL DELAYSJMP LOOP0 9.穿越MAIN: MOV R2,#0FEHMOV R3, #7FH LOOP0: MOV R1,#016 LOOP1: MOV A,R2ANL A,R3MOV P1, ALCALL DELAYMOV A,R2RL AMOV R2, AMOV A,R3RR AMOV R3, ADJNZ R1,LOOP1LCALL DELAYSJMP LOOP0 LCALL DELAY。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
//文件名称:flash_led.c
文件说明:流水灯C程序
编写人员:Jerry Yang
编写日期:2006年10月5日
程序说明:MCU采用AT89S51,外接12M晶振,P1口输出// #include<reg51.h> //51系列单片机定义文件
#define uchar unsigned char //定义无符号字符
#define uint unsigned int //定义无符号整数
void delay(uint); //声明延时函数
void main(void)
{
uint i;
uchar temp;
while(1)
{
temp=0x01;
for(i=0;i<8;i++) //8个流水灯逐个闪动
{
P1=~temp;
delay(100); //调用延时函数
temp<<=1;
}
temp=0x80;
for(i=0;i<8;i++) //8个流水灯反向逐个闪动
{
P1=~temp;
delay(100); //调用延时函数
temp>>=1;
}
temp=0xFE;
for(i=0;i<8;i++) //8个流水灯依次全部点亮
{
P1=temp;
delay(100); //调用延时函数
temp<<=1;
}
temp=0x7F;
for(i=0;i<8;i++) //8个流水灯依次反向全部点亮
{
P1=temp;
delay(100); //调用延时函数
temp>>=1;
}
}
}
void delay(uint t) //定义延时函数{
register uint bt;
for(;t;t--)
for(bt=0;bt<255;bt++);。