搜索
bottom↓
回复: 22

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

[复制链接]

出0入0汤圆

发表于 2012-5-11 18:06:01 | 显示全部楼层 |阅读模式
初学51,自己写的LED灯程序,希望和大家交流,互相学习

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x

阿莫论坛20周年了!感谢大家的支持与爱护!!

知道什么是神吗?其实神本来也是人,只不过神做了人做不到的事情 所以才成了神。 (头文字D, 杜汶泽)

出0入0汤圆

发表于 2012-5-12 16:15:18 | 显示全部楼层
谢谢分享啦~~~~~

出0入0汤圆

发表于 2012-5-12 16:18:47 | 显示全部楼层
en 顶一个。。。

出0入0汤圆

发表于 2012-5-12 16:46:31 | 显示全部楼层
你最好注明一下功能

出0入0汤圆

发表于 2012-5-12 17:23:19 | 显示全部楼层
hqsdz1 发表于 2012-5-12 16:46
你最好注明一下功能

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

出0入0汤圆

发表于 2012-5-12 18:31:03 | 显示全部楼层
顶一个,再接再励!

出0入12汤圆

发表于 2012-5-12 19:53:24 | 显示全部楼层
有神马效果??彗尾??触摸感应?手指灵动?

出0入0汤圆

发表于 2012-5-12 20:05:49 | 显示全部楼层
  1. /***************************************************************************************************
  2. *AT89C52RC单片机操作8个led灯
  3. *菜鸟一号
  4. *2012.5.8
  5. ****************************************************************************************************/
  6. #include<reg52.h>
  7. //sbit LED=P2^0;
  8. typedef unsigned int uInt;
  9. typedef unsigned char uChar;

  10. void delay(uInt);            //the function to delay for a moment
  11. void BothToMid();
  12. void MidToBoth();
  13. void LeftToRight();
  14. void RightToLeft();
  15. void Lighter();
  16. void Darker();
  17. /************************************************main()********************************************/
  18. void main()
  19. {
  20.   void BothToMid();
  21.   void MidToBoth();
  22.   void LeftToRight();
  23.   void RightToLeft();
  24.   void Lighter();
  25.   void Darker();
  26.   while(1)
  27.   {
  28.    
  29.     BothToMid();
  30.     MidToBoth();
  31.         LeftToRight();
  32.         RightToLeft();
  33.         Lighter();
  34.         Darker();
  35.         delay(500);
  36.        
  37.   }
  38. }
  39. /*************************************************
  40. *function: delay for a moment
  41. *input:    time     type:unsigned int
  42. *output:   none
  43. **************************************************/
  44. void delay(uInt time)
  45. {
  46.   uInt i;
  47.   uChar j;
  48.   for(i=0;i<time;i++)
  49.      for(j=0;j<125;j++);
  50. }
  51. /**************************************************
  52. *function: let the led light from left to right one by one
  53. *input:    none
  54. *output:   none
  55. ***************************************************/
  56. void LeftToRight()
  57. {
  58.     uChar i;
  59.     uChar temp=0xfe;
  60.         for(i=0;i<8;i++)
  61.         {
  62.          P2=temp;
  63.          temp=(temp<<1)|1;
  64.          delay(500);
  65.          }
  66. }
  67. /**************************************************
  68. *function: let the led light from  right to left one by one
  69. *input:    none
  70. *output:   none
  71. ***************************************************/
  72. void RightToLeft()
  73. {
  74.     uChar i;
  75.     uChar temp=0x7f;
  76.         for(i=0;i<8;i++)
  77.         {
  78.          P2=temp;
  79.          temp=(temp>>1)|0x80;
  80.          delay(500);
  81.          }
  82. }
  83. /***************************************************
  84. *function: let the led light from both edge to middle one by one
  85. *input:    none
  86. *output:   none
  87. ****************************************************/
  88. void BothToMid()
  89. {
  90.   uChar temp[4]={0x7e,0xbd,0xdb,0xe7};
  91.   uChar i;
  92.   for(i=0;i<4;i++)
  93.   {
  94.     P2=temp[i];
  95.         delay(1000);
  96.   }
  97. }
  98. /****************************************************
  99. *function: let the led light from middle to both edge one by one
  100. *input:    none
  101. *output:   none
  102. *****************************************************/
  103. void MidToBoth()
  104. {
  105.   
  106.   uChar temp[4]={0xe7,0xdb,0xbd,0x7e,};
  107.   uChar j;
  108.   for(j=0;j<4;j++)
  109.   {
  110.     P2=temp[j];
  111.         delay(1000);
  112.   }
  113. }
  114. /*****************************************************
  115. *function :wait for a moment to let the led become lighter or darker
  116. *input: timer   type:unsigned char
  117. *output: none
  118. *******************************************************/
  119. void delay1(uChar timer)
  120. {
  121.   uChar i;
  122.   while(timer--)
  123.   {
  124.    for(i=0;i<1;i++);
  125.   }
  126. }
  127. /******************************************************
  128. *function:let the led become lighter and lighter
  129. *input:none
  130. *output:none
  131. ********************************************************/
  132. void Lighter()
  133. {
  134. void delay1(uChar);
  135. uChar maxNum;
  136. uChar timer;
  137. unsigned char j;
  138. P2=0xff;
  139. maxNum=250;
  140. timer=0;

  141. for(j=0;j<maxNum;j++)
  142. {
  143.    timer++;
  144.    delay1(timer);
  145.    P2=0xff;
  146.    timer=~timer;
  147.    delay1(timer);
  148.    P2=0x00;
  149.    timer=~timer;
  150.    
  151. }
  152. }
  153. /*********************************************************
  154. *function:let the led become darker and darker
  155. *input:none
  156. *output:none
  157. **********************************************************/
  158. void Darker()
  159. {
  160. void delay1(uChar);
  161. uChar maxNum;
  162. uChar timer;
  163. unsigned char j;
  164. P2=0x00;
  165. maxNum=250;
  166. timer=0;

  167. for(j=0;j<maxNum;j++)
  168. {
  169.    timer++;
  170.    delay1(timer);
  171.    P2=0x00;
  172.    timer=~timer;
  173.    delay1(timer);
  174.    P2=0xff;
  175.    timer=~timer;
  176.    
  177. }
  178. }
复制代码
楼主的代码格式还是很好看的~~~

出0入0汤圆

发表于 2012-5-12 22:26:27 | 显示全部楼层
楼主初学51就能写出这样的程序,看来c语言功底不浅啊!程序格式也很到位....我很好奇楼主是在学51看那些比较好的资料啊

出0入0汤圆

发表于 2012-5-12 23:54:15 | 显示全部楼层
楼主最后那两段使LED渐亮渐灭的代码很巧啊,学习了不少。

出0入0汤圆

发表于 2012-5-13 00:02:41 | 显示全部楼层
多思考,还是不错的

出0入0汤圆

发表于 2012-5-13 18:58:04 | 显示全部楼层
無智 发表于 2012-5-12 17:23
是啊,最希望这类分享的帖子,如果有附件,能大概介绍一下附件内容的细节。 ...

赞同!!!顶你!

出0入0汤圆

发表于 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 from  right 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[4]={0x7e,0xbd,0xdb,0xe7};
  uChar i;
  for(i=0;i<4;i++)
  {
    P2=temp[i];
        delay(1000);
  }
}
/****************************************************
*function: let the led light from middle to both edge one by one
*input:    none
*output:   none
*****************************************************/
void MidToBoth()
{
  
  uChar temp[4]={0xe7,0xdb,0xbd,0x7e,};
  uChar j;
  for(j=0;j<4;j++)
  {
    P2=temp[j];
        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;
   
}
}
这代码格式挺好的,细节很重要啊,相信你!

出0入0汤圆

发表于 2012-5-15 12:48:05 | 显示全部楼层
不错啊!!

出0入0汤圆

 楼主| 发表于 2012-5-18 23:45:13 | 显示全部楼层
hqsdz1 发表于 2012-5-12 16:46
你最好注明一下功能

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

出0入0汤圆

发表于 2012-5-21 12:15:53 | 显示全部楼层
菜鸟一号 发表于 2012-5-18 23:45
谢谢,我下次一定注意,把注释写的详细一点。

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

出0入0汤圆

 楼主| 发表于 2012-5-21 20:15:27 | 显示全部楼层
hqsdz1 发表于 2012-5-21 12:15
我说的不是注释,是你发的帖子里直接说明你的led灯是什么效果

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

出0入0汤圆

 楼主| 发表于 2012-5-21 20:16:15 | 显示全部楼层
lryxr2507 发表于 2012-5-12 18:31
顶一个,再接再励!

谢谢!

出0入0汤圆

 楼主| 发表于 2012-5-21 20:17:31 | 显示全部楼层
hqk1992 发表于 2012-5-12 23:54
楼主最后那两段使LED渐亮渐灭的代码很巧啊,学习了不少。

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

出0入0汤圆

发表于 2012-5-21 20:28:59 | 显示全部楼层
不错,代码看的很舒服…注释很重要,我自己写的放个几个月我自己都不知道啥意思了。

出0入0汤圆

发表于 2012-5-21 20:37:26 | 显示全部楼层
谢谢分享

出0入0汤圆

发表于 2012-5-21 23:07:25 | 显示全部楼层
楼主 以后写程序时候 程序内忌讳出现Delay字样,Delay时主程序停下来什么也不执行光等待了,你这只是个灯,要是在加上别的东西部麻烦了,程序什么事也不做了光停下来了。

出0入0汤圆

发表于 2012-5-22 01:03:28 | 显示全部楼层
楼主的程序写的还是挺漂亮的!
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片。注意:要连续压缩2次才能满足要求!!】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-7-24 02:21

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表