#include <p24FJ64GA006.h>
#include "p24fxxxx.h"
#include "xc.h"
#ifdef __PIC24FJ64GA006__ //Defined by MPLAB when using 24FJ256GB110 device
// JTAG/Code Protect/Write Protect/Clip-on Emulation mode
// Watchdog Timer/ICD pins select
_CONFIG1(JTAGEN_OFF & GCP_OFF & GWRP_OFF & COE_OFF & FWDTEN_OFF & ICS_PGx1)
// Disable CLK switch and CLK monitor, OSCO or Fosc/2, HS oscillator,
// Primary oscillator
_CONFIG2(FCKSM_CSDCMD & OSCIOFNC_OFF & POSCMOD_HS & FNOSC_PRIPLL)
#endif
// RB6 as SS for SPI slave MCU
#define SPI_SS_TRIS TRISBbits.TRISB6
#define SPI_SS_PORT PORTBbits.RB6
unsigned short spiBufT[]={0x02,0x03,0x0d,0};
int Counter = 0;
void InitClock()
{
CLKDIVbits.RCDIV=0;
while(OSCCONbits.LOCK != 1) {};
}
void SPI2Init(void)
{
//config SPI1
SPI2STATbits.SPIEN = 0; // disable SPI port
SPI2STATbits.SPISIDL = 0; // Continue module operation in Idle mode
SPI2BUF = 0; // clear SPI buffer
IFS2bits.SPI2IF = 0; // clear interrupt flag
IEC2bits.SPI2IE = 0; // disable interrupt
SPI2CON1bits.DISSCK = 0; // Internal SPIx clock is enabled
SPI2CON1bits.DISSDO = 0; // SDOx pin is controlled by the module
SPI2CON1bits.MODE16 = 0; // set in 16-bit mode, clear in 8-bit mode
SPI2CON1bits.SMP = 0; // Input data sampled at middle of data output time
SPI2CON1bits.CKP = 0; // CKP and CKE is subject to change ...
SPI2CON1bits.CKE = 0; // ... based on your communication mode.
SPI2CON1bits.MSTEN = 1; // 1 = Master mode; 0 = Slave mode
SPI2CON1bits.SPRE = 4; // Secondary Prescaler = 4:1
SPI2CON1bits.PPRE = 2; // Primary Prescaler = 4:1
SPI2CON2 = 0; // non-framed mode
SPI_SS_PORT = 1; // ALL SPI_SS_PORT PINS ARE CLEARED
SPI_SS_TRIS = 0; // set SS as output -RB6
SPI2STATbits.SPIEN = 1; // enable SPI port, clear status
}
unsigned short writeSPI2( unsigned short data )
{
SPI2BUF = data;
while(!SPI2STATbits.SPIRBF); // wait for transfer to complete
return SPI2BUF; // read the received value
}
int main (void)
{
unsigned short i;
InitClock();
SPI2Init();
SPI_SS_PORT = 0;
//Enable interrupts...
//INTCON1bits.NSTDIS = 1; // Disable nested interrupts
INTCON2bits.INT3EP = 1; // Interrupt on positive edge of External Interrupt 3
IPC13bits.INT3IP = 001; // Priority level
//IFS3bits.INT3IF = 1; // Interrupt req flag status
IEC3bits.INT3IE = 1; // Enable interrupt
while(1)
{
}
SPI_SS_PORT = 1;
return 0;
}
void __attribute__((__interrupt__, no_auto_psv)) _INT3Interrupt(void)
{
//clear interrupt flag
IFS3bits.INT3IF = 0;
int i;
// SPI Read...
Counter++;
}