开发者

ATmega 328p behaving weird with a static function

开发者 https://www.devze.com 2023-04-06 02:13 出处:网络
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 pro

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 开发者_JAVA百科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?

0

精彩评论

暂无评论...
验证码 换一张
取 消