Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

For input capture, the settings that are initialized include (for channel 1):

Code Block
languagecpp
   IC1CONbits.ICM = 0b00; // Disable Input Capture 1 module

   IC1CONbits.ICTMR = 1; // Select Timer2 as the IC1 Time base

   IC1CONbits.ICI = 0b11; // Interrupt on every capture event

   IC1CONbits.ICM = 0b001; // Generate capture event on every Rising and Falling edge

   // Enable Capture Interrupt And Timer2

   IPC0bits.IC1IP = 7; // Setup IC1 interrupt priority level - Highest

   IFS0bits.IC1IF = 0; // Clear IC1 Interrupt Status Flag

   IEC0bits.IC1IE = 1; // Enable IC1 interrupt

...

For output capture, the settings that are initialized include (for channel 1):

Code Block
languagecpp
// Initialize Output Compare Module

OC1CONbits.OCM = 0b000; // Disable Output Compare Module

OC1R = MIDDLE\_PWM; // Write the duty cycle for the first PWM pulse = 1.5ms/4688

OC1RS = MIDDLE\_PWM; // Write the duty cycle for the second PWM pulse] = 1.5ms/4688

OC1CONbits.OCTSEL = 0; // Select Timer 2 as output compare time base

OC1CONbits.OCM = 0b110; // Select the Output Compare mode (without fault protection)

...

Likewise, since both IC and OC useĀ timer2. An initialization of this component is also required:

Code Block
languagecpp
T2CONbits.TON = 0; // Disable Timer

T2CONbits.TCS = 0; // Select internal instruction cycle clock

T2CONbits.TGATE = 0; // Disable Gated Timer mode

T2CONbits.TCKPS = 0b01; // Select 1:8 Prescaler

TMR2 = 0x00; // Clear timer register

setPeriod(20);

IPC1bits.T2IP = 0x01; // Set Timer 2 Interrupt Priority Level - Lowest

IFS0bits.T2IF = 0; // Clear Timer 2 Interrupt Flag

IEC0bits.T2IE = 1; // Enable Timer 2 interrupt

T2CONbits.TON = 1; // Start Timer

...