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

General Clock Question, how is it calculated?

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



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Feb 23, 2009 5:11 pm     Reply with quote

I took your fuse settings and made a test program. I don't have your PIC
but I have a related one. The LED blinks at 1 Hz. So the PIC is running
at 48 MHz with your settings.
Code:

#include <18F4550.h>
#fuses HSPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN
#use delay(clock=48000000)

//==========================
void main()
{

while(1)
  {
   output_high(PIN_B0);  // Blink LED
   delay_ms(500);
   output_low(PIN_B0);
   delay_ms(500);
  }

}
Jakezilla3



Joined: 23 Feb 2009
Posts: 5

View user's profile Send private message

PostPosted: Mon Feb 23, 2009 6:08 pm     Reply with quote

I assume you have a 20MHz external clock?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Feb 23, 2009 6:13 pm     Reply with quote

Yes.
Jakezilla3



Joined: 23 Feb 2009
Posts: 5

View user's profile Send private message

PostPosted: Mon Feb 23, 2009 6:31 pm     Reply with quote

I am sorry if I am not explaining my problem/question very well.

From the help file
Quote:
With an internal clock at 20mhz and with the T1_DIV_BY_8 mode, the timer will increment every 1.6us.


So as I understand it, internal clock is 20MHz, that is divided by 4, then the T1_DIV_BY_8 divides it by 8 more, so 20/(4*8)=625kHz. 1/625kHz = 1.6us, the given value.

When using timers in my code with the settings int the OP, each timer tick is =0.0833333us, which extrapolated backwards through that same argument is -- internal clock = 48MHz, that is divided by 4 is 12MHz, so 1/12MHz is indeed .083333us

Where does this arbitrary divide by 4 take place? I don't see it in the diagram from the datasheet?
Ttelmah
Guest







PostPosted: Tue Feb 24, 2009 3:55 am     Reply with quote

First thing.
The _#USE DELAY_ statement, is for the CPU, not the USB. The USB clock is separate from this.

Start with your external clock. 20MHz. Selecting HSPLL, says 'feed the CPU, from the clock coming from the USB PLL'. PLL5, says 'divide incoming clock by 5, to feed the USB PLL'. So you have 4MHz feeding this. The PLL performs a multiplication by 24, to give the USB master clock at 96MHz. Now, all the Microchip data sheet diagrams in a sense, slightly 'go wrong' at this point. This is divided by 2, to feed the USB system (as shown), but the feed to the CPU divider, also really comes off this 48MHz /2 output. CPUDIV1, then gives a CPU clock at 48MHz. Now this is 'Fosc' for the CPU. If you want to run the CPU at 24MHz, simply select CPUDIV2, and change the delay statement to match this. If you want to run at 20MHz, select 'HS', instead of 'HSPLL', and CPUDIV1, with a 20MHz DELAY statement. You can happily run the CPU, at almost any frequency you want (actually 'handling' the USB timings can become borderline, down at about 4MHz), even if you want using the RC clock. All that matters for USB, is that the external crystal, divided by the PLLx statement, gives 4Mhz. Smile
The 'external clock', on the oscillator pins, is not the internal clock, with HSPLL selected. If you want to use the crystal frequency to run the CPU, use HS. The USB will still run OK.

Then if you now look at the timer diagram (Fig 12.1), note that it's clock, is either a separate external input, or "Fosc/4 internal clock". This is the 'extra' division by 4. In fact almost all the internal peripherals use this clock (commonly called the 'instruction' clock, since it corresponds to one instruction time).

Best Wishes
Jakezilla3



Joined: 23 Feb 2009
Posts: 5

View user's profile Send private message

PostPosted: Tue Feb 24, 2009 1:57 pm     Reply with quote

Thank you so much for that excellent description! I knew that everything works but I just have a hard time accepting things happening behind the scenes unless I know how they are working.

So just a quick recap
external clock (OSC1) = 20MHz
internal clock (FOSC) = 48MHz
instruction clock (FOSC/4) = 12MHz
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