具體表現:
1、使用 20M 的外部晶振,程式也能正常工作。
2、使用 24M 的外部晶振,就不能工作,經示波器測量,晶振沒有起振。
3、為了去除晶振的原因,就換了一個 24M 的晶振,結果還是不行,可見應該不是晶振的問題。
4、去群裡問,一個叫“碎片”的網友說,這是因為沒有添加 FLASH 等待周期造成的。外部晶振大于16M 就得添加 FLASH 等待周期,雖然 20M 也能運作,但長時間運作會不穩定。
5、我按他的方法添加了 FLASH 等待周期,果然程式正常運作了,用示波器的 x10 檔測量晶振,能看到很完美的正弦波。但開始用 x1 檔,表筆一接觸晶振引腳,程式就立即停止,考慮到是示波器的影響,就換成了 x10 檔,果然能正常測量。
6、具體添加辦法如下圖,在STVD環境下,右擊 No wait state 在彈出的菜單中,選擇 1 wait state 就可以了,然後再燒錄就正常了。

7、查找資料:
4.4.2 Memory access/ wait state configuration
The Flash/ data EEPROM access time allows the device to run atup to 16 MHz without wait states.
When using the high-speed external clock (HSE) at higher frequencies up to 24 MHz,one wait state is necessary. In this case the device option byte should be programmed to insert this wait state. Refer to the datasheet option byte section.