打印
[开发工具]

函数前后位置不同编译出错

[复制链接]
919|2
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
poorhack|  楼主 | 2015-1-16 16:27 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
下面是一段pic开发板上自带的一段代码
是关于1602 4数据线的代码
两个函数void WriteNibble(UINT8_T Cmd,UINT8_T Dat);和void WriteByte(UINT8_T Cmd,UINT8_T Dat);都写在了lcd.c文档里。

从功能上看void WriteNibble(UINT8_T Cmd,UINT8_T Dat)函数是void WriteByte(UINT8_T Cmd,UINT8_T Dat)的子函数
在编译的时候如果将void WriteNibble(UINT8_T Cmd,UINT8_T Dat)放在void WriteByte(UINT8_T Cmd,UINT8_T Dat)后面就出错,提示子函数没有声明类型,默认为int,然后哇啦哇啦还有一堆错误。
但是把子函数放在前面就一切正常。
我知道在main函数里如果调用某段程序需要在它前面声明,难道其它函数也这样吗?但是如果是这样,接下来的LCDInit()函数调用LCDClear()函数时,二者的位置不管怎么放置都不会出错,这是个什么情况啊?
/******************************************************
******************************************************/
void WriteNibble(UINT8_T Cmd,UINT8_T Dat)
{
  UINT8_T buf;

  if (Cmd)                                          // If Command To be written
    LCD_RS = CLEAR;
  else                                         // Otherwise we are writing data
    LCD_RS = SET;                              // Set register select according to specified
  LCD_RW = WRITE;                                   // Set write mode
  LCD_EN = ENABLE;                                         // Disable LCD

  LCD_DAT0 = LCD_DAT1 = LCD_DAT2 = LCD_DAT3 = CLEAR;    // Clear the data lines
  NOP();                                                               // Small delay
  NOP();

  buf =LATD;                                              // Getting the high nibble
  buf &= 0xF0;                                      // Clear the low nibble
  LATD = buf | (Dat & 0x0F);                                          // Combine & write back to the data lines
  NOP();                                                                  // Give the data a small delay to settle
  NOP();

  LCD_EN = DISABLE;                                                    // Enable LCD => The data is taken now
}
/************************************************************

************************************************************/
void WriteByte(UINT8_T Cmd,UINT8_T Dat)
{
  WriteNibble(Cmd,Dat >> 4);            // Output the high nibble to the LCD
  WriteNibble(Cmd,Dat);                 // Now send the low nibble
}

沙发
ayb_ice| | 2015-1-16 17:17 | 只看该作者
放在前面,会先被编译,编译器已经知道原型了,所以不需要声明。

但最好声明一下,这样适应性更强。

使用特权

评论回复
板凳
huarana| | 2015-1-16 17:22 | 只看该作者
最好全部声明。

不声明的话 后面函数调用前面函数不会出错。

前面的调用后面的就会出问题。

使用特权

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

本版积分规则

40

主题

198

帖子

6

粉丝