2015-06-02 45 views
0

這是我第一個AVR程序。在建設中,代碼示值誤差: 衝突的類型'編碼「編碼」隱式聲明函數和衝突類型 - AVR

我寫了下面的代碼 隱含聲明:

#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等,但它仍然無法正常工作並顯示相同的錯誤。 請幫幫我。

+0

@ mathematician1975 AFAIK,'PORTx'是可用作正常c變量的AVR特定寄存器。如果我錯了,請糾正我。 –

回答

2

implicit declaration of 'Encode'

C的功能必須要麼聲明定義已經使用之前(被呼叫)。

要麼

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

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


關於這裏使用的programmic邏輯,

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

是相當有問題的,因爲

  1. adc_value使用未初始化,從而導致undefined behaviour
  2. adc_valuechar的類型。由1023值除以總會給的0結果作爲劃分將發生作爲整數除法並不會產生一個float結果本身,你migh預料。

我的建議,該代碼塊改爲

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); 
+0

它工作。謝謝。但是你能告訴我爲什麼這個函數必須在C之前被調用。因爲這不是針對C++或者Java或者Python – Yash

+0

@Yash之前調用過的嗎?不,這是另一種方式,它必須在_called_之前_declared_。你可以點擊[這裏](http://stackoverflow.com/q/2575153/2173917)瞭解更多信息。 –

0

的第一個錯誤:

unsigned char adc_value; 
float volt = adc_value/1023; 

您defind adc_value作爲unsigned char並在下一行,你試圖將其分攤1023並將結果分配給float類型變量。你不能在C語言中執行這些操作。 (超過你沒有分配任何價值adc_value!這將是零或隨機值)

第二個錯誤:

你的第二個問題是,你調用它main()後定義了Encode功能。您必須在main()函數之前移動整個函數,或者僅在main()函數之前添加其原型。

即前main()

口渴誤增加unsigned char Encode(int digit);

您嘗試一些值分配給你#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定義的變量是恆定的,你不能試圖在程序體中改變它們。

+0

adc_value是我的模擬輸入。所以,它的範圍從0到1023. – Yash

+0

在所有應有的尊重,你會介意再次檢查你的第三點? –

+0

@SouravGhosh我錯了嗎? – Abraham