51单片机流水灯C语言源程序
51单片机流水灯左移 右移控制程序
51 单片机流水灯左移右移控制程序电路很简单8 位流水灯接在单片机的P1 口.如下是源代码://============================================================= //程序名:LLL22_4.C //程序功能:流水灯控制左移右移//============================================================== #include//头文件L_M(); //流水灯左移控制程序R_M();//流水灯右移控制程序//=============================================================== main() //主函数{ while(1) //无限循环{L_M(); //调用左移程序R_M(); //调用右移程序} } //========左移控制程序============================================== L_M() //左移主函数{ unsigned char i,temp,a,b; //声明无符号字符型变量A,TEMP I,B unsigned int s; //声明无符号整型变量S temp=0xfe; //左移初始值P1=temp; //P1 输出信号点亮发光管for( s=0;s>(8-i) ; //数据右移(8-I)位P1=a|b ; //逻辑或运算,并输出到P1 口for(s=0;s>i;b=temp<<(8-i); //数据右移(8-I)位P1=a|b; //逻辑或运算,并输出到P1 口for(s=0;s<30000;s++); //延时程序} }//=======================================================================tips:感谢大家的阅读,本文由我司收集整编。
多种方法写出的51单片机流水灯C语言程序
目录流水灯最原始 (1)流水灯位左移 (3)流水灯移位函数 (4)流水灯数组 (5)流水灯精确定时器 (6)流水灯最原始#include "reg51.h"sbit p0=P1^0;sbit p1=P1^1;sbit p2=P1^2;sbit p3=P1^3;sbit p4=P1^4;sbit p5=P1^5;sbit p6=P1^6;sbit p7=P1^7;voidmdelay(unsigned int t){unsigned char n;for(;t>0;t--)for(n=0;n<125;n++){;}}void main(){while(1){P1=0;p0=1;mdelay(1000);p0=0;p1=1;mdelay(1000);p1=0;p2=1;mdelay(1000);p2=0;p3=1;mdelay(1000);p3=0;p4=1;mdelay(1000);p4=0;p5=1;mdelay(1000);p5=0;p6=1;mdelay(1000);p6=0;p7=1;mdelay(1000);p7=0;}}流水灯位左移#include "reg51.h"voidmdelay(unsigned int t){unsigned char n;for(;t>0;t--)for(n=0;n<125;n++){;}}void main(){unsigned char i;unsignedint led;while(1){led=0xfe;for(i=0;i<8;i++){P1=led;mdelay(1000);led=led<<1;led=led|0x01;}}}流水灯移位函数#include "reg51.h"#include "intrins.h"voidmdelay(unsigned int t){unsigned char n;for(;t>0;t--)for(n=0;n<125;n++){;}}void main(){unsigned char led;led=0x01;while(1){P1=led;led=_crol_(led,1);mdelay(1000);}}流水灯数组#include "reg51.h"unsigned char table[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80}; voidmdelay(unsigned int t){unsigned char n;for(;t>0;t--)for(n=0;n<125;n++){;}}void main(){unsigned char i;while(1){for(i=0;i<8;i++){P1=table[i];mdelay(1000);}}}流水灯精确定时器#include "reg51.h"#include "intrins.h"unsignedint count=0,led;void main(){P1=0x00;led=0x01;EA=1;ET0=1;TMOD=0x01;TH0=0x3c;TL0=0xb0;TR0=1;for(;;){;}}voidbiao() interrupt 1{count++;if(count==20){count=0;P1=led;led=_crol_(led,1);}TH0=0x3c;TL0=0xb0;}。
51单片机爱心流水灯原理及制作
电路原理图:原件清单:1、51单片机x1、40Pin 座x12、LED x32(建议用5mm 七彩的)3、电阻470Ωx334、晶振12MHz x15、10uf 电解电容x1、谐振瓷片电容30pf x26·其他的可以看自己的爱好去加7、其实也可以不用那么多的电阻,用几个排阻就OK了。
~效果展示:作品程序:#include<>#define uchar unsigned char;uchar flag=200;///////////////////////////////////////////////////////////////////////uchar code Tab1[]={0xFE,0xFD,0xFB,0xF7,0xEF,0xDF,0xBF,0x7F,0xFF};//暗中左移向下uchar code Tab2[]={0x7F,0xBF,0xDF,0xEF,0xF7,0xFB,0xFD,0xFE,0xFF};//暗中右移向上uchar code Tab3[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80,0x00};//亮中左移向下uchar code Tab4[]={0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01,0x00};//亮中右移向上uchar code Tab11[]={0xFE,0xFC,0xF8,0xF0,0xE0,0xC0,0x80,0x00,0xff};//暗中左移向下uchar code Tab22[]={0x7F,0x3F,0x1F,0x0F,0x07,0x03,0x01,0x00,0xff};////////////////////////////////////////////////////////////////////uchar code Tab33[]={0x80,0xC0,0xE0,0xF0,0xF8,0xFC,0xFE,0xFF};;uchar code Tab44[]={0x01,0x03,0x07,0x0F,0x1F,0x3F,0x7F,0xFF};uchar code Tab55[]={0x08,0xc0,0xe0,0xf0,0xf8,0xfc,0xfe,0xff,0xff};uchar code Tab5[]={0x00,0x80,0xC0,0xE0,0xF0,0xF8,0xFC,0xFE,0xff};uchar code Tab6[]={0x00,0x01,0x03,0x07,0x0F,0x1F,0x3F,0x7F,0xff}; uchar code Tab7[]={0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe}; uchar code Tab8[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};////////////////////////////////////////////////////////////////void shansuo();void xl();///////////////////////////////////////////////////////////////}void delay(void){unsigned char m,n;for(m=0;m<flag;m++)for(n=0;n<250;n++);}///////////////////////////////////void hy1(void) //点亮状态逆时针旋转90度(一个一个灭){.unsigned char i;for(i=0;i<8;i++){P0=Tab11[i];P1=Tab22[i];P2=Tab11[i];P3=Tab22[i];delay();}for(i=0;i<8;i++){P0=Tab44[i];P1=Tab55[i];!P2=Tab44[i];P3=Tab55[i];delay();}}///////////////////////////////////////////void hy2(void) //暗中逆时针转360。
多种方法写出的51单片机流水灯C语言程序
多种方法写出的51单片机流水灯C语言程序流水灯最原始下面是一个最基本的流水灯程序,使用了51单片机的寄存器编程方法。
include "reg51.h"sbit p0=P1^0;sbit p1=P1^1;sbit p2=P1^2;sbit p3=P1^3;sbit p4=P1^4;sbit p5=P1^5;sbit p6=P1^6;sbit p7=P1^7;void mdelay(unsigned int t){unsigned char n;for(;t>0;t--)for(n=0;n<125;n++)void main()while(1)P1=0;p0=1;mdelay(1000);p0=0;p1=1;mdelay(1000);p1=0;p2=1;流水灯位左移在流水灯的基础上,我们可以通过左移位的方式来实现流水灯的效果。
void main()unsigned char led = 0x01.// 初始化led为xxxxxxxxwhile(1)P1 = ~led。
// 取反输出,因为我们的电路是低电平亮灯mdelay(1000);led <<= 1.// 左移一位if(led == 0) // 如果led变为0了,说明已经左移到最后一位了,需要重新开始led = 0x01;流水灯移位函数为了方便实现流水灯的位移,我们可以封装一个移位函数。
void shift_left(unsigned char *led。
unsigned char n)for(unsigned char i=0.i<n。
i++)led <<= 1;if(*led == 0)led = 0x01;void main()unsigned char led = 0x01;while(1)P1 = ~led;mdelay(1000);shift_left(&led。
51单片机C语言程序设计源代码
新概念51单片机C语言教程----入门、提高、开发、拓展全攻略郭天祥编著电子工业出版社例2.2.1编写程序,点亮第一个发光二极管(part2_1.c P27)#include <reg52.h> //52系列单片机头文件sbit led1=P1^0; //声明单片机P1口的第一位void main() //主函数{led1=0; /*点亮第一个发光二极管*/}例2.2.2编写程序,点亮P1口的若干二极管(part2_2.c P39)#include <reg52.h> //52系列单片机头文件void main() //主函数{P1=0xaa;//while(1);}例2.5.1利用for语句延时特性,编写第一个发光二极管以间隔1S亮灭闪动的程序(part2_3.c P42)#include <reg52.h> //52系列单片机头文件#define uint unsigned int //宏定义sbit led1=P1^0; //声明单片机P1口的第一位uint i,j;void main() //主函数{while(1) //大循环{led1=0; /*点亮第一个发光二极管*/for(i=1000;i>0;i--) //延时for(j=110;j>0;j--);led1=1; /*关闭第一个发光二极管*/for(i=1000;i>0;i--) //延时for(j=110;j>0;j--);}}- 2 - 例2.6.1编写程序使第一个发光二极管以间隔500ms亮灭闪动。
(part2_4.c P48)#include <reg52.h> //52系列单片机头文件#define uint unsigned int //宏定义sbit led1=P1^0; //声明单片机P1口的第一位void delay1s(); //声明子函数void main() //主函数{while(1) //大循环{led1=0; /*点亮第一个发光二极管*/delay1s(); //调用延时子函数led1=1; /*关闭第一个发光二极管*/delay1s(); //调用延时子函数}}void delay1s() //子函数体{uint i,j;for(i=500;i>0;i--)for(j=110;j>0;j--);}例2.7.1编写程序使第一个二极管以亮200ms、灭800ms的方式闪动。
51单片机音乐流水灯程序
#include <reg52."h>sbitspeaker = P1^6;sbitkey1=P1^5;sbitkey2=P1^7;unsigned char kk=0;unsigned long t3;unsigned char timer0h, timer0l, time,n;codecharled[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f,0x00,0x55,0xaa,0x00,0x5 5,0xaa,0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe,0x7e,0x3c,0x18,0x00,0x18,0x3c,0x7e,0x00,0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x 7f,0x00,0x55,0xaa,0x00,0x55,0xaa,0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe,0x7e,0x3c,0x18,0x00,0x18,0x3c,0x7e,0x00,0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x 7f,0x00,0x55,0xaa,0x00,0x55,0xaa,0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe,0x7e,0x3c,0x18,0x00,0x18,0x3c,0x7e,0x00,0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x 7f,0x00,0x55,0xaa,0x00,0x55,0xaa,0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe,0x7e,0x3c,0x18,0x00,0x18,0x3c,0x7e,0x00,0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x 7f,0x00,0x55,0xaa,0x00,0x55,0xaa,0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe,0x7e,0x3c,0x18,0x00,0x18,0x3c,0x7e,0x00}; //灯亮数组//--------------------------------------//单片机晶振采用11."0592MHz//频率-半周期数据表高八位本软件共保存了四个八度的28个频率数据code unsigned char FREQH[] = {0xF2, 0xF3, 0xF5, 0xF5, 0xF6, 0xF7, 0xF8,//低音0xF9, 0xF9, 0xFA, 0xFA, 0xFB, 0xFB, 0xFC, 0xFC,//1,2,3,4,5,6,7,i0xFC, 0xFD, 0xFD, 0xFD, 0xFD, 0xFE,//高音2345670xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFF};//超高音//频率-半周期数据表低八位code unsigned char FREQL[] = {0x42, 0xC1, 0x17, 0xB6, 0xD0, 0xD1, 0xB6,//低音0x21, 0xE1, 0x8C, 0xD8, 0x68, 0xE9, 0x5B, 0x8F, //1,2,3,4,5,6,7,i0xEE, 0x44, 0x6B, 0xB4, 0xF4, 0x2D,//高音2345670x47, 0x77, 0xA2, 0xB6, 0xDA, 0xFA, 0x16};//超高音//--------------------------------------//世上只有妈妈好数据表要想演奏不同的乐曲,只需要修改这个数据表codeint shengrikuaile[] = {5,1,1, 5,1,1, 6,1,2, 5,1,2, 1,2,2, 7,1,4,//生日快乐5,1,1, 5,1,1, 6,1,2, 5,1,2, 2,2,2, 1,2,4,5,1,1, 5,1,1, 5,2,2, 3,2,2, 1,2,2, 7,1,2, 6,1,2,4,2,1, 4,2,1, 3,2,2, 1,2,2, 2,2,2, 1,2,4,0,0,0};code int tonghua[]={5,1,2, 5,2,2, 4,2,2,3,2,4, 3,2,2, 4,2,1, 3,2,1, 3,2,4, 3,2,2, 4,2,2,3,2,2, 4,2,2, 3,2,2, 2,2,1, 1,2,1, 1,2,2, 1,2,2, 3,2,2, 5,2,2,6,2,4, 6,2,2, 6,2,1, 5,2,1, 5,2,2, 2,2,2, 2,2,2, 4,2,2,3,2,8, 1,2,2, 3,2,2, 5,2,2,6,2,4, 6,2,2, 6,2,1, 5,2,1, 2,2,2, 2,2,2, 4,2,2,3,2,2, 4,2,2, 3,2,2, 2,2,1, 1,2,1, 1,2,4, 2,2,2, 3,2,2,6,1,4, 6,1,2, 1,2,2, 1,2,2, 7,1,2, 7,1,2, 1,2,1, 1,2,16,0,0,0};//童话//一个音符有三个数字。
简单的51单片机花样流水灯C语言源代码
简单的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);}。
[设计]80C51控制流水灯
80C51单片机控制流水灯1使用汇编语言编写程序,8个LED灯接在P0口,显示出流水灯效果2用mov指令移位3用查表法显示流水灯4将P1口拨码开关设置状态读出,作为流水灯设置显示模式5在4的基础上加入控制功能,K0为低电平时,LED停止移动,K0高电平时,LED开始移动;K1为低电平时,LED左移,K1高电平时,LED 右移。
接线如下图:(一)用mov指令编写ORG 0000H ;定义程序初始地址JMP MAIN ;跳转至MAINORG 0030HMAIN: MOV A,#0FEH ;MOV R0,#08H ;设置移动8位LOOP1:MOV P0,A ;点亮第一个灯CALL DELAY ;调用延时程序RL A ;左移DJNZ R0,LOOP1 ;循环左移MOV R1,#08H ;设置移动8位LOOP2:RR A ;右移MOV P0,A ;输出CALL DELAY ;调用延时程序DJNZ R1,LOOP2 ;循环右移JMP MAIN ;跳转至MAIN DELAY:MOV R3,#20 ;延时程序定义D1: MOV R4,#100D2: MOV R5,#230DJNZ R5,$DJNZ R4,D2DJNZ R3,D1RETEND(二)用查表法ORG 0000H ; 定义程序地址从0000H开始存放JMP MAIN ;跳转至主程序ORG 0030H ;设置主程序地址MAIN:MOV DPTR,#TABLE ;将TABLE的地址赋给DPTRONE: CLR A;累加器清零MOVC A,@A+DPTR ;取出表中的数据CJNE A,#0FFH,TWO ;判断是不是表中最后的0FFH,如果不是则跳转至TWOJMP MAIN ;如果是表中最后的0FFH,则跳转至main,重新运行TWO: MOV P1,A ;将数据输出至p1口CALL DELAY ;调用延时程序INC DPTR ;修改指针DPTR,指向表中下一个数据JMP ONE ;跳转至oneDELAY:MOV R3,#20 ; 延时子程序定义D1: MOV R4,#100D2: MOV R5,#230DJNZ R5,$DJNZ R4,D2DJNZ R3,D1RETTABLE: ; 花样流水灯的表,可以自由定义DB 01111111BDB 10111111BDB 11011111BDB 11101111BDB 11110111BDB 11111011BDB 11111101BDB 11111110BDB 11111110BDB 11111101BDB 11111011BDB 11110111BDB 11101111BDB 11011111BDB 01111111B DB 11100111B DB 11011011B DB 10111101B DB 01111110B DB 10111101B DB 11011011B DB 11100111B DB 11100111B DB 11011011B DB 10111101B DB 01111110B DB 10111101B DB 11011011B DB 11100111B DB 01010101B DB 10101010B DB 00110011B DB 11001100B DB 11110000B DB 00001111BDB 0FFH ;用作表的结尾的标志,可自由定义END(三)拨码开关控制ORG OOOOH ;JMP MAINORG 0030H ;MAIN:MOV P1,#0FFH ;准双向口,先写1再读,防止读取出错MOV A,P1 ;读取P1的状态MOV P0,A ;将p1的状态输出到P0CALL DELA Y ;调用延时子程序JMP MAIN ;跳转到MAINDELAY:MOV R3,#20 ;延时子程序定义D1: MOV R4,#100D2: MOV R5,#230DJNZ R5,$DJNZ R4,D2DJNZ R3,D1RETEND(四)在(三)的基础上加上控制功能ORG 0000HJMP MAINORG 0030HMAIN: MOV P1,#0FFH ;准双向口,先写1再读,防止出错MOV A,P1 ;读取P1的状态MOV R0,A ; 保存A的状态TEST: JB P2.0,MOVING ;判断P2.0的电平,高电平时跳转至MOVINGJMP DISPLA Y ;跳转到DISPLA YMOVING:JB P2.1,RIGHT ;判断P2.1的状态,高电平时右移LEFT: RL A ;A左移一位JMP DISPLA Y ;跳转至DISPLA YRIGHT:RR A ;A右移一位JMP DISPLA Y ;跳转至DISPLA YDISPLA Y:MOV P0,A ;输出到P0口MOV R1,A ;保存A的状态CALL DELAY ;调用延时程序MOV A,R0 ;将R0保存的状态存到A中XRL A,P1 ;判断P1口的状态是否变化JNZ MAIN ;P1口状态变化,跳转至MAIN重新显示MOV A,R1 ;P1口状态不变,继续当前的状态显示JMP TEST ;跳转至TESTDELAY:MOV R3,#20 ; 延时程序定义D1: MOV R4,#100D2: MOV R5,#200DJNZ R5,$DJNZ R4,D2DJNZ R3,D1RET。
单片机流水灯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++)
基于51单片机的C语言程序设计
基于51单片机的C语言程序设计实训100例第 01 篇基础程序设计01 闪烁的LED/* 名称:闪烁的LED说明:LED按设定的时间间隔闪烁*/#include<reg51.h>#define uchar unsigned char#define uint unsigned intsbit LED=P1^0;//延时void DelayMS(uint x){uchar i;while(x--){for(i=0;i<120;i++);}}//主程序void main(){while(1){LED=~LED;DelayMS(150);}}02 从左到右的流水灯/* 名称:从左到右的流水灯说明:接在P0口的8个LED从左到右循环依次点亮,产生走马灯效果*/#include<reg51.h>#include<intrins.h>#define uchar unsigned char#define uint unsigned intvoid DelayMS(uint x){uchar i;while(x--){for(i=0;i<120;i++);}}//主程序void main(){P0=0xfe;while(1){P0=_crol_(P0,1); //P0的值向左循环移动DelayMS(150);}}03 8只LED左右来回点亮/* 名称:8只LED左右来回点亮说明:程序利用循环移位函数_crol_和_cror_形成来回滚动的效果*/#include<reg51.h>#include<intrins.h>#define uchar unsigned char#define uint unsigned int//延时void DelayMS(uint x){uchar i;while(x--){for(i=0;i<120;i++);}}//主程序void main(){uchar i;P2=0x01;while(1){{P2=_crol_(P2,1); //P2的值向左循环移动DelayMS(150);}for(i=0;i<7;i++){P2=_cror_(P2,1); //P2的值向右循环移动DelayMS(150);}}}04 花样流水灯/* 名称:花样流水灯说明:16只LED分两组按预设的多种花样变换显示*/#include<reg51.h>#define uchar unsignedchar#define uint unsigned intuchar code Pattern_P0[]={0xfc,0xf9,0xf3,0xe7,0xcf,0x9f,0x3f,0x7f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, 0xe7,0xdb,0xbd,0x7e,0xbd,0xdb,0xe7,0xff,0xe7,0xc3,0x81,0x00,0x81,0xc3,0xe7,0xff, 0xaa,0x55,0x18,0xff,0xf0,0x0f,0x00,0xff,0xf8,0xf1,0xe3,0xc7,0x8f,0x1f,0x3f,0x7f, 0x7f,0x3f,0x1f,0x8f,0xc7,0xe3,0xf1,0xf8,0xff,0x00,0x00,0xff,0xff,0x0f,0xf0,0xff, 0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe, 0xfe,0xfc,0xf8,0xf0,0xe0,0xc0,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xc0,0xe0,0xf0,0xf8,0xfc,0xfe, 0x00,0xff,0x00,0xff,0x00,0xff,0x00,0xff};uchar code Pattern_P2[]={0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe,0xfc,0xf9,0xf3,0xe7,0xcf,0x9f,0x3f,0xff, 0xe7,0xdb,0xbd,0x7e,0xbd,0xdb,0xe7,0xff,0xe7,0xc3,0x81,0x00,0x81,0xc3,0xe7,0xff, 0xaa,0x55,0x18,0xff,0xf0,0x0f,0x00,0xff,0xf8,0xf1,0xe3,0xc7,0x8f,0x1f,0x3f,0x7f, 0x7f,0x3f,0x1f,0x8f,0xc7,0xe3,0xf1,0xf8,0xff,0x00,0x00,0xff,0xff,0x0f,0xf0,0xff, 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f, 0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe,0xfc,0xf8,0xf0,0xe0,0xc0,0x80,0x00, 0x00,0x80,0xc0,0xe0,0xf0,0xf8,0xfc,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, 0x00,0xff,0x00,0xff,0x00,0xff,0x00,0xff//延时void DelayMS(uint x){uchar i;while(x--){for(i=0;i<120;i++);}}//主程序void main(){uchar i;while(1){ //从数组中读取数据送至P0和P2口显示for(i=0;i<136;i++){P0=Pattern_P0[i];P2=Pattern_P2[i];DelayMS(100);}}}05 LED模拟交通灯/* 名称:LED模拟交通灯说明:东西向绿灯亮若干秒,黄灯闪烁5次后红灯亮,红灯亮后,南北向由红灯变为绿灯,若干秒后南北向黄灯闪烁5此后变红灯,东西向变绿灯,如此重复。
51单片机爱心流水灯原理及制作
电路原理图:原件清单:1、51单片机x1、40Pin 座x12、LED x32(建议用5mm 七彩的)3、电阻470Ωx334、晶振12MHz x15、10uf 电解电容x1、谐振瓷片电容30pf x26·其他的可以看自己的爱好去加7、其实也可以不用那么多的电阻,用几个排阻就OK了。
效果展示:作品程序:#include<reg52.h>#define uchar unsigned charuchar flag=200;///////////////////////////////////////////////////////////////////////uchar code Tab1[]={0xFE,0xFD,0xFB,0xF7,0xEF,0xDF,0xBF,0x7F,0xFF};//暗中左移向下uchar code Tab2[]={0x7F,0xBF,0xDF,0xEF,0xF7,0xFB,0xFD,0xFE,0xFF};//暗中右移向上uchar code Tab3[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80,0x00};//亮中左移向下uchar code Tab4[]={0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01,0x00};//亮中右移向上uchar code Tab11[]={0xFE,0xFC,0xF8,0xF0,0xE0,0xC0,0x80,0x00,0xff};//暗中左移向下uchar code Tab22[]={0x7F,0x3F,0x1F,0x0F,0x07,0x03,0x01,0x00,0xff};////////////////////////////////////////////////////////////////////uchar code Tab33[]={0x80,0xC0,0xE0,0xF0,0xF8,0xFC,0xFE,0xFF};uchar code Tab44[]={0x01,0x03,0x07,0x0F,0x1F,0x3F,0x7F,0xFF};uchar code Tab55[]={0x08,0xc0,0xe0,0xf0,0xf8,0xfc,0xfe,0xff,0xff};uchar code Tab5[]={0x00,0x80,0xC0,0xE0,0xF0,0xF8,0xFC,0xFE,0xff};uchar code Tab6[]={0x00,0x01,0x03,0x07,0x0F,0x1F,0x3F,0x7F,0xff};uchar code Tab7[]={0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe};uchar code Tab8[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f}; ////////////////////////////////////////////////////////////////void shansuo();void xl();///////////////////////////////////////////////////////////////void delay(void){unsigned char m,n;for(m=0;m<flag;m++)for(n=0;n<250;n++);}///////////////////////////////////void hy1(void) //点亮状态逆时针旋转90度(一个一个灭){unsigned char i;for(i=0;i<8;i++){P0=Tab11[i];P1=Tab22[i];P2=Tab11[i];P3=Tab22[i];delay();}for(i=0;i<8;i++){P0=Tab44[i];P1=Tab55[i];P2=Tab44[i];P3=Tab55[i];delay();}}///////////////////////////////////////////void hy2(void) //暗中逆时针转360。
单片机应用-流水灯设计
一、任务导入
任务要求
使用单片机控制8个发光二极管按顺序点亮 。 将8个发光二极管连接到51单片机的P1口,用C语言编程来控制 单片机的并行I/O端口,显示流水灯效果。
硬件电路
向P1口写入一个8位二进 制数来改变每个引脚的输出电 平状态,从而控制8个发光二 极管的亮灭。
二、程序设计
#include <intrins.h>
//包含内部函数库,提供移位和延时操作函数
void main()
//主函数
{
P1 = 0x7F;
//P1端口输出0x7F
while(1)
//无限循环
{
//循环体语句组开始
P1 = _cror_(P1,1); //调用内部函数_cror_(),将P1的二进制数值循环右移
将P1端口在: 11111110 、 11111101 、 11111011 、 11110111 、 11101111 、 11011111 、 10111111 、 01111111
这8种状态之间顺序转换, 就可以实现左移流水灯。
0xFE 左
程序设计思路
P1 = _crol_(P1,1);
举一反三2
使用移位运算符和循环程序结构编程,实现流水灯。
左移位运算符: <<
若 a: 11111110
执行:a<<1; //左移一位
高位移出
1 11111100 低位补0
右移位运算符: <<
若 b: 01111111
执行:b>>1; //右移一位
高位补0
00111111 1 低位移出
程序设计思路
a=0x80;