为什么我的代码为同一代码产生不同的值

Tar*_*ing 2 c++ stm32

我正在开发STM32L072,并且正在创建一个timeLimit类,该类的工作是跟踪经过的时间以及是否已超过设置的时间。如果所需时间已过,则类将返回此时间,因此可以执行与时间有关的代码。此类在内部使用滴答声,因此该函数HW_RTC_Tick2ms用于转换回ms。ST的I-CUBE-LRWAN扩展软件包提供了此功能,我正在使用v1.2.1。

我已printTimeToSend出于记录/调试和监视的原因编写了该函数。原始表格会打印出当前经过的时间,等待设置的时间以及下一次超时的时间。

问题在于,134318771以原始方式使用时此代码返回。我已经将所有单独的步骤拆分为变量,并将其打印出来以跟踪转换出错的地方。但是,当拆分为单独的步骤时,它将返回正确的结果。

PPRINTFprintfST提供的变体,正在阻止。它内部使用了一个circulair缓冲区,并且具有与参数传递相同的语法。
__nop()操作已经插入调试。我使用Keil进行开发,而stackviewer在C ++中有时无法更新变量。因此,__nop()在执行相关代码的下一位之前,请确保在更新该值并使之可见之前, 以秒为单位,
HW_RTC_GetTimerValue获取当前时间值。

/** @author Tarick Welling
 * @brief outputs the current time, timeout time and time to transmission.
 */
void timeLimit::printTimeToSend() const {
  PPRINTF("%s\r\n", __PRETTY_FUNCTION__);
  auto now = HW_RTC_GetTimerValue();
  PPRINTF("now: %i\r\n", now);
  PPRINTF("now: %i\r\n",HW_RTC_Tick2ms(now));
  __nop();
  __nop();
  auto then = startTime;
  PPRINTF("then: %i\r\n", then);
  PPRINTF("then: %i\r\n",HW_RTC_Tick2ms(then));
  __nop();
  __nop();
  auto between = sendDelayTime;
  PPRINTF("between: %i\r\n", between);
  PPRINTF("between: %i\r\n",HW_RTC_Tick2ms(between));
  __nop();
  __nop();
  auto diff = (now - then);
  PPRINTF("diff: %i\r\n", diff);
  PPRINTF("diff: %i\r\n",HW_RTC_Tick2ms(diff));
  __nop();
  __nop();
  PPRINTF("bigger: %i\r\n", between < diff);
  PPRINTF("bigger: %i\r\n",HW_RTC_Tick2ms(between) < HW_RTC_Tick2ms(diff));

  PPRINTF("SEMI\r\n");
  auto subdiv = now - then;
  PPRINTF("\t current time: %i\r\n", __PRETTY_FUNCTION__,
          HW_RTC_Tick2ms(subdiv));
  PPRINTF("\t timeout time: %i\r\n", __PRETTY_FUNCTION__,
          HW_RTC_Tick2ms(between));
  PPRINTF("\t time to send: %i\r\n", __PRETTY_FUNCTION__,
          HW_RTC_Tick2ms(between - subdiv));

  PPRINTF("ORI\r\n");
  PPRINTF("\t current time: %i\r\n", __PRETTY_FUNCTION__,
          HW_RTC_Tick2ms(HW_RTC_GetTimerValue() - startTime));
  PPRINTF("\t timeout time: %i\r\n", __PRETTY_FUNCTION__,
          HW_RTC_Tick2ms(sendDelayTime));
  PPRINTF("\t time to send: %i\r\n", __PRETTY_FUNCTION__,
          HW_RTC_Tick2ms(sendDelayTime - (HW_RTC_GetTimerValue() - startTime)));
}
Run Code Online (Sandbox Code Playgroud)

输出:

void timeLimit::printTimeToSend() const
now: 9702
now: 9474
then: 837
then: 817
between: 7372
between: 7199
diff: 8865
diff: 8657
bigger: 1
bigger: 1
SEMI
         current time: 134318771
         timeout time: 134318771
         time to send: 134318771
ORI
         current time: 134318771
         timeout time: 134318771
         time to send: 134318771

Run Code Online (Sandbox Code Playgroud)
TimerTime_t HW_RTC_Tick2ms(uint32_t tick) {
  uint32_t seconds = tick >> 0x0A;
  tick = tick & 0x03FF;
  return ((seconds * 1000) + ((tick * 1000) >> 0x0A));
}
Run Code Online (Sandbox Code Playgroud)

I'm not able to reason my way from 134318771 which is 0x8018AB3 and 1000000000011000101010110011 be it bit flipped, shifted or otherwise modified in place to the original values.

The architecture of the system is 32 bits and as such the should be more than enough space for all the values which is confirmed in the demonstration of the concept.

pro*_*-fh 5

All your print calls at the end of your function look like this.

PPRINTF("\t current time: %i\r\n", __PRETTY_FUNCTION__,
          HW_RTC_Tick2ms(subdiv));
Run Code Online (Sandbox Code Playgroud)

You want to format an integer but you provide a C-string and an integer.
134318771 is probably the decimal representation of the address of the C-string __PRETTY_FUNCTION__.

You need to make the format and the parameters match.

PPRINTF("\t current time: %i\r\n", HW_RTC_Tick2ms(subdiv));
Run Code Online (Sandbox Code Playgroud)

or

PPRINTF("\t %s current time: %i\r\n", __PRETTY_FUNCTION__,
          HW_RTC_Tick2ms(subdiv));
Run Code Online (Sandbox Code Playgroud)