发布网友 发布时间:2022-04-23 10:01
共1个回答
热心网友 时间:2023-07-15 02:44
C语言流水灯程序是学习的基础,一定要搞懂!给你发两个!
(一)
#include<reg51.h> //51系列单片机定义文件
#define uchar unsigned char //定义无符号字符
#define uint unsigned int //定义无符号整数
void delay(uint); //声明延时函数
void main()
{
uint i;
uchar temp;
while(1)
{
temp=0x01;
for(i=0;i<8;i++) //8个流水灯逐个闪动
{
P2=~temp;
delay(500); //调用延时函数
temp<<=1;
}
temp=0x80;
for(i=0;i<8;i++) //8个流水灯反向逐个闪动
{
P2=~temp;
delay(500); //调用延时函数
temp>>=1;
}
temp=0xFE;
for(i=0;i<8;i++) //8个流水灯依次全部点亮
{
P2=temp;
delay(500); //调用延时函数
temp<<=1;
}
temp=0x7F;
for(i=0;i<8;i++) //8个流水灯依次反向全部点亮
{
P2=temp;
delay(500); //调用延时函数
temp>>=1;
}
}
}
void delay(uint a) //延时函数,毫秒级别
{
uint i;
while(--a!=0) //执行空操作循环,其中a为形参,由调用该函数的函数语句提供值
{
for(i=0;i<125;i++);//执行当0加到125时,大约会用掉1ms的时间
}
}
(二)
*****************************以下程序均采用12M晶振*****************************************************/
#include<reg51.h>
#define uint unsigned int
#define uchar unsigned char
#define ulong unsigned long
sbit 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 long
bit 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 long
bit 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 long
bit 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 long
bit 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;
}