C++ 函数和冲突类型的隐式声明-AVR
这是我的第一个AVR程序。生成时,代码显示错误: “Encode”的冲突类型 “Encode”的隐式声明 我编写了以下代码:C++ 函数和冲突类型的隐式声明-AVR,c++,c,compiler-errors,avr,atmega16,C++,C,Compiler Errors,Avr,Atmega16,这是我的第一个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 #d
#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;
}
#包括
#包括
#包括
#定义SegDataPort端口
#定义segdatapinc
#定义SegDataDDR DDRC
#定义SegCntrlPort端口d
#定义segcntrlpind
#定义SegCntrlDDR DDRD
内部主(空)
{
SegDataDDR=0xFF;
SegCntrlDDR=0xF3;
SegCntrlPort=0xF3;
SegDataPort=0x00;
无符号字符adc_值;
浮动电压=adc_值/1023;
内部温度=地板(电压*10+0.5);
SegDataPort=编码(temp1%10);
//^^^^“Encode”的隐式声明
SegCntrlPort=~0x01;
SegDataPort=编码((temp1/10)%10);
SegCntrlPort=~0x02;
SegDataPort=编码(temp1/100);
SegCntrlPort=~0x04;
}
无符号字符编码(整数位)
{
无符号字符val;
开关(数字)
{
案例0:Val=0B0011111;
案例1:val=0b00000110;
///一直到案例9
}
返回val;
}
我使用ATmega16作为微控制器。我还添加了更多的库,如楼层函数数学等。我曾尝试将int改为unsigned int、unsigned char和其他,但仍然不起作用,并显示相同的错误。
请帮帮我
“Encode”的隐式声明
在C
中,函数在使用(调用)之前需要声明或定义
或者
- 将
函数的定义移到Encode()
main()
- 在
之前向main()
添加转发声明Encode()
floor()
是一个函数,在math.h
中定义,在math
库中定义。要使用它,您需要在编译时包含#include
并链接到-lm
关于这里使用的程序逻辑
unsigned char adc_value;
float volt = adc_value/1023;
int temp = floor(volt*10 + 0.5);
问题很严重,因为
adc\u值
未初始化使用,这会导致adc\u值
的类型为char
。将其除以1023
值将始终得到0
的结果,因为该除法将作为整数除法进行,并且不会像您预期的那样产生浮点
结果本身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);
intadc_值=//或者一些输入函数
浮动电压=(浮动)adc_值/1023//强制浮点除法
内部温度=地板(电压*10+0.5);
第一个错误:
unsigned char adc_value;
float volt = adc_value/1023;
您将adc\u值定义为无符号字符
,并在下一行尝试将其除以1023
,并将结果分配给float
类型变量。在C
语言中无法执行这些操作。(更多信息,您没有为adc\u值
分配任何值!它将是零或随机值)
第二个错误:
unsigned char adc_value;
float volt = adc_value/1023;
第二个问题是在main()
中调用Encode
函数后定义了该函数。您必须将整个函数移到main()
函数之前,或者仅将其原型添加到main()
函数之前
i、 e添加无符号字符编码(整数位数)代码>在main()之前
口渴错误:
unsigned char adc_value;
float volt = adc_value/1023;
您试图为使用#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
定义的变量是常量,您不能试图在程序体中更改它们。它起作用了。非常感谢。但是你能告诉我为什么在C.之前必须调用函数,因为C++和java不是这样的Python@Yash以前打过电话吗?不,这是另一种方式,它必须在调用之前声明。您可以查看更多信息。@MathematicalIAN1975 AFAIK,PORTx
是AVR特定的寄存器,可以用作普通c变量。如果我错了,请纠正我。adc_值是我的模拟输入。所以,它的范围从0到1023。恕我直言,你介意再次检查你的第三点吗?我不是很确定,没有AVR的经验。请验证一次。@Yash其定义为无符号字符
,因此其值可以从0
到255
。(它有8位长度。)为什么不将它定义为int
?那么,我怎么能说我的微控制器的端口是输入还是输出呢。我在冲浪时只找到了这条路。