• Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

PIC Clock question

Status
Not open for further replies.

jenniferkpj

New Member
Hi all! I am having troubles understanding how the clock for PIC18F4682 works. I'm not doing anything fancy. i am using CCS C compiler with the following code:

#fuse HS

main()
{

while(1)
{
output_toggle(PIN_C7);
}
}

The crystals I used have the following frequency output (from PIN_C7):
8Mhz -> 333khz
10Mhz -> 416.67khz
16Mhz -> 666.7khz

It looks like the input for the PIC's clock (OSC1) is being divided by 24 for the output (ex: 8Mhz/333khz = 24, 10/416.67khz = 24, 16Mhz/666.7khz = 24). I saw people posting the system clock should be the input clock divided by 4 (ex. 10Mhz input should have a system clock of 2.5Mhz)

Are there a lot of low level assembly instructions for the function out_toggle() that would cause the low frequency when i output? or is there a setting I should look into to increase this output frequency?

Thanks a lot!
 

Nigel Goodwin

Super Moderator
Most Helpful Member
Examine the code that the C compiler produces, it doesn't look to be very efficient.

If you're wanting something precise and easy, use assembler, which for such a simple program would be trivial anyway.
 

mister_e

New Member
If you need a constant frequecy/clock, I would suggest to use CCP module in PWM mode. @8MHz, you can produce 1MHz PWM out
 

jenniferkpj

New Member
Thanks for the suggestions. In fact I might not have enough time to learn about CCP module in PWM mode. I am still fairly new to PIC. Would anyone suggest a more simple solution to my problem?

Thanks in advance.
 
If you're just wondering why the the output at that pin (C7) has a lower frequency, then it's probably because output_toggle and while(1) statements are compiled into 12 assembly instructions that are executed at the oscillator's frequency.

The output freq is divided by 24 because the pin must be toggled twice for each period.
 
Status
Not open for further replies.

Latest threads

EE World Online Articles

Loading
Top