hi guys...
The code for clock switching from datasheet:
A recommended code sequence for a clock switch includes the following actions:
1. Disable interrupts during the OSCCON register unlock-and-write sequence.
2. Execute the unlock sequence for the OSCCON high byte. In 2 back-to-back instructions:
• write 0x78 to OSCCON<15:8>
• write 0x9A to OSCCON<15:8>
3. In the instruction immediately following the unlock sequence, write the new oscillator
source to the NOSC<2:0> Control bits (OSCCON<10:8>).
4. Execute the unlock sequence for the OSCCON low byte. In 2 back-to-back instructions:
• write 0x46 to OSCCON<7:0>
• write 0x57 to OSCCON<7:0>
5. In the instruction immediately following the unlock sequence, set the OSWEN bit
(OSCCON<0>).
6. Continue to execute code that is not clock-sensitive (optional).
7. Check to see if the OSWEN bit (OSCCON<0>) is ‘0’. If it is, the switch was successful.
ok, so I made that in the setup function:
Code: Select all
void setup()
{
/*
01111000b = 78h
10011010b = 9Ah
01000110b = 46h
01010111b = 57h
*/
//Set OSCCONH as 78h
/////////////////////
OSCCONbits.NOSC0 = 0;
OSCCONbits.NOSC1 = 0;
OSCCONbits.NOSC2 = 0;
OSCCONbits.COSC0 = 1;
OSCCONbits.COSC1 = 1;
OSCCONbits.COSC2 = 1;
/////////////////////
//Set OSCCONH as 9Ah
/////////////////////
OSCCONbits.NOSC0 = 0;
OSCCONbits.NOSC1 = 1;
OSCCONbits.NOSC2 = 0;
OSCCONbits.COSC0 = 1;
OSCCONbits.COSC1 = 0;
OSCCONbits.COSC2 = 0;
/////////////////////
//Set the new NOSC value, PLL
OSCCONbits.NOSC0 = 1;
OSCCONbits.NOSC1 = 1;
OSCCONbits.NOSC1 = 0;
//Set OSCCONL as 46h
/////////////////////
OSCCONbits.OSWEN = 0;
OSCCONbits.LPOSCEN = 1;
OSCCONbits.CF = 0;
OSCCONbits.LOCK = 0;
OSCCONbits.CLKLOCK = 0;
/////////////////////
//Set OSCCONL as 57h
/////////////////////
OSCCONbits.OSWEN = 1;
OSCCONbits.LPOSCEN = 1;
OSCCONbits.CF = 0;
OSCCONbits.LOCK = 0;
OSCCONbits.CLKLOCK = 0;
/////////////////////
OSCCONbits.OSWEN = 1;
while(OSCCONbits.OSWEN == 1)
{}
}
void main()
{
setup();
TRISB.F6 = 0;
while(1)
{
PORTB.F6 = 1;
PORTB.F6 = 0;
}
}
In the edit project I set Oscilator mode as (XT, HS, EC), Primary Osclillator source as HS and clock switching is enable.
I also tried this:
Code: Select all
void setup()
{
OSCCON = 0x78;
OSCCON = 0x9A;
//Set the new NOSC value, PLL
OSCCONbits.NOSC0 = 1;
OSCCONbits.NOSC1 = 1;
OSCCONbits.NOSC1 = 0;
OSCCONL = 0x46;
OSCCONL = 0x57;
OSCCONbits.OSWEN = 1;
while(OSCCONbits.OSWEN == 1)
{}
}
But I continue to get a signal from PORTB.F6 with 1Mhz.
*I'm using 8MHz crystal.
Why I can't change to pll mode?
Thanks and sorry for my english!