初学51,自己写的LED灯程序,希望和大家交流,互相学习
初学51,自己写的LED灯程序,希望和大家交流,互相学习 谢谢分享啦~~~~~{:smile:} en 顶一个。。。 你最好注明一下功能 hqsdz1 发表于 2012-5-12 16:46 static/image/common/back.gif你最好注明一下功能
是啊,最希望这类分享的帖子,如果有附件,能大概介绍一下附件内容的细节。 顶一个,再接再励! 有神马效果??彗尾??触摸感应?手指灵动? /***************************************************************************************************
*AT89C52RC单片机操作8个led灯
*菜鸟一号
*2012.5.8
****************************************************************************************************/
#include<reg52.h>
//sbit LED=P2^0;
typedef unsigned int uInt;
typedef unsigned char uChar;
void delay(uInt); //the function to delay for a moment
void BothToMid();
void MidToBoth();
void LeftToRight();
void RightToLeft();
void Lighter();
void Darker();
/************************************************main()********************************************/
void main()
{
void BothToMid();
void MidToBoth();
void LeftToRight();
void RightToLeft();
void Lighter();
void Darker();
while(1)
{
BothToMid();
MidToBoth();
LeftToRight();
RightToLeft();
Lighter();
Darker();
delay(500);
}
}
/*************************************************
*function: delay for a moment
*input: time type:unsigned int
*output: none
**************************************************/
void delay(uInt time)
{
uInt i;
uChar j;
for(i=0;i<time;i++)
for(j=0;j<125;j++);
}
/**************************************************
*function: let the led light from left to right one by one
*input: none
*output: none
***************************************************/
void LeftToRight()
{
uChar i;
uChar temp=0xfe;
for(i=0;i<8;i++)
{
P2=temp;
temp=(temp<<1)|1;
delay(500);
}
}
/**************************************************
*function: let the led light fromright to left one by one
*input: none
*output: none
***************************************************/
void RightToLeft()
{
uChar i;
uChar temp=0x7f;
for(i=0;i<8;i++)
{
P2=temp;
temp=(temp>>1)|0x80;
delay(500);
}
}
/***************************************************
*function: let the led light from both edge to middle one by one
*input: none
*output: none
****************************************************/
void BothToMid()
{
uChar temp={0x7e,0xbd,0xdb,0xe7};
uChar i;
for(i=0;i<4;i++)
{
P2=temp;
delay(1000);
}
}
/****************************************************
*function: let the led light from middle to both edge one by one
*input: none
*output: none
*****************************************************/
void MidToBoth()
{
uChar temp={0xe7,0xdb,0xbd,0x7e,};
uChar j;
for(j=0;j<4;j++)
{
P2=temp;
delay(1000);
}
}
/*****************************************************
*function :wait for a moment to let the led become lighter or darker
*input: timer type:unsigned char
*output: none
*******************************************************/
void delay1(uChar timer)
{
uChar i;
while(timer--)
{
for(i=0;i<1;i++);
}
}
/******************************************************
*function:let the led become lighter and lighter
*input:none
*output:none
********************************************************/
void Lighter()
{
void delay1(uChar);
uChar maxNum;
uChar timer;
unsigned char j;
P2=0xff;
maxNum=250;
timer=0;
for(j=0;j<maxNum;j++)
{
timer++;
delay1(timer);
P2=0xff;
timer=~timer;
delay1(timer);
P2=0x00;
timer=~timer;
}
}
/*********************************************************
*function:let the led become darker and darker
*input:none
*output:none
**********************************************************/
void Darker()
{
void delay1(uChar);
uChar maxNum;
uChar timer;
unsigned char j;
P2=0x00;
maxNum=250;
timer=0;
for(j=0;j<maxNum;j++)
{
timer++;
delay1(timer);
P2=0x00;
timer=~timer;
delay1(timer);
P2=0xff;
timer=~timer;
}
}
楼主的代码格式还是很好看的~~~{:2_27:} 楼主初学51就能写出这样的程序,看来c语言功底不浅啊!程序格式也很到位....我很好奇楼主是在学51看那些比较好的资料啊 楼主最后那两段使LED渐亮渐灭的代码很巧啊,学习了不少。{:smile:} 多思考,还是不错的 無智 发表于 2012-5-12 17:23 static/image/common/back.gif
是啊,最希望这类分享的帖子,如果有附件,能大概介绍一下附件内容的细节。 ...
赞同!!!顶你! /***************************************************************************************************
*AT89C52RC单片机操作8个led灯
*菜鸟一号
*2012.5.8
****************************************************************************************************/
#include<reg52.h>
//sbit LED=P2^0;
typedef unsigned int uInt;
typedef unsigned char uChar;
void delay(uInt); //the function to delay for a moment
void BothToMid();
void MidToBoth();
void LeftToRight();
void RightToLeft();
void Lighter();
void Darker();
/************************************************main()********************************************/
void main()
{
void BothToMid();
void MidToBoth();
void LeftToRight();
void RightToLeft();
void Lighter();
void Darker();
while(1)
{
BothToMid();
MidToBoth();
LeftToRight();
RightToLeft();
Lighter();
Darker();
delay(500);
}
}
/*************************************************
*function: delay for a moment
*input: time type:unsigned int
*output: none
**************************************************/
void delay(uInt time)
{
uInt i;
uChar j;
for(i=0;i<time;i++)
for(j=0;j<125;j++);
}
/**************************************************
*function: let the led light from left to right one by one
*input: none
*output: none
***************************************************/
void LeftToRight()
{
uChar i;
uChar temp=0xfe;
for(i=0;i<8;i++)
{
P2=temp;
temp=(temp<<1)|1;
delay(500);
}
}
/**************************************************
*function: let the led light fromright to left one by one
*input: none
*output: none
***************************************************/
void RightToLeft()
{
uChar i;
uChar temp=0x7f;
for(i=0;i<8;i++)
{
P2=temp;
temp=(temp>>1)|0x80;
delay(500);
}
}
/***************************************************
*function: let the led light from both edge to middle one by one
*input: none
*output: none
****************************************************/
void BothToMid()
{
uChar temp={0x7e,0xbd,0xdb,0xe7};
uChar i;
for(i=0;i<4;i++)
{
P2=temp;
delay(1000);
}
}
/****************************************************
*function: let the led light from middle to both edge one by one
*input: none
*output: none
*****************************************************/
void MidToBoth()
{
uChar temp={0xe7,0xdb,0xbd,0x7e,};
uChar j;
for(j=0;j<4;j++)
{
P2=temp;
delay(1000);
}
}
/*****************************************************
*function :wait for a moment to let the led become lighter or darker
*input: timer type:unsigned char
*output: none
*******************************************************/
void delay1(uChar timer)
{
uChar i;
while(timer--)
{
for(i=0;i<1;i++);
}
}
/******************************************************
*function:let the led become lighter and lighter
*input:none
*output:none
********************************************************/
void Lighter()
{
void delay1(uChar);
uChar maxNum;
uChar timer;
unsigned char j;
P2=0xff;
maxNum=250;
timer=0;
for(j=0;j<maxNum;j++)
{
timer++;
delay1(timer);
P2=0xff;
timer=~timer;
delay1(timer);
P2=0x00;
timer=~timer;
}
}
/*********************************************************
*function:let the led become darker and darker
*input:none
*output:none
**********************************************************/
void Darker()
{
void delay1(uChar);
uChar maxNum;
uChar timer;
unsigned char j;
P2=0x00;
maxNum=250;
timer=0;
for(j=0;j<maxNum;j++)
{
timer++;
delay1(timer);
P2=0x00;
timer=~timer;
delay1(timer);
P2=0xff;
timer=~timer;
}
}
这代码格式挺好的,细节很重要啊,相信你! 不错啊!! hqsdz1 发表于 2012-5-12 16:46 static/image/common/back.gif
你最好注明一下功能
谢谢,我下次一定注意,把注释写的详细一点。{:handshake:} 菜鸟一号 发表于 2012-5-18 23:45 static/image/common/back.gif
谢谢,我下次一定注意,把注释写的详细一点。
我说的不是注释,是你发的帖子里直接说明你的led灯是什么效果 hqsdz1 发表于 2012-5-21 12:15 static/image/common/back.gif
我说的不是注释,是你发的帖子里直接说明你的led灯是什么效果
哦!明白了,下次一定改正。 lryxr2507 发表于 2012-5-12 18:31 static/image/common/back.gif
顶一个,再接再励!
谢谢!{:tongue:} hqk1992 发表于 2012-5-12 23:54 static/image/common/back.gif
楼主最后那两段使LED渐亮渐灭的代码很巧啊,学习了不少。
过奖了,我也是别人教的,多吸取别人的东西进步会快一点。 不错,代码看的很舒服…注释很重要,我自己写的放个几个月我自己都不知道啥意思了。 谢谢分享{:3_61:} 楼主 以后写程序时候 程序内忌讳出现Delay字样,Delay时主程序停下来什么也不执行光等待了,你这只是个灯,要是在加上别的东西部麻烦了,程序什么事也不做了光停下来了。 楼主的程序写的还是挺漂亮的!
页:
[1]