C语言流水灯程序是学习的基础,一定要搞懂!给你发两个!
(一)
#include
#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
#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
#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
#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
#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
#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;
}
#include
#define uchar unsigned char //数据类型的宏定义
#define uint unsigned int //数据类型的宏定义
uchar code table[]={0xfe, 0xfd, 0xfb, 0xf7, 0xef, 0xdf, 0xbf, 0x7f}; //定义八个LED灯的工作状态
void delay(uint);
void main()
{
uchar a;
while(1) //死循环,让程序不断的重复
{
for(a=0; a<8; a++) //for循环让程序跑八次
{
delay(50);
P2=table[a]; //分八次分别让8个LED灯点亮
}
}
}
/******************************************
函数功能:延时一段时间
*******************************************/
void delay(uint i)
{
unsigned int j;
for(; i>0; i--)
for(j=0; j<125; j++)
{
;
}
}
#include
#include
void delayms(unsigned char ms)
// 延时子程序
{
unsigned char i;
while(ms--)
{
for(i = 0; i < 120; i++);
}
}
flash()
{ P0=0x00;
delayms(250);
P0=0xff;
delayms(250);
P0=0x00;
delayms(250);
P0=0xff;
delayms(250);
}
main()
{
unsigned char LED;
LED = 0xfe;
P0 = LED;
while(1)
{
LED = _crol_(LED,1); //循环右移1位,点亮下一个LED
P0 = LED;
delayms(250);
if(LED==0x7f)
{ flash();
P0=0x7f;
while(P0!=0xfe)
{
P0=P0<<7|P0>>1;//循环左移
delayms(250);
}
flash();
}
}
}