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:

Electricity poses inherent risks. Adequate skills are crucial for handling it safely. Undertake tasks at your own risk; the author disclaims responsibility for misuse, harm, or errors. Website content is unique and copyrighted; refrain from unauthorized copying. Most articles are open-source for your benefit. Feel free to use the knowledge freely, and consider purchasing available resources. For assistance or guidance, comment below; the author aims to help. Some articles may contain affiliate links, having no impact on you but supporting the author with a commission. Thank you for your understanding.

Advertisements

If you missed the previous chapter, you can read that from here:

https://labprojectsbd.com/2024/01/28/using-timer0-as-a-counter-learning-pic-microcontrollers-chapter-10/

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:

  1. 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.
  2. 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.
  3. 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.

PWM and CCP module

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:

PWM and CCP module

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:

For Professional Designs or Help:

Loading

MKDas

Mithun K. Das; B. Sc. in EEE from KUET; Head of R&D @ M's Lab Engineering Solution. "This is my personal blog. I post articles on different subjects related to electronics in the easiest way so that everything becomes easy for all, especially for beginners. If you have any questions, feel free to ask through the contact us page." Thanks.

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *