打印

这段程序不能编译

[复制链接]
2196|11
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
break001|  楼主 | 2012-5-11 19:31 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
#define uchar unsigned char

void write_byte24c02(uchar date) //写字节
{
sj=0;
  uchar i,temp;
temp=date;   
for(i=0;i<8;i++)
{
  temp=temp<<1;
  scl=0;
  delay();
  sda=CY;
  delay();
  scl=1;
  delay();
}
scl=0;
delay();
sda=1;
delay();   
sj=1;
}
uchar read_byte24c02()  //读字节
{
sj=0;
uchar j,l;
scl=0;
delay();
sda=1;
delay();
for(j=0;j<8;j++)
{
  scl=1;
  delay();
  l=(l<<1)|sda;
  scl=0;
  delay();
}
return l;
sj=1;
}

不知道为什么不能编译
出现如下:
compiling main.c...
MAIN.C(64): error C141: syntax error near 'unsigned'
MAIN.C(64): error C202: 'i': undefined identifier
MAIN.C(65): error C202: 'temp': undefined identifier
MAIN.C(66): error C202: 'i': undefined identifier
MAIN.C(68): error C202: 'temp': undefined identifier
MAIN.C(86): error C141: syntax error near 'unsigned'
MAIN.C(86): error C202: 'j': undefined identifier
MAIN.C(91): error C202: 'j': undefined identifier
MAIN.C(95): error C202: 'l': undefined identifier
MAIN.C(99): error C202: 'l': undefined identifier
Target not created

相关帖子

沙发
lyjian| | 2012-5-11 19:43 | 只看该作者
一大堆符号都没定义

使用特权

评论回复
板凳
break001|  楼主 | 2012-5-11 19:56 | 只看该作者
我定义过了,还是不明白出现的问题在哪?

使用特权

评论回复
地板
lyjian| | 2012-5-11 19:59 | 只看该作者
数据类型重新定义用typedef

使用特权

评论回复
5
ningling_21| | 2012-5-11 21:12 | 只看该作者
LZ最好多看看c语言基础的书。。。
1,所有的变量都要先定义再使用;
2,位地址scl, sda 也要先定义,再使用;
3,该包含的头文件要包含;
4,子程序要有定义和声明,才能调用;

以下为改过的,可以编译通过,但有警告。
#include "reg52.h"

#define uchar unsigned char
sbit scl=P2^0;
sbit sda=P2^1;

void write_byte24c02(uchar date) //写字节
{
        uchar sj=0;
  uchar i,temp;
temp=date;   
for(i=0;i<8;i++)
{
  temp=temp<<1;
  scl=0;
  delay();
  sda=CY;
  delay();
  scl=1;
  delay();
}
scl=0;
delay();
sda=1;
delay();   
sj=1;
}
uchar read_byte24c02()  //读字节
{
uchar sj=0;
uchar j,l;
scl=0;
delay();
sda=1;
delay();
for(j=0;j<8;j++)
{
  scl=1;
  delay();
  l=(l<<1)|sda;
  scl=0;
  delay();
}
return l;
sj=1;
}

使用特权

评论回复
6
lbygtwx| | 2012-5-11 21:13 | 只看该作者
本帖最后由 lbygtwx 于 2012-5-11 21:15 编辑

void write_byte24c02(uchar date) //写字节
{
sj=0;
  uchar i,temp;
改为
void write_byte24c02(uchar date) //写字节
{
uchar i,temp;
sj=0;
试试。

使用特权

评论回复
7
changdacekong| | 2012-5-11 21:55 | 只看该作者
变量声明必须放在函数的第一行:
void write_byte24c02(uchar date) //写字节
{
    uchar i,temp;
    sj=0;
     .......
    sj=1;
}

使用特权

评论回复
8
changdacekong| | 2012-5-11 22:01 | 只看该作者
5楼的程序也有点问题,读子节return l;应放在函数最后一行,否则程序执行到return一行后就停止了,跳出本函数,return后的sj=1;就不会被执行。

使用特权

评论回复
9
gbz7640088| | 2012-5-11 22:07 | 只看该作者
基础很重要,预处理都没。

使用特权

评论回复
10
wukunshan| | 2012-5-11 23:48 | 只看该作者
编译信息提示那么变量没有定义,看不出来吗?走一走,看一看,学一学,灌一灌。

使用特权

评论回复
11
break001|  楼主 | 2012-5-12 08:04 | 只看该作者
谢谢大家!已经找到问题的地方了

使用特权

评论回复
12
hys0401| | 2012-5-12 15:30 | 只看该作者
你这个程序即使编译通过了都有可能有时运行正确有时候不正确。

好吧,算俺神棍了……

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

0

主题

9

帖子

0

粉丝