您好!
我正在尝试使用 PERSISTENT pragma 使变量保持持久性。 该代码是 Andreas Dannenberg 所做的示例的副本(发布在 TI 论坛上某处):
//============================================================================ // Name : #pragma PERSISTENT Test for C++ // Author : Andreas Dannenberg // Version : 1.0 //============================================================================ #include "msp430.h" // Declare a simple class that maintains a static counter variable. Note that // only one copy of this variable is shared by all instances of this class. class PersistentTest { public: int getCounter() { return counter; } void setCounter(int newCounterValue) { counter = newCounterValue; } void incrementCounter() { counter++; } private: static int counter; }; // In C++ the counter needs to be explicitly defined/initialized. For data // members this has to be done outside the class in the namespace scope. This // is also where we are going to apply the PERSISTENT pragma. #pragma PERSISTENT int PersistentTest::counter = 0; int main() { // Stop the MSP430's watchdog timer WDTCTL = WDTPW | WDTHOLD; // Create an instance of our test class PersistentTest persistentTest; // Call the member function to increment the class-internal counter persistentTest.incrementCounter(); // Obtain a snapshot of the class-internal counter volatile int currentCounterValue = persistentTest.getCounter(); // Set breakpoint here and use the debugger to look at the value of // 'currentCounterValue'. Then, reset the device and run and re-inspect. __no_operation(); return 0; }
我已经创建了一个空白项目、上传了此代码、但 persistent 变量不会更改值。 它将保持为0。 调试器不能更改该值(程序在断点处停止):它可以简单地反转至0。
map 文件显示该变量位于 persistent 段中:
output attributes/ section page origin length input sections -------- ---- ---------- ---------- ---------------- .TI.persistent * 0 0000f100 00000002 0000f100 00000002 main.obj (.TI.persistent)
问题可能是什么?
编辑:在一个电源循环(关闭电源几秒钟)后、这个示例就有效了。 但是、我有一个更复杂的程序、仍然显示相同的行为。 线索非常受欢迎
谢谢。
尤斯图斯