100% found this document useful (3 votes)
2K views3 pages

PIC PWM Calculator

This document provides a PWM calculator and code generator for various PIC microcontrollers. The user inputs their microcontroller clock speed and desired PWM frequency and duty cycle. The tool then outputs the appropriate register values and a code example to configure PWM on the selected PIC. It supports PIC12F, PIC16F and PIC18F families for PWM frequencies up to the resolution of 6 bits with prescaling. An example C source code is also provided to smoothly blink two LEDs using the PIC's CCP module configured as a PWM output.

Uploaded by

Neha Sharma
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100% found this document useful (3 votes)
2K views3 pages

PIC PWM Calculator

This document provides a PWM calculator and code generator for various PIC microcontrollers. The user inputs their microcontroller clock speed and desired PWM frequency and duty cycle. The tool then outputs the appropriate register values and a code example to configure PWM on the selected PIC. It supports PIC12F, PIC16F and PIC18F families for PWM frequencies up to the resolution of 6 bits with prescaling. An example C source code is also provided to smoothly blink two LEDs using the PIC's CCP module configured as a PWM output.

Uploaded by

Neha Sharma
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

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-touse 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

Herz My PWM frequency must be : 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 %

TIMER2 Frequency Resolution Prescaler

PWM

REGISTERS PR2 T2CON CCPR1L CCP1CON

(Herz) 38461.54 40000.00 41666.67 41666.67

(Bits) 6 6 6 6 1 0b00011001 0b00000100 0b00001100 0b00111100 1 0b00011000 0b00000100 0b00001100 0b00011100 4 0b00000101 0b00000101 0b00000010 0b00111100 1 0b00010111 0b00000100 0b00001011 0b00111100

C Source code example for highlighted frequency : /* * PWM registers configuration * Fosc = 4000000 Hz * Fpwm = 40000.00 Hz (Requested : 40000 Hz) * Duty Cycle = 50 % * Resolution is 6 bits * Prescaler is 1 * Ensure that your PWM pin is configured as digital output * see more details on http://www.micro-examples.com/ * this source code is provided 'as is', * use it at your own risks */ PR2 = 0b00011000 ; T2CON = 0b00000100 ; CCPR1L = 0b00001100 ; CCP1CON = 0b00011100 ;

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 TRISC = 0 ; PORTC = 0 ;

dc ; // set PORTC as output // 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) ; } }

You might also like