#include <p18f2510.h>        // Register defions
#include <stdlib.h>
#include <timers.h>         // Timer library functions
#include <delays.h>         // Time delay functions
#include <capture.h>        // CCP library functions
#include <portb.h>
#include <usart.h>
#include <i2c.h>
#include <spi.h>
#include <math.h>
#include <adc.h>

#pragma config OSC = XT
#pragma config FCMEN = OFF
#pragma config IESO = OFF
#pragma config PWRT = ON
#pragma config BOREN = NOSLP
#pragma config BORV = 2
#pragma config WDT = ON
//#pragma config WDT = OFF
#pragma config WDTPS = 8192
#pragma config MCLRE = ON
#pragma config PBADEN = OFF
#pragma config LVP = OFF
#pragma config XINST = OFF
#pragma config DEBUG = OFF
#pragma config CP0 = OFF
#pragma config CP1 = OFF
#pragma config CP2 = OFF
#pragma config CP3 = OFF
#pragma config CPB = OFF
#pragma config WRT0 = OFF
#pragma config WRT1 = OFF
#pragma config WRT2 = OFF
#pragma config WRT3 = OFF
#pragma config WRTB = OFF
#pragma config WRTC = OFF
#pragma config EBTR0 = OFF
#pragma config EBTR1 = OFF
#pragma config EBTR2 = OFF
#pragma config EBTR3 = OFF
#pragma config EBTRB = OFF

#define WIRELESS_TX LATBbits.LATB5
#define WIRELESS_TX_ENABLE LATBbits.LATB4
#define TC77_ENABLE LATCbits.LATC2

#define TEST_PIN LATBbits.LATB6

#define word unsigned int
#define byte unsigned char
// function prototypes

void timer_isr(void);
void SendTemp(void);
int get_temp (void);
void send_temp(void);
void send_bit(int tconst, int periods, byte delay_mode);
void send_byte(byte data);
void TempPower(byte onoff);
int get_moisture(void);


int		Global_Temp;
int		Global_Moisture;
byte	Carrier_Detect;
byte	Signal_Level;
#pragma code isrcode=0x0008

void isrhandler(void)        // This function directs execution to the
{                            //   actual interrupt code
_asm
goto timer_isr
_endasm
}

#pragma code

#pragma interrupt timer_isr save=W

//---------------------------------------------------------------------
// timer_isr()
// Performs the capture interupt
//---------------------------------------------------------------------
void timer_isr(void)
{
// Timer interupt - used for carrier detect timeout
	if ((INTCONbits.TMR0IE==1)&&(INTCONbits.TMR0IF==1))
		{
			INTCONbits.TMR0IF=0;							// reset interupt
		}

	if ((PIE2bits.TMR3IE==1)&&(PIR2bits.TMR3IF==1))
		{
			PIR2bits.TMR3IF=0;
		}
}

void main (void)
{
int	  locx;
	LATA = 0;
	TRISA = 0x01;			// changed from 00 to 01 for moisture detect
	TRISB = 0x80;
	TRISC = 0x10;
	ADCON1 = 0x0e;			// added for moisture detect, AD0 is analog input
	ADCON0 = 0x01;			// turn on ADC, channel 0, added for moisture detect
	ADCON2 = 0xff;			// right justified, sow acc and clocks, Added for moisture detect
	LATBbits.LATB3 = 1;		// Turn on power to moisture circuit, added for moisture detect	
	WIRELESS_TX_ENABLE = 1;
	TC77_ENABLE = 1;
	WIRELESS_TX = 0;
// desired 1070, 1270
// 100 = 250hz
// 25 = 909 hz
// 21 = 1075 hz
// 19 = 1183
// 18 = 1245
// 17 = 1314


// 21 = 1075 hz
// 18 = 1245 hz
#define tconst0 21
#define tconst1 18
#define low_periods 10
#define high_periods 13
#define sync_periods 500
TEST_PIN = 0;
//	OpenTimer3(TIMER_INT_OFF & 	T3_16BIT_RW & T3_SOURCE_INT & T3_PS_1_1 & T3_OSC1EN_OFF & T3_SYNC_EXT_OFF & T3_SOURCE_CCP);
	T3CON=0xc1;
	PIR2bits.TMR3IF=0;
	for(locx=0; locx<40; locx++)
		{
			while(PIR2bits.TMR3IF==0)ClrWdt();
			PIR2bits.TMR3IF=0;					// give the TC77 time to do an initial temperature measurement
		}

	while(1)
		{
			ADCON0bits.ADON = 1;				// turn on A2D, added for moisture detect
			LATBbits.LATB3 = 1;					// Turn on power to moisture circuit, added for moisture detect
			get_temp();							// read temp, turn off power to temp chip
			get_moisture();						// read moisture level, added for moisture detect
			LATBbits.LATB3 = 0;					// Turn off power to moisture circuit, added for moisture detect
			ADCON0bits.ADON = 0;				// turn off A2D, added for moisture detect
			send_temp();						// transmit temp
TEST_PIN = 1;
			OSCCON = 0x82;						// 32khz, idle enabled
  			INTCONbits.GIE = 1;
  			INTCONbits.PEIE = 1;
////			for(locx=68; locx>0; locx--)			// 1 count of locx = 8.5 seconds - 68 = 9min, 37 sec
			for(locx=14; locx>0; locx--)			// 1 count of locx = 8.5 seconds
//			for(locx=2; locx>0; locx--)			// 1 count of locx = 8.5 seconds
				{
					PIR2bits.TMR3IF=0;
					PIE2bits.TMR3IE=1;
					Sleep();
					ClrWdt();
TEST_PIN = 	!TEST_PIN;
					PIE2bits.TMR3IE=0;
					if(locx==2)TempPower(1);	// turn on temp chip
					TempPower(1);				// turn on temp chip
				}
			OSCCON = 0x80;						// xt oscilator, idle enabled
			INTCONbits.GIE = 0;
 			INTCONbits.PEIE = 0;
		}

}

int get_temp(void)
{
byte tempc,tempcl;
int tempi, raw;

	OpenSPI(SPI_FOSC_64,MODE_00,SMPMID);
	TC77_ENABLE=0;
	while(SSPSTATbits.BF==1) tempc=SSPBUF;	// clear any data in buffer
	WriteSPI(0xff);							// send 8 clocks
	while(SSPSTATbits.BF==0);				// wait for transmission to end
	tempc=SSPBUF;							// get msb of temp
	WriteSPI(0xff);							// send 8 MORE clocks
	while(SSPSTATbits.BF==0);				// wait for transmission to end
	tempcl=SSPBUF;							// get lsb of temp

	tempi=tempc;
	tempi*=256;
	tempi+=tempcl;
	raw = tempi;
	tempi/= 8;
	Global_Temp = tempi;


// now disable tc77 chip
	WriteSPI(0xff);							// send 8 MORE clocks
	while(SSPSTATbits.BF==0);				// wait for transmission to end
	tempcl=SSPBUF;							// get lsb of temp
	WriteSPI(0xff);							// send 8 MORE clocks
	while(SSPSTATbits.BF==0);				// wait for transmission to end
	tempcl=SSPBUF;							// get lsb of temp
	TC77_ENABLE=1;
	CloseSPI();

	return(raw);							// and return the temperature
}	

int get_moisture(void)						// Added fot moisture detect
{
int average, count;

	average = 0;
	for(count=0;count<8;count++)
		{
			ADCON0bits.GO = 1;
			while(ADCON0bits.GO == 1)ClrWdt();
			average += ADRES;
		}
	average>>=3;
	Global_Moisture = average;
	return(average);							// and return the moisture
}	
void TempPower(byte onoff)
{
byte tempc,tempcl;
int tempi;

	OpenSPI(SPI_FOSC_16,MODE_00,SMPMID);
	TC77_ENABLE=0;
	while(SSPSTATbits.BF==1) tempc=SSPBUF;	// clear any data in buffer
	WriteSPI(0x00);							// send 8 clocks
	while(SSPSTATbits.BF==0);				// wait for transmission to end
	tempc=SSPBUF;							// get msb of temp
	WriteSPI(0x00);							// send 8 MORE clocks
	while(SSPSTATbits.BF==0);				// wait for transmission to end
	tempcl=SSPBUF;							// get lsb of temp

	if(onoff != 0) WriteSPI(0x00);			// enable the chip
	else WriteSPI(0xff);					// disable the chip
	while(SSPSTATbits.BF==0);				// wait for transmission to end
	tempcl=SSPBUF;							// get lsb of temp
	if(onoff != 0) WriteSPI(0x00);			// enable the chip
	else WriteSPI(0xff);					// disable the chip
	while(SSPSTATbits.BF==0);				// wait for transmission to end
	tempcl=SSPBUF;							// get lsb of temp

	TC77_ENABLE=1;
	CloseSPI();
}

void send_temp(void)
{
int		locx, locy;
word	locw;
byte	data_buf[10];


	data_buf[0] = 0xe5;
	data_buf[1] = 0xaa;
	data_buf[2] = 0x55;
	data_buf[3] = 'T';
	data_buf[4] = (Global_Temp >> 8);
	data_buf[5] = (Global_Temp & 0xff);
	data_buf[6] = (Global_Moisture >> 8);
	data_buf[7] = (Global_Moisture & 0xff);
	
	if(PORTBbits.RB7)data_buf[8] = 1;else data_buf[8] = 0;
	
	locw = data_buf[4];
	locw += data_buf[5];
	locw += data_buf[6];
	locw += data_buf[7];
	locw += data_buf[8];
	data_buf[9] = locw;
	WIRELESS_TX_ENABLE = 0;
	for(locy=0; locy<5; locy++)
		{
			send_bit(tconst1, sync_periods, 0);
			for(locx=0; locx<10; locx++) send_byte(data_buf[locx]);
			send_bit(tconst1, sync_periods, 1);
		}
	WIRELESS_TX_ENABLE = 1;
}

void send_byte(byte data)
{
byte	bit_ctr;
	send_bit(tconst0, low_periods,0);						// start bit
	for(bit_ctr=0; bit_ctr<8; bit_ctr++)
		{
			if(data & 1)send_bit(tconst1, high_periods,0);// 1 data bit	
			else send_bit(tconst0, low_periods,0);		// 0 data bit	
			data >>= 1;
		}
	send_bit(tconst1, high_periods,0);					// stop bit
	send_bit(tconst1, high_periods,0);					// stop bit
}

void send_bit(int tconst, int periods, byte delay_mode)
{
int	loc_x, loc_y;
	for(loc_y=0; loc_y < periods; loc_y++)
		{
			if(delay_mode == 0)WIRELESS_TX = 1;
			for(loc_x=0; loc_x<tconst; loc_x++)ClrWdt();
			WIRELESS_TX = 0;
			for(loc_x=0; loc_x<tconst; loc_x++)ClrWdt();
		}
}
