PIC18F6722 - CCP Module - PWM.

0.Contents

1.CCP4 PWM - Timer2.

// Configuration registers.
#pragma config FCMEN = ON, IESO = OFF, OSC = INTIO7
#pragma config BOREN = SBORDIS, BORV = 3, PWRT = OFF
#pragma config WDTPS = 32768, WDT = OFF
#pragma config LPT1OSC = OFF, MCLRE = ON, CCP2MX = PORTC
#pragma config STVREN = ON, LVP = ON, BBSIZ = BB2K, XINST = OFF
#pragma config CP0 = OFF, CP1 = OFF, CP2 = OFF, CP3 = OFF
#pragma config CP4 = OFF, CP5 = OFF, CP6 = OFF, CP7 = OFF
#pragma config CPB = OFF, CPD = OFF
#pragma config WRT0 = OFF, WRT1 = OFF, WRT2 = OFF, WRT3 = OFF
#pragma config WRT4 = OFF, WRT5 = OFF, WRT6 = OFF, WRT7 = OFF
#pragma config WRTC = OFF, WRTB = OFF,WRTD = OFF
#pragma config EBTR0 = OFF, EBTR1 = OFF, EBTR2 = OFF, EBTR3 = OFF
#pragma config EBTR4 = OFF, EBTR5 = OFF, EBTR6 = OFF, EBTR7 = OFF
#pragma config EBTRB = OFF

#include <xc.h>
#include <stdint.h>
#define _XTAL_FREQ 8000000
// PIC18F6722 - Compile with XC8(v1.44).
// Internal Oscillator @8MHz.

// PWM - CCP4 Timer2.
// RG3(6) -> Oscilloscope Probe.

// Definitions.
#define CCP4        LATGbits.LATG3
#define CCP4_TRIS   TRISGbits.TRISG3

// Function prototypes.
void mcu_initialize(void);

// Main.
int main(void)
{
    mcu_initialize();

   	// T3CCP - Timer3 and Timer1 CCPx Enable Bit.
	// T3CCP6.3 = 1 1 - Timer3 and timer4 are the clock sources for ECCP1, ECCP2, ECCP3, CCP4 & CCP5.
	// T3CCP6.3 = 1 0 - Timer3 and timer4 are the clock sources for ECCP3, CCP4 & CPP5,
	//                  Timer1 and timer2 are the clock sources for ECCP1, ECCP2,
	// T3CCP6.3 = 0 1 - Timer3 and timer4 are the clock sources for ECCP2, ECCP3, CCP4 & CCP5.
	//                  Timer1 and timer2 are the clock sources for ECCP1.
	// T3CCP6.3 = 0 0 - Timer1 and timer2 are the clock sources for ECCP1, ECCP2, ECCP3, CCP4 & CCP5.
	T3CONbits.T3CCP2 = 0;
	T3CONbits.T3CCP1 = 0;
    
	// T2OUTPS - Timer2 Postcale Select Bits.
	// T2OUTPS3.0 = 1 1 1 1 - 1:16 Postscale.
	// T2OUTPS3.0 = ...
	// T2OUTPS3.0 = 0 0 0 0 - 1:1 Postscale.
	T2CONbits.T2OUTPS3 = 0;
	T2CONbits.T2OUTPS2 = 0;
	T2CONbits.T2OUTPS1 = 0;
	T2CONbits.T2OUTPS0 = 0;
	//T2CKPS - Timer2 Prescale Select Bits.
	//T2CKPS1.0 = 1 0 - 1:16 Prescale value.
	//T2CKPS1.0 = 0 1 - 1:4 Prescale value.
	//T2CKPS1.0 = 0 0 - 1:1 Prescale value.
	T2CONbits.T2CKPS1 = 0;
	T2CONbits.T2CKPS0 = 0;
	// Timer2 ON.
	T2CONbits.TMR2ON = 1;
    // Timer2.
    TMR2 = 0x00;
    // Timer2 Periode.
    PR2 = 0x7f;

    //
    CCP4CONbits.DC4B1 = 0;
    CCP4CONbits.DC4B0 = 0;
    // CCP4CON - CCP4 Mode Select Bits.
    // CCP4CON3.0 = 1 1 0 0 - PWM.
    CCP4CONbits.CCP4M3 = 1;
    CCP4CONbits.CCP4M2 = 1;
    CCP4CONbits.CCP4M1 = 0;
    CCP4CONbits.CCP4M0 = 0;
    //
    CCPR4L = 0x7f;
    
	while(1){
        uint8_t i;
        uint16_t j;
        for(i=0; i<127; i++){
            CCPR4L = i;
            for(j=0; j<0xffff; j++);
            for(j=0; j<0xffff; j++);
        }
    }
	return(0);
}

// Functions.
void mcu_initialize(void)
{
	// Internal Oscillator @8MHz.
	OSCCON = 0b01110000;
	OSCTUNE = 0b00000000;
	while(!OSCCONbits.IOFS);

	CCP4 = 0;
	CCP4_TRIS = 0;
}

2.CCP4 PWM - Timer4.

// Configuration registers.
#pragma config FCMEN = ON, IESO = OFF, OSC = INTIO7
#pragma config BOREN = SBORDIS, BORV = 3, PWRT = OFF
#pragma config WDTPS = 32768, WDT = OFF
#pragma config LPT1OSC = OFF, MCLRE = ON, CCP2MX = PORTC
#pragma config STVREN = ON, LVP = ON, BBSIZ = BB2K, XINST = OFF
#pragma config CP0 = OFF, CP1 = OFF, CP2 = OFF, CP3 = OFF
#pragma config CP4 = OFF, CP5 = OFF, CP6 = OFF, CP7 = OFF
#pragma config CPB = OFF, CPD = OFF
#pragma config WRT0 = OFF, WRT1 = OFF, WRT2 = OFF, WRT3 = OFF
#pragma config WRT4 = OFF, WRT5 = OFF, WRT6 = OFF, WRT7 = OFF
#pragma config WRTC = OFF, WRTB = OFF,WRTD = OFF
#pragma config EBTR0 = OFF, EBTR1 = OFF, EBTR2 = OFF, EBTR3 = OFF
#pragma config EBTR4 = OFF, EBTR5 = OFF, EBTR6 = OFF, EBTR7 = OFF
#pragma config EBTRB = OFF

#include <xc.h>
#include <stdint.h>
#define _XTAL_FREQ 8000000
// PIC18F6722 - Compile with XC8(v1.44).
// Internal Oscillator @8MHz.

// PWM - CCP4 Timer4.
// RG3(6) -> Oscilloscope Probe.

// Definitions.
#define CCP4        LATGbits.LATG3
#define CCP4_TRIS   TRISGbits.TRISG3

// Function prototypes.
void mcu_initialize(void);

// Main.
int main(void)
{
    mcu_initialize();

   	// T3CCP - Timer3 and Timer1 CCPx Enable Bit.
	// T3CCP6.3 = 1 1 - Timer3 and timer4 are the clock sources for ECCP1, ECCP2, ECCP3, CCP4 & CCP5.
	// T3CCP6.3 = 1 0 - Timer3 and timer4 are the clock sources for ECCP3, CCP4 & CPP5,
	//                  Timer1 and timer2 are the clock sources for ECCP1, ECCP2,
	// T3CCP6.3 = 0 1 - Timer3 and timer4 are the clock sources for ECCP2, ECCP3, CCP4 & CCP5.
	//                  Timer1 and timer2 are the clock sources for ECCP1.
	// T3CCP6.3 = 0 0 - Timer1 and timer2 are the clock sources for ECCP1, ECCP2, ECCP3, CCP4 & CCP5.
	T3CONbits.T3CCP2 = 0;
	T3CONbits.T3CCP1 = 1;
    
	// T2OUTPS - Timer2 Postcale Select Bits.
	// T2OUTPS3.0 = 1 1 1 1 - 1:16 Postscale.
	// T2OUTPS3.0 = ...
	// T2OUTPS3.0 = 0 0 0 0 - 1:1 Postscale.
	T4CONbits.T4OUTPS3 = 0;
	T4CONbits.T4OUTPS2 = 0;
	T4CONbits.T4OUTPS1 = 0;
	T4CONbits.T4OUTPS0 = 0;
	//T2CKPS - Timer2 Prescale Select Bits.
	//T2CKPS1.0 = 1 0 - 1:16 Prescale value.
	//T2CKPS1.0 = 0 1 - 1:4 Prescale value.
	//T2CKPS1.0 = 0 0 - 1:1 Prescale value.
	T4CONbits.T4CKPS1 = 0;
	T4CONbits.T4CKPS0 = 0;
	// Timer2 ON.
	T4CONbits.TMR4ON = 1;
    // Timer2.
    TMR4 = 0x00;
    // Timer2 Periode.
    PR4 = 0x7f;

    //
    CCP4CONbits.DC4B1 = 0;
    CCP4CONbits.DC4B0 = 0;
    // CCP4CON - CCP4 Mode Select Bits.
    // CCP4CON3.0 = 1 1 0 0 - PWM.
    CCP4CONbits.CCP4M3 = 1;
    CCP4CONbits.CCP4M2 = 1;
    CCP4CONbits.CCP4M1 = 0;
    CCP4CONbits.CCP4M0 = 0;
    //
    CCPR4L = 0x7f;
    
	while(1){
        uint8_t i;
        uint16_t j;
        for(i=0; i<127; i++){
            CCPR4L = i;
            for(j=0; j<0xffff; j++);
            for(j=0; j<0xffff; j++);
        }
    }
	return(0);
}

// Functions.
void mcu_initialize(void)
{
	// Internal Oscillator @8MHz.
	OSCCON = 0b01110000;
	OSCTUNE = 0b00000000;
	while(!OSCCONbits.IOFS);

	CCP4 = 0;
	CCP4_TRIS = 0;
}

3.CCP5 PWM - Timer2.

// Configuration registers.
#pragma config FCMEN = ON, IESO = OFF, OSC = INTIO7
#pragma config BOREN = SBORDIS, BORV = 3, PWRT = OFF
#pragma config WDTPS = 32768, WDT = OFF
#pragma config LPT1OSC = OFF, MCLRE = ON, CCP2MX = PORTC
#pragma config STVREN = ON, LVP = ON, BBSIZ = BB2K, XINST = OFF
#pragma config CP0 = OFF, CP1 = OFF, CP2 = OFF, CP3 = OFF
#pragma config CP4 = OFF, CP5 = OFF, CP6 = OFF, CP7 = OFF
#pragma config CPB = OFF, CPD = OFF
#pragma config WRT0 = OFF, WRT1 = OFF, WRT2 = OFF, WRT3 = OFF
#pragma config WRT4 = OFF, WRT5 = OFF, WRT6 = OFF, WRT7 = OFF
#pragma config WRTC = OFF, WRTB = OFF,WRTD = OFF
#pragma config EBTR0 = OFF, EBTR1 = OFF, EBTR2 = OFF, EBTR3 = OFF
#pragma config EBTR4 = OFF, EBTR5 = OFF, EBTR6 = OFF, EBTR7 = OFF
#pragma config EBTRB = OFF

#include <xc.h>
#include <stdint.h>
#define _XTAL_FREQ 8000000
// PIC18F6722 - Compile with XC8(v1.44).
// Internal Oscillator @8MHz.

// PWM - CCP5 Timer2.
// RG3(6) -> Oscilloscope Probe.

// Definitions.
#define CCP5        LATGbits.LATG4
#define CCP5_TRIS   TRISGbits.TRISG4

// Function prototypes.
void mcu_initialize(void);

// Main.
int main(void)
{
    mcu_initialize();

   	// T3CCP - Timer3 and Timer1 CCPx Enable Bit.
	// T3CCP6.3 = 1 1 - Timer3 and timer4 are the clock sources for ECCP1, ECCP2, ECCP3, CCP4 & CCP5.
	// T3CCP6.3 = 1 0 - Timer3 and timer4 are the clock sources for ECCP3, CCP4 & CPP5,
	//                  Timer1 and timer2 are the clock sources for ECCP1, ECCP2,
	// T3CCP6.3 = 0 1 - Timer3 and timer4 are the clock sources for ECCP2, ECCP3, CCP4 & CCP5.
	//                  Timer1 and timer2 are the clock sources for ECCP1.
	// T3CCP6.3 = 0 0 - Timer1 and timer2 are the clock sources for ECCP1, ECCP2, ECCP3, CCP4 & CCP5.
	T3CONbits.T3CCP2 = 0;
	T3CONbits.T3CCP1 = 0;
    
	// T2OUTPS - Timer2 Postcale Select Bits.
	// T2OUTPS3.0 = 1 1 1 1 - 1:16 Postscale.
	// T2OUTPS3.0 = ...
	// T2OUTPS3.0 = 0 0 0 0 - 1:1 Postscale.
	T2CONbits.T2OUTPS3 = 0;
	T2CONbits.T2OUTPS2 = 0;
	T2CONbits.T2OUTPS1 = 0;
	T2CONbits.T2OUTPS0 = 0;
	//T2CKPS - Timer2 Prescale Select Bits.
	//T2CKPS1.0 = 1 0 - 1:16 Prescale value.
	//T2CKPS1.0 = 0 1 - 1:4 Prescale value.
	//T2CKPS1.0 = 0 0 - 1:1 Prescale value.
	T2CONbits.T2CKPS1 = 0;
	T2CONbits.T2CKPS0 = 0;
	// Timer2 ON.
	T2CONbits.TMR2ON = 1;
    // Timer2.
    TMR2 = 0x00;
    // Timer2 Periode.
    PR2 = 0x7f;

    //
    CCP4CONbits.DC4B1 = 0;
    CCP4CONbits.DC4B0 = 0;
    // CCP4CON - CCP4 Mode Select Bits.
    // CCP4CON3.0 = 1 1 0 0 - PWM.
    CCP4CONbits.CCP4M3 = 1;
    CCP4CONbits.CCP4M2 = 1;
    CCP4CONbits.CCP4M1 = 0;
    CCP4CONbits.CCP4M0 = 0;
    //
    CCPR4L = 0x7f;
    
	while(1){
        uint8_t i;
        uint16_t j;
        for(i=0; i<127; i++){
            CCPR4L = i;
            for(j=0; j<0xffff; j++);
            for(j=0; j<0xffff; j++);
        }
    }
	return(0);
}

// Functions.
void mcu_initialize(void)
{
	// Internal Oscillator @8MHz.
	OSCCON = 0b01110000;
	OSCTUNE = 0b00000000;
	while(!OSCCONbits.IOFS);

	CCP5 = 0;
	CCP5_TRIS = 0;
}

4.CCP5 PWM - Timer4.


12.2017