AVR / Atmega128rfa1:读取寄存器会产生不同的结果

时间:2015-12-01 11:07:01

标签: avr

所以,我正在使用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。 怎么了?我对这一点感到非常惊讶。

1 个答案:

答案 0 :(得分:1)

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位情况下结果是否仍然相同。