com/)
Home (https://electrosome.com) /
Tutorials (https://electrosome.com/category/tutorials/), PIC Microcontroller (https://electrosome.com/category/tutorials/pic-microcontroller/),
MikroC (https://electrosome.com/category/tutorials/pic-microcontroller/mikroc/) /
Generating PWM with PIC Microcontroller using CCP Module
MikroC Functions
PWM1_Init(constantlong frequency) :This function initializes the PWM module with duty ratio 0. Frequency parameter is the
desired frequency in Hz. It should be a numeric constant, should not be a variable.
PWM1_Set_Duty(unsigned short duty_ratio) :This function is used to set the duty cycle of the PWM. The parameter duty_ratio
takes values from 0 to 255, ie 0 means 0% , 127 means 50% and 255 means 100% duty cycle. The PWM1_Init() routine must be
called before using this.
PWM1_Start() :This function starts the PWM output. PWM1_Init() must be called before calling this routine,
PWM1_Stop() :This function stops the PWM output. PWM1_Init() must be called before calling this routine. PWM1_Start() should be
called before calling this function otherwise calling this function will not have any e ect as PWM module is not running.
Note 1 : For microcontrollers with more than one CCP module, to use the desired CCP module for PWM generation simply change the
number 1 in the prototype with desired module number. eg: PWM2_Init(5000), PWM2_Start().
Note 2 :All PWM modules in a PIC Microcontroller uses Timer 2 for its operation, so you cannot set di erent frequencies for di erent
PWM modules.
In this tutorial we are using PIC 16F877A for demonstrating PWM generation using CCP module. PIC 16F877A contains two CCP modules.
The output of the two CCP modules are given to a CRO to observe the changes in pulse width. It may be given to two LEDs and you can
see the changes in brightness as Duty Ratio changes.
MikroC Program
void main()
{
short current_duty_1 = 16; // initial value for current_duty_1
short current_duty_2 = 16; // initial value for current_duty_2
I thinks the program is selfexplanatory, so if you have any doubts please comment below
Download
You can download the MikroC Source Code, Proteus les etc here
Video
Generating PWM with CCP Module of PIC Microcontroller
Related Posts:
Generating PWM
with PIC Temperature Interfacing LCD with Interfacing EM-18
Microcontroller Controlled Fan using PIC Microcontroller RFID Module with PIC Interfacing LCD with
MPLAB XC8 Arduino CCS C Microcontroller Arduino Uno
Author
Ligo George (https://electrosome.com/author/lijoppans/)
I am a technology enthusiast who likes to work with circuits, microcontrollers, linux, RTOS, IoT etc. You can hire me for your project, just
drop a mail to [emailprotected] (mailto:) I do research and developments in hardware, software and rmware. Follow Me on LinkedIn
(https://www.linkedin.com/in/ligo-george-726b482b/)
hassan590
Sir i cant understand the command if (!RD0_bit) can we use if(RD0==1) instead of if (!RD0_bit)???
Ligo George (http://www.electrosome.com/)
if(!RD0_bit) is equivalent to if(RD0_bit == 0) or if(PORTD.F0 == 0) or if(!PORTD.F0)
sti
sir, can i have the hitech c equivalent code of the above code????
sairamya
sir can you give the code for running two dc motors using di erent pwm
Give the PWM outputs of PIC to enable inputs of L293D. Just add the code for controlling clockwise or anticlockwise direction of
motor to the above code..
adpeian
Sir how to control the motor using the electronic speed controller and pic using pic16f628a? what is probable source code of
mikroC for that?
emufambirwi
Thanks so much for the tutorial. How do I adapt this code and circuit for H-Bridge Sine wave Inverter
adpeian
thank you sir. another question sir what is the output of the pic using ccp1 module to the input of the electronic speed controller?
and how can i control like a servo controller? thank you sir
emufambirwi
I am a bit of newbie, maybe what I should have asked for is how to generate the sine wave form from the same PIC and use PWM
to make an H-bridge Inverter
adpeian
sir we are using electronic speed controller rated 40amps. to control the speed of the brushless motor likewise the brushless
motor nee. The electronic speed controller is like a servo motor with input signal whose pulse width varies from 1 ms to 2 ms. We
want to have a program that can generate these pulses to turn on and control the brushless motor. Do you have a program for
that sir?
adpeian
the brushless motor needs the esc to operate
AGU
HOW CAN I MEASURE THE SPEED OF MOTOR?
Hwang
Hi, can i know the 12V power supply using what ampere? thank you very much =D
http://www.electrosome.com/pwm-pic-microcontroller-hi-tech-c/ (http://www.electrosome.com/pwm-pic-microcontroller-hi-tech-
c/)
Zahid Aftab
can we control heater with this pwm, and the tempreture of heater can control with tempreture controller and the digital output of
tempreture controller replace with digital INCREASE or DECREASE inputs
karuna
plz suggest me a microcontroller having more than four independent PWM.currently I am using dsPIC33f family that has 4 PWM
http://www.microchip.com/maps/microcontroller.aspx (http://www.microchip.com/maps/microcontroller.aspx)
hystrix
sir, how to combine pwm coding in micro c with coding of dc motor ?
hystrix
thank you sir,,means that we do not need to combine dc code,,just put pwm coding.
one more thing,,i tried to combine micro c coding for stepper,dc and servo motor but i got some errors..can you give me some tips
on how to combine micro c coding,,,actually im doing training kit using pic 16f877aa part of that contains dc,, stepper and servo
motor.i got some problem on how to combine those 3 coding
hope can help,,really appreciate
hystrix
thanks sir
Sheik Mohammed
Mr.Ligo, Good work. Im trying to generate code for the following.
I wanna generate two independent pulses- So we will PWM1 and PWM2 with same frequency.
The pulses are of same duty cylcle.. But, There should be a delay between two pulses.
For example- Let us say the Freq is 1000. therefore the Time = 1ms In this case, Pulse 1 starts at 0 sec, second pulse should start
after 0.5ms. What is the solution. I have tried so many things using PWM1 aND pwm2 WITH DELAY. bUT, NOTHING WORKS>>>
yusuf
why cant we control ac mains..??
rohit
what will be the pwm code for PIC18F4431??? or can i used above one..same
rohit
i want to write a pwm code for BLDC motorplease help me
Ogbachi Ibrahim
Ligo,
I would want to nd out if i can use PIC16F877A and the code above to control 2-phase interleaved dc-dc converter? I am using four
MOSFETs of which i will have PWM on two going from high to low and the other two will have PWM going from low to high.
Constant duty cycle of 0.5, frequency is 50kHz. Please advise.
PACO MAN
How ca i use this code for a sine sine wave 60 hz using the 16f877a.
Chetan Kumar
Sir can you give the code for switching 4 switches with di erent pwm
Alexis
Hi,
Im looking for a solution for every time a rising edge occurs on RB1 I should increase the duty cycle by 10 on the signal provided by
RB0.Every time a rising edge occurs on RB2 I should decrease the duty cycle by 10% on the signal provided by RB0.
I achieved to generate a PWM signal on RBO with a period of 200us and a 10% duty cycle but I dont know how increase or decrease
the duty cycle, using only a main.c solution.
Thanks
John M.
Thank you very much for all of these, it really shed some light on how can I use PIC18F for such an application. This design is
actually what I really need except for 1 thing, I need the PWM output to be at 25Khz. As you have attached the hex and source les
here, I tried to modify it myself from 5000 to 25000. But Im having problem compiling it to hex. I tried cx8 and Hi-tech PICC
compiler to no avail. The cx8 shows Error [1098] while the PICC tool suite shows Halting build on rst failure as requested. error.
Can you help me in modifying the code to 25khz and compiling it? Im really sorry for Im just a newbie in PIC programming. Also,
will the circuit be able to function properly with 25khz output? Thanks! =)
John M.
Ok I got it now.. Ill try to program this on the PIC tommorow, and I hope it works.. Thanks!
John M.
Hello Ligo George.. Thank you very much! I built one of this and it worked perfectly for my application (Fan PWM controller).. Now
Im planning to use this for some other application, but rst, will this work on 120Khz output? Im also thinking about hooking it up
to an LCD to show the current duty cycle, or maybe the fan rpm someday if I can learn how to program it.. Thanks a lot!
mahee
i want to control a LED using PWM according to a variable resister(protentio meter). can you plese explain me the micro c code for
it? ADC to PWM . using micro controller
AHMEDQ Kh
hello dear
can i use pwm for buck and boost ?
my regards
ishra
Sir can you please provide me the code to generate a single pulse with a speci c frequency to drive a stepper motor one step?
AHMEDQ Kh
thanks bro
please i have been written that code of buck converter but my mosfet can not be work
please can you help my how to inter that code
shelaz
can i have the cicuit for pwm
Azhar Hayat
hello , I wanna help with the title Closed-loop DC Motor Drive for
maintaining Constant speed & Torque on Variable loads. I want to use PIC controller with PWM for this purpose and in addition ,
PID controller would be used to handle the feedback signal and give it to the Motor drive circuitry.
Nustian
hey Ligo, can you tell me the rating of the motors for which this cct is applicable? and is it veri ed in terms of hardware ???
sujith thycattussery
Please post a tutorial on 50hz pure sinewave generator.
Akito
Can you upload example code CCP module for PWM generation for PIC32?
Dal
Hi can you make a simple program explaining this above concept
I will be thankful
Debabrata Ghosh
Dear Mr. Ligo
Can you help me providing a simple code of PWM for PIC 12F675 with which I can build a simple obstacle sensor.
I am a senior citizen and passing my time with experimenting with di erent circuit (discreat ICs) & interested in PIC, already
experimented with simple PIC codes published in net. I have read your tutorials and liked them as those are very clearly
explained. My requirement is like this
1) One pin of PIC 12F675( with this I am comfortable) will deliver 40kHz signal to an ultrasonic Tx module at a regular intervels.
2) Another pin of PIC as input where another Ultrasonic RX is connected will receive the re ected signal.
3) 3rd. pin will produce PWM signal whose duty cycle will change in proportion with the received signal strength.
That mean if I connect a LED here, its brightness will be maximum when the obstacle is very close to the sensors
Try to help me, Please send the code to my mail. I have Micro C.
Thanks.
D. Ghosh.
[emailprotected] (mailto:debabrata1449@yahoo.com).
FAch
Hi, I am using pic16f887 to generate 3 pwm signals for my 3 phase ac motor. I have done the rst 2 pwm signals because there are
in built modules for pwm in the pic but the third one i need to make with interrupts and timers. Could you please help? And due to
it being a 3 phase motor, the three pwm signals have to spaced by 120degrees, any ideas how to do that aswell please. thanks
yeshwanth
hie can tell me how u compared both sine and triangle wave for to get pwm??
mahee
thank you it worked. can you tell us how can we control a motor using PID controlling according to an analog input. please put a
tutorial on that. i think that is one of major issue among most students.
mahee
Sir, can you tell us how can we control a motor (both DC and AC) using PID controlling according to an analog input. please put a
tutorial on that. i think that is one of major issue among most students. (MIkRO C and arduino)
Zeus
in stead of using switches cant we use a pot to change duty cycle
Vani
What is the maximum switching frequency I can achieve using PIC16F877A
alia
#include
#fuses hs,nowdt,nobrownout,noprotect
void main()
set_tris_b(0); //set portB as output. portB set as ccp1 connected to green light.
while(1)
if(input(pin_d0))
{
set_pwm1_duty(150);//at 7.03v
set_timer2(0);
delay_ms(40);
set_pwm1_duty(175);//at 8.20v
set_timer2(0);
delay_ms(40);
set_pwm1_duty(200);//at 9.44v
set_timer2(0);
delay_ms(40);
set_pwm1_duty(225);//at 10.55v
set_timer2(0);
delay_ms(40);
set_pwm1_duty(250);//at 11.72v
set_timer2(0);
delay_ms(40);
set_pwm1_duty(255);//at 11.96v
set_timer2(0);
delay_ms(40);
else
output_high(pin_b3);//to on enable
set_timer2(0);
delay_ms(40);
set_pwm1_duty(175);//at 8.20v
set_timer2(0);
delay_ms(40);
set_pwm1_duty(200);//at 9.44v
set_timer2(0);
delay_ms(40);
set_pwm1_duty(225);//at 10.55v
set_timer2(0);
delay_ms(40);
set_pwm1_duty(250);//at 11.72v
set_timer2(0);
delay_ms(40);
set_pwm1_duty(255);//at 11.96v
set_timer2(0);
delay_ms(40);
i dunno what happen, but when i attached to my 9v battery, they only give red color..
Arul Chozhan
hi sir am new to mikro c compiler,in eariler am using mplab and ccs compilier. i need to pwm program for pic18f66k80 how can i
use the same code for that ic please tell me . thanks in advance
Iman Nurashid
sir ligo, how can i control the speed of two dc water pump one for inlet and one for outlet.. im using pic16f877a with ultrasonic as
a sensor i need it asap for my nal year project
i appreciate it if u help me
Simon Ravyts
I am new to PIC and I would like to use it to make 2 PWM signals that are shifted. Is there a standard command/way to do this?
kK
hello sir thank you for you share this with us.. for inverter i am using spwm technique for 1Khz carrier frequency. and picf16f**&
chip. can you plz help me in cooding for this converter.?
kK
hello sir thank you for you share this with us.. for inverter i am using spwm technique for 1Khz carrier frequency. and pic16f886
chip. can you plz help me in cooding for this converter.?
Ligo George (http://www.electrosome.com/)
Sorry, I dont have any codes for inverter.
Mustafa Adenwala
Hey, I am making SPIM using micro controller, So can I provide pulses to my inverter with this (16F877A) micro controller?
Kunal Mahajan
Hello Sir,
Paulino jr.c
when i use PWM2_Stop() the PWM1 stop also. Why ?
Ajeet Kumar
Can we generate 50kHz PWM signal using PIC16F883?
Er Nitin Ahuja
sir, i have to generate a pulse of 2 second period and 50 % duty cycle in pic16f877a having clock frequency is 8 mhz. please help
ghaghabay
Hi dear George, rst of all thank you,sir i need to 3 phase pwm for 3 phase inverter progect with 120 and 240 degree phase shift?
can you help me?
regards
HASAN
I need code for PIc18
lakshmi
sir how can i produce two pwm signals with same duty cycle and a delay between two using mikroC I tried and i am not able to give
delay
lakshmi
void main() {
TRISC.F2=0;
TRISC.F1=0;
T2CON.T2CKPS1=0;
T2CON.T2CKPS0=1;
PR2=0XF9;
CCP1CON.CCP1M3=1;
CCP1CON.CCP1M2=1;
CCP1CON.CCP1M1=0;
CCP1CON.CCP1M0=0;
CCPR1L=0X52;
CCP1CON.CCP1X=0;
CCP1CON.CCP1Y=0;
T2CON.TMR2ON=1;
DELAY_us(250);
T2CON.T2CKPS1=0;
T2CON.T2CKPS0=1;
PR2=0XF9;
CCP2CON.CCP2M3=1;
CCP2CON.CCP2M2=1;
CCP2CON.CCP2M1=0;
CCP2CON.CCP2M0=0;
CCPR2L=0X52;
CCP2CON.CCP2X=0;
CCP2CON.CCP2Y=0;
T2CON.TMR2ON=1;
while(1);
}
this is the code and i am getting two PWM without delay
Umair Rehman
Do anybody have a code for PLECS [C-subscript] for PWM generation of three phase induction motor?
eee angels
how to generate 3 di erent delayed pulses to switch on 3 mosfets in micro c?
nj
in which software i do the programming
Phunsukh Wangdu
Please help mei want to control light brightness by using PIC Micro
controller with button (PWM)..I found this in your site. Why my LED
always blinkingin oscillation PWM working. Please give me a proper
solution or give me a source code for light brightness control PIC with
push buttons
Rolando Aguilera
Do you know how to generate SPWM with a TI microcontroller?
Mark Alfeche
Sir our thesis is all about buck-boost converter using pic16f877a..can you help me how to use the pins on that microcontroller so
that the switch of our microcontroller is that pic
RELATED POSTS
RECENT POSTS
CATEGORIES
Electronics (https://electrosome.com/category/electronics/)
Opamp (https://electrosome.com/category/electronics/basic-electronics/opamp/)
IC (https://electrosome.com/category/electronics/ic/)
Sensors (https://electrosome.com/category/electronics/sensors/)
Featured (https://electrosome.com/category/featured/)
IoT (https://electrosome.com/category/iot/)
Mobiles (https://electrosome.com/category/mobiles/)
News (https://electrosome.com/category/news/)
Processors (https://electrosome.com/category/processors/)
Projects (https://electrosome.com/category/projects/)
Embedded (https://electrosome.com/category/projects/embedded-projects/)
Arduino (https://electrosome.com/category/projects/embedded-projects/arduino-embedded-projects/)
ESP8266 (https://electrosome.com/category/projects/embedded-projects/esp8266-embedded-projects/)
IoT (https://electrosome.com/category/projects/iot-projects/)
Softwares (https://electrosome.com/category/softwares/)
Tablets (https://electrosome.com/category/tablets/)
Tutorials (https://electrosome.com/category/tutorials/)
8085 (https://electrosome.com/category/tutorials/8085/)
Arduino (https://electrosome.com/category/tutorials/arduino/)
ESP8266 (https://electrosome.com/category/tutorials/esp8266/)
MATLAB (https://electrosome.com/category/tutorials/matlab/)
DipTrace (https://electrosome.com/category/tutorials/pcb/diptrace/)