can I generate a PWM frequency of 5HZ using PIC16F877

Thread Starter

ParkerIII

Joined Aug 16, 2009
4
:(i need to generate the lowest possible PWM frequency using a PIC16F877. I've tried using maximum values for PR2(i.e 255) and to slow down Timer2 with a prescale option of 1:16, but I can only go down to 247Hz.

I'm using a crystal oscillator of 4MHz and assembly as a programming language...

Tx!
 

jpanhalt

Joined Jan 18, 2008
11,087
You probably can't do it with the simple hardware PWM as you found out.

That means a software method, which is simply toggling a pin high and low with delays. Check this site for code to develop the concatenated loops needed: http://www.piclist.com/techref/piclist/codegen/delay.htm

There are also interrupt driven methods, but if you don't have a lot of other things going on, the simple toggle works.

John
 

Thread Starter

ParkerIII

Joined Aug 16, 2009
4
You probably can't do it with the simple hardware PWM as you found out.

That means a software method, which is simply toggling a pin high and low with delays. Check this site for code to develop the concatenated loops needed: http://www.piclist.com/techref/piclist/codegen/delay.htm

There are also interrupt driven methods, but if you don't have a lot of other things going on, the simple toggle works.

John

Tx!, but I will try the interrupt approach as the PIC will also be doing other stuff..
 

thatoneguy

Joined Feb 19, 2009
6,359
Look for sample code to control R/C type Servos, they need to send a pulse out between 1 and 2 milliseconds to hold position.

Most of them also do other tasks as well, so control the servo using interrupts.

The code should be a good start to modify for your PWM project. I believe I've posted it here before, but am not entirely sure, there is a lot of code in older posts. :)
 

eblc1388

Joined Nov 28, 2008
1,542
Tx 4 the suggestion, but can you tell me where to start I've been at it for hours but can't seem to get it right:mad:
The easiest is to assume your 5Hz output can be subdivided into 256 PWM steps, so you ends up with a 8-bit 5Hz PWM output.

You will need two variables, COUNT and PWMX. PWMX acts as the desired PWM duty cycle value and another one acts as counter.

Now configure the PIC timers so that interrupt occurs at 256x5Hz = 1280Hz.

Inside the interrupt routine, increment COUNT. If the value of COUNT equals to PWMX value, set any port pin HIGH.

If the 8-bit COUNT variable rolls over to zero, set that port pin to LOW.

If you then changes the value of the PWMX in your main code, the output 5Hz PWM will change along.

That's all to it really.
 
Top