在調試MSP430G系列的單片機的過程,發現不怎么的,時鐘頻率發生了變化,時間一下都對不上了。查收些資料說,DCO的值有可能被擦除了導致時鐘不對的。細想一下,好像是在調試的過程中,出現調試不了的現象就在IAR里點擊的“Erase memory”。把查找的資料記錄下來,所以就有這篇文章。
問題描述因為DCO校準值在MCU出廠時保存于信息段A,一般是不允許清除信息段A的。
且示例代碼中,有如下語句:
if (CALBC1_1MHZ ==0xFF || CALDCO_1MHZ == 0xFF) { while(1);// If calibration constants erased // do not load, trap CPU!! } BCSCTL1 = CALBC1_1MHZ; // Set range DCOCTL = CALDCO_1MHZ; // Set DCO step + modulation可以看出在啟動時都先檢查DCO1MHZ的校準值是否為0xFF,是則進入while(1)死循環。
正常情況下因為信息段A不清除沒問題的,但是我測試BSL下載時,故意給錯誤的BSL密碼,導致FLASH都被擦除,發現轉載DCO校準值的信息段A也被清掉了,于是程序運行到上面的代碼時即進入死循環。
針對于這種DCO數據被擦除的情況,一般的解決辦法是,通過利用Timer捕捉外部的32.768K晶振或是時鐘源,然后得到1M,8M或是12Mhz之類的DCO的數值,然后在直接寫入segment flashA里面。
有兩個解決方案供選擇:
1、外接晶體,用晶體配合timer校正DCO,TI的msp4300ware有源代碼。如下:
/******************************************************************************* * * MSP430 CODE EXAMPLE DISCLAIMER * * MSP430 code examples are self-contained low-level programs that typically * demonstrate a single peripheral function or device feature in a highly * concise manner. For this the code may rely on the device's power-on default * register values and settings such as the clock configuration and care must * be taken when combining code from several examples to avoid potential side * effects. Also see for a GUI- and * for an API functional library-approach to peripheral configuration. * * --/COPYRIGHT-- //******************************************************************************/ // MSP430G2xx1 Demo - DCO Calibration Constants Programmer // // NOTE: THIS CODE REPLACES THE TI FACTORY-PROGRAMMED DCO CALIBRATION // CONSTANTS LOCATED IN INFOA WITH NEW VALUES. USE ONLY IF THE ORIGINAL // CONSTANTS ACCIDENTALLY GOT CORRUPTED OR ERASED. // // Description: This code re-programs the G2xx1 DCO calibration constants. // A software FLL mechanism is used to set the DCO based on an external // 32kHz reference clock. After each calibration, the values from the // clock system are read out and stored in a temporary variable. The final // frequency the DCO is set to is 1MHz, and this frequency is also used // during Flash programming of the constants. The program end is indicated // by the blinking LED. // ACLK = LFXT1/8 = 32768/8, MCLK = SMCLK = target DCO // //* External watch crystal installed on XIN XOUT is required for ACLK *// // // MSP430G2xx1 // --------------- // /|\| XIN|- // | | | 32kHz // --|RST XOUT|- // | | // | P1.0|--> LED // | P1.4|--> SMLCK = target DCO // // A. Dannenberg // Texas Instruments Inc. // May 2010 // Built with CCS Version 4.2.0 and IAR Embedded Workbench Version: 3.42A //****************************************************************************** #include第二個辦法是, , Elprotronic新版MSP430下載工具帶有DCO校正功能。也可以使用MSP-GANG燒錄工具來恢復,該工具支持重新效驗DCO校準數據,然后寫入information memory。
下次也記得個教訓,在做BSL的時候,事先把DCO數據讀出,做好備份工作。
參考
評論
查看更多