PIC PWM Register Calculator

Use the calculator below to generate the parameters for the PWM Registers. It was designed for the 16F877A, but will work for all chips with the same Registers. It tries to calculate the Registers for all possible values of the prescaler. If a prescaler value results in a PR2 value that is not possible. It will not fill in the values. You can cut and paste the register values from the text area below the calculator. Chose the code for the prescaler you desire. The code is provided 'AS IS'. User assumes all risk.
Parameter Value Unit  
Oscillator Frequency Hz  
PWM Frequency Hz  
Duty Cycle
 
Prescaler
PR2      
T2CON      
CCPR1L      
CCP1CON      
Actual PWM Freq      
Max Duty Value      
     

Sample Code


/*
 * Project name:
     BJD_PWM_Registers
 * Author:
     Barton J.  Dring 08/2007
     Dring Engineering services
     www.eng-serve.com
 * Description:
     This code demonstrates how to set PWM registers manually
 * Test configuration:
     MCU:             PIC16F877A
     Dev.Board:       EasyPIC4
     Oscillator:      HS, 08.0000 MHz
     SW:              mikroC v6.0
 * NOTES:
     None.
*/

void main() {


  TRISC = 0; // need PWM pin (2) to be an output

  /*
  * PWM Register Values
  * Oscillator Frequency Fosc = 8000000
  * Clock Frequency Fclk = 2000000
  * PWM Freq = 10000 desired...actual: 10000
  * Prescaler Value = 1
  * PR2 = 199
  * Maximum duty value = 800
  * Requested Duty Value = 400
  *
  * Code Provided AS IS....Use at your own risk!
  */

  T2CON = 0b00000100; // prescaler + turn on TMR2;
  PR2 = 0b11000111;
  CCPR1L = 0b01100100;  // set duty MSB
  CCP1CON = 0b00001100; // duty lowest bits + PWM mode

  while (1)
  {
  
  }
}
Dring Engineering Services