标题: [求助]c51开发板 led灯闪烁问题 c语言 [打印本页]

作者: nghngh    时间: 2012-11-12 22:15
标题: [求助]c51开发板 led灯闪烁问题 c语言
#include sbit p=P1^0; void delay(int i) { int j; for(;i>0;i--) for(j=100;j>0;j--); } void man() {int x; for(x=20;x>0;x--) { p=~p; delay(100); } while(1); } 这是我写的代码 烧入单片机灯不亮也不闪 把p=~p改成 p=0;delay(100); p=1;delay(100);灯就可以闪烁 求解答
[此贴子已经被作者于2012-11-12 22:20:01编辑过]

作者: nghngh    时间: 2012-11-12 22:20
这 显示的程序为什么不能换行。。。晕。。
作者: 彭金玉    时间: 2012-11-13 10:13

这位同学你好,你的程序我给你写在这里,列清楚了给你解释下

#incoude<reg52.h>

sbit p=P1^0;

void delay(int i)

{

     for(; i>0;i--)

          for(j=100;j>0;j--);

}

void main()

{

     {

           int x;

           for(x=20;x>0;x--)

            {

                  p=~p;

                  delay(100);

             }

 

     }

      while(1);

}

 

 

 

这是你的程序,在这里,有问题的地方我已经给你标注出来 。头文件你没有写完整。重要的是你的while(1)语句位置放错了。你的这个顺序,在主函数执行一遍了以后到达while(1)这里,就形成了死循环,就会停留在while(1)这里。所以想要让小灯闪烁的话,这里只需要把while(1)去掉即可。令外附上我自己写的程序,你参考一下

 

#include<reg52.h>

#define uchar unsigned char

sbit light=P1^0;

void delay(uchar z)

{

     uchar x,y;

     for(x=z;x>0;x--)

          for(y=110;y>0;y--);

}

void main()

{

     while(1)

     {

           delay(500);

           light=~light;

      }

}


作者: nghngh    时间: 2012-11-13 12:35
你好 谢谢你回复我的帖子 那个头文件是我复制的时候忘记贴了 那个while语句的作用应该是当程序执行完所有语句后不让单片机停工作 昨晚我进过keil软件调试程序后 发现在延时程序中要把第二个参数也就是 j改成 unsigned char类型 程序才会从main()开始执行 否则程序会先执行delay()子程序 为什么呢? 如果不改j的定义类型 也就是最先开始定义的 int j 把 p=~p;改成 p=0;delay(100); p=1;delay(100);程序也是从main()开始执行的 我也会试一下 看while放到前面来看会怎么样




欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1