#include <P18F97J60.h>
void sampleADC (void);
void init_spi(void)
{
/* Configure the SPI port pins */
TRISDbits.RD6 = 0x00;
/* Configure control register */
SSP2CON1 = 0x00;
/* Slave mode with SS2 pin disabled */
SSP2CON1bits.SSPM1 = 0x01;
//SSP2CON1bits.SSPM2 = 0x01;
/* Clock idles low */
SSP2CON1bits.CKP = 0x01;
/* Configure status register */
SSP2STAT = 0x00;
SSP2STATbits.CKE = 0x00; /* Transmit data on falling edge. */
SSP2STATbits.SMP = 0; /* Input sampled at end of data output time. */
SSP2STATbits.BF = 0; /* Clear receive buffer */
/* Clear buffer */
SSP2BUF = 0x00;
/* CS idles high */
LATCbits.LATC7 = 1;
/* Make SPI CS an output */
TRISCbits.RC7 = 0;
/* We need to disable the UART so we can use the pin as GPIO */
RCSTA1bits.SPEN = 0;
/* Enable SPI port */
SSP2CON1bits.SSPEN = 0x01;
/* Need to reset the CS line */
PORTCbits.RC7 = 0;
PORTCbits.RC7 = 1;
sampleADC();
}
void sampleADC (void)
{
unsigned char temp[2] = {0,0};
unsigned int cnt = 0;
while(1)
{
PORTCbits.RC7 = 0;
SSP2BUF = 0x01;
while(SSP2STATbits.BF != 1);
temp[0] = SSP2BUF;
SSP2STATbits.BF = 0;
SSP2BUF = 0x01;
while(SSP2STATbits.BF != 1);
temp[1] = SSP2BUF;
SSP2BUF = 0;
SSP2STATbits.BF = 0;
PORTCbits.RC7 = 1;
while(++cnt);
}
while(1);
}