#include<p24FJ256GB106.h>

/****************************** CONFIGURATION **************************************/
_CONFIG1( JTAGEN_OFF & GCP_OFF & GWRP_OFF & FWDTEN_OFF & ICS_PGx3 )
_CONFIG2( PLL_96MHZ_ON & IESO_OFF & FCKSM_CSDCMD & DISUVREG_OFF & OSCIOFNC_OFF & POSCMOD_HS & FNOSC_PRIPLL & PLLDIV_DIV5 & IOL1WAY_ON)
/***********************************************************************************/

typedef unsigned char 	UINT8;
typedef signed char		INT8;

typedef unsigned int 	UINT16;
typedef signed int		INT16;

typedef unsigned long 	UINT32;
typedef signed long int INT32;
#define ADC_START PORTDbits.RD11
#define ADC_DRDY PORTDbits.RD0
#define ADC_RESET PORTCbits.RC14

void Pin_Remap(void);
void init_spi(void);
void delay(UINT16);

INT16 ADC_VALUE=0;

INT16 main (void)
{
TRISDbits.TRISD11 = 0;
TRISDbits.TRISD0 = 1;
TRISCbits.TRISC14 = 0;
Pin_Remap();
init_spi();
delay(30);
ADC_RESET = 0;
delay(20);
ADC_RESET = 1;
ADC_START = 1;				// Begin Conversion
delay(210);					

		SPI1BUF = 0x17;		// command to stop reading data continuously
             	  delay(210);
                            
		SPI1BUF = 0b01000001;    	  // command for writing to VBIAS reg
                           delay(50);

       		 SPI1BUF = 0b00000010;	// Command  for selectng number of bytes to be written
           	 	delay(50);
                            
		SPI1BUF = 0b00000000;			// Data to VBIAS Reg
                            delay(50);

		SPI1BUF = 0b00100000;		// Data to MUX1 Reg
                          delay(50);

		SPI1BUF = 0b01010000	;	// Data to SYS0 Reg
                        delay(50);
                            
     		SPI1BUF = 0b01001010;     	  // command for writing to IDAC0 reg
                          delay(50);
            		   		 
		SPI1BUF = 0b00000001;		// Command  for selectng number of bytes to be written
           	 	delay(50);

		SPI1BUF = 0b00000100	;	// Data to IDAC0
                          
		delay(50);

    		SPI1BUF = 0b00000001;			// Data to IDAC1
                             delay(50);
		
		SPI1BUF = 0x15;		//Read data continuously
		delay(210);
                           
		while(1)
		{
		SPI1BUF= 0xFF;				// NOP
		SPI1BUF= 0xFF;				// NOP
		SPI1BUF= 0xFF;				// NOP
		delay(210);
		if(SPI1STATbits.SPIRBF==1)
		{
		ADC_VALUE=SPI1BUF;		// read register value
		}
		delay(210);
		}

}

void __attribute__((__interrupt__, auto_psv)) _SPI1Interrupt(void)
{
 IFS0bits.SPI1IF = 0;
 flag = 1;
 delay(100);

}
void Pin_Remap(void)
{
	/* Unlock Registers */
	OSCCON = 0X46;
	OSCCON = 0X57;
	OSCCONbits.IOLOCK = 0;

	/*SPI1*/
	RPOR1bits.RP3R = 8;		// Configure Output Functions for SPI1
	RPOR2bits.RP4R = 7;
	RPINR20bits.SDI1R = 2;	// Configure Input Functions for SPI1

	/*Lock Registers*/
	OSCCON = 0X46;
	OSCCON = 0X57;
	OSCCONbits.IOLOCK = 1;
}
void init_spi (void)
{
  //  IFS0bits.SPI1IF = 0;
   // IEC0bits.SPI1IE = 1;                        //SPI1 Interrupt enable
    //IPC2bits.SPI1IP = 5;
SPI1STATbits.SPIRBF = 0;
SPI1CON1bits.PPRE = 2;				// Primary Prescaler
SPI1CON1bits.SPRE = 6;				// Secondary Prescaler
SPI1CON1bits.DISSCK = 0;			// Internal Serial Clock is Enabled
SPI1CON1bits.DISSDO = 0;			// SDO1 pin is controlled by the module
SPI1CON1bits.MODE16 = 1;			// Communication is word-wide (16 bits)
SPI1CON1bits.SMP = 0;				// Input data sampled at end of data output time
SPI1CON1bits.CKP = 0;				// Idle state for clock is a low level; active state is a high level
SPI1CON1bits.CKE = 0;				// Output data changes on transition from active to Idle clock state
SPI1CON1bits.MSTEN = 1;				// Enable Master Mode
SPI1STATbits.SPIEN = 1;				// Enable SPI1 module
//ADC_START = 1;
//delay(500);

//while(SPI1STATbits.SPITBF);			// Cleared by hardware when transfers data from SPI2TXB to SPI2SR
//SPI1BUF = 0b01001010;               // command for writing to mux1 reg
}

void delay(UINT16 delay)
{
    UINT16 count1,count2;
	for(count1 = 0; count1 < delay; count1++)
	{
		for(count2=0; count2 < 2133; count2++);  //one milli second delay
	}
}