Did you find this site useful ? Please
to help www.micro-examples.com

If you need a coder or a freelance programmer, submit your project to me


PIC PWM Calculator & Code Generator

Printer-friendly version | Forums | FAQs |

This calculator will save you from insomnia and headaches !

This page will help you to configure the PIC TIMER2 and PWM modules, you will also get a ready-to-use C source code (for MikroC compiler).

This code generator should work with the following devices :

P12 FAMILY :

PIC12F683

P16 FAMILY :

PIC16F627 PIC16F627A PIC16F628 PIC16F628A PIC16F648A PIC16F684 PIC16F690 PIC16F716 PIC16F72 PIC16F73 PIC16F737 PIC16F74 PIC16F747 PIC16F76 PIC16F767 PIC16F77 PIC16F777 PIC16F785 PIC16F818 PIC16F819 PIC16F87 PIC16F870 PIC16F871 PIC16F872 PIC16F873 PIC16F873A PIC16F874 PIC16F874A PIC16F876 PIC16F876A PIC16F877 PIC16F877A PIC16F88 PIC16F913 PIC16F914 PIC16F916 PIC16F917 PIC16F946

P18 FAMILY :

This should work with all P18 family devices

Please use the forum to report other compliant PICs or errors.


My PIC is clocked at : Mhz
Input your Fosc clock frequency
My PWM frequency must be : Herz
Leave blank to see all solutions (it may take a few seconds, no code will be generated)
My PWM duty cycle must be : %
From 0 to 100 %

Your PWM registers will be here

C source code example : How to smoothly blink two LEDs

/*
 * this example smoothly blinks LEDs on RC1 and RC2 alternatvely
 * using PIC CCP module configured as PWM output
 *
 * source code example for mikroC
 * feel free to use this code at your own risks
 *
 * target : PIC16F877A, 8 Mhz crystal
 * HS clock, no watchdog.
 *
 * easyPIC4 settings :
 *      LEDs on PORTC enabled
 *
 * Author : Bruno Gavand, September 2007
 * see more details on http://www.micro-examples.com/
 *
 *******************************************************************************
 */

void main()
        {
        unsigned char   dc ;

        TRISC = 0 ;                     // set PORTC as output
        PORTC = 0 ;                     // clear PORTC

        /*
         * configure CCP module as 4000 Hz PWM output
         */
        PR2 = 0b01111100 ;
        T2CON = 0b00000101 ;
        CCP1CON = 0b00001100 ;
        CCP2CON = 0b00111100 ;

        for(;;)                         // forever
                {
                /*
                 * PWM resolution is 10 bits
                 * don't use last 2 less significant bits CCPxCON,
                 * so only CCPRxL have to be touched to change duty cycle
                 */
                for(dc = 0 ; dc < 128 ; dc++)
                        {
                        CCPR1L = dc ;
                        CCPR2L = 128 - dc ;
                        Delay_ms(10) ;
                        }
                for(dc = 127 ; dc > 0 ; dc--)
                        {
                        CCPR1L = dc ;
                        CCPR2L = 128 - dc ;
                        Delay_ms(10) ;
                        }
                }
        }
All trademarks and registered trademarks are the property of their respective owners