找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 8161|回复: 14
打印 上一主题 下一主题
收起左侧

[求助]这个简单的流水灯程序怎么了

[复制链接]
跳转到指定楼层
楼主
ID:6799 发表于 2009-6-30 22:18 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

#include <reg51.h>
#define uchar unsigned char
#define uint unsigned int
delay(uint t);
sbit P17=P1^7; //设置发光管的开关
main()
{

 P17=0;
 uchar k=0; //设置流水灯的方向标志位
 uchar leds=0x01;
 P0=~leds;
 while(1)
 {
  delay(1000); 
  if(k==0&&leds==0x80)
   {
     k=1;
   }
  else if(k==1&&leds==0x01) 
   {
    k=0;
   }
  if(k==0)
   {
    leds=leds<<1;
    P0=~leds;
   }
  else
   {
    leds=leds>>1;
    P0=~leds;
   }
  }

}
delay(uint t)
{
 uint i,j;
 for(i=0;1<t;i++);
 for(j=0;j<223;j++);
}

就这个简单流水灯,为什么编译老是说有K,leds没有被定义,编译无法通过,试着调整摆放P17的位置,却能编译成功,这到底为什么啊?谁能给个解释啊?

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:8993 发表于 2009-7-1 08:49 | 只看该作者

#include <at89x51.h>
#define uchar unsigned char
#define uint unsigned int
uchar k,leds;
delay(uint t);
sbit P17=P1^7; //设置发光管的开关
main()
{

 P17=0;
 k=0; //设置流水灯的方向标志位
leds=0x01;
 P0=~leds;
 while(1)
 {
  delay(1000);
  if((k==0)&&(leds==0x80))
   {
     k=1;
   }
  else if((k==1)&&(leds==0x01))
   {
    k=0;
   }
  if(k==0)
   {
    leds=leds<<1;
    P0=~leds;
   }
  else
   {
    leds=leds>>1;
    P0=~leds;
   }
  }

}
delay(uint t)
{
 uint i,j;
 for(i=0;1<t;i++);
 for(j=0;j<223;j++);
}


这样就可以了,什么原因,我也不清楚,请高手回答一下,谢谢??
回复

使用道具 举报

板凳
ID:8993 发表于 2009-7-1 08:50 | 只看该作者

上面的〈at89x51.h>是我随便改的,和错误没关系。

回复

使用道具 举报

地板
ID:8993 发表于 2009-7-1 08:55 | 只看该作者
还有一种方法是把P17=0写在uchar led=0x01的后面也行。
main()
{
 uchar k=0; //设置流水灯的方向标志位
uchar leds=0x01;
 P17=0;
回复

使用道具 举报

5#
ID:14775 发表于 2009-7-1 19:40 | 只看该作者

把定义放在main前面时,定义的量在全局有效

回复

使用道具 举报

6#
ID:6799 发表于 2009-7-4 07:08 | 只看该作者
如果这样的话,编译有错通不过,
#include <reg51.h>
#define uchar unsigned char
#define uint unsigned int
delay(uint t);
sbit P17=P1^7;
main()
{
 P17=0;
 uchar k,leds;
 leds=0x01;
 P0=~leds;
 while(1)

但是如果只是把P17=0放到uchar k,leds;的后面就可以
#include <reg51.h>
#define uchar unsigned char
#define uint unsigned int
delay(uint t);
sbit P17=P1^7;
main()
{
 uchar k,leds;
 leds=0x01;
 P0=~leds;
 P17=0;
 while(1)

真是不得其解啊?想不通为什么...
回复

使用道具 举报

7#
ID:14964 发表于 2009-7-4 19:26 | 只看该作者

在函数中,变量的定义必须放在执行语句前面!

回复

使用道具 举报

8#
ID:15556 发表于 2009-7-24 15:34 | 只看该作者

把定义部分应该放在主函数MAIN之前吧,全局变量

回复

使用道具 举报

9#
ID:15225 发表于 2009-7-27 09:36 | 只看该作者
#define uchar unsigned char
#define uint unsigned int

#include <reg51.h>
。。
。。
。。
这样?
回复

使用道具 举报

10#
ID:15867 发表于 2009-10-26 10:25 | 只看该作者

#include <reg51.h>
#define uchar unsigned char
#define uint unsigned int
delay(uint t);                  //什么意思??
sbit P17=P1^7;

uchar k,leds;

main()
{
 P17=0;
  leds=0x01;
 P0=~leds;
 while(1)

}

回复

使用道具 举报

11#
ID:19203 发表于 2009-12-7 16:29 | 只看该作者

P17=0;赋值语句 unchar leds;变量定义

 

变量定义放在后面肯定不能通过编译。

回复

使用道具 举报

12#
ID:20780 发表于 2010-1-6 17:05 | 只看该作者
定义一般都在每个函数的最前面
回复

使用道具 举报

13#
ID:24218 发表于 2010-6-2 15:50 | 只看该作者
void delay(uint z);
回复

使用道具 举报

14#
ID:5392 发表于 2010-6-30 14:10 | 只看该作者

main里面的

P17=0;是赋值语句

肯定要放在

uchar k=0; //设置流水灯的方向标志位 
uchar leds=0x01;

之后了,这里没有什么全局变量不全局变量的问题

回复

使用道具 举报

15#
ID:24810 发表于 2010-6-30 16:28 | 只看该作者

“为什么编译老是说有K,leds没有被定义”

我也遇到过这样的问题,不过现在不会这样的了。

 

定义变量和常量一定要在要定义,再写执行语句,否则不可能编译通过

把:

P17=0;
 uchar k=0; //设置流水灯的方向标志位
 uchar leds=0x01;

改为

uchar k=0; //设置流水灯的方向标志位
 uchar leds=0x01;
P17=0;
 应该可以的了。

[此贴子已经被作者于2010-6-30 16:28:24编辑过]
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

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