stc89c51单片机流水灯程序.docx

上传人:b****5 文档编号:14774444 上传时间:2023-06-27 格式:DOCX 页数:12 大小:15.97KB
下载 相关 举报
stc89c51单片机流水灯程序.docx_第1页
第1页 / 共12页
stc89c51单片机流水灯程序.docx_第2页
第2页 / 共12页
stc89c51单片机流水灯程序.docx_第3页
第3页 / 共12页
stc89c51单片机流水灯程序.docx_第4页
第4页 / 共12页
stc89c51单片机流水灯程序.docx_第5页
第5页 / 共12页
stc89c51单片机流水灯程序.docx_第6页
第6页 / 共12页
stc89c51单片机流水灯程序.docx_第7页
第7页 / 共12页
stc89c51单片机流水灯程序.docx_第8页
第8页 / 共12页
stc89c51单片机流水灯程序.docx_第9页
第9页 / 共12页
stc89c51单片机流水灯程序.docx_第10页
第10页 / 共12页
stc89c51单片机流水灯程序.docx_第11页
第11页 / 共12页
stc89c51单片机流水灯程序.docx_第12页
第12页 / 共12页
亲,该文档总共12页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

stc89c51单片机流水灯程序.docx

《stc89c51单片机流水灯程序.docx》由会员分享,可在线阅读,更多相关《stc89c51单片机流水灯程序.docx(12页珍藏版)》请在冰点文库上搜索。

stc89c51单片机流水灯程序.docx

stc89c51单片机流水灯程序

51单片机流水灯法式之巴公井开创作

时间:

二O二一年七月二十九日

法式一(用C语言编的最基础的法式)

#include

#include

sbitD0=P1^0;//位界说,把P1口的第一个管脚界说为D0

sbitD1=P1^1;//位界说,把P1口的第二个管脚界说为D1

sbitD2=P1^2;//位界说,把P1口的第3个管脚界说为D2

sbitD3=P1^3;//位界说,把P1口的第4个管脚界说为D3

sbitD4=P1^4;//位界说,把P1口的第5个管脚界说为D4

sbitD5=P1^5;//位界说,把P1口的第6个管脚界说为D5

sbitD6=P1^6;//位界说,把P1口的第7个管脚界说为D6

sbitD7=P1^7;//位界说,把P1口的第8个管脚界说为D7

voidmain()

{

while

(1)//建立年夜的死循环.

{

unsignedinti,j;

D0=0,D7=1;//亮灯1,关灯8

for(i=50;i>0;i--)

for(j=200;j>0;j--);//两个FOR组成的延时

D0=1,D1=0;//亮灯2,关灯1

for(i=50;i>0;i--)

for(j=200;j>0;j--);

D1=1,D2=0;

for(i=50;i>0;i--)

for(j=200;j>0;j--);

D2=1,D3=0;

for(i=50;i>0;i--)

for(j=200;j>0;j--);

D3=1,D4=0;

for(i=50;i>0;i--)

for(j=200;j>0;j--);

D4=1,D5=0;

for(i=50;i>0;i--)

for(j=200;j>0;j--);

D5=1,D6=0;

for(i=50;i>0;i--)

for(j=200;j>0;j--);

D6=1,D7=0;

for(i=50;i>0;i--)

for(j=200;j>0;j--);

}

}

法式二(用swichcase语句编的)

#include//51单片机的头文件.

sbitd1=P1^0;//

sbitd2=P1^1;

sbitd3=P1^2;

sbitd4=P1^3;

sbitd5=P1^4;

sbitd6=P1^5;

sbitd7=P1^6;

sbitd8=P1^7;

voidmain()

{

unsignedinti=1,a,b;

while

(1)//年夜循环

{

for(i=1;i<15;i++)

{

for(a=0;a<80;a++)//延时.

for(b=0;b<200;b++);

switch(i)

{

case1:

d2=1,d1=0,d8=1;

break;

case2:

d1=1,d2=0;

break;

case3:

d2=1,d3=0;

break;

case4:

d3=1,d4=0;

break;

case5:

d4=1,d5=0;

break;

case6:

d5=1,d6=0;

break;

case7:

d6=1,d7=0;

break;

case8:

d7=1,d8=0;

break;

case9:

d8=1,d7=0;

break;

case10:

d7=1,d6=0;

break;

case11:

d6=1,d5=0;

break;

case12:

d5=1,d4=0;

break;

case13:

d4=1,d3=0;

break;

case14:

d3=1,d2=0;

break;

}

};

}

}

法式三(最简洁的):

#include

#include//_crol_和_cror_的头文件.

voiddelay(unsignedintz)//建立延时子函数.

{

unsignedinti,j;

for(i=0;i

for(j=0;j<200;j++);

}

voidmain()

{

unsignedchard;

d=0xfe;

while

(1)//

{

for(;d>0x7f;)

{

P1=d;

delay(100);//调用延时子函数延时.

d=_crol_(d,1);//循环左移.

}

for(;d<0xfe;)

{

P1=d;

delay(100);

d=_cror_(d,1);//循环右移,让灯倒回去.

}

}

}

数码管流水灯:

#include

#include

voiddelay(unsignedinta)

{

unsignedintb;

b=a;

while(b>0)

b--;

}

voidmain()

{

unsignedinti,j;

unsignedcodeA[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,

0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};//0x3f:

为0,即数码管亮“0”.

P2=0;//翻开数码管的第一个,

P0=j;//对数码管进行赋值

while

(1)

{

i=0;

for(j=0;j<16;j++)

{

P2=i;

P0=A[j];

delay(50000);

i++;

}

}

}

数码管法式二:

#include

voiddelay(unsignedinta)

{

unsignedintb;

b=a;

while(b>0)

b--;

}

voidmain()

{

inti,j;

unsignedcodeA[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,

0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};

P2=0;

P0=j;

while

(1)

{

j=0;

for(i=0;i<8;i++)

{

P2=i;

P0=A[j];

delay(50000);

j++;

}

for(i=7;i>=0;i--)

{

P2=i;

P0=A[j];

delay(50000);

j++;

}

}

}

中断法式一

#include

#include

voiddelay(unsignedinta)

{

unsignedintb;

b=a;

while(b>0)

b--;

}

voidmain()

{

inti,j;

unsignedcodeA[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,

0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};

EA=1;//翻开总中断

EX0=1;//翻开外部中断0.

IT0=0;//选择其为低电平有效有效.当CPU检测到P3.3引脚上呈现有效的中断信号时,中断标识表记标帜IE1(TCON.3)置1,向CPU申请中断.

P2=0;

P0=j;

P1=0xfe;

while

(1)

{

j=0;

for(i=0;i<8;i++)

{

P2=i;

P0=A[j];

delay(50000);

j++;

}

for(i=7;i>=0;i--)

{

P2=i;

P0=A[j];

delay(50000);

j++;

}

}

}

voidexist()interrupt0//中断子法式,其中“0”为外部中断的序号

{

P1=_crol_(P1,1);

delay(50000);

}

按时法式;

include

unsignedinti=0,j=0,t;

unsignedcodea[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,

0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};

voidmain()

{

TMOD=0x01;//对TMOD赋值,以确定T0和T1的工作方式.

TH0=(65536-50000)/256;

TL0=(65536-50000)%256;//按时/计数器的实质是加1计数器(16位),由高8位和低8位两个寄存器组成.对高8位和低8位赋值.

EA=1;//开总中断

ET0=1;//则对IE赋值,开放中断

TR0=1;//使TR0或TR1置位,启动按时/计数器按时.

while

(1)

{

if(t==20)

{

t=0;

if(j==8)

j=0;

if(i==16)

i=0;

P2=j;

P0=a[i];

i++;

j++;

}

}

}

voidtime()interrupt1//按时子法式,其中“1”为按时/计数器序号

{

TH0=(65536-50000)/256;

TL0=(65536-50000)%256;

t++;

}

时间:

二O二一年七月二十九日

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 自然科学 > 物理

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2