花样流水灯C语言源程序
- 格式:pdf
- 大小:42.54 KB
- 文档页数:2
//文件名称: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++);。
五种编程方式实现流水灯的单片机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.中断编程方式:中断编程方式是一种基于中断事件的编程方式,通过在特定的中断事件触发时改变灯光状态来实现流水灯效果。
多种方法写出的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。
#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语言源程序精编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();// 花样闪烁}。
七彩流水装饰灯程序设计#include <reg51.h>void delay_ms(unsigned short ms){unsigned short i;unsigned char j;for(i=0;i<ms;i++){for(j=0;j<200;j++);for(j=0;j<102;j++);}} //控制灯的时延子程序void main(void){unsigned char led_contrl; //指示灯控制字节unsigned char i,j; //循环变量unsigned char contrl1,contrl2; //指示灯移位变量while(1){for(j=0;j<3;j++) //模式一:按次序闪烁灯{P2=0xff; //熄灭所有指示灯led_contrl=0x01; //初始化指示灯控制字节delay_ms(300); //延时300MSfor(i=0;i<8;i++){P2=~led_contrl; //点亮控制字节相应指示灯delay_ms(250);if(led_contrl<0x80) led_contrl<<=1; //控制字节移位操作else led_contrl=0x01;}}for(j=0;j<3;j++) //模式二:按次序闪烁灯(一,二流动的方向不一样){P2=0xff; //熄灭所有指示灯led_contrl=0x80; //初始化指示灯控制字节delay_ms(300); //延时300MSfor(i=0;i<8;i++){P2=~led_contrl; //点亮控制字节相应指示灯delay_ms(250);if(led_contrl>0x01) led_contrl>>=1; //控制字节移位操作else led_contrl=0x80;}}for(j=0;j<3;j++) //模式三:依次点亮所有指示灯{P2=0xff; //熄灭所有指示灯led_contrl=0xff; //初始化指示灯控制字节contrl1=0xfe; //初始化移位变量1delay_ms(300);for(i=0;i<8;i++){P2=led_contrl&contrl1; //点亮控制字节相应的指示灯delay_ms(250);contrl1<<=1; //移位变量左移1位}}for(j=0;j<3;j++) //模式四:依次点亮所有指示灯(三,四流动的方向不一样){P2=0xff; //熄灭所有指示灯led_contrl=0xff; //初始化指示灯控制字节contrl1=0x7f; //初始化移位变量1delay_ms(300);for(i=0;i<8;i++){P2=led_contrl&contrl1; //点亮控制字节相应的指示灯delay_ms(250);contrl1>>=1; //移位变量右移1位}}for(j=0;j<3;j++) //模式五:交叉闪烁指示灯{P2=0xff; //熄灭所有指示灯contrl1=0x02; //初始化移位变量1contrl2=0x80; //初始化移位变量2delay_ms(300);for(i=0;i<8;i++){led_contrl=contrl1|contrl2;P2=~led_contrl; //点亮控制字节相应指示灯delay_ms(250);contrl1<<=1; //移位变量1左移1位contrl2>>=1; //移位变量2右移1位}}}。
//C编译LED流水灯实验程序/*包含头文件*/#include<reg51.h>/* 宏定义*/#define uchar unsigned char#define uint unsigned int/* 延时子程序*/void delay(uint N){while(N!=0) N--;}/*主函数开始*/void main(){uchar a,b;a=0x01; //对变量a赋初始值为1while(1) //进入循环{if(a|0x00) //判断a的值是否为0{b=0x0ff^a; //对a的值与ff异或后赋值给变量bP2=b; //输出b变量的值,点亮相应的LEDa=a<<1; //对a进行左移一位运算delay(50000); //延时,使LED走马效果让人眼可以捕捉到}else //若a的值为0,说明8个LED均依次被点亮{P2=0x0ff; //关闭所有LED等待下次循环a=0x01; //重载a变量的值delay(50000);}}}/*********************************************************C语言编写:串行通信实验,用来测试学习板上的串口是否可用;在测试的时候用线将DB9串行接口RS232中的2脚和3脚短接;用短路卡将J41短接;运行程序后,可以观察LED显示数值的变化,验证程序的正确性**********************************************************/#include <reg51.h>#define uchar unsigned char#define uint unsigned intuchar code tab[16]={0x0fe,0x0fd,0x0fb,0x0f7,0x0ef,0x0df,0x0bf,0x7F}; //LED流水灯字符表void delay(uint v) //通用延时子程序{for(;v>0;v--) ;}main(){uint i;uchar j;TMOD=0x20; //初始化T1TH1=0xCC;TL1=0xCC;TR1=1; //无限循环执行发送和接收语句SCON=0x50;P2=0xFF; //灭P1口所有LEDwhile(j!=8){for(j=0;j<8;j++) //设置显示流水灯的显示循环次数{TI=0;delay(20000);SBUF=tab[j]; //查表得到当前要发送的数据,且通过串口发送出去while(RI==0) ; //RI=0等待串口中断RI=0; //RI=1清RIP2=SBUF; //接收数据并送P1口,LED显示接收到的数据while(TI==0) ;delay(20000); //延时}j=0; //重置参数j计数值}点阵LCD显示实验/*********************************************************************** * LCD.C,液晶操作程序* RS=1--状态,=0--数据* RW=1--读取,=0--写* EN=1--芯片有效,=0--芯片无效***********************************************************************/ /***********************************************************************功能描述:液晶应用程序,人机交互接口*显示参数: outdata,要输出的数据X:表示行数(0-1) Y:表示列数(0-15).x,y均为十六进制**********************************************************************/ #include "reg52.H"#define Lcd_Data P0#define Busy 0x80 //用于检测LCM状态字中的Busy标识sbit Lcd_RS=P1^3; //定义引脚sbit Lcd_RW=P1^2;sbit Lcd_EN=P1^1;typedef unsigned char uint8;typedef unsigned int uint16;uint8 code *Str_Display[]={"C","*EZST-51*"," Study&Expold!"};//显示提示内容void Lcd_WriteData(uint8 W_Data); //LCD写数据子程序void Lcd_WriteCmd(uint8 W_Cmd,BusyC); //LCD写指令子程序unsigned char Lcd_WaitBusy(void); //LCD检测忙子程序void Lcd_Init(void); //LCD初始化子程序void Lcd_CLS(void); //LCD清屏子程序void Lcd_Display(uint8 *buff,uint8 x,uint8 y); //在指定的位置显示特定的字符串void Byte_Display(uint8 Byte,uint8 x,uint8 y); //在指定位置显示一个字节的ASCII void Byte_CHG(uint8 Byte,uint8 * Byte_H,uint8 * Byte_L);void Delay5Ms(void);void delay(uint16 N); //通用延时子程序void main(void){Lcd_Init() ; //LCM初始化while(1) //显示提示内容的方式:先在第一行中间位置显示字符“C”;//延时40000MS后、清屏在下一屏第一行显示:*EZST-5X*// 第二行显示:Study&Expold!{Lcd_CLS(); //LCD显示清屏Lcd_Display(Str_Display[0],0,8); //在LCD正中间显示字符“C "delay(60000);Lcd_CLS(); //LCD显示清屏delay(60000);Byte_Display(0x59,1,6); //在LCD第二行显示一个字节的十六进制数59Hdelay(60000);Lcd_CLS();Lcd_Display(Str_Display[1],0,0);Lcd_Display(Str_Display[2],1,0);delay(60000);}}/***********************************************************************功能描述:液晶自定义字符显示子程序*入口参数: outdata--要输出的数据X--表示行数(0-1) Y--表示列数(0-15)x,y均为十六进制.**********************************************************************/void Lcd_Display(uint8 *buff,uint8 x,uint8 y){uint8 i;uint8 ramadd=0x80;if(x) ramadd+=0x40;ramadd+=y;Lcd_WriteCmd(ramadd,1);for(i=0;buff[i]!='\0';i++){ Lcd_WriteData(buff[i]);delay(10000);}}/************************************************************************ 功能描述: 将一个BYTE分离成两个字符,用来显示(如45H-->52H('4')-53H('5')).* 入口参数: BYTE,要分离的字节,BYTE_H,BYTE_L两个指针(返回用BYTE的高低四位) ***********************************************************************/void Byte_CHG(uint8 Byte,uint8 * Byte_H,uint8 * Byte_L){*Byte_L=(Byte&0x0f);if(*Byte_L>9)*Byte_L+=7;*Byte_L+=48;*Byte_H=(Byte&0xf0)>>4;if(*Byte_H>9)*Byte_H+=7;*Byte_H+=48;}/************************************************************************ 功能描述: 在指定位置显示一个字节的ASCII* 入口参数: BYTE,要显示的字节;(x,y)在LCD中的坐标***********************************************************************/void Byte_Display(uint8 Byte,uint8 x,uint8 y){uint8 Byte_H,Byte_L,End_H='H';Byte_CHG(Byte,&Byte_H,&Byte_L);Lcd_Display(&Byte_H,x,y);Lcd_Display(&Byte_L,x,y+1);Lcd_Display(&End_H,x,y+2);}//通用延时子程序void delay(uint16 N){for(;N>0;N--);}//-------------------------------------//LCD写数据子程序void Lcd_WriteData(uint8 W_Data){Lcd_WaitBusy(); //检测忙Lcd_Data = W_Data;Lcd_RS = 1;Lcd_RW = 0;Lcd_EN = 0; //若晶振速度太高可以在这后加小的延时Lcd_EN = 1;Lcd_EN = 0;}//LCD写指令子程序void Lcd_WriteCmd(uint8 W_Cmd,BusyC) //BusyC为0时忽略忙检测{if (BusyC) Lcd_WaitBusy(); //根据需要检测忙Lcd_Data = W_Cmd;Lcd_RS = 0;Lcd_RW = 0;Lcd_EN = 0;Lcd_EN = 1;Lcd_EN = 0;}//LCD检测忙子程序uint8 Lcd_WaitBusy(void){Lcd_Data = 0xFF;Lcd_RS = 0;Lcd_RW = 1;Lcd_EN = 0;Lcd_EN = 1;Lcd_EN = 1;while (Lcd_Data & Busy); //检测忙信号Lcd_EN = 0;return(Lcd_Data); //若不处于忙状态显示字符}//LCD清屏子程序void Lcd_CLS(void){Delay5Ms();Delay5Ms();Lcd_WriteCmd(0x38,1); //显示模式设置,开始要求每次检测忙信号Lcd_WriteCmd(0x08,1); //关闭显示Lcd_WriteCmd(0x01,1); //显示清屏Lcd_WriteCmd(0x06,1); // 显示光标移动设置Lcd_WriteCmd(0x0C,1); // 显示开及光标设置}//LCD初始化子程序void Lcd_Init(void) //LCM初始化{ delay(4000);Lcd_WriteCmd(0x38,0); //显示模式设置,不检测忙信号Delay5Ms();Delay5Ms();Lcd_WriteCmd(0x38,1); //显示模式设置,开始要求每次检测忙信号Lcd_WriteCmd(0x08,1); //关闭显示Lcd_WriteCmd(0x01,1); //显示清屏Lcd_WriteCmd(0x06,1); // 显示光标移动设置Lcd_WriteCmd(0x0C,1); // 显示开及光标设置}//5ms延时void Delay5Ms(void){uint8 i=5552;while(i--);}电压采集(ADC0809)实验#include "reg52.h"typedef unsigned char BYTE;typedef unsigned int WORD;sbit ADC0809_CS=P3^5;sbit ADC0809_EOC=P3^3;sbit SMG_EN1=P1^3;sbit SMG_EN2=P1^2;sbit SMG_EN3=P1^1;sbit SMG_EN4=P1^0;#define SMG_DA TA P0// 字形表, 为0亮// a_// f|_|b// e|_|c .p// d// 00001101//0 1 2 3 4 5 6 7 8 9 A B C D E Funsigned char code SMG_TABLE[16]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};/* Function */void Delay_10us(WORD n);void SMG_Display(BYTE S MGNum, BYTE tdata);/*********************************************************************** main()/*********************************************************************/void main(void){BYTE rdata=0,i=0;BYTE BCD_H=0,BCD_L=0;BYTE xdata * ptr;float result=0;int temp;while(1){ADC0809_CS=0;*ptr=0xff;i=200;while(ADC0809_EOC && (i>0))i--;rdata=*ptr;ADC0809_CS=1;BCD_H=(rdata>>4)&0x0f; //get the high half BYTEBCD_L=rdata&0x0f; //get the low half BYTESMG_Display(1,BCD_H);SMG_Display(2,BCD_L);/*// 显示十进制数据result=5*rdata/256.0;temp=(int)(10*result+0.5);BCD_H=temp/10;BCD_L=temp%10;SMG_Display(3,BCD_H);SMG_Display(4,BCD_L);*/}}// write the tdata to the SMGNumvoid SMG_Display(BYTE S MGNum, BYTE tdata){switch(SMGNum){case 1:SMG_EN1=0;break;case 2:SMG_EN2=0;break;case 3:SMG_EN3=0;break;case 4:SMG_EN4=0;break;default:break;}SMG_DA TA=S MG_TABLE[tdata];Delay_10us(100);SMG_EN1=1;S MG_EN2=1;SMG_EN3=1;SMG_EN4=1;}// delay_ms(WORD n), default use 12M XTAL,others should be modifed.void Delay_10us(WORD n){BYTE i=0;for(;n>0;n--)for(i=0;i<10;i++);}定时-计数器实验TIMER+LED.C.c/*C语言编译——定时计数器当定时器用实验分析:采用T1完成定时,每隔一段时间,发光二极管左移一次;*/#include <reg51.h>#define uint unsigned int#define uchar unsigned charvoid main(){uint i; //设置i为中间变量i=0x01;P2=0xff; //灭P2口所有二极管TMOD=0x10; //设置定时器1方式1EA=1;ET1=1;TR1=1;for(;;){TH1=0x00; //装载计数初值TL1=0x01;do{}while(!TF1); //查询等待TF1置位if(i<0x81) //判断变量i是否垒加八次,若已经垒加八次,则置i初始值0x01{ // 若为垒加八次,则继续循环P2=i^0xff;i<<=1;}elsei=0x01;TF1=0; //软件清TF1}}CONT+SMG.C.txt蜂鸣器发声控制实验/* 文件名:C语言编译蜂鸣器发声实验功能:学习I/O的基本操作,控制蜂鸣器的发声。