大家好
我使用PIC24FJ256GB106, 為了省電的關係
我必須要進 sleep mode 後再將 osc 切到 FNOSC_FRCPLL
然後使用者按任何鍵後切回 FNOSC_PRIPLL
但是我遇到一個問題, 那就是我切到 FNOSC_FRCPLL 成功後, 想要再切回來 FNOSC_PRIPLL, 程式就會卡在底下紅色那行程式碼上, 請問我少作了什麼動作嗎
main()
{
........
......
SwitchClock(GET_OSC_SEL_BITS(FNOSC_FRC)); // 可以切換成功r
iOSC= OSCCON; // 偵測, 讀取 OSCCON, debug 用 only
_TUN = 0;
SwitchClock(GET_OSC_SEL_BITS(FNOSC_PRIPLL)); // 切不回來, 卡在下方紅色字那裡
}
//---------------------------------------------------------------------------
void SwitchClock(unsigned char u8_source)
{
// Create a union that mirrors the OSCCON structure
// with all its bit names but is also byte-accessable.
OSCCONBITS OSCCONBITS_copy;
// Switch clock to use new choice specified by u8_choice.
// Valid values are 0-7.
// 1. Disable interrupts per 7.11.2 FRM rev B under
// "A recommended code sequence for a clock switch
// includes the following:" heading.
// Assumes there are no priority 7 interrupts enabled.
asm("DISI #0x3FFF"); // Disable interrupts for a long time
// 2. Switch to the PLL. Use compiler built-ins to unlock
// clock switch registers. See 7.11.1 of the FRM rev B.
OSCCONBITS_copy = OSCCONbits; // Copy OSCCON bits
OSCCONBITS_copy.NOSC = u8_source; // Select new clock source
OSCCONBITS_copy.OSWEN = 1; // Request clock switch
// First write high byte, containing new clock source NOSC
__builtin_write_OSCCONH(BITS2BYTEH(OSCCONBITS_copy));
// Then write low byte, requesting clock switch with OSWEN
__builtin_write_OSCCONL(BITS2BYTEL(OSCCONBITS_copy));
asm("DISI #0"); // Re-enable them at the next instruction
//#define SIM
#ifndef SIM
// 3. Wait for switch to complete.
// Note that oscillator switching is not supported by
// the simulator, causing the statements below to
// run forever.
while (_OSWEN == 1);
// 4. Wait for the PLL to lock if using the PLL.
if ( (u8_source == GET_OSC_SEL_BITS(FNOSC_FRCPLL)) ||
(u8_source == GET_OSC_SEL_BITS(FNOSC_PRIPLL)) )
while (_LOCK == 0);
#endif
}