can I generate a PWM frequency of 5HZ using PIC16F877

Discussion in 'Embedded Systems and Microcontrollers' started by ParkerIII, Aug 16, 2009.

  1. ParkerIII

    Thread Starter New Member

    Aug 16, 2009
    :(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...

  2. jpanhalt


    Jan 18, 2008
    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:

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

  3. ParkerIII

    Thread Starter New Member

    Aug 16, 2009

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

    Active Member

    Apr 18, 2009
    I ran into the same proplem once. The interrupts are the best aproach if you have to do other stuff aswell.
  5. ParkerIII

    Thread Starter New Member

    Aug 16, 2009
    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:
  6. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    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. :)
  7. eblc1388

    AAC Fanatic!

    Nov 28, 2008
    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.
  8. ParkerIII

    Thread Starter New Member

    Aug 16, 2009
    Tried something similar and it works,
    Tx for the idea..:D