菜鸟一号 发表于 2012-5-11 18:06:01

初学51,自己写的LED灯程序,希望和大家交流,互相学习

初学51,自己写的LED灯程序,希望和大家交流,互相学习

xinhoujue 发表于 2012-5-12 16:15:18

谢谢分享啦~~~~~{:smile:}

J-kai 发表于 2012-5-12 16:18:47

en 顶一个。。。

hqsdz1 发表于 2012-5-12 16:46:31

你最好注明一下功能

無智 发表于 2012-5-12 17:23:19

hqsdz1 发表于 2012-5-12 16:46 static/image/common/back.gif
你最好注明一下功能

是啊,最希望这类分享的帖子,如果有附件,能大概介绍一下附件内容的细节。

lryxr2507 发表于 2012-5-12 18:31:03

顶一个,再接再励!

skynet 发表于 2012-5-12 19:53:24

有神马效果??彗尾??触摸感应?手指灵动?

goolloo 发表于 2012-5-12 20:05:49

/***************************************************************************************************
*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:}

天涯行客 发表于 2012-5-12 22:26:27

楼主初学51就能写出这样的程序,看来c语言功底不浅啊!程序格式也很到位....我很好奇楼主是在学51看那些比较好的资料啊

hqk1992 发表于 2012-5-12 23:54:15

楼主最后那两段使LED渐亮渐灭的代码很巧啊,学习了不少。{:smile:}

chenxujiaoyang 发表于 2012-5-13 00:02:41

多思考,还是不错的

niukai 发表于 2012-5-13 18:58:04

無智 发表于 2012-5-12 17:23 static/image/common/back.gif
是啊,最希望这类分享的帖子,如果有附件,能大概介绍一下附件内容的细节。 ...

赞同!!!顶你!

niukai 发表于 2012-5-13 18:59:31

/***************************************************************************************************
*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;
   
}
}
这代码格式挺好的,细节很重要啊,相信你!

jqbkl007 发表于 2012-5-15 12:48:05

不错啊!!

菜鸟一号 发表于 2012-5-18 23:45:13

hqsdz1 发表于 2012-5-12 16:46 static/image/common/back.gif
你最好注明一下功能

谢谢,我下次一定注意,把注释写的详细一点。{:handshake:}

hqsdz1 发表于 2012-5-21 12:15:53

菜鸟一号 发表于 2012-5-18 23:45 static/image/common/back.gif
谢谢,我下次一定注意,把注释写的详细一点。

我说的不是注释,是你发的帖子里直接说明你的led灯是什么效果

菜鸟一号 发表于 2012-5-21 20:15:27

hqsdz1 发表于 2012-5-21 12:15 static/image/common/back.gif
我说的不是注释,是你发的帖子里直接说明你的led灯是什么效果

哦!明白了,下次一定改正。

菜鸟一号 发表于 2012-5-21 20:16:15

lryxr2507 发表于 2012-5-12 18:31 static/image/common/back.gif
顶一个,再接再励!

谢谢!{:tongue:}

菜鸟一号 发表于 2012-5-21 20:17:31

hqk1992 发表于 2012-5-12 23:54 static/image/common/back.gif
楼主最后那两段使LED渐亮渐灭的代码很巧啊,学习了不少。

过奖了,我也是别人教的,多吸取别人的东西进步会快一点。

yanglong5918 发表于 2012-5-21 20:28:59

不错,代码看的很舒服…注释很重要,我自己写的放个几个月我自己都不知道啥意思了。

liuchangkui 发表于 2012-5-21 20:37:26

谢谢分享{:3_61:}

STT 发表于 2012-5-21 23:07:25

楼主 以后写程序时候 程序内忌讳出现Delay字样,Delay时主程序停下来什么也不执行光等待了,你这只是个灯,要是在加上别的东西部麻烦了,程序什么事也不做了光停下来了。

無智 发表于 2012-5-22 01:03:28

楼主的程序写的还是挺漂亮的!
页: [1]
查看完整版本: 初学51,自己写的LED灯程序,希望和大家交流,互相学习