Rearranging these formulae to get value of CCPR1L and CCP1CON will give:ĬCPRIL:CCP1Con = PWM Duty Cycle / (Tosc * TMR2 Prescale Value) PWM Duty Cycle = (CCPRIL:CCP1CON) * Tosc * (TMR2 Prescale Value)
The PWM duty cycle time can be calculated by using the below formulae: So we have use to other two bits of CCP1CON (CCP1X and CCP1Y) to store the last two LSB and then store the remaining 8 bits in the CCPR1L Register. Our PWM signal has 10-bit resolution hence this value cannot be stored in a single register since our PIC has only 8-bit data lines. Now let us set the duty cycle of the PWM by using the below function In our tutorial I have assigned PWM_freq = 5000 so that we can get a 5 KHz operating frequency for our PWM signal. Once the frequency is set this function need not be called again unless and until we need to change the frequency again. PR2 = (_XTAL_FREQ/ (PWM_freq*4*TMR2PRESCALE)) – 1 We know that Period = (1/PWM_freq) and Tosc = (1/_XTAL_FREQ).
PR2 = (Period / (4 * Tosc * TMR2 Prescale )) - 1 Rearranging these formulae to get PR2 will give PWM Period = * 4 * TOSC * (TMR2 Prescale Value) The desired frequency can be set by using the below formulae The value of the frequency has to be written to the PR2 register. Now, we have to set the Frequency of the PWM signal. The timer module’s prescaler is set by making the bit T2CKPS0 as high and T2CKPS1 as low the bit TMR2ON is set to start the timer. The above function is the PWM initialize function, in this function The CCP1 module is set to use PWM by making the bit CCP1M3 and CCP1M2 as high. TRISC2 = 0 // make port pin on C as output T2CKPS0 = 1 T2CKPS1 = 0 TMR2ON = 1 //Configure the Timer module PR2 = (_XTAL_FREQ/(PWM_freq*4*TMR2PRESCALE)) - 1 //Setting the PR2 formulae using Datasheet // Makes the PWM work in 5KHZĬCP1M3 = 1 CCP1M2 = 1 //Configure the CCP1 module One is the PWM_Initialize() function which will initialize the registers required to set up PWM module and then set the frequency at which the PWM should operate, the other function is the PWM_Duty() function which will set the duty cycle of the PWM signal in the required registers. There are two important functions in this program to generate PWM signals. Configure the CCP1 module for PWM operation.Set the TMR2 prescale value and enable Timer2 by writing to T2CON.Make the CCP1 pin an output by clearing the TRISC bit.Set the PWM duty cycle by writing to the CCPR1L register and CCP1CON bits.Set the PWM period by writing to the PR2 register.The following steps should be taken when configuring the CCP module for PWM operation: So, once the configuration bits are set and program is written to read an Analog value, we can proceed with PWM. This tutorial assumes that you have already learnt to use ADC in PIC if not, read it from here, because we will skip details about it in this tutorial. That is 0-1024 will be converted to 0%-100% Duty cycle. Then we generate a PWM signal with frequency 5000Hz and vary its duty cycle based on the input Analog voltage. In our program we will read an Analog voltage of 0-5v from a potentiometer and map it to 0-1024 using our ADC module. The following registers are used to generate PWM signals using our PIC MCU: There are two CCP modules in our PIC MCU (CCP1 And CCP2), this means we can generate two PWM signals on two different pins (pin 17 and 16) simultaneously, in our tutorial we are using CCP1 to generate PWM signals on pin 17. The resolution of our PWM signal is 10-bit, that is for a value of 0 there will be a duty cycle of 0% and for a value of 1024 (2^10) there be a duty cycle of 100%. PWM signals can be generated in our PIC Microcontroller by using the CCP (Compare Capture PWM) module. In our tutorial we will set a frequency of 5KHz. One Period is complete ON and OFF of a PWM signal as shown in the above figure.
The frequency of a PWM signal determines how fast a PWM completes one period. If the signal is always ON it is in 100% duty cycle and if it is always off it is 0% duty cycle.ĭuty Cycle =Turn ON time/ (Turn ON time + Turn OFF time) The percentage of time in which the PWM signal remains HIGH (on time) is called as duty cycle. There are two important parameters for a PWM as discussed below: The time during which the signal stays high is called the “on time” and the time during which the signal stays low is called the “off time”. This signal is set high (5v) and low (0v) in a predefined time and speed. Pulse Width Modulation (PWM) is a digital signal which is most commonly used in control circuitry.