51单片机流水灯C语言源代码
(完整版)51单片机流水灯程序
1.第一个发光管以间隔200ms 闪烁。
2.8 个发光管由上至下间隔1s 流动,其中每个管亮500ms, 灭500ms 。
3.8 个发光管来回流动,第个管亮100ms 。
4.用8 个发光管演示出8 位二进制数累加过程。
5.8 个发光管间隔200ms 由上至下,再由下至上,再重复一次,然后全部熄灭再以300ms 间隔全部闪烁 5 次。
重复此过程。
6.间隔300ms 第一次一个管亮流动一次,第二次两个管亮流动,依次到8 个管亮,然后重复整个过程。
7.间隔300ms 先奇数亮再偶数亮,循环三次;一个灯上下循环三次;两个分别从两边往中间流动三次;再从中间往两边流动三次;8 个全部闪烁 3 次;关闭发光管,程序停止。
1#include<reg52.h>#define uint unsigned intsbit led 仁P"0;void delay();void main(){while(1){led1=0;delay();led1=1;delay();}}void delay(){uint x,y;for(x=200;x>0;x--) for(y=100;y>0;y--);}#include<reg52.h> #include<intrins.h> #define uint unsigned int #define uchar unsigned char sbit p P1A0; uchar a;void delay(); void main() {a=0xfe;P1=a;while(1){ a=_crol_(a,1); delay();P1=a; delay();}}void delay(){uint b; for(b=55000;b>0;b--);}3#include<reg52.h>#include <intrins.h>#define uint unsigned int #define uchar unsigned char void delay() {uint x,y; for(x=100;x>0;x--) for(y=110;y>0;y--);}void main() {uchar a,i;while(1)a=0xfe; for(i=0;i<8;i++){P1=a; delay(100); a=_crol_(a,1);}a=0x7f; for(i=0;i<8;i++){P1=a; delay(100); a=_cror_(a,1);4#include<reg52.h>#include <intrins.h> #define uint unsigned int #define uchar unsigned char void delay(uint a) { uint x,y; for(x=a;x>0;x--) for(y=110;y>0;y--);}void main() {uchar b;while(1){b++;P1=~b; delay(200);5#include<reg52.h>#include <intrins.h> #define uint unsigned int #define uchar unsigned char void main() { uchar a,i,j;while(1){ for(j=0;j<2;j++){a=0xfe; for(i=0;i<8;i++){P1=a;delay(200); a=_crol_(a,1);}a=0x7f; for(i=0;i<8;i++){P1=a; delay(200); a=_cror_(a,1);}}P1=0xff; for(j=0;j<10;j++) {delay(300);P1=~P1;}}}void delay(){uint x,y; for(x=200;x>0;x--) for(y=110;y>0;y--); 6 #include<reg52.h> #include <intrins.h> #define uint unsigned int #define uchar unsigned char void delay(){uint x,y; for(x=300;x>0;x--) for(y=110;y>0;y--);} void main(){uchar a,i,j; while(1) {a=0xfe; for(j=0;j<8;j++) { for(i=0;i<8-j;i++) {P1=a; delay(200); a=_crol_(a,1);} a=_crol_(a,j); P1=0xff; a=a<<1;}} }7#include<reg52.h> #include <intrins.h> #define uint unsigned int #define uchar unsigned char void delay(uint z){uint x,y; for(x=z;x>0;x--)for(y=110;y>0;y--);}void main(){uchar a,i,j;for(j=0;j<3;j++){P1=0x55;delay(300);P1=0xaa; delay(300);}for(j=0;j<3;j++){a=0xfe;for(i=0;i<8;i++){P1=a;delay(300); a=_crol_(a,1);}}P1=0xff; for(j=0;j<3;j++){P1=0x7e;delay(300);P1=0xbd; delay(300);P1=0xdb; delay(300);P1=0xe7; delay(300);}P1=0xff;for(j=0;j<3;j++){P1=0xe7;delay(300);P1=0xdb;delay(300);P1=0xbd;delay(300);P1=0x7e;delay(300);}P1=0xff; for(j=0;j<6;j++) {P1=~P1; delay(300);}P1=0xff;while(1);}。
51单片机流水灯程序
1.第一个发光管以间隔200ms闪烁。
2.8个发光管由上至下间隔1s流动,其中每个管亮500ms,灭500ms。
3.8个发光管来回流动,第个管亮100ms。
4。
用8个发光管演示出8位二进制数累加过程。
5。
8个发光管间隔200ms由上至下,再由下至上,再重复一次,然后全部熄灭再以300ms间隔全部闪烁5次。
重复此过程。
6.间隔300ms第一次一个管亮流动一次,第二次两个管亮流动,依次到8个管亮,然后重复整个过程。
7。
间隔300ms先奇数亮再偶数亮,循环三次;一个灯上下循环三次;两个分别从两边往中间流动三次;再从中间往两边流动三次;8个全部闪烁3次;关闭发光管,程序停止。
1#include<reg52。
h〉#define uint unsigned intsbit led1=P1^0;void delay();void main(){while(1){led1=0;delay();led1=1;delay();}}void delay(){uint x,y;for(x=200;x>0;x—-)for(y=100;y〉0;y-—);}2#include<reg52。
h>#include〈intrins。
h〉#define uint unsigned intuchar a;void delay();void main(){a=0xfe;P1=a;while(1){a=_crol_(a,1);delay();P1=a;delay();}}void delay(){uint b;for(b=55000;b>0;b—-);}3#include<reg52。
h〉#include <intrins。
h>#define uint unsigned int #define uchar unsigned char void delay(){uint x,y;for(x=100;x〉0;x--)for(y=110;y>0;y—-);}void main(){uchar a,i;while(1){a=0xfe;for(i=0;i〈8;i++){P1=a;delay(100);a=_crol_(a,1);for(i=0;i〈8;i++){P1=a;delay(100);a=_cror_(a,1);}}}4#include<reg52.h>#include 〈intrins。
51单片机 流水灯 ~ 花样灯 程序
单片机为89c52 晶振为11.0592,/***此程序为流水灯*** /#include<reg52.h>#include<intrins.h>#define uchar unsigned char //宏定义#define uint unsigned intuchar led;void delay(uint z) //延时子函数体{uint x,y;for(x=z;x>0;x--)for(y=110;y>0;y--);}void main(){led=0xfe; //赋初值while(1){P1=led; //点亮第一个小灯delay(100); //延时100毫秒led=_crol_(led,1); 将led的变量左移给下一位}}/*8个发光管间隔200ms由上至下,返回再由上至下,一个个往下亮,后全亮由下至上,返回再由下至上,一个个往下亮,后全亮再重复2次,然后全部熄灭再以500ms间隔全部闪烁3次。
重复此过程*/#include<reg52.h>#include<intrins.h>#define uchar unsigned char#define uint unsigned intuchar led;uint i,j;void delay(uint z){uint x,y;for(x=z;x>0;x--)for(y=110;y>0;y--);}void main(){while(1){for(j=0;j<2;j++){led=0xfe; //赋初值for(i=0;i<8;i++){P1=led; //点亮第一个小灯delay(200); //延时200毫秒led=_crol_(led,1); //将led变量循环左移给下一位}led=0xfe; //赋初值for(i=0;i<8;i++){P1=led; //点亮第一个小灯delay(200); //延时200毫秒led<<=1; //左移给下一位}led=0x7f; //赋初值for(i=0;i<8;i++){P1=led; //点亮第一个小灯delay(200); //延时200毫秒led=_cror_(led,1); //将led变量循环右移给下一位}led=0x7f; //赋初值for(i=0;i<8;i++){P1=led; //点亮第一个小灯delay(200); //延时200毫秒led>>=1; //右移给下一位}}for(j=0;j<6;j++){P1=~P1; //全部取反6次,即闪烁5次delay(500); //延时500毫秒}}}。
基于51单片机的流水灯程序
下面给出两个程序。
//================================================//利用单片机定时器中断,使P0口外接的8个发光二极管以1位1s的速度,轮流发光。
//================================================#include<reg52.h>#include<intrins.h>#define ucha unsigned char#define uint unsigned intvoid init();ucha b = 0;//----------------------------main(){ucha a = 0xfe;P0 = a;init();while(1) {if(b == 20) { //如果中断了20次.b = 0; //就是1sa = _crol_(a, 1);//移位P0 = a; //输出}}}//----------------------------void init(){TMOD = 0x01; //T0定时方式0TH0 = (65536 - 50000) / 256; //定时50ms@12MHzTL0 = (65536 - 50000) % 256;TR0 = 1; //启动ET0 = 1; //开放中断EA = 1;}//----------------------------void timer0() interrupt 1 //每隔50ms中断一次{TH0 = (65536 - 50000) / 256; //恢复初始值TL0 = (65536 - 50000) % 256;b++; //中断次数加一}/*//================================================//利用单片机软件延时,使P0口外接的8个发光二极管以1位1s的速度,轮流发光。
多种方法写出的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单片机的各种花样的流水灯c51程序
基于51单片机的各种花样的流水灯c51程序/*-----------------------------------------------功能:流水灯对称移动闪烁(双闪烁)------------------------------------------------*/#include<REG52.H>#define uint unsigned intvoid delay(uint);main(){uint comp1=0xfe;uint comp2=0x80;P1=0x7e;delay(30000);while(1){P1=0xff;comp1<<=1;comp1|=0x01;comp2>>=1;P1&=comp1;P1^=comp2;delay(30000);if(P1==0xe7){comp1<<=1;comp1|=0x01;comp2>>=1;}if(comp1==0x7f){comp1=0xfe;comp2=0x80;}}}void delay(uint cnt){while(cnt--);}/*----------------------------------------------------------------- 只循环一次,而没有一直循环下去,出错地方在:通过添加一条测试语句:if(comp1==0x7f){comp1=0xfe; comp2=0x80; P1=0x00; delay(30000);}发现if语句没有被执行,自然继续左右移动:1111 1111&1111 1111^0000 0000==11111 1111所以看起来是执行了一次while中的代码。
具体为什么不行,还不清楚……更正下列代码后,能够实现功能。
if(P1==0x7e){comp1=0xfe;comp2=0x80;}或者:if(comp2==0x01){comp1=0xfe;comp2=0x80;}--------------------------------------------------------------*/*********************************************/*-----------------------------------------------功能:流水灯(单向单闪烁)------------------------------------------------*/#include<reg52.h>#define uint unsigned intvoid delay(uint);main(){//uint fre=0x03;//uint comp1=0xfe,comp2=0x80;P1=0xfe;while(1){/*------------------------------------------------------------------模块1:循环单向闪烁,只有一个灯亮灭执行3次,转入下一种闪烁--------------------------------------------------------------------*/ while(1){delay(30000);P1<<=1;P1|=0x01;if(P1=0x7f){delay(30000);P1=0xfe;}}}}void delay(uint cnt){while(cnt--);}/*-----------------------------------------程序运行结果左右两端跳动,原因是:if(P1=0x7f);中的等号也成了赋值号,更正为if(P1==0x7f);特别注意,不要把判断语句种的等号误写为赋值号。
多种方法写出的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语言程序
ALEA B C D E F G DPALE A B C D E F G D P1234ALE 2134XTAL218XTAL119ALE 30EA31PSEN 29RST9P0.0/AD039P0.1/AD138P0.2/AD237P0.3/AD336P0.4/AD435P0.5/AD534P0.6/AD633P0.7/AD732P1.0/T21P1.1/T2EX 2P1.23P1.34P1.45P1.56P1.67P1.78P3.0/RXD 10P3.1/TXD 11P3.2/INT012P3.3/INT113P3.4/T014P3.7/RD17P3.6/WR 16P3.5/T115P2.7/A1528P2.0/A821P2.1/A922P2.2/A1023P2.3/A1124P2.4/A1225P2.5/A1326P2.6/A1427U1AT89C52234567891RP1RESPACK-8D02D13D24D35D46D57D68D79Q019Q118Q217Q316Q415Q514Q613Q712LE 11OE 1U274HC573R1R2R3R4R5R6R7R8D1LED-REDD2LED-YELLOW D3LED-GREEND4LED-GREEND5LED-REDD6LED-YELLOWD7LED-GREEND8LED-GREEND02D13D24D35D46D57D68D79Q019Q118Q217Q316Q415Q514Q613Q712LE 11OE 1U374HC573R171kR181kR191kR201k/*******flowing water light*********///晶振:12M ,P0口接8个led进行流水灯显示,74HC573驱动,8位led共阴接法ORG 0000HLJMP MAINORG 0030H //主程序从程序存储器0030H单元开始存放MAIN: MOV P0,#0FFH //P0口初始化MOV A,#01H //共阴接法,点亮最低位LEDLOOP: MOV P0,ALCALL DELAY_1S //延时1sRL A //循环左移一次,点亮下一位SJMP LOOP //跳转到LOOP处执行,死循环// 3层嵌套软件延时子程序,延时1sDELAY_1S: MOV R1,#10 //最外层循环体执行10次DELAY0: MOV R2,#200 //中间层循环体执行200次DELAY1: MOV R3,#248 //最内层循环体执行248次DJNZ R3,$ //循环子程序最内层,执行时间248*2=496usNOP //空操作,为了凑到准确的500us执行时间DJNZ R2,DELAY1 //中间层循环体(1+496+1+2)*200=100000us=100msDJNZ R1,DELAY0 //最外层循环体(100ms+2us)*10=1000.02msRETEND/*******flowing water light*********/#include<reg51.h>#include<intrins.h>#define uchar unsigned char#define uint unsigned int//延时函数void Delay_ms(uint x){uchar i;while(x--){for(i=0;i<120;i++);}}//主函数void main(){P0=0xff; //P0口初始化P0=0x01; //共阴接法,点亮最低位LEDwhile(1){Delay_ms(1000); //延时约1sP0=_crol_(P0,1); //P0 的值向左循环移动,_crol_()函数在头文件intrins.h中}}。
51单片机流水灯、数码管、定时器、秒表、计算器程序
temp=temp&0xf0;
while(temp!=0xf0){
delay(1);
temp=P1;
temp=temp&0xf0;
while(temp!=0xf0){
switch(temp){
case 0xe0:number=0;break;
case 0xd0:number=1;break;
}
}
void move_mid_side(void)
{
int m;
for(m=0;m<7;m++){
P1=data_group[m];
delay(500);
}
}
void move_right_only(void)
{
int a,temp;
delay(500);
temp=0x80;
for(a=7;a>=0;a--){
}
while(temp!=0xf0){
temp=P1;
temp=temp&0xf0;
}
}
}
P1=0xfb;
temp=P1;
temp=temp&0xf0;
while(temp!=0xf0){
delay(1);
temp=P1;
temp=temp&0xf0;
while(temp!=0xf0){
switch(temp){
int number=0,time=0;
int main(void)
{
//TMOD=0x01;//设置工作方式定时器0
//TH0=(65536-50000)/256;//给计数器装初值
}
单片机流水灯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单片机流水灯左移右移控制程序
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; //声明无符号整型变量Stemp=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++); //延时程序}}//===================================== ==================================。
51单片机流水灯、数码管、定时器、秒表、计算器程序
51单片机流水灯、数码管、定时器、秒表、计算器程序流水灯 #include<reg52.h>char data_group[]={0x7e,0xbd,0xdb,0xe7,0xdb,0xbd,0x7e};unsigned int a;void delay(unsigned int time) {int i,j;for(i=time;i>0;i--){for(j=100;j>0;j--);}}void move_mid_side(void) {int m;for(m=0;m<7;m++){P1=data_group[m];delay(500);}}void move_right_only(void) {int a,temp;delay(500);temp=0x80;for(a=7;a>=0;a--){temp=~temp;P1=temp;delay(500);temp=~temp;temp>>=1;}temp=0x80;delay(1000);}void move_left_only(void) { int a,temp;delay(500);temp=0x01;for(a=7;a>=0;a--){temp=~temp;P1=temp;delay(500);temp=~temp;temp<<=1;}temp=0x01;delay(1000);}void move_right_hlod(void) { int a,temp;temp=0x7f;for(a=8;a>=0;a--){P1=temp;delay(500);temp=temp>>1;}temp=~temp;delay(1000);}void move_left_hold(void) { int a,temp;temp=0xfe;for(a=8;a>=0;a--){P1=temp;delay(500);temp<<=1;}temp=~temp;delay(1000);}void main(){while(1){move_left_hold();move_mid_side();move_right_hlod();move_mid_side();move_left_only();move_right_only();}}数码管 #include <reg52.h>chardata_duan[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};//0~9 char data_wei[]={0xff,0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe,0x00}; //0xff全关,之后为第一个数码管到第八个数码管//char data_wei_add[]={0x7f,0x3f,0x1f,0x0f,0x07,0x03,0x01,0x00};void delay(unsigned int time) {int i,j;for(i=time;i>0;i--){for(j=0;j<100;j++);}}void show_number_only(void){int i;P2=data_wei[1]; //P2为位选for(i=0;i<10;i++){P0=data_duan[i]; //P0为段选delay(500);}}/*******************数码管动态显示子程序***************/ void move_show1(void){int i;for(i=1;i<2;i++){P2=data_wei[i];P0=data_duan[i];delay(1);}}void move_show2(void){int i;for(i=1;i<3;i++){P2=data_wei[i];P0=data_duan[i];delay(1);}}void move_show3(void) {int i;for(i=1;i<4;i++){P2=data_wei[i];P0=data_duan[i]; delay(1);}}void move_show4(void) {int i;for(i=0;i<5;i++){P2=data_wei[i];P0=data_duan[i]; delay(1);}}void move_show5(void) {int i;for(i=0;i<6;i++){P2=data_wei[i];P0=data_duan[i]; delay(1);}}void move_show6(void) {int i;for(i=0;i<7;i++){P2=data_wei[i];P0=data_duan[i]; delay(1);}}void move_show7(void) {int i;for(i=0;i<8;i++){P2=data_wei[i];P0=data_duan[i]; delay(1);}}void move_show8(void) {int i;for(i=0;i<9;i++){P2=data_wei[i];P0=data_duan[i];delay(1);}}/********数码管动态显示,实现数码管由1~8在八个数码管上一次显示************/void move_show_hold(void){int temp,i;temp=1000;for(i=temp;i>0;i--)move_show1();temp=600;for(i=temp;i>0;i--)move_show2();temp=400;for(i=temp;i>0;i--)move_show3();temp=200;for(i=temp;i>0;i--)move_show4();temp=200;for(i=temp;i>0;i--)move_show5();temp=200;for(i=temp;i>0;i--)move_show6();temp=100;for(i=temp;i>0;i--)move_show7();temp=100;for(i=temp;i>0;i--)move_show8();}void show_all(void) //将所有的位选端口打开,变化段选,显示整体变化1~9{int i;P2=data_wei[9];for(i=0;i<10;i++){P0=data_duan[i];delay(500);}}int main(void) {while(1){// show_number_only();move_show8();//move_show_hold(); // show_all(); }}独立按键#include <reg52.h>sbit key1=P3^0; sbit d2=P1^0;int temp,number=0;void delay(unsigned int time) {int i,j;for(i=time;i>0;i--){for(j=100;j>0;j--) ;}}int main(void) {temp=0xfe;while(1){d2=1;key1=1;P1=temp;if(key1==0){delay(100);if(key1==0){number++;if(number<=7){temp=~temp;temp=temp<<1;temp=~temp;P1=temp;}else{temp=0xfe;number=0;}}}}}矩阵按键 #include<reg52.h>chardata_duan[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x80 ,0xc6,0xc0,0x86,0x8e,};//0~9 chardata_wei[]={0xff,0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe,0x00};int number=0;void delay(unsigned int time) {int i,j;for(i=time;i>0;i--){for(j=0;j<100;j++);}}//void move_show1(void)//{// int i;// for(i=1;i<2;i++){ // P2=data_wei[i]; // P0=data_duan[i]; // delay(1);// }//}//void move_show2(void)//{// int i;// for(i=1;i<3;i++){ // P2=data_wei[i];// P0=data_duan[i]; // delay(1);// }//}void scan_key(void){int temp;P1=0xfe;temp=P1;temp=temp&0xf0;while(temp!=0xf0){delay(1);temp=P1;temp=temp&0xf0;while(temp!=0xf0){switch(temp){case 0xe0:number=12;break; case 0xd0:number=13;break; case 0xb0:number=14;break; case 0x70:number=15;break; }while(temp!=0xf0){temp=P1;temp=temp&0xf0;}}}P1=0xfd;temp=P1;temp=temp&0xf0;while(temp!=0xf0){delay(1);temp=P1;temp=temp&0xf0;while(temp!=0xf0){switch(temp){case 0xe0:number=8;break; case 0xd0:number=9;break; case 0xb0:number=10;break; case 0x70:number=11;break; }while(temp!=0xf0){temp=P1;temp=temp&0xf0;}}}P1=0xfb;temp=P1;temp=temp&0xf0;while(temp!=0xf0){delay(1);temp=P1;temp=temp&0xf0;while(temp!=0xf0){switch(temp){case 0xe0:number=4;break; case 0xd0:number=5;break; case 0xb0:number=6;break; case 0x70:number=7;break; }while(temp!=0xf0){temp=P1;temp=temp&0xf0;}}}P1=0xf7;temp=P1;temp=temp&0xf0;while(temp!=0xf0){delay(1);temp=P1;temp=temp&0xf0;while(temp!=0xf0){switch(temp){case 0xe0:number=0;break; case 0xd0:number=1;break; case 0xb0:number=2;break; case 0x70:number=3;break;}while(temp!=0xf0){temp=P1;temp=temp&0xf0;}}}// return number;}int main(void){// int num=0;while(1){// num=scan_key(num);scan_key();P2=data_wei[1];P0=data_duan[number];}}定时器中断 #include <reg52.h>chardata_duan[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};//0~9 char data_wei[]={0xff,0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe,0x00}; //0xff全关,之后为第一个数码管到第八个数码管int number=0,time=0;int main(void){// TMOD=0x01;//设置工作方式定时器0// TH0=(65536-50000)/256; //给计数器装初值 // TL0=(65535-50000)%256;// EA=1; //开总中断 // ET0=1; //定时器T0中断允许 // TR0=1; //启动定时器0TMOD=0x10;//设置工作方式定时器1TH1=(65536-50000)/256; //给计数器装初值TL1=(65535-50000)%256;EA=1; //开总中断ET1=1; //定时器T1中断允许TR1=1; //启动定时器1while(1){if(number==20){P2=data_wei[2];P0=data_duan[4];}if(time==40){P2=data_wei[0];number=0;time=0;}}}void exter3() interrupt 3{TH1=(65536-50000)/256; //给计数器装初值TL1=(65535-50000)%256;time++;number++;}//void exter1() interrupt 1//{// TH0=(65536-50000)/256; //给计数器装初值 // TL0=(65535-50000)%256;// time++;// number++;//}时钟 #include <reg52.h>chardata_duan[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};//0~9 char data_wei[]={0xff,0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe,0x00};unsigned long int second=0,minute=0,hour=0,point=0,tip=0;unsigned long int time=0,sign=0,number=0,key=0,variable=0; unsigned long int S1=0,S2=0,M1=0,M2=0,H1=0;H2=0;void delay(unsigned int time) {int i,j;for(i=time;i>0;i--){for(j=0;j<100;j++);}}void key_scan(void){int temp; //应用按键检测P1=0xf7;temp=P1;temp=temp&0xf0;while(temp!=0xf0){delay(1);temp=P1;temp=temp&0xf0;while(temp!=0xf0){switch(temp){case 0xe0:number=1;break; case 0xd0:number=2;break; case 0xb0:number=3;break; case 0x70:number=4;break; }while(temp!=0xf0){temp=P1;temp=temp&0xf0;}}}// P1=0xfb;// temp=P1;// temp=temp&0xf0; // while(temp!=0xf0){ // delay(1);// temp=P1;// temp=temp&0xf0; // while(temp!=0xf0){ // switch(temp){ // case 0xe0:number=5;break; // case 0xd0:number=6;break; // case0xb0:number=7;break; // case 0x70:number=8;break; // } // while(temp!=0xf0){ // temp=P1;// temp=temp&0xf0; // }// }////// }}void adjust_alarm(void) {if(number==1){ //控制时钟的启动与停止key=!key;if(key==1){EA=0;variable=time;}if(key==0){EA=1;}number=0;}if(number==2){ //调节时间选择标志位tip=tip+1;tip=tip%6;if(tip==0){tip=6;}number=0;}if((number==3)&&(EA==0)){if(tip==1)time=time-1;if(tip==2)time=time-10;if(tip==3)time=time-60;if(tip==4)time=time-600;if(tip==5)time=time-3600;if(tip==6)time=time-36000;number=0;}if((number==4)&(EA==0)){if(tip==1)time=time+1;if(tip==2)time=time+10;if(tip==3)time=time+60;if(tip==4)time=time+600;if(tip==5)time=time+3600;if(tip==6)time=time+36000;number=0;}// if((number==5)&&(EA==0)){ // if(tip==1) // variable=variable-1; // if(tip==2)// variable=variable-10; // if(tip==3)// variable=variable-60; // if(tip==4)// variable=variable-600; // if(tip==5)// variable=variable-3600; // if(tip==6)// variable=variable-3600; // number=0;// }// if((number==6)&(EA==0)){ // if(tip==1)// variable=variable+1; // if(tip==2)// variable=variable+10; // if(tip==3)// variable=variable+60; // if(tip==4)// variable=variable+600; // if(tip==5)// variable=variable+3600; // if(tip==6)// variable=variable+36000; // number=0;// }}void compare_time(void){if(time>variable){;}}void alarm_show_s(void){int sign2;for(sign2=1;sign2<2;sign2++){ //数码管动态显示S1=second%10;P2=data_wei[sign2];P0=data_duan[S1];delay(1);}for(sign2=2;sign2<3;sign2++){ S2=second/10;P2=data_wei[sign2];P0=data_duan[S2];delay(1);}for(sign2=3;sign2<4;sign2++){ P2=data_wei[sign2];P0=0xbf;delay(1);}for(sign2=4;sign2<5;sign2++){ M1=minute%10;P2=data_wei[sign2];P0=data_duan[M1];delay(1);}for(sign2=5;sign2<6;sign2++){ M2=minute/10;P2=data_wei[sign2];P0=data_duan[M2];delay(1);}for(sign2=6;sign2<7;sign2++){P2=data_wei[sign2];P0=0xbf;delay(1);}for(sign2=7;sign2<8;sign2++){H1=hour%10;P2=data_wei[sign2];P0=data_duan[H1];delay(1);}for(sign2=8;sign2<9;sign2++){H2=hour/10;P2=data_wei[sign2];P0=data_duan[H2];delay(1);}}void record_set_alarm(void) //设定闹钟所改变的值 { second=variable%60;minute=variable/60%60;hour=variable/3600%24;if(hour==24){variable=0;}}void record_time(void) {second=time%60;minute=time/60%60; hour=time/3600%24;if(hour==24){time=0;}}int main(void){TMOD=0x01;TH0=(65536-50000)/256; TL0=(65536-50000)%256; EA=1;ET0=1;TR0=1;while(1){record_time();// record_set_alarm(); alarm_show_s();key_scan();adjust_alarm();}}void extern1() interrupt 1 //定时器中断0 { TH0=(65536-50000)/256; //重装值TL0=(65536-50000)%256;sign++;if(sign==20){time++;sign=0;}}。
51单片机流水灯、数码管、定时器、秒表、计算器程序
51单片机流水灯、数码管、定时器、秒表、计算器程序流水灯 #include<reg52.h>char data_group[]={0x7e,0xbd,0xdb,0xe7,0xdb,0xbd,0x7e};unsigned int a;void delay(unsigned int time) {int i,j;for(i=time;i>0;i--){for(j=100;j>0;j--);}}void move_mid_side(void) {int m;for(m=0;m<7;m++){P1=data_group[m];delay(500);}}void move_right_only(void) {int a,temp;delay(500);temp=0x80;for(a=7;a>=0;a--){temp=~temp;P1=temp;delay(500);temp=~temp;temp>>=1;}temp=0x80;delay(1000);}void move_left_only(void) { int a,temp;delay(500);temp=0x01;for(a=7;a>=0;a--){temp=~temp;P1=temp;delay(500);temp=~temp;temp<<=1;}temp=0x01;delay(1000);}void move_right_hlod(void) { int a,temp;temp=0x7f;for(a=8;a>=0;a--){P1=temp;delay(500);temp=temp>>1;}temp=~temp;delay(1000);}void move_left_hold(void) { int a,temp;temp=0xfe;for(a=8;a>=0;a--){P1=temp;delay(500);temp<<=1;}temp=~temp;delay(1000);}void main(){while(1){move_left_hold();move_mid_side();move_right_hlod();move_mid_side();move_left_only();move_right_only();}}数码管 #include <reg52.h>chardata_duan[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};//0~9 char data_wei[]={0xff,0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe,0x00}; //0xff全关,之后为第一个数码管到第八个数码管//char data_wei_add[]={0x7f,0x3f,0x1f,0x0f,0x07,0x03,0x01,0x00};void delay(unsigned int time) {int i,j;for(i=time;i>0;i--){for(j=0;j<100;j++);}}void show_number_only(void){int i;P2=data_wei[1]; //P2为位选for(i=0;i<10;i++){P0=data_duan[i]; //P0为段选delay(500);}}/*******************数码管动态显示子程序***************/ void move_show1(void){int i;for(i=1;i<2;i++){P2=data_wei[i];P0=data_duan[i];delay(1);}}void move_show2(void){int i;for(i=1;i<3;i++){P2=data_wei[i];P0=data_duan[i];delay(1);}}void move_show3(void) {int i;for(i=1;i<4;i++){P2=data_wei[i];P0=data_duan[i]; delay(1);}}void move_show4(void) {int i;for(i=0;i<5;i++){P2=data_wei[i];P0=data_duan[i]; delay(1);}}void move_show5(void) {int i;for(i=0;i<6;i++){P2=data_wei[i];P0=data_duan[i]; delay(1);}}void move_show6(void) {int i;for(i=0;i<7;i++){P2=data_wei[i];P0=data_duan[i]; delay(1);}}void move_show7(void) {int i;for(i=0;i<8;i++){P2=data_wei[i];P0=data_duan[i]; delay(1);}}void move_show8(void) {int i;for(i=0;i<9;i++){P2=data_wei[i];P0=data_duan[i];delay(1);}}/********数码管动态显示,实现数码管由1~8在八个数码管上一次显示************/void move_show_hold(void){int temp,i;temp=1000;for(i=temp;i>0;i--)move_show1();temp=600;for(i=temp;i>0;i--)move_show2();temp=400;for(i=temp;i>0;i--)move_show3();temp=200;for(i=temp;i>0;i--)move_show4();temp=200;for(i=temp;i>0;i--)move_show5();temp=200;for(i=temp;i>0;i--)move_show6();temp=100;for(i=temp;i>0;i--)move_show7();temp=100;for(i=temp;i>0;i--)move_show8();}void show_all(void) //将所有的位选端口打开,变化段选,显示整体变化1~9{int i;P2=data_wei[9];for(i=0;i<10;i++){P0=data_duan[i];delay(500);}}int main(void) {while(1){// show_number_only();move_show8();//move_show_hold(); // show_all(); }}独立按键#include <reg52.h>sbit key1=P3^0; sbit d2=P1^0;int temp,number=0;void delay(unsigned int time) {int i,j;for(i=time;i>0;i--){for(j=100;j>0;j--) ;}}int main(void) {temp=0xfe;while(1){d2=1;key1=1;P1=temp;if(key1==0){delay(100);if(key1==0){number++;if(number<=7){temp=~temp;temp=temp<<1;temp=~temp;P1=temp;}else{temp=0xfe;number=0;}}}}}矩阵按键 #include<reg52.h>chardata_duan[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x80 ,0xc6,0xc0,0x86,0x8e,};//0~9 chardata_wei[]={0xff,0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe,0x00};int number=0;void delay(unsigned int time) {int i,j;for(i=time;i>0;i--){for(j=0;j<100;j++);}}//void move_show1(void)//{// int i;// for(i=1;i<2;i++){ // P2=data_wei[i]; // P0=data_duan[i]; // delay(1);// }//}//void move_show2(void)//{// int i;// for(i=1;i<3;i++){ // P2=data_wei[i];// P0=data_duan[i]; // delay(1);// }//}void scan_key(void){int temp;P1=0xfe;temp=P1;temp=temp&0xf0;while(temp!=0xf0){delay(1);temp=P1;temp=temp&0xf0;while(temp!=0xf0){switch(temp){case 0xe0:number=12;break; case 0xd0:number=13;break; case 0xb0:number=14;break; case 0x70:number=15;break; }while(temp!=0xf0){temp=P1;temp=temp&0xf0;}}}P1=0xfd;temp=P1;temp=temp&0xf0;while(temp!=0xf0){delay(1);temp=P1;temp=temp&0xf0;while(temp!=0xf0){switch(temp){case 0xe0:number=8;break; case 0xd0:number=9;break; case 0xb0:number=10;break; case 0x70:number=11;break; }while(temp!=0xf0){temp=P1;temp=temp&0xf0;}}}P1=0xfb;temp=P1;temp=temp&0xf0;while(temp!=0xf0){delay(1);temp=P1;temp=temp&0xf0;while(temp!=0xf0){switch(temp){case 0xe0:number=4;break; case 0xd0:number=5;break; case 0xb0:number=6;break; case 0x70:number=7;break; }while(temp!=0xf0){temp=P1;temp=temp&0xf0;}}}P1=0xf7;temp=P1;temp=temp&0xf0;while(temp!=0xf0){delay(1);temp=P1;temp=temp&0xf0;while(temp!=0xf0){switch(temp){case 0xe0:number=0;break; case 0xd0:number=1;break; case 0xb0:number=2;break; case 0x70:number=3;break;}while(temp!=0xf0){temp=P1;temp=temp&0xf0;}}}// return number;}int main(void){// int num=0;while(1){// num=scan_key(num);scan_key();P2=data_wei[1];P0=data_duan[number];}}定时器中断 #include <reg52.h>chardata_duan[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};//0~9 char data_wei[]={0xff,0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe,0x00}; //0xff全关,之后为第一个数码管到第八个数码管int number=0,time=0;int main(void){// TMOD=0x01;//设置工作方式定时器0// TH0=(65536-50000)/256; //给计数器装初值 // TL0=(65535-50000)%256;// EA=1; //开总中断 // ET0=1; //定时器T0中断允许 // TR0=1; //启动定时器0TMOD=0x10;//设置工作方式定时器1TH1=(65536-50000)/256; //给计数器装初值TL1=(65535-50000)%256;EA=1; //开总中断ET1=1; //定时器T1中断允许TR1=1; //启动定时器1while(1){if(number==20){P2=data_wei[2];P0=data_duan[4];}if(time==40){P2=data_wei[0];number=0;time=0;}}}void exter3() interrupt 3{TH1=(65536-50000)/256; //给计数器装初值TL1=(65535-50000)%256;time++;number++;}//void exter1() interrupt 1//{// TH0=(65536-50000)/256; //给计数器装初值 // TL0=(65535-50000)%256;// time++;// number++;//}时钟 #include <reg52.h>chardata_duan[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};//0~9 char data_wei[]={0xff,0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe,0x00};unsigned long int second=0,minute=0,hour=0,point=0,tip=0;unsigned long int time=0,sign=0,number=0,key=0,variable=0; unsigned long int S1=0,S2=0,M1=0,M2=0,H1=0;H2=0;void delay(unsigned int time) {int i,j;for(i=time;i>0;i--){for(j=0;j<100;j++);}}void key_scan(void){int temp; //应用按键检测P1=0xf7;temp=P1;temp=temp&0xf0;while(temp!=0xf0){delay(1);temp=P1;temp=temp&0xf0;while(temp!=0xf0){switch(temp){case 0xe0:number=1;break; case 0xd0:number=2;break; case 0xb0:number=3;break; case 0x70:number=4;break; }while(temp!=0xf0){temp=P1;temp=temp&0xf0;}}}// P1=0xfb;// temp=P1;// temp=temp&0xf0; // while(temp!=0xf0){ // delay(1);// temp=P1;// temp=temp&0xf0; // while(temp!=0xf0){ // switch(temp){ // case 0xe0:number=5;break; // case 0xd0:number=6;break; // case0xb0:number=7;break; // case 0x70:number=8;break; // } // while(temp!=0xf0){ // temp=P1;// temp=temp&0xf0; // }// }////// }}void adjust_alarm(void) {if(number==1){ //控制时钟的启动与停止key=!key;if(key==1){EA=0;variable=time;}if(key==0){EA=1;}number=0;}if(number==2){ //调节时间选择标志位tip=tip+1;tip=tip%6;if(tip==0){tip=6;}number=0;}if((number==3)&&(EA==0)){if(tip==1)time=time-1;if(tip==2)time=time-10;if(tip==3)time=time-60;if(tip==4)time=time-600;if(tip==5)time=time-3600;if(tip==6)time=time-36000;number=0;}if((number==4)&(EA==0)){if(tip==1)time=time+1;if(tip==2)time=time+10;if(tip==3)time=time+60;if(tip==4)time=time+600;if(tip==5)time=time+3600;if(tip==6)time=time+36000;number=0;}// if((number==5)&&(EA==0)){ // if(tip==1) // variable=variable-1; // if(tip==2)// variable=variable-10; // if(tip==3)// variable=variable-60; // if(tip==4)// variable=variable-600; // if(tip==5)// variable=variable-3600; // if(tip==6)// variable=variable-3600; // number=0;// }// if((number==6)&(EA==0)){ // if(tip==1)// variable=variable+1; // if(tip==2)// variable=variable+10; // if(tip==3)// variable=variable+60; // if(tip==4)// variable=variable+600; // if(tip==5)// variable=variable+3600; // if(tip==6)// variable=variable+36000; // number=0;// }}void compare_time(void){if(time>variable){;}}void alarm_show_s(void){int sign2;for(sign2=1;sign2<2;sign2++){ //数码管动态显示S1=second%10;P2=data_wei[sign2];P0=data_duan[S1];delay(1);}for(sign2=2;sign2<3;sign2++){ S2=second/10;P2=data_wei[sign2];P0=data_duan[S2];delay(1);}for(sign2=3;sign2<4;sign2++){ P2=data_wei[sign2];P0=0xbf;delay(1);}for(sign2=4;sign2<5;sign2++){ M1=minute%10;P2=data_wei[sign2];P0=data_duan[M1];delay(1);}for(sign2=5;sign2<6;sign2++){ M2=minute/10;P2=data_wei[sign2];P0=data_duan[M2];delay(1);}for(sign2=6;sign2<7;sign2++){P2=data_wei[sign2];P0=0xbf;delay(1);}for(sign2=7;sign2<8;sign2++){H1=hour%10;P2=data_wei[sign2];P0=data_duan[H1];delay(1);}for(sign2=8;sign2<9;sign2++){H2=hour/10;P2=data_wei[sign2];P0=data_duan[H2];delay(1);}}void record_set_alarm(void) //设定闹钟所改变的值 { second=variable%60;minute=variable/60%60;hour=variable/3600%24;if(hour==24){variable=0;}}void record_time(void) {second=time%60;minute=time/60%60; hour=time/3600%24;if(hour==24){time=0;}}int main(void){TMOD=0x01;TH0=(65536-50000)/256; TL0=(65536-50000)%256; EA=1;ET0=1;TR0=1;while(1){record_time();// record_set_alarm(); alarm_show_s();key_scan();adjust_alarm();}}void extern1() interrupt 1 //定时器中断0 { TH0=(65536-50000)/256; //重装值TL0=(65536-50000)%256;sign++;if(sign==20){time++;sign=0;}}。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
#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);a=0xfe;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);P3=a;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;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;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);}。