GENERATING PWM USING PIC MICROCONTROLLER,In this tutorial, you will learn to generate a PWM signal with the help of PIC microcontroller 16F877A. PWM is supposed to be a new concept for the beginners and by the end of the tutorial, you will have a sound knowledge of this term and will be able to work with it in different projects.
What is pulse width modulation ?
PWM (Pulse Width Modulation) is a powerful technique used to control analog circuits with the digital output from the microcontroller. There are two major components of a PWM signal that defines its behavior; PWM duty cycle and frequency.
Duty cycle describes the ‘on-time’ of a signal. An on-time is the duration of a signal for which the signal stays HIGH. Duty cycle is measured in percentage. For example, if a digital signal is on for half of the time duration and off for the other half, the digital signal is said to have a duty cycle of 50%. Similarly, if a signal stays high for a longer period of time than it stays low, the signal will have a duty cycle greater than 50%.
The frequency determines the amount of time taken by PWM to complete one cycle. For example a frequency of 1000Hz would mean 1000 cycles completed per second.
PWM USING MICROCONTROLLER
To generate PWM with the help of a microcontroller, built-in CCP modules are used. CCP stands for Capture/Compare/PWM. There are two CCP modules present in PIC16F877A; CCP1 and CCP2 at pins RC2 and RC1 respectively.
CIRCUIT DIAGRAM :
Make the above circuit diagram in Proteus. Four switches (SW1, SW2, SW3, and SW4) are connected to the lower four bits of PORTB of the controller with pull-up resisters of 10K. The other ends of the switches are grounded. These switches are used to control theduty ratio of PWM which is generated by the CCP modules. An oscilloscope has been connected to the CCP1 and CCP2 pins of the microcontroller which is used to display the waveforms and variations in it, as the duty cycle is increased or decreased. The remaining standard circuit includes the crystal oscillator connection and that of the MCLR pin.
The figure on the right shows the resulting waveforms received from the two CCP modules. CCP2 pin of controller is connected to the channel ‘A’ (yellow) of the oscilloscope and CCP1 pin to channel ‘B’ (blue). The PWM duty ratio, generated from CCP2 pin, has been increased via switch SW3.
C-CODE FOR GENERATING PWM SIGNAL USING PIC16F877A
Write the following code in mikroC compiler:
- void main()
- short current_duty_1 = 16; // initial value for current_duty_1
- short current_duty_2 = 16; // initial value for current_duty_2
- TRISB = 0xFF; // PORTB as input
- TRISC = 0x00; // PORTC as output
- PWM1_Init(5000); // Initialize PWM1
- PWM2_Init(5000); // Initialize PWM2
- PWM1_Start(); // start PWM1
- PWM2_Start(); // start PWM2
- PWM1_Set_Duty(current_duty_1); // Set current duty for PWM1
- PWM2_Set_Duty(current_duty_2); // Set current duty for PWM2
- while (1) // endless loop
- if (!RB0_bit) // SW1 is pressed
- current_duty_1++; // increment current_duty_1
- PWM1_Set_Duty(current_duty_1); //Change the duty cycle
- if (!RB1_bit) // SW2 is pressed
- current_duty_1–; // decrement current_duty_1
- if (!RB2_bit) // SW3 is pressed
- current_duty_2++; // increment current_duty_2
- if (!RB3_bit) // SW4 is pressed
- current_duty_2–; // decrement current_duty_2
- Delay_ms(10); // slow down change pace a little
This code uses many built-in library functions of PWM present in the mikroC Compiler. Initial value of the duty cycle has been declared in the beginning of the code. PORTB is set as an input port on which the switches are connected. PORTC is set as an output port for generating PWM signals. The four ‘if’ statements determine the state of the two waveforms. If switch SW1 is pressed, the duty ratio of PWM produced by CCP1 is increased andif SW2 is pressed, the duty ratio produced by CCP1 is decreased. Similarly for CCP2 module, SW3 increases the PWM duty ratio and SW4 decreases the PWM duty ratio.
PWM can be used in a variety of applications. It can be used to control a servo motor as well as other motors requiring speed control. PWM is also used to control the average power delivered to a load.
To download files of this article click on this link.