PIC18F2620 - CCP Module - PWM 10-bit.

0.Contents

PIC18F Trainer : PIC18F2620 @8MHz Internal oscillator.

1.PWM 10-bit - CCP1 & CCP2.

// Configuration registers.
#pragma config IESO = OFF, FCMEN = OFF, OSC = INTIO7
#pragma config BORV = 3, BOREN = OFF, PWRT = OFF
#pragma config WDTPS = 32768, WDT = OFF
#pragma config MCLRE = ON, LPT1OSC = OFF, PBADEN = OFF, CCP2MX = PORTC
#pragma config DEBUG = OFF, XINST = OFF, LVP = OFF, STVREN = ON
#pragma config CP3 = OFF, CP2 = OFF, CP1 = OFF, CP0 = OFF
#pragma config CPD = OFF, CPB = OFF
#pragma config WRT3 = OFF, WRT2 = OFF, WRT1 = OFF, WRT0 = OFF
#pragma config WRTD = OFF, WRTB = OFF, WRTC = OFF
#pragma config EBTR3 = OFF, EBTR2 = OFF, EBTR1 = OFF, EBTR0 = OFF
#pragma config EBTRB = OFF

#include <xc.h>
#include <stdint.h>
#define _XTAL_FREQ 8000000
// PIC18F2620 - Compile with XC8(v2.00).
// PIC18F2620 - @8MHz Internal oscillator.

// CCP Module - PWM 10-Bit.

// MCU.RC2 -> PROBE_A.
// MCU.RC1 -> PROBE_B.

// Definitions.
// PWM Outputs.
#define PWM_1			LATCbits.LATC2
#define PWM_1_TRIS		TRISCbits.TRISC2
#define PWM_2			LATCbits.LATC1
#define PWM_2_TRIS		TRISCbits.TRISC1

// Main.
int main(void)
{
	// MCU Initialization.
	// Internal Oscillator 8MHz.
	OSCCONbits.IRCF2  = 1;
	OSCCONbits.IRCF1  = 1;
	OSCCONbits.IRCF0  = 1;
	OSCTUNEbits.PLLEN = 0;
	while(!OSCCONbits.IOFS);

	// Timer2 - 16kHz @8MHz.
	T2CON = 0b00000100;
	TMR2 = 0x00;
	PR2 = 0x7a;
	// PWM Mode.
	CCP1CON = 0b00001100;
	CCP2CON = 0b00001100;

	// Output for PWM.
	PWM_1 = 0;
	PWM_2 = 0;
	PWM_1_TRIS = 0;
	PWM_2_TRIS = 0;
	
	uint16_t i;
	while(1){
		for(i=0; i<0x1ff; i++){
			CCPR1L = (i>>2);
			CCP1CON |= 0x30 & (i<<4);
			CCPR2L = (i>>2);
			CCP2CON |= 0x30 & (i<<4);
			__delay_ms(50);
		}
	}
	return(0);
}

08.2018