In this chapter, we’ll delve into the powerful realm of Pulse Width Modulation (PWM) and explore how to leverage the CCP (Capture/Compare/PWM) module of the microcontroller. PWM, a digital technique, empowers us to finely control various parameters such as loads, speed, and voltage with precision. Its versatility makes it an indispensable tool in the microcontroller’s arsenal. As we unravel PWM’s intricacies, we’ll discover its symbiotic relationship with the CCP module, unlocking a deeper understanding of their interplay. Join me as we embark on this journey of discovery!
Disclaimer:
Handling electricity carries inherent risks. It’s essential to have the appropriate skills to manage it safely. Proceed at your own risk, as the author disclaims responsibility for any misuse, harm, or errors. All content on this website is unique and copyrighted; please avoid unauthorized copying. While most articles are open-source for your benefit, feel free to use the knowledge provided. If you find our resources helpful, consider purchasing available materials to support our work.
For assistance or guidance, leave a comment below; the author is committed to helping. Some articles may contain affiliate links that support the author with a commission at no additional cost to you. Thank you for your understanding and support.
If you missed the previous chapter, you can read that from here:
Table of Contents
What is a CCP:
CCP stands for Capture/Compare/PWM, and it’s a versatile module found in many PIC (Peripheral Interface Controller) microcontrollers. Here’s a breakdown of its key functionalities:
- Capture: This feature allows the microcontroller to capture the value of a timer when an external event occurs. It’s commonly used in applications such as measuring the duration between external events or generating precise time intervals.
- Compare: The compare function enables the microcontroller to compare the value of a timer with a predetermined reference value. It’s often utilized in tasks like generating precise timing signals or triggering specific actions when certain conditions are met.
- PWM (Pulse Width Modulation): PWM capability enables the microcontroller to generate precise digital pulses with adjustable widths. This feature is extensively used in applications like controlling the speed of motors, regulating the intensity of LED lighting, or simulating analog signals.
The CCP module’s integration of these three functionalities provides a powerful toolset for a wide range of applications, making it a valuable component in PIC microcontroller development.
Capture Mode Explanation:
In Capture mode, the CCP module captures the current value of the Timer upon the occurrence of an external event (e.g., a rising or falling edge on an input pin). This captured value can then be used to calculate the time between events, which is particularly useful in applications like measuring the frequency or pulse width of a signal.
Example Code in MikroC:
Assuming we are working with a PIC microcontroller and using MikroC for programming, here’s a basic example of setting up the Capture mode for the CCP module. This example configures Capture mode on CCP1 (Capture/Compare/PWM module 1) and uses Timer1.
// Define input pin for capture (change according to your setup) sbit CCP1_Pin at RC2_bit; sbit CCP1_Port at PORTC2_bit; // Define variables to store captured values unsigned int captureValue; void main() { // Initialize ports and other settings as needed // Configure Timer1 T1CON.TMR1ON = 1; // Enable Timer1 T1CON.T1CKPS1 = 0; // Set prescaler to 1:1 // Configure CCP1 for Capture mode CCP1CON.CCP1M0 = 0; // Set mode to Capture on every rising edge // Enable CCP1 interrupt INTCON.GIE = 1; // Global interrupt enable INTCON.PEIE = 1; // Peripheral interrupt enable PIE1.CCP1IE = 1; // Enable CCP1 interrupt while(1) { // Main program loop } } // Interrupt service routine for CCP1 void interrupt() { if (PIR1.CCP1IF) { // CCP1 capture interrupt flag is set captureValue = CCPR1; // Read captured value PIR1.CCP1IF = 0; // Clear the interrupt flag } }
In this example, when a rising edge is detected on the CCP1 input pin, the CCP module captures the current value of Timer1. The captured value is then stored in the captureValue
variable, which can be used for further calculations based on the application requirements.
Note: This example assumes you have configured the microcontroller properly for your specific hardware setup. Adjust pin assignments and other settings according to your needs.
The Compare mode of the Capture/Compare/PWM (CCP) module in a PIC microcontroller is often used to generate an output signal or trigger an action when the Timer value matches a pre-set comparison value. Below is an explanation along with an example code snippet using MikroC for a PIC microcontroller.
Compare Mode Explanation:
In Compare mode, the CCP module continuously compares the Timer value with a pre-set value stored in a register. When the Timer value matches the comparison value, the CCP module can generate an output signal, trigger an interrupt, or perform other specified actions. This mode is useful for generating precise timing events.
Example Code in MikroC:
Assuming we are working with a PIC microcontroller and using MikroC for programming, here’s a basic example of setting up the Compare mode for the CCP module. This example configures Compare mode on CCP1 (Capture/Compare/PWM module 1) and uses Timer1.
// Define output pin for compare (change according to your setup) sbit CCP1_Pin at RC2_bit; sbit CCP1_Port at PORTC2_bit; void main() { // Initialize ports and other settings as needed // Configure Timer1 T1CON.TMR1ON = 1; // Enable Timer1 T1CON.T1CKPS1 = 0; // Set prescaler to 1:1 // Set the comparison value (adjust as needed) CCPR1 = 500; // Configure CCP1 for Compare mode CCP1CON.CCP1M0 = 1; // Set mode to Compare mode while(1) { // Main program loop // You can add your main program logic here } }
In this example, the CCP module is configured in Compare mode, and the comparison value is set to 500. The program continuously checks if the Timer1 value matches the pre-set comparison value. When the match occurs, you can add the desired action in the main program loop.
Note: This example assumes you have configured the microcontroller properly for your specific hardware setup. Adjust pin assignments and other settings according to your needs.
PWM mode Explanation:
PWM Mode Explanation:
In PWM mode, the CCP module generates a square wave signal with a variable duty cycle. The duty cycle represents the ratio of the pulse width (on-time) to the period of the signal. By adjusting the duty cycle, the average voltage or power delivered to the load can be controlled.
Example Code in MikroC:
Assuming we are working with a PIC microcontroller and using MikroC for programming, here’s a basic example of setting up the PWM mode for the CCP module. This example configures PWM mode on CCP1 (Capture/Compare/PWM module 1) and uses Timer2 for the PWM frequency generation.
// Define output pin for PWM (change according to your setup) sbit PWM_Pin at RC2_bit; sbit PWM_Port at PORTC2_bit; void main() { // Initialize ports and other settings as needed // Configure Timer2 for PWM frequency T2CON.TMR2ON = 1; // Enable Timer2 T2CON.T2CKPS = 0b01; // Set prescaler to 4 (adjust for desired frequency) // Configure CCP1 for PWM mode CCP1CON.CCP1M3 = 1; // Set mode to PWM CCP1CON.CCP1M2 = 1; // Set mode to PWM // Set PWM duty cycle (adjust as needed, range: 0-255) CCPR1L = 128; // Example: 50% duty cycle while(1) { // Main program loop } }
In this example, the CCP module generates a PWM signal on the CCP1 output pin (PWM_Pin
). The duty cycle of the PWM signal is determined by the value written to the CCPR1L register. By adjusting this value, you can control the duty cycle and hence the average voltage or power delivered to the load.
Note: This example assumes you have configured the microcontroller properly for your specific hardware setup. Adjust pin assignments, PWM frequency, and other settings according to your needs.
Practical example:
Above this, all the information was so theoretical. Sometimes it may become very boring and hard to understand. So let’s be practical and do something in practice.
In our works, we use the PWM the maximum and capture and compare a little bit. So, here I’ll demonstrate how you can use a PWM signal to control any DC load.
Circuit:
In this basic circuit, I used the PWM signal directly but you may need resistors and gate-driving circuits depending on MOSFET and operating voltage.
Code:
int adc_rd=0; int duty=0; void main() { TRISA = 0xFF;//all input TRISC = 0x00;//all output TRISB = 0x00;//all output PORTB = 0x00; PORTC = 0x00; ADCON1 = 0x00; ADCON0 = 0x01;//AN0 PWM1_Init(2000); PWM1_Start(); while(1) { //read ADC adc_rd = ADC_Read(0); PORTB = adc_rd;//print value on PortB duty = adc_rd; PWM1_Set_Duty(duty); //also can be written as // PWM1_Set_Duty(ADC_Read(0)); } }
Very simple code, No need to explain again I hope.
And the result is here:
Conclusion:
Among all the timers PWM is the most useful feature and timer interrupts. But other features are also useful in some particular tasks. I tried to make it easy to understand. You can read the chapters serially to catch the flow. Overall, I hope this chapter will help you understand and utilize the features in your task. See you soon with the next topics.
You can read:
- Using Timer0 as a Counter: Learning PIC Microcontrollers [Chapter 10]
- Understanding Timer0: Learning PIC Microcontrollers [Chapter 9]
- Learning ADC of PIC microcontrollers: Learning PIC microcontrollers [Chapter 8]
- Learning PIC MCU: How to interface buttons with microcontrollers [Chapter 7]
For Professional Designs or Help:
0 Comments