AVR / Atmega128rfa1:读取寄存器给出不同的结果

AVR / Atmega128rfa1: Reading registers gives different results

所以,我正在使用 Atmega128rfa1 开发板,但我得到了奇怪的结果。

我的代码使用 MAC 符号计数器。它使用手动信标时间戳。 在我的代码中发生了以下情况,PORT_RADIOTIMER_WIDTH 是 uint32_t 的宏:

....
*((PORT_RADIOTIMER_WIDTH *)(&SCOCR2LL)) = 0;
SCOCR2LL = 0;   //set compare registers

*((PORT_RADIOTIMER_WIDTH *)(&SCBTSRLL)) = 0;
SCBTSRLL = 0;   //set compare registers

SCCNTHH = SCCNTHL = SCCNTLH = 0;
SCCNTLL = 0;    // reset timer value

radiotimer_setPeriod(period);   //set period


SCCR0 |= (1 << SCMBTS); // "reset" radiotimer

在此之后,我立即尝试使用以下代码获取自时间戳以来的相对时间:

    PORT_RADIOTIMER_WIDTH count_time = (uint8_t) SCCNTLL;
count_time |= (uint8_t)SCCNTLH << 8;
count_time |= (uint8_t)SCCNTHL << 16;
count_time |= (uint8_t)SCCNTHH << 24;

PORT_RADIOTIMER_WIDTH beacon_time2;
beacon_time2 = (uint8_t) SCBTSRLL;
beacon_time2 |= (uint8_t)SCBTSRLH << 8;
beacon_time2 |= (uint8_t)SCBTSRHL << 16;
beacon_time2 |= (uint8_t)SCBTSRHH << 24;
PORT_RADIOTIMER_WIDTH beacon_time = (uint16_t)beacon_time2;
PORT_RADIOTIMER_WIDTH captured_time = count_time - beacon_time;
print_debug("radiotimer_getCapturedTime count_time %lu or %u\n", count_time);
print_debug("radiotimer_getCapturedTime beacon_time %lu  or %u\n", beacon_time);
print_debug("radiotimer_getCapturedTime captured_time %lu  or %u\n",captured_time);

打印出来的是:

radiotimer_getCapturedTime count_time 859 or 859
radiotimer_getCapturedTime beacon_time 7090  or 859
radiotimer_getCapturedTime captured_time 4294961065  or 859

与预期相反,信标时间与 count_time 大致相同,仅当作为 unsigned int 进行比较时,而不是作为 unsigned long int。 怎么了?我完全被这个惊呆了。

print_debug() 语句看起来很可疑。该格式需要两个变量,但您只提供一个。每次调用中打印的 859 可能来自您不期望的变量。

否则,ul版本的算法是正确的。 859 - 7090 在 32 位无符号算术中等于 4294961065。

试试这样的东西:

print_debug("radiotimer_getCapturedTime count_time %lu or %u\n", count_time, count_time);
print_debug("radiotimer_getCapturedTime beacon_time %lu  or %u\n", beacon_time, beacon_time);
print_debug("radiotimer_getCapturedTime captured_time %lu  or %u\n",captured_time, captured_time);

看看在 16 位情况下结果是否始终相同。