簡體   English   中英

函數的隱式聲明和沖突類型-AVR

[英]Implicit declaration of function and conflicting type - AVR

這是我的第一個AVR程序。 生成時,代碼顯示錯誤:“ Encode”隱式聲明的“ Encode”類型沖突

我寫了以下代碼:

#include <avr/io.h>
#include <util/delay.h>
#include <stdlib.h>

#define SegDataPort PORTC
#define SegDataPin PINC
#define SegDataDDR DDRC

#define SegCntrlPort PORTD
#define SegCntrlPin PIND
#define SegCntrlDDR DDRD

int main(void)
{
   SegDataDDR = 0xFF;
   SegCntrlDDR = 0xF3;
   SegCntrlPort = 0xF3;
   SegDataPort = 0x00;
   unsigned char adc_value;
   float volt = adc_value/1023;
   int temp = floor(volt*10 + 0.5);

   SegDataPort = Encode(temp1%10);

   //^^^^ implicit declaration of 'Encode' 

   SegCntrlPort = ~0x01;
   SegDataPort = Encode((temp1/10)%10);
   SegCntrlPort = ~0x02;
   SegDataPort = Encode(temp1/100);
   SegCntrlPort = ~0x04;
}

unsigned char Encode(int digit)
{
   unsigned char val;
   switch(digit)
   {
      case 0 : Val = 0b00111111;
      case 1 : val = 0b00000110;

      /// so on till case 9
   }
   return val;
}

我正在使用ATmega16作為微控制器。 我還添加了更多庫,例如用於底數函數的數學庫和其他庫。 我嘗試將int更改為unsigned int,unsigned char和其他符號,但是它仍然無法正常工作並顯示相同的錯誤。 請幫我。

隱式聲明“編碼”

C需要在使用(調用) 函數之前先聲明定義一個函數。

要么

  • Encode()函數的定義Encode() main()之前
  • main()之前向Encode()添加一個前向聲明。

就是說, floor()是一個函數,在math.h定義,並且在math庫中定義。 要使用它,您需要#include <math.h>並在編譯時與-lm鏈接。


關於此處使用的編程邏輯,

unsigned char adc_value;
float volt = adc_value/1023;
int temp = floor(volt*10 + 0.5);

非常有問題,因為

  1. adc_value未初始化使用,這導致未定義的行為
  2. adc_value的類型為char 將其除以1023的值將始終得到0的結果,因為除法將作為整數除法發生,並且本身不會像您期望的那樣產生 float結果。

我的建議,將代碼塊更改為

int adc_value = <some value to initialize>;  //or some input function
float volt = (float)adc_value/1023;          //enforce floating point division
int temp = floor(volt*10 + 0.5);

第一個錯誤:

 unsigned char adc_value;
 float volt = adc_value/1023;

您將adc_value定義為unsigned char並在下一行中嘗試將其除以1023 ,並將結果分配給float類型變量。 您無法使用C語言完成這些操作。 (更重要的是,您沒有為adc_value分配任何值!它將為零或隨機值)

第二個錯誤:

第二個問題是您在main()調用了Encode函數后就定義了它。 您必須將整個函數移到main()函數之前,或者僅將其原型添加到main()函數之前。

即添加unsigned char Encode(int digit); main()之前

口渴的錯誤:

您嘗試為使用#define聲明的變量分配一些值:

#define SegDataPort PORTC
#define SegDataPin PINC
#define SegDataDDR DDRC

#define SegCntrlPort PORTD
#define SegCntrlPin PIND
#define SegCntrlDDR DDRD

int main(void)
{
   SegDataDDR = 0xFF;
   SegCntrlDDR = 0xF3;
   SegCntrlPort = 0xF3;
   SegDataPort = 0x00;
   .
   .
   .

這也是非法的。 #define定義的那些變量是常量,您不得嘗試在程序主體中對其進行更改。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM