Pulse Width Modulation (PWM) with PIC Microcontroller – Flowcode
Watch the Video Tutorial
The CCP/ECCP is a multipurpose peripheral module inside most PIC Microcontroller devices. CCP which stands for Capture, Compare and Pulse width modulation while ECCP stands for Enhanced Capture, Compare and Pulse width modulation. The module includes four unique but related peripherals:
- Capture mode: The peripheral allows timing of duration of an event.
- Compare mode: The peripheral constantly monitors a timer counter value and compare it to a value set in the application. When they match, it will trigger an event.
- Pulse Width Modulation (PWM): The peripheral will produce a variable pulse width signal based on a pulse width and period value set in the applications code.
- The Enhanced Pulse Width Modulation (EPWM): The peripheral will produce a variable pulse width signal based on a pulse width and period value set in the applications code along with Auto-shutdown, Auto-restart, Dead-band Delay and PWM Steering modes. 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
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. This is the simplest technique that can be used to produce analog voltages from a digital one.
The fraction of the period for which the signal is ON to the total period is known as the duty cycle. The average DC value of the signal can be varied by varying the duty cycle. The duty cycle can be anywhere between 0 (signal is always off) to 1 (signal is constantly on). 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. 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.
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).
The Capture/Compare/PWM (CCP2 in PIC18F26K20) module is very versatile. 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 CCP2 module has two 8-bit registers, called CCPR2L and CCPR2H. 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 is controlled by the CCP2CON register.
In PWM mode, the CCP module produces up to a 10-bit resolution PWM output on the CCPx pin. To operate in PWM mode, the CCPx pin must be configured for output. Figure 4 below shows a simplified block diagram of PWM operation.
PWM with Flowcode
As with other peripherals, Flowcode makes it super easy to configure PWM.
Figure 5: Inserting a PWM component
Under the output components category, select the PWM. In this example we inserted it in the 3D System panel. Select the component to set its properties as shown on figure 6 below.
- Channel: You can select the CCP channel you are going to use, this will depend from PIC to PIC as some PIC models have more channels than others. The PIC18F26K20 has two CCP channels, CCP1 on Pin RC2 (Enhanced Capture, Compare and Pulse width modulation) and CCP2 on Pin RC1 (Standard Capture, Compare and Pulse width modulation). In this example channel 2 was selected. The default PWM pin is set to PORTC.1
- Alternative pin: If the PIC allows, a different PWM hardware pin can be reassigned to another pin .
- PWM Timer: This is the Timer associated with PWM to drive the output. Please note that all PWM channels linked to the same timer will share the same period and prescaler settings.
- Period Overflow: This property is of type Unsigned integer and can be referenced with the variable name period. It allows the user to change the number of counts for the whole PWM period.
PIC/AVR Range: 0 – 255
16-bit PIC Range: 0 – 65535
- Prescaler: This property is of type Fixed list of ints and can be referenced with the variable name prescale. It allows the user to change the number of program cycles per PWM cycle count.
- Period (us): This property is of type Floating point and can be referenced with the variable name period_calc. It displays the length of time to complete one PWM cycle.
- Frequency (KHz): This property is of type Floating point and can be referenced with the variable name frequency_calc. It displays the frequency of PWM cycles per second.
The PWM has 5 macrons:
- ChangePeriod: This sets the overall period and prescaler of the output PWM signal. It takes 2 parameters:
UINT Period: Which is the maximum number that will represent 100% on, PIC/AVR: 0-255 16-bit PIC: 0-65535
INT Prescaler: Which is the scaler used to divide the system clock speed down to the PWM rate.
- Disable: This disables a PWM channel and allows the default output / input state to be resumed
- SetDutyCycle: This sets the PWM duty cycle in terms of on/off based on the current period setting.
E.g. if period = 255 then duty of 128 is equal to 50% on and 50% off.
16-bit PIC users should use the 10bit duty function to access the full range.
- Enable: This enables a PWM channel as an output overriding the default output pin state.
- SetDutyCycle10Bit: 16-bit PICs have a 16-bit period range available. PWM duty PIC/AVR: 0-1023 16-bit PIC: 0-65535
In this example an LED connected to a PWM2 is dimmed by changing the duty cycle continually with some values after a delay of 3 seconds. The project starts with a bright LED 100% duty cycle, then slightly dimmed at 80% then 50%, 30% and finally 10%
The project circuit is shown here below on figure 7.
Figure 7: an LED connected to PWM pin
Below is 80% duty cycle as displayed on the oscilloscope
Figure 9: 80% duty cycle
You can download the full project files (Flowcode source code and Proteus Schematic design) below here. All the files are zipped, you will need to unzip them (Download a free version of the Winzip utility to unzip files).