

The RC1 pin actually shows up with the label CCP2 in green to show that the CCP2 peripheral now controls the I/O pin. The prescaler is selected as 1:16 from the drop-down menu.īy selecting the CCP2: PWM the MCC automatically selects the I/O pin RC1 in the I/O selection window. The time of 1.0 milliseconds is entered for the period to yield a 1KHz frequency. Timer 2 uses the oscillator selected in the System section to adjust the Timer 2 period. This will automatically add the Timer 2 (TMR2) peripheral to your project as well because PWM uses Timer 2. In the “Device Resources” window, which should be located in the lower left of MCC, add the PWM peripheral under CCP to your project by double clicking. The 8 MHz internal oscillator is selected as shown in the picture below, the MCLR pin is also gonna be disabled. The System is where the oscillator speed is selected and any changes to the configuration settings you may need. The module will be configured to create a PWM signal at: 1KHz, different duty cycles will be used 100%, 80%, 50%, 30% and 10% using a 8Mhz system clock and 1:16 prescaler. An LED will be connected to PIC18F26K20 Capture/Compare/PWM peripheral output. In this demonstration, we are going to create a simple example.
MPLAB XC8 PWM CODE
The MCC will automatically generate the code to load the proper registers and initialize the proper values to produce the desired PWM signal.

The steps include setting up the system clock, Timer 2 and the PWM module. We are going to use MPLAB Code Configurator to confugure the PWM peripheral. Figure 4 below shows a simplified block diagram of PWM operation.įigure 4: PWM Simplified Block Diagram – PIC18F26K20 To operate in PWM mode, the CCPx pin must be configured for output. In PWM mode, the CCP module produces up to a 10-bit resolution PWM output on the CCPx pin. The CCP2 module is controlled by the CCP2CON register.įigure 3: PIC18F26K20 – CCP2CON Satyandard Capture/Compare/PWM Control Register Together they form a 16-bit register that can be used for capture, compare or to form the duty cycle of a PWM stream. The CCP2 module has two 8-bit registers, called CCPR2L and CCPR2H. The Capture and Compare features integrate closely with the 16-bit TMR1 and the PWM feature uses the third timer, the 8-bit TMR2. The Capture/Compare/PWM (CCP2 in PIC18F26K20) module is very versatile. PIC18F26K20 has two CCP modules, named as CCP1 on Pin RC2 (Enhanced Capture, Compare and Pulse width modulation) and CCP2 on Pin RC1 (Standard Capture, Compare and Pulse width modulation). This method is commonly used for controlling speeds of DC motors, brightness of lamps, Sine wave inverters, Digital to Analog Converter (DAC) etc. As you can see in this figure 1 below, when On time is small and Off time long, the Bulb hardly gets any time to turn ON, As the ON time is increased and OFF time decreased it gets brighter.įigure 1: Controlling brightness of lamps with PWM signal Suppose, if the signal has +5 V while it is ON and 0 V during OFF condition, then by changing the duty cycle of the signal, the amount of energy transferred to device can be varied. The duty cycle can be anywhere between 0 (signal is always off) to 1 (signal is constantly on). The average DC value of the signal can be varied by varying the duty cycle. The fraction of the period for which the signal is ON to the total period is known as the duty cycle. This is the simplest technique that can be used to produce analog voltages from a digital one. In this article we will discuss the PWM. Pulse width modulation (PWM) is a technique of controlling the amount of power delivered to an electronic load by switching ON and OFF a digital signal. In addition the EPWM can drive various PWM arrangements: Single PWM, Half-Bridge PWM, Full-Bridge PWM, Forward Mode, Full-Bridge PWM, Reverse Mode, Single PWM with PWM Steering Mode

When they match, it will trigger an event.
