http:\/\/www.pyroelectro.com<\/a><\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\nThis analogy while not being 100% accurate in a theoretical sense. But practically, it’s the way we add sweet & smooth control over light instead of On-OFF behavior with the full lighting power which can be annoying for specific applications. And we can also add a soft-start soft-end motor speed control instead of running an RC robot with 100% speed and breaking to 0% instantaneously which will result in undesirable drifts.<\/span><\/p>\n <\/p>\n
\n\u00a0 \u00a0What Are The Applications Of PWM?\u00a0 \u00a0<\/span><\/strong><\/span><\/h3>\n <\/p>\n
There are numerous situations in which it’s ideal to have a PWM output signal. That’s why there are countless applications which are mainly dependent on the pulse width modulation technique. I’m going to introduce some of them down below:<\/span><\/p>\n\n- Light intensity control.<\/span><\/li>\n
- Motor speed control.<\/span><\/li>\n
- \u0650Audio signal generation.<\/span><\/li>\n
- Servo control (valves, motors, etc).<\/span><\/li>\n
- Voltage regulation.\u00a0By switching voltage to the load with the appropriate duty cycle, the output will approximate a voltage at the desired level. The switching noise is usually filtered with an LC network.<\/span><\/li>\n
- The solar tracking and charging systems.<\/span><\/li>\n<\/ul>\n
And much more, you can google “PWM Applications” to find out hundreds of applications related to PWM.<\/span><\/p>\n <\/p>\n
\n\u00a0 \u00a0CCP Modules – PWM Mode\u00a0 \u00a0<\/span><\/strong><\/span><\/h3>\n <\/p>\n
In PWM Mode, the CCPx pin (CCP1=RC2, CCP2=RC1) produces up-to a 10-Bit resolution PWM output signal. Since the CCP1 pin is multiplexed with the PORTC data latch, the RC2 pin must be configured as an output pin by clearing the TRISC2 bit. And the same goes for the CCP2 pin (RC1).<\/span><\/p>\n\u00a0PWM Mode Diagram\u00a0<\/span><\/h4>\nHere is the logic (Block) diagram for the PWM mode as found in the datasheet (8.3 page-67)<\/span><\/p>\n<\/p>\n
Or equivalently this hand-drawn version for the same diagram<\/span><\/p>\n<\/p>\n
\u00a0CCP Operation In PWM Mode\u00a0<\/span><\/h4>\nAs you might have noticed in the above diagram, the operation of the ccp module in PWM mode goes as follows. First, the CCP module is set to operate in PWM mode by writing to the CCP1CON register. Then, the required PWM frequency will tell us the PWM time period which we’ll be loading to the PR2<\/strong> register. Timer2 should be turned ON with the pre-specified prescaler value. And the duty cycle is set by writing to the [ CCPR1L register & CCP1CON<5:4> Bits ].<\/span><\/p>\nFrom the PWM Block Diagram, the PR2 value is constantly compared against the TMR2 register until a match occurs which indicates the end of the PWM cycle and the starting of a new cycle. Hence, the value of PR2 determines the time period of each PWM cycle and it consequently provides a mean of controlling the PWM output Frequency!<\/span><\/p>\n<\/p>\n
The CCP1 pin (RC2) is set (High = 1<\/strong>) at the beginning of each cycle. And the DC (duty cycle) 10-Bit value is latched to the [ CCPR1H + 2-Bit internal latches ]. This DC value is constantly compared against the [ TMR2 + 2-Bit Q Clock ]. When a match occurs, the output latch for the CCP1 pin (RC2) is reset (Low = 0<\/strong>). And it stays low till the end of the current PWM cycle. At the beginning of a new cycle, the output latch is set again to High and so on. Hence, the DC value controls for how long the PWM output pin will be set (High). Hence, the DC 10-Bit value provides a mean for controlling the PWM output Duty Cycle!<\/span><\/p>\n<\/p>\n
\u00a0Notes For PWM Mode\u00a0<\/span><\/h4>\nI- Clearing the CCP1CON register<\/strong>:<\/span><\/p>\n Clearing the CCP1CON register will force the CCP1 PWM output latch to the default low level. This is not the PORTC I\/O data latch.<\/span><\/p>\nII- Timer2 postscaler<\/strong>:<\/span><\/p>\nThe Timer2 postscaler is not used in the determination of the PWM frequency. The postscaler could be used to have a servo update rate at a different frequency than the PWM output.<\/span><\/p>\nIII- Duty Cycle value<\/strong>:<\/span><\/p>\n If the PWM duty cycle value is longer than the PWM period, the CCP1 pin will not be cleared.<\/span><\/p>\nIV- Double-Buffering & G<\/strong><\/span>litches<\/strong>:<\/span><\/p>\n CCPR1L and CCP1CON<5:4> can be written to at any time, but the duty cycle value is not latched into CCPR1H until after a match between PR2 and TMR2 occurs (i.e., the period is complete). In PWM mode, CCPR1H is a read-only register. The CCPR1H register and a 2-bit internal latch are used to double-buffer the PWM duty cycle. This double-buffering is essential for glitch-free PWM operation.<\/span><\/p>\nV- PWM Resolution<\/strong>:<\/span><\/p>\nThe PWM duty cycle is specified by writing to the CCPR1L register and to the CCP1CON<5:4> bits. Up to 10-bit resolution is available. The maximum PWM resolution (bits) for a given PWM frequency is given by the following formula.<\/span>
\n<\/p>\nThe typical resolution (in bits) for a specific PWM frequency, with a specific Prescaler ratio PS, for a system running at Fosc clock rate is given by the equation down below<\/span><\/p>\n<\/p>\n
<\/p>\n
\n\u00a0 \u00a0Configuring CCP Modules For PWM Mode\u00a0 \u00a0<\/span><\/strong><\/span><\/h3>\n <\/p>\n
Here is the step-by-step procedure that you should follow in order to operate the CCP module in the PWM mode to generate the PWM output signal with your desired duty cycle and frequency.<\/span><\/p>\n\u00a0Configuring The CCP1 Module For PWM Operation\u00a0<\/span><\/p>\nStep1<\/strong> –\u00a0 Configure the CCP module for PWM operation<\/span><\/p>\n\n- As mentioned in the datasheet in the CCP1CON register’s description. Writing 11xx to the CCP1M0:CCP1M3 4-Bits selects the PWM mode of operation. Note that xx means that Bit0,1 are don’t cares! that’s why we’ll neglect them while writing the code.<\/span><\/li>\n<\/ul>\n
Step2<\/strong> –\u00a0 Configure the CCP1 pin (RC2) to be an output pin by clearing the corresponding TRISC2 bit<\/span><\/p>\n\n- The CCP1\/RC2 pin should be configured as an output pin.<\/span><\/li>\n<\/ul>\n
Step3<\/strong> –\u00a0 Determine the PWM frequency and get the PWM period value<\/span><\/p>\n\n- Let’s say we need to have a PWM signal with a frequency of 5kHz. This means that the time period should be = 1\/F = 1\/5000 = 200\u00b5s<\/span><\/li>\n<\/ul>\n
Step4<\/strong> –\u00a0 Using The PWM period, Calculate the value which we’ll load to the PR2<\/strong> Register<\/span><\/p>\n\n- This step involves choosing a value for Timer2 prescaler prior to determining the PR2 register’s value. For this step, we’ll be using the following equation<\/span><\/li>\n<\/ul>\n
<\/p>\n
\n- We now have the PWMperiod<\/sub>, the Tosc<\/sub> = 1\/Fosc<\/sub>, and we’ll pick an initial value for Timer2Prescaler (say 1 for 1:1 ratio). It should be easy to solve for PR2<\/strong> to get its value. The rule of thumb is that PR2 register is an 8-Bit register which means its value ranges from (0 to 255). Your result should never exceed this 255 cap. Otherwise, you should try another value for the Timer2Prescaler.\u00a0<\/span>Finally, after getting the PR2 value, we should move this number to the PR2 register.<\/span><\/li>\n<\/ul>\n
Step5<\/strong> –\u00a0 Set the prescaler of Timer2<\/span><\/p>\n\n- Adjust the Timer2 prescaler value to match the value which you’ve already used in the previous step. This is done by writing to the T2CON register T2CKPSx bits.<\/span><\/li>\n<\/ul>\n
Step6<\/strong> –\u00a0 Step The PWM Duty Cycle, by writing to the CCPR1L register & CCP1CON<5:4> Bits<\/span><\/p>\n\n- To set or change the Duty Cycle of the PWM output signal, you should calculate and write the DC value to the 10-Bit buffer register which consists of CCPR1L:CCP1CON<5:4>. The CCPR1L contains the eight MSBs and the CCP1CON<5:4> contains the two LSbs. This 10-bit value is represented by CCPR1L:CCP1CON<5:4>.<\/span><\/li>\n
- The following equation is used to calculate the PWM duty cycle in time:<\/span><\/li>\n<\/ul>\n
<\/p>\n
\n- Let’s say you’re willing to get 40% DC for your PWM output. Then you should multiply 0.40 by PWMperiod\u00a0<\/sub>To get the PWMDutyCycle\u00a0<\/sub>in time. Now, substitute for PWMDutyCycle\u00a0<\/sub>, Tosc\u00a0<\/sub>, TMR2prescaler\u00a0<\/sub>in the previous equation and solve for the 10-Bit value between the ( )<\/span><\/li>\n
- After getting the 10-Bit duty cycle from your calculations, just write this value to the buffer\u00a0CCPR1L:CCP1CON<5:4><\/span><\/li>\n<\/ul>\n
Step7<\/strong> –\u00a0 Turn ON Timer2<\/span><\/p>\n