These lines initialize 16×2 LCD according to pins connection setting defines above. PWM1_Init(5000) initialize the CCPI module with the frequency of 5KHz and PWM1_Start() enables the module and enables functionality on RC2 pin of PIC16F877A microcontroller. TRISC = 0x00 // PORTC as outputĪs mentioned earlier, before using the PWM library, we need to initialize frequency and call pwm_start function. This line set sets PORTC as an output PORT using TRISC register ( TRISC is a direction control register). Inside the main function, we always initialize variables, define GPIO pins settings and peripherals initialization. PORTB pins RB2-RB7 are used and these lines declare pins.
If you don’t know how to interface LCD with PIC16F877A microcontroller, read this post: In this circuit, we connected a 16×2 LCD with PIC16F877A and used the CCPI module to get an output signal ( from RC2 pin). In this example, we will see how to generate a fix duty cycle PWM signal using PIC16F877A microcontroller. Generate Fix Duty Cycle PWM using PIC16F877A
For instance, PIC16F877A microcontroller has two CCP modules CCP1 and CCP2.įor example, we want to use CCP1 module andwant set frequency of 5000Hz or 5KHz. Because some pic microcontrollers come in more than one CCP modules. The input parameter to this function is required frequency. Here x denotes the number of CCP module. PWMx_Init(long frequency) function used to set/declare frequency of signal. These are the four functions that are used to generate PWM, set frequency and change duty cycle.
This library is generic and can be used with all PIC16F, PIC18 series microcontrollers. MikroC for PIC provides built-in libraries for to generate PWM with variables frequency and variable duty cycle.Īs mentioned earlier, MikroC provides a built-in library for pulse width modulation module of pic microcontroller. There are two CCP modules present in PIC16F877A CCP1 and CCP2 at pins RC2 and RC1 respectively. To generate PWM with the help of a PIC16F877A microcontroller, built-in CCP modules are used. First let’s begin with MikroC for PIC compiler. As mentioned earlier, we will discuss an example with two compilers, namely, MPLAB XC8 and MikroC Pro. Now let’s start understanding how to generate different frequency and duty cycle digital signals using PIC16F877A microcontroller.Īlthough, we use PIC16F877A in this post, but you can easily apply the same concepts and examples to other PIC microcontrollers also. Now you know the basics of pulse width modulation and you also know its related terminology. Before using PWM module of Pic microcontroller, we should define the frequency/timer period of the signal. For example, a frequency of 1000Hz would mean 1000 cycles completed per second. The frequency determines the amount of time taken by PWM to complete one cycle. 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%. 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%.
An on-time is the duration of a signal for which the signal stays HIGH. There are two major components of a PWM signal that defines its behavior PWM duty cycle, time period and frequency. PWM (Pulse Width Modulation) is a powerful technique used to control analog circuits with the digital output from the microcontroller.
PWM Generation Example MPLAB XC8 Compiler.Generate PWM Using PIC Microcontroller with MPLAB XC8 Compiler.Use ADC value to Control PWM using PIC Microcontroller.Variable Duty Cycle PWM using PIC Microcontroller.Generate Fix Duty Cycle PWM using PIC16F877A.