#define	PIC_CLK 10000000
/////////////////////////////////////
//N.B: Delayus() is in SERIAL.c
/////////////////////////////////////

#include <pic.h>
#include "SERIAL.c"

__CONFIG(0x30E2);


typedef unsigned char uchar;
typedef unsigned int uint;

#define SDI RD1
#define SDO RD2
#define SCK RD3
#define nSEL RC5
#define SDI_OUT() 	TRISD1=0
#define SDO_IN() 	TRISD2=1
#define SCK_OUT() 	TRISD3=0
#define nSEL_OUT() 	TRISC5=0

#define LED1 		RC2
#define LED1_OUT() 	TRISC2=0
#define SW			RC0
#define SW_IN()		TRISC0=1

void Init_RF12(void);
void Write0(void);
void Write1(void);
void WriteCMD(uint CMD);
void DelayUs(uint us);
void DelayMs(uint ms);
void WriteFSKbyte(uchar DATA);


void Init_RF12(void)
{
	VLCDEN=0;

	LED1_OUT();
	SW_IN();
	nSEL_OUT();
	SDI_OUT();
	SDO_IN();
	SCK_OUT();
	nSEL = 1;
	SDI = 1;
	SCK = 0;
	WriteCMD(0x80D8); //config setting command: enable data reg & FIFO, select 433MHz & 12.5pF
	WriteCMD(0x8208); //power management command: same as POR values
	WriteCMD(0xA640); //frequency setting command
	WriteCMD(0xC647); //data rate command
	WriteCMD(0x94C0); //RX control command: bandwidth = 67KHz
	WriteCMD(0xC2AC); //data filter command
	WriteCMD(0xCA80); //FIFO and reset mode command: same as POR values
	WriteCMD(0xCA83); //FIFO and reset mode command: FIFO fill enable, reset mode disable
	WriteCMD(0xC49B); //AFC command
	WriteCMD(0x9820); //TX config control command: M = 2, deviation = +-45KHz
	WriteCMD(0xE000); //wake-up timer command
	WriteCMD(0xC80E); //low duty-cycle command
	WriteCMD(0xC000); //low battery detector and microcontroller clock divider command
	WriteCMD(0xCC17); //PLL setting command
}


void main()
{
	uint ChkSum=0;
	char key;
	Init_RF12(); //initialize transceiver
	serial_setup(); //initialize serial connection
	while(1)
	{
		if(RCIF)
		{
			key = getbyte();
			putch(key);
		}
		LED1=!LED1;
		WriteCMD(0x8228); //OPEN PA
		DelayUs( 4 );
		WriteCMD(0x8238);
		NOP();
		NOP();
		WriteFSKbyte( 0xAA );
		WriteFSKbyte( 0xAA );
		WriteFSKbyte( 0x2D );
		WriteFSKbyte( 0xD4 );

		if(key == 'U')
		{
			WriteFSKbyte( 0x44 );
		}
		else
		{
			WriteFSKbyte( 0x22 );
		}	

/*		//detect sw and send byte without checksum
		if(SW==1)
		{
			WriteFSKbyte( 0x22 );
		}

		if(SW==0)
		{
			WriteFSKbyte( 0x44 );
		}


		WriteFSKbyte( 0x30 );//DATA0
		ChkSum+=0x30;
		WriteFSKbyte( 0x31 );//DATA1
		ChkSum+=0x31;
		WriteFSKbyte( 0x32 );
		ChkSum+=0x32;
		WriteFSKbyte( 0x33 );
		ChkSum+=0x33;
		WriteFSKbyte( 0x34 );
		ChkSum+=0x34;
		WriteFSKbyte( 0x35 );
		ChkSum+=0x35;
		WriteFSKbyte( 0x36 );
		ChkSum+=0x36;
		WriteFSKbyte( 0x37 );
		ChkSum+=0x37;
		WriteFSKbyte( 0x38 );
		ChkSum+=0x38;
		WriteFSKbyte( 0x39 );
		ChkSum+=0x39;
		WriteFSKbyte( 0x3A );
		ChkSum+=0x3A;
		WriteFSKbyte( 0x3B );
		ChkSum+=0x3B;
		WriteFSKbyte( 0x3C );
		ChkSum+=0x3C;
		WriteFSKbyte( 0x3D );
		ChkSum+=0x3D;
		WriteFSKbyte( 0x3E );
		ChkSum+=0x3E;
		WriteFSKbyte( 0x3F );//DATA15
		ChkSum+=0x3F;
		ChkSum&=0x0FF;
		WriteFSKbyte( ChkSum );
*/

		WriteFSKbyte( 0xAA );
		WriteFSKbyte( 0xAA );
		WriteCMD( 0x8208 ); //CLOSE PA
		ChkSum=0x00;
		DelayMs(10);
	}
} 

void Write0( void )
{
	SDI=0;
	SCK=0;
	NOP();
	NOP();
	NOP();
	NOP();
	NOP();
	NOP();
	NOP();
	NOP();
	NOP();
	NOP();
	NOP();
	NOP();
	NOP();
	NOP();
	NOP();
	NOP();
	SCK=1;
	NOP();
}

void Write1( void )
{
	SDI=1;
	SCK=0;
	NOP();
	NOP();
	NOP();
	NOP();
	NOP();
	NOP();
	NOP();
	NOP();
	NOP();
	NOP();
	NOP();
	NOP();
	NOP();
	NOP();
	NOP();
	NOP();
	SCK=1;
	NOP();
}

void WriteCMD(uint CMD)
{
	uchar n=16;//16 bits to write
	SCK=0;
	nSEL=0;
	while(n--)
	{
		if(CMD&0x8000) //if the first bit in CMD is 1, then write 1
			Write1();
		else
			Write0();//if the first bit is 0, then write 0
		CMD=CMD<<1; //shift CMD left 1 bit
	}
	SCK=0;
	nSEL=1;
}

void WriteFSKbyte( uchar DATA )
{
	uchar RGIT=0;
	uint temp=0xB800;
	temp|=DATA;
	Loop: SCK=0;
	nSEL=0;
	SDI=0;
	SCK=1;
	if(SDO) //Polling SDO
	{
		RGIT=1;
	}
	else
	{
		RGIT=0;
	}
	SCK=0;
	SDI=1;
	nSEL=1;
	if(RGIT==0)
	{
		goto Loop;
	}
	else
	{
		RGIT=0;
		WriteCMD(temp);
	}
}

void DelayUs( uint us )
{
	uint i;
	while( us-- )
	{
		i=2;
		while( i-- )
		{
			NOP();
		}
	}
}

void DelayMs(uint ms)
{
	uchar i;
	while(ms--)
	{
		i=35;
		while(i--)
		{
			DelayUs(1);
		}
	}
}