ATmega 328p 的静态函数表现很奇怪
我正在尝试使用 ATmega 328p(Arduino 以太网)来控制 Wiznet W5100 以太网控制器。我正在使用的库已经过测试并且可以工作,但对我来说却不行。以下是我跟踪问题的原因:
我正在尝试读取两个寄存器,它们告诉我 W5100 已接收到多少字节的数据:
uint16_t readTest()
{
uint16_t res = W5100.read(0x0426);
res = (res << 8) + W5100.read(0x0427);
return res;
}
但是即使我没有发送任何数据,该函数还是从 main 调用的(),将返回 1024 而不是 0。现在奇怪的部分来了:如果我像这样添加 static 关键字:
static uint16_t readTest()
{
uint16_t res = W5100.read(0x0426);
res = (res << 8) + W5100.read(0x0427);
return res;
}
那么该函数会突然返回 0,正如它应该的那样! 我也可以将其更改为:
uint16_t readTest()
{
return (W5100.read(0x0426) << 8) + W5100.read(0x0427);
}
这一定是编译器在搞乱我。我真的不明白。为了进行编译,我使用标准 Arduino IDE,命令如下所示:
avr-g++ -c -g -Os -w -fno-exceptions -ffunction-sections -fdata-sections -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=22 -I/usr/share/arduino/hardware/arduino/cores/arduino -I/usr/share/arduino/libraries/SoftwareSerial -I/home/xc317/sketchbook/libraries/serlcd -I/usr/share/arduino/libraries/SPI -I/usr/share/arduino/libraries/Ethernet -I/usr/share/arduino/libraries/Ethernet/utility /usr/share/arduino/libraries/Ethernet/Server.cpp -o/tmp/build1305752250561284982.tmp/Ethernet/Server.cpp.o
此行为的原因是什么?
I'm trying to use an ATmega 328p (Arduino Ethernet) to control a Wiznet W5100 Ethernet controller. The libraries I'm using are tested and work, but for me they don't. Here's what I tracked the problem down to:
I'm trying to read two registers, that tell me how many bytes of data have been received by the W5100:
uint16_t readTest()
{
uint16_t res = W5100.read(0x0426);
res = (res << 8) + W5100.read(0x0427);
return res;
}
But even though I'm not sending any data, this function, called from main(), will return 1024 instead of 0. Now comes the weird part: If I add the static keyword like this:
static uint16_t readTest()
{
uint16_t res = W5100.read(0x0426);
res = (res << 8) + W5100.read(0x0427);
return res;
}
Then the function suddently returns 0 as it is supposed to!
Also I could change it to this:
uint16_t readTest()
{
return (W5100.read(0x0426) << 8) + W5100.read(0x0427);
}
This must be the compiler messing with me. I really don't get it. To compile I use the standard Arduino IDE, the command looks something like this:
avr-g++ -c -g -Os -w -fno-exceptions -ffunction-sections -fdata-sections -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=22 -I/usr/share/arduino/hardware/arduino/cores/arduino -I/usr/share/arduino/libraries/SoftwareSerial -I/home/xc317/sketchbook/libraries/serlcd -I/usr/share/arduino/libraries/SPI -I/usr/share/arduino/libraries/Ethernet -I/usr/share/arduino/libraries/Ethernet/utility /usr/share/arduino/libraries/Ethernet/Server.cpp -o/tmp/build1305752250561284982.tmp/Ethernet/Server.cpp.o
What is the reason for this behaviour?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论