Continue to Site

Welcome to

Welcome to our site! is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Changing the output frequency of a PIC to 2kHz

Not open for further replies.


Newbie level 2
Jul 20, 2009
Reaction score
Trophy points
Activity points
Hi All

I have a question about a 12V Dc Motor project I am undertaking at school. I am using a PIC 16F84A and all I need to do is change the output frequency from the chip to 2kHz to the motor. I have looked in Microchip and searched the forum but I must be looking in the wrong places. Just need a little puch in the right direction.


interrupt 16f628 mikroc timer0 function

In nearly 50 years of experience, I have never seen a motor that needs 2Mhz to drive it. It must be a VERY fast motor!!

Can you re-phrase your question so it explains why 2MHz is needed and not the low frequency or DC that motors usually need.


up/down counter with 16f628a

Theoretically you can use TMR0 to Set at Interrupt at 4Khz and then Toggle the Output.But you could rather go for a 628 like blueroom says as it is much easier to use. Also 84A is an obsolete part which is expensive than the 628

pic frequency interrupt

Trevoor has contacted me by PM to explain the error in his first message which originally said 2MHz not KHz.

As the other have said, the 16F84 is not a good choice for this application and the 16F628A is *almost* identical but has extra features. I have used 628A's as plug in upgrades for 84A devices and mailed them to customers to swap over they are so similar. The 628A is also cheaper and you may be able to utilize it's internal oscilator to reduce your component count as well.

You haven't said why you need 2KHz to drive a DC motor so I'm guessing you are controlling it's speed by PWM. The 628A has built in PWM, the 84A does not.

Having said that, if you really want to use the 84A, this is what you do:

Based on your clock frequency, work out the prescaler ratio and value to pre-load into TMR0 to give a 2KHz roll-over interrupt.

In your interrupt routine, re-load the value into TMR0 for the next roll-over (remember it counts up not down so load the number of remaining counts, not the number you actually want - also remember that as the time in interrupt TMR0 = 0)
Now clear your PWM output pin, this is your frame reference point.

At this point you are resetting you output pin at a 2KHz rate.

Now you need to use a counter in software to set the pin (switch it on). Ideally, you want a counter that reaches maximum value in 500uS so it gives as many possible different values. Set it at zero at the start of your frame period. When you reach the desired count, turn the pin on.

This is only one of many ways you can do it. For example you could reverse the on and off in the example above or you could drive your counter from the timer and provide the framing period in software. You can even do it without the timer at all.


PIC Frequency

simply if you are using MikroC or C18 you can use software delay functions to create 2Khz frequency to the motor, but as most of the previous colleagues mentioned use PIC timer or another PIC with a PWM module.

good luck

Not open for further replies.

Part and Inventory Search

Welcome to