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 CCS Technical Support

24FJ128GA108 - Problem - Switching internal clock from 8Mhz

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



Joined: 08 Sep 2010
Posts: 3
Location: Lincoln

View user's profile Send private message

24FJ128GA108 - Problem - Switching internal clock from 8Mhz
PostPosted: Wed Sep 08, 2010 6:45 am     Reply with quote

CCS – Problem - Switching internal clock from 8Mhz to 31khz
CCS compiler - Version 4.093

I am using the internal clock running at 8mhz. During its running time, I need to switch the clock to 31khz and back again to save current

I can get the clock to change using the setup_oscillator command, but only once (i.e the first time)

I believe this because you need to unlock the oscillator first, but looking the ASM code, the CCS compiler looks to be doing this anyway behind the scenes

HELP!!
Code:

#include <24FJ128GA108.h>
 
#FUSES NOWDT                    //No Watch Dog Timer
#FUSES NOJTAG                   //JTAG disabled
#FUSES NOPROTECT                //Code not protected from reading
#FUSES NOWRT                    //Program memory not write protected
#FUSES NODEBUG                  //Debug mode for use with ICD         ################ FUSE BUG  ##############
#FUSES ICSP1                    //ICD uses PGC1/PGD1 pins
#FUSES NOWINDIS                 //Watch Dog Timer in Window mode
#FUSES WPRES128                 //Watch Dog Timer PreScalar 1:128
#FUSES WPOSTS16                 //Watch Dog Timer PostScalar 1:32768
#FUSES IESO                     //Internal External Switch Over mode enabled

// PTH 07/09/2010 - This setting allows you to switch between fast and slow clock rates
#FUSES FRC_PS                  //No  PLL!!! (baud rate /4)
//#FUSES FRC_PLL
//not use -      #FUSES FRC_PLL                  //ignore setup_osc --- Internal Fast RC oscillator with PLL
//               #FUSES NOCKSFSM                 //Clock Switching is disabled, fail Safe clock monitor is disabled

#FUSES CKSFSM
#FUSES NOOSCIO                  //OSC2 is clock output      ONNNNN!!!!!
#FUSES NOPR                     //Pimary oscillaotr disabled
#FUSES NOIOL1WAY                //Allows multiple reconfigurations of peripheral pins
#FUSES WPEND_LOW
#FUSES NOWPCFG               
#FUSES NOWPDIS               
#FUSES WPFP0
//#FUSES OSCIO                  //OSC2 is clock output      OFFFFFFFF!!!!!!

#define BW_SEL_OUT_1   PIN_F4
#define BW_SEL_OUT_2   PIN_F5

#include "main.h"

#define MFREQ1 31250
#define MFREQ2 16000000

#use delay(clock=MFREQ1)

/*
      Main Routine

     Test clock switching..
*/

void main()
{
// pth 27/08/2010 - Set oscillator to 31 khz
   
   unsigned int16 wswait=0;
   
   delay_ms(200);
   
   do
   {
      // Slow
      for (wswait=0;wswait<16000;wswait++){}
     
      output_toggle(BW_SEL_OUT_2);
      setup_oscillator(OSC_INTERNAL, MFREQ1);
     
      // Fast
      for (wswait=0;wswait<16000;wswait++){}
      output_toggle(BW_SEL_OUT_2);
      setup_oscillator(OSC_INTERNAL, MFREQ2);

   } while(1);
   
}
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Wed Sep 08, 2010 12:04 pm     Reply with quote

I'm using the below function for 24F16KA102, it should work with 24FxxGA106 as well.

Usage:
clockswitch(5); // LPRC
clockswitch(0); // FRC

Code:
#byte OSCCONH = 0x743
#bit OSWEN = 0x742.0

#separate ARG=W0:W0
void clockswitch(unsigned int8 nosc)
{
// Place the new oscillator selection in W0
// OSCCONH (high byte) Unlock Sequence
#asm
   mov #0x743, w1
   mov #0x78, w2
   mov #0x9A, w3
   mov.b w2, [w1]
   mov.b w3, [w1]
#endasm
   OSCCONH = W0L;   
#asm
   mov #0x742, w1
   mov #0x46, w2
   mov #0x57, w3
   mov.b w2, [w1]
   mov.b w3, [w1]
#endasm
   OSWEN = 1;      // Start oscillator switch operation
   while (OSWEN) ; // Wait until done
}
vandenplas



Joined: 08 Sep 2010
Posts: 3
Location: Lincoln

View user's profile Send private message

PostPosted: Sun Sep 12, 2010 12:36 pm     Reply with quote

Many thanks - that is excellent
I will give it a try!!!
Sorry for the slow reply but the system failed to email a notification of a posting!


Last edited by vandenplas on Mon Sep 20, 2010 7:30 am; edited 1 time in total
vandenplas



Joined: 08 Sep 2010
Posts: 3
Location: Lincoln

View user's profile Send private message

Yes, this is now working ok
PostPosted: Mon Sep 20, 2010 7:29 am     Reply with quote

Many thanks for this solution

It has fixed the original problem

Thanks again
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