# How to translate a frequency in PIC

Status
Not open for further replies.

#### jenniferkpj

##### New Member
Hi all. I am using an external crystal 10MHz and would like to somehow output a 5.6MHz using software codes. For some reason, the output of my PIC has a frequency ~ 136kHz (too slow). Any ideas?

Thanks!

#### ericgibbs

##### Well-Known Member
Hi all. I am using an external crystal 10MHz and would like to somehow output a 5.6MHz using software codes. For some reason, the output of my PIC has a frequency ~ 136kHz (too slow). Any ideas?

Thanks!
hi J,
The PIC divides the crystal freq by 4, so the 'internal' clock is 2.5MHz.!

#### blueroomelectronics

##### Well-Known Member
Some 24H series PICs have an very very flexible PLL. You can get some amazing frequency combinations just by setting the PLL

#### UTMonkey

##### New Member
The PIC divides the crystal freq by 4, so the 'internal' clock is 2.5MHz.!
How true, thats 2.5 million "single cycle" instructions per second.

If you are using software that would be:-
• Bring output pin High = 1 Cycle
• Bring output pin low = 1 Cycle
• Repeat = 1 cycle (I think)

So in 3 cycles a pin has been brought high and then low, so in reality you have 2.5 million cycles / 3 = .833Mhz output.

But thats not all, if you wanted a 50% duty cycle and based on the above process you would need to insert a NOP after the HIGH to compensate for the repeat instruction - so thats 4 cycles or .625Mhz.

But thats if you do it in software. If your PIC has hardware PWM the best you could do with a 10Mhz crystal would be 2.5Mhz (without all the above kerfuffle)

Feel free to correct my maths.

All the best.

Mark

#### nickelflippr

##### Member
But thats not all, if you wanted a 50% duty cycle and based on the above process you would need to insert a NOP after the HIGH to compensate for the repeat instruction - so thats 4 cycles or .625Mhz.
Mark
If the OP han an 18f Pic with HSPLL, than you are back to 2.5Mhz (4x.625Mhz = 2.5Mhz).

Code:
MAIN
bsf	PORTB,5,ACCESS
bcf	PORTB,5,ACCESS
goto	MAIN

#### UTMonkey

##### New Member
Never mucked around with HSPLL (he says not having the faintest idea).

You still need the NOP though in your code.

#### monkeybiter

##### Member
If I remember rightly chips such 18f4550 can generate a clock of 48MHz from a 4MHz input [neccessary for usb operation]

##### Banned
I gave the PDF a quick once over, it does say you can run the system clock off of the 48mhz pll. The only thing I didn't find was what the max main system clock allowance is, just because you have a 48mhz clock doesn't mean you can use it to clock the system. You may though.

Last edited:

#### blueroomelectronics

##### Well-Known Member
Yes the 18F4550 can use the 48MHz clock, it's a 12MIPs PIC.

The 40MIPs 24H are such speed demons.

#### nickelflippr

##### Member
Never mucked around with HSPLL (he says not having the faintest idea).

You still need the NOP though in your code.
High Speed Phase Lock Loop, not that I understand it completely either, 4x clock multiplier. Agreed, the duty cycle not symmetric with my example.

The 18fxxkxx has a HSPLL of 64Mhz for 16 Mips.

#### blueroomelectronics

##### Well-Known Member
The 18fxxkxx has a HSPLL of 64Mhz for 16 Mips.
I'll have to get me one.

##### Banned
I'd rather run an AVR at 20mhz and get 20MIPS =)

#### nickelflippr

##### Member
I'd rather run an AVR at 20mhz and get 20MIPS =)
Perhaps on Pic 18f? For sure on the 12f, 16f Pics. From a strictly bit toggle case as presented, it appears to me that the same (or more?) instruction cycles are required for the AVR as the PIC. The sbi, sbc, and rjmp are all 2 cycle instructions?

Code:
START:
sbi	PORTB,0
cbi	PORTB,0
rjmp	START
Looking at an m168 with 8Mhz intosc and its saying 1.358Mhz with my cheapo Radio Shack DVM. And for the 18f4620 Pic with an 8Mhz intosc and PLLEN enabled, measured 2Mhz.

Granted this is not likely a real world case, don't want to start a PIC-AVR throw-down either. Total newb at AVR. Perhaps there is another instruction for the AVR that improves the bit toggle code?