[英]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);
非常有問題,因為
adc_value
未初始化使用,這導致未定義的行為 。 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.