CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Pic24H clock switch FRC to XT [SOLVED]

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
pieter



Joined: 16 Jan 2011
Posts: 27

View user's profile Send private message

Pic24H clock switch FRC to XT [SOLVED]
PostPosted: Tue Mar 22, 2011 7:40 am     Reply with quote

Hi,
please help me to switch the clock. I have been trying for > 2 weeks without success, using the asm routines from microchip.
When I program the code with a programmer, it works.
When I load the code from a bootloader it works but I need the uCro to run fast.
The uCro still runs slow after the bootloaded code.

I need the loaded code to switch from the bootloader's FRC to the loaded code's XT with PLL. Crystal = 8 Mhz.
I do program the bootloader with FRC, no PLL, Clock switching enabled.

My Code: please see SWITCH CLOCK (for 10Mhz Xtal) below.

Code:

//                                    *
//***********************************************
//
//
#list
#include <c:\mpc\devices\24HJ256GP610.h>
#device ADC=12
#FUSES WDT                    //Watch Dog Timer
#FUSES NOWRTB                   //Boot block not write protected
#FUSES NOBSS                    //No boot segment
#FUSES NOPROTECT                //Code not protected from reading
#FUSES NOWRT                    //Program memory not write protected
#FUSES NOOSCIO                    //OSC2 is general purpose output
#FUSES PR_PLL                     //Pimary oscillator
#FUSES XT
#FUSES WPRES128                 //Watch Dog Timer PreScalar 1:128
#FUSES WPOSTS16                 //Watch Dog Timer PostScalar 1:32768
#FUSES PUT128                   //Power On Reset Timer value 128ms
#FUSES NOIESO                   //Internal External Switch Over mode disabled
#FUSES NORSS                    //No secure segment RAM
#FUSES NOSSS                    //No secure segment
#FUSES NOWRTSS                  //Secure segment not write protected
#FUSES NORBS                    //No Boot RAM defined
#FUSES NODEBUG                  //No Debug mode for ICD
#FUSES NOJTAG                   //JTAG disabled

#use delay(clock=80M, Oscillator=8M,RESTART_WDT)
#use rs232(baud=19200,Uart1, xmit=PIN_F3, rcv=PIN_F2,Errors, STREAM=RS232)  // baud=38400 19200
#id      0x0001
#PRAGMA   ZERO_RAM

#use fast_io(A)
#use fast_io(B)
#use fast_io(C)
#use fast_io(D)
#use fast_io(E)
#use fast_io(F)
#use fast_io(G)

#byte PLLFBD   = 0x746  // bits 8--0 = Pll div = 0x1e
#byte CLKDIV = 0x744  // bits 7--6 = Pll post = 0  // bits 4--0 = Pll Pre = 0
#byte OSCCONH = 0x742
#byte OSCCONL = 0x743

//
//   Pin definitions
#define Led   Pin_A0
#define Buzzer   Pin_G13
#define Led2   Pin_E4
#define URst Pin_B12
#define URts Pin_F13

//
//**********************************************************************************
//
main()
{
//   SWITCH CLOCK (for 10Mhz Xtal)
#asm
   ; 10 Mhz xtl example - modify PLLPRE and PLLPOST values
        ;Place the New Oscillator Selection (NOSC=0b011) in W0
        MOV #0x13,W0
        ;OSCCONH (high byte) Unlock Sequence
        MOV #0x743, W1      ; OSCCONH = 742
        MOV #0x78, W2
        MOV #0x9A, W3
        MOV.B W2, [W1] ; Write 0x78
        MOV.B W3, [W1] ; Write 0x9A
        ;Set New Oscillator Selection
        MOV.B W0, [W1]
        ; Place 0x01 in W0 for setting clock switch enabled bit
        MOV #0x01, W0
        ;OSCCONL (low byte) Unlock Sequence
        MOV #0x742, W1      ; OSCCONL = 743
        MOV #0x46, W2
        MOV #0x57, W3
        MOV.B W2, [W1] ; Write 0x46
        MOV.B W3, [W1] ; Write 0x57
        ; Enable Clock Switch
        MOV.B W0, [W1]
;wait:
;             BTSC #0x743, #0      ; OSCCON = 742
;             BRA wait
;

#endasm
   delay_ms(5);
   RESTART_WDT();
   SETUP_ADC_PORTS(No_Analogs);
   SET_TRIS_A(0xffca);   // 1111 1111 1100 1010
   SET_TRIS_B(0xeffb);   // 1110 1111 1111 1011
   SET_TRIS_C(0x9fff);   // 1001 1111 1111 1111
   SET_TRIS_D(0x003e);   // 0000 0000 0011 1110    //
   SET_TRIS_E(0xe7);     // 0000 0111    //
   SET_TRIS_F(0xde57); // 1101 1110 0101 0111    //
   SET_TRIS_G(0x8ff8);  // 1000 1111 1111 1000
   Output_A(0x0000);
   Output_B(0x0000);
   Output_C(0x0000);
   Output_D(0x0000);
   Output_E(0x00);
   Output_F(0x0000);
   Output_G(0x0000);
   fprintf(RS232, "Hello World");
   
//
   while (TRUE)
   {
      RESTART_WDT();            // reset watchdog
      Output_Low(Led2);
      delay_ms(300);
      Output_High(Led2);
      delay_ms(300);
      fprintf(RS232, "Hello World");
      Output_Low(Led);
      delay_ms(400);
   }
}


Thanks!

ok, I programmed the bootloader with the following Config flags:
; FNOSC: internal fast RC
; IESO: start with user select osc
; POSCMD: XT Osc mode
; FCKSM: clock switching is enabled. monitor disabled

the above code now works fine, switches to 80Mhz clock after bootloading
ae6zw



Joined: 25 Oct 2011
Posts: 1

View user's profile Send private message

PIC24F16KA102 // setup_oscillator( )
PostPosted: Thu Jan 05, 2012 3:21 pm     Reply with quote

I also had a problem with setup_oscillator( ) function with PIC24F16KA102
so I wrote my own version of setup_oscillator2( )
which works well, except secondary OSC


Code:

void  setup_oscillator2( UI16 ui16_OSCCON_01, UI8  ui8_CLK_DIV )
{

  UI8  ui8_temp_01, ui8_temp_02, ui8_temp_03, ui8_temp_04;
 
 
  ui8_temp_01 =  ( UI8 ) ( ui16_OSCCON_01 / 0x0100 );   // higher byte
  ui8_temp_02 =  ( UI8 ) ( ui16_OSCCON_01 & 0x00ff );   // lower byte 
 

  ui8_temp_03 = ( ui8_CLK_DIV );
 
  #ASM
 
 
  ;OSCCON (high byte) Unlock Sequence
  MOV.B ui8_temp_01, W0
  MOV #0x743, W1
  MOV #0x78, W2
  MOV #0x9A, W3
  MOV.B  W2, [ W1 ]
  MOV.B  W3, [ W1 ]
  MOV.B  W0, [ W1 ]
   
   
  ;OSCCON (low byte) Unlock Sequence     Start oscillator switch operation
  MOV.B  ui8_temp_02, W0 
  MOV #0x742, W1
  MOV #0x46, W2
  MOV #0x57, W3
  MOV.B  W2, [ W1 ]
  MOV.B  W3, [ W1 ]
  MOV.B  W0, [ W1 ]
 
 
  //BSET 0x742, #0  ;OSWEN
 
     
  ;CLKDIV ( high byte )
  MOV  #0x745, W1
  MOV.B  ui8_temp_03, W0
  MOV.B W0, [ W1 ]
     
  #ENDASM
 
 
}  // void  setup_oscillator2( UI16 ui16_input_01  )



void setup_clk( UI8 ui8_clk )
{
  // 0 -- 32K internal  // 1 -- 32k secondary OSC  //  2 -- 500k Internal //
  ui8g_clk_freq = ui8_clk;
 
  switch ( ui8g_clk_freq )
  {
    //
    case 0:
      setup_oscillator2( 0x0501, 0x00 );  // 32khz LPRC, 17 uA
      //setup_oscillator( OSC_INTERNAL, 32000 );    //  if 31000   draw 77 uA,  32000  draw 15 uA    ???
      break;
     
    case 1:
      //setup_oscillator( OSC_INTERNAL, 31000 );    //  if 31000   draw 77 uA,  32000  draw 15 uA    ???
      setup_oscillator2( 0x0401, 0x00 );  // 32khz secondary osc
      break;
     
    case 2:
      setup_oscillator2( 0x0601, 0x00 );  //  500 kHz  , 258 uA
      //setup_oscillator( OSC_INTERNAL, 500000 );    // 
      break;
 
    case 3:
      setup_oscillator2( 0x0701, 0x03 );  //  8 MHz div 8 = 1 MHz ,  568 uA
      //setup_oscillator( OSC_INTERNAL, 1000000 );    // 
      break;
     
    case 4:
      setup_oscillator2( 0x0701, 0x02 );  //  8 MHz div 4 = 2 MHz  ,  880 uA
      //setup_oscillator( OSC_INTERNAL, 1000000 );    // 
      break;

    case 5:
      setup_oscillator2( 0x0701, 0x01 );  //  8 MHz div 2 = 4 MHz  ,  1.50 mA
      //setup_oscillator( OSC_INTERNAL, 1000000 );    // 
      break;
     
    case 6:
      setup_oscillator2( 0x0701, 0x00 );  //  8 MHz div 1 = 8 MHz,  2.66 mA
      //setup_oscillator( OSC_INTERNAL, 1000000 );    // 
      break;
 
  } // switch ( ui8_clk_01 )
   

} //  void setup_clk( )
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group