Datasheet

Table Of Contents
PIC18F87J72 FAMILY
DS39979A-page 290 Preliminary 2010 Microchip Technology Inc.
EXAMPLE 22-2: INITIALIZING THE MSSP MODULE
EXAMPLE 22-3: AFE CLOCK SOURCE AND INTERRUPT CONFIGURATION
///////////////////////////////////////////////////////////////////////////////////
// STEP 1: Initialize the MSSP in SPI Master mode to access the AFE
// Connections: SCK--SCKA, SDI--SDOA, SDO--SDIA
////////////////////////////////////////////////////////////////////////////////////
SSPCON1bits.CKP = 1; // SPI mode 1,1: idle state for SCK is high,
SSPCON1bits.CKE = 0; // data transmitted on transition from idle to active state
// SSPCON1bits.CKP = 0; // If SPI mode 0,0 is used instead, SCK idle state is low,
// SSPCON1bits.CKE = 1; // data trasmitted on transition from active to idle state
SSPCON1bits.SSPEN = 1; // Enable SPI
TRISCbits.TRISC3 = 0; // define SCK pin as output
TRISCbits.TRISC4 = 1; // define SDI pin as input
TRISCbits.TRISC5 = 0; // define SDO pin as output
///////////////////////////////////////////////////////////////////////////////
// STEP 2: Issue Reset to AFE. ARESET pin is connected to RD0 in this example
/////////////////////////////////////////////////////////////////////////////////////
LATDbits.LATD0 = 0;
TRISDbits.TRISD0=0; // Put the Delta Sigma ADC module in reset
LATDbits.LATD0 = 1; // Release the Delta Sigma ADC module from reset
////////////////////////////////////////////////////////////////////////////////////
// STEP 3:
// Disable all chip selects for all devices connected to SPI, including chip select
// for the AFE. CSA is connected to RD7 in this example
////////////////////////////////////////////////////////////////////////////////////
TRISDbits.TRISD7=0;
LATDbits.LATD7=1;
///////////////////////////////////////////////////////////////////////////////////////////////
// STEP 5: Set up Clock to AFE.
// Connections: In this example CLKIA is connected to CCP1.
///////////////////////////////////////////////////////////////////////////////////////////////
CCP1CON |= 0b00001100; // ccpxm3:ccpxm0 11xx=pwm mode
CCPR1L=0x01; // 50% Duty Cycle Clock
TRISCbits.TRISC2 = 0; // Make RC2 Output; RC2 is connected to CLKIA of AFE
T2CONbits.TMR2ON = 0; // STOP TIMER2 registers to POR state
PR2 = 0x01; // Set period
T2CONbits.TMR2ON = 1; // Turn on PWM1
///////////////////////////////////////////////////////////////////////////////////////////////
// STEP 6: Interrupt Configuration
// DR output of AFE can be used as interrupt. It can be connected to any external interrupt,
// like INT0. It can be declared as low or high priority interrupt.
// This example configures INT0 (connected to DR)as a high-priority interrupt.
///////////////////////////////////////////////////////////////////////////////////////////////
RCONbits.IPEN=1; //Priority Interrupt
INTCON2bits.RBPU=0; //Enable INT0 pull-up; required
INTCON2bits.INTEDG0=0; //Falling edge select; DR is active low pulse
INTCONbits.GIEH = 1; //Enable high pririty interrupts
INTCONbits.INT0IE = 1; //Enable INT0 interrupt