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

时间:2019-08-30 09:51:40

标签: c++ stm32

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

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

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

PPRINTF是ST提供的printf变体,正在阻止。它内部使用了一个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)));
}

输出:

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

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

我无法从1343187710x8018AB3的{​​{1}}中推断出自己的方式,因为它被翻转,移位或以其他方式修改为原始值。

系统的体系结构为32位,因此对于所有值而言,其空间应足够大,这在概念验证中得到了证实。

1 个答案:

答案 0 :(得分:5)

函数结尾处的所有打印调用都如下所示。

FileInputStream fis = new FileInputStream(theNewestFile);
XSSFWorkbook workbook = new XSSFWorkbook(fis);
XSSFSheet spreadsheet = workbook.getSheetAt(0);
XSSFRow row;
String cellValueMaybeNull;
List<Cell> cells = new ArrayList<Cell>();

Map<String,List<String>> duplicateCheck = new HashMap<String,List<String>>();
for (int rowIndex = 0; rowIndex <= spreadsheet.getLastRowNum(); rowIndex++) {
         row = (XSSFRow) spreadsheet.getRow(rowIndex);
         if (row != null) {
           int colIndex = 0;
           Cell cell = row.getCell(colIndex);
           if (cell != null) {
           // Found column and there is value in the cell.
           cellValueMaybeNull = cell.getStringCellValue();
           // Do something with the cellValueMaybeNull here ...
           if(duplicateCheck.get(cellValueMaybeNull) != null) {
                 duplicateCheck.get(cellValueMaybeNull).add(cellValueMaybeNull);
           }
           else {
           List<String> list = new ArrayList<String>();
               list.add(cellValueMaybeNull);
               duplicateCheck.put(cellValueMaybeNull, list);
            }

               }
            }
        } 

for(List<String> duplicateValue : duplicateCheck.values()) {
            if(duplicateValue.size() > 1) {
                System.out.println("Duplicate values :"+duplicateValue);
            }
        }

您要设置整数格式,但要提供C字符串和整数。
PPRINTF("\t current time: %i\r\n", __PRETTY_FUNCTION__, HW_RTC_Tick2ms(subdiv)); 可能是C字符串134318771地址的十进制表示形式。

您需要使格式和参数匹配。

__PRETTY_FUNCTION__

PPRINTF("\t current time: %i\r\n", HW_RTC_Tick2ms(subdiv));