////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/*
Project 017 - USB interface with FT245BM
CCS compiler
		
by Michel Bavin, TechDesign.be (c) November, 2004. --- bavin@skynet.be --- http://users.skynet.be/bk317494/ ---  

... enjoy !

Open source for personal or educational use only.
*/
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#include <18f452.h>			// device selection  
#device adc=10  *=16 

#fuses XT,NOWDT,NOPROTECT,NOLVP,PUT,NOBROWNOUT,NOOSCSEN,CCP2C1,NOSTVREN,NODEBUG  

#use delay(clock=4000000) 	// 4 MhZ  ! ADC is not optimal at 20 MhZ + power consumption doubles

#use rs232(baud=19200, xmit=PIN_C6, rcv=PIN_C7)
#use i2c(master, sda=PIN_C4, scl=PIN_C3, fast) 

#zero_ram 

// #PRIORITY  

#use fast_io(A)
#use fast_io(B)
#use fast_io(C)
#use fast_io(D)
#use fast_io(E)

#define NOP #asm nop #endasm

#byte pa	=0xF80	//port a	// 
#byte pb	=0xF81	//b			// 
#byte pc	=0xF82	//c			// 
#byte pd	=0xF83	//d			// usb port 8 bits parallel
#byte pe	=0xF84	//port e	// 

// *********************************
// PORT A
// analog inputs 



// *********************************
// PORT B: 

#bit usb_pwren	=pb.2			// RB2 pin35	from FT245
#bit usb_rd 	=pb.3			// RB3 pin36	to FT245BM	
#bit usbwr 		=pb.4			// RB4 pin37	"	"	"
#bit usb_txe	=pb.5			// RB5 pin38	from FT245
#bit usb_rxf 	=pb.6			// RB6 pin39	"	"	"
#bit led 		=pb.7			// RB7 pin40




// *********************************
// PORT C 
// i²c / RS232 / emulated spi

// RC3 pin18 = SCL i²c 			// already defined (hardware)
// RC4 pin23 = SDA i²c

// RC6 pin25 = RS232 TX			// already defined (hardware)
// RC7 pin26 = RS232 RX

// emulated SPI bits
#bit TC77_CS		=pc.5		// RC5 pin,	TC77 !CS
#bit TC77_SIO		=pc.2		// RC2 pin,	TC77 S I/O
#bit TC77_SCK		=pc.0		// RC0 pin,	TC77 SCK


// *********************************
// PORT D
// 

#bit usbd0		=pd.0			// RD0 pin19	in/out
#bit usbd1		=pd.1			// RD1 pin20 
#bit usbd2		=pd.2			// RD2 pin21
#bit usbd3		=pd.3			// RD3 pin22
#bit usbd4		=pd.4			// RD4 pin27 
#bit usbd5		=pd.5			// RD5 pin28
#bit usbd6		=pd.6			// RD6 pin29 
#bit usbd7		=pd.7			// RD7 pin30


// *********************************
// PORT E

#bit tps	=pe.2		// RE2 pin10

// *********************************


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//
void tc77_read(void);					// for SPI temperature sensor TC77
signed int16 t_in,t_unused;
float tc_in_float;
float tf_in_float;
char tc77_ready;
signed char temp_outp;
void tc77_write(short bitfortc77);		
void store_t_in(void);
void show_t_in_graph(void);
//

//										// for FT245BM (USB routines)
char i;
char USB_data[4];
void TD_PIC_send(int32 USB_address);
void PIC_send(char USB_tx);
void TD_PIC_receive(void);	// not used here
//

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//-------------------------------------------

void main(){


SET_TRIS_A(0x00);		// oooo oooo
SET_TRIS_B(0x60);		// oiio oioo	
SET_TRIS_C(0x02);		// oooo ooio
SET_TRIS_D(0xff);		// iiii iiii	
SET_TRIS_E(0x00);		// oooo oioo	

printf("017 USB interface FT245BM \n\r");		// RS232 test

//
//


usb_rd=1;usbwr=0;	// disable read or write to/from usb
temp_outp=0;		// select celsius notation --> change to  "1" if you want Fahrenheit


/////////////////////////////////////////////
while(1) 
{ 

// delay_ms(100);

led=1;
tc77_read();				// Temp in
led=0;


/*	printf support (for debugging only)
if (temp_outp==0){printf("Celsius= %2.1f ",tc_in_float);float_temp = tc_in_float;}	// percent two point one ef
else if (temp_outp==1){printf("Fahrenheit= %3.1f ",tf_in_float);float_temp = tf_in_float;}	// percent three point one ef
*/


TD_PIC_send(0x76543210);	// send 4 data bytes to address 0x76543210



// sleep();

} //end while 

		} //end main 


//-------------------------------------------
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void TD_PIC_send(int32 USB_address)
{
if (!usb_txe){

		SET_TRIS_D(0x00);

		for (i=0;i<2;i++){USB_data[i]=(*(&t_in+i));}			// convert float to 2 USB_data bytes

		for (i=4;i!=0;i--){PIC_send(*(&USB_address+(i-1)));}	// get from int32 USB_address


		for (i=2;i!=0;i--){PIC_send(0x00);}
		for (i=2;i!=0;i--){PIC_send(USB_data[(i-1)]);}			// get from 4 USB_data bytes


		/*	printf support (for debugging only) these replace the preceding two lines
		for (i=2;i!=0;i--){PIC_send(0x00);printf("00 ");}
		for (i=2;i!=0;i--){PIC_send(USB_data[(i-1)]);printf("%02x ",USB_data[(i-1)]);}			// get from 4 USB_data bytes
		printf(" \n\r");
		*/

			}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void PIC_send(char USB_tx)
{

if (!usb_txe){

			pd=USB_tx;				
			usbwr=1;
			usbwr=0;
				}

}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void TD_PIC_receive(void)
{
SET_TRIS_D(0xff);

}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void tc77_read(){				// emulated SPI read

char b;

							
tc77_write(0);					// power-up the tc77

SET_TRIS_C(0x06);				// oooo oiio
tc77_ready=0;					// reset

while (!tc77_ready){

	TC77_CS=0;						// select (chip enable)
	t_in=0;

	for(b=16;b!=0;b--){  			// 16-bit read
		TC77_SCK=0;
//		delay_ms(2);
		TC77_SCK=1;
		if (TC77_SIO){bit_set(t_in,b-1);}
//		delay_ms(2);
						}
	TC77_CS=1;											// deselect (chip disable)
	if(bit_test(t_in,2)==1){tc77_ready=1;break;}		// status of adc (tc77 ready?) takes 300mS after each power up
					
							
					}


t_in&=0xfff8; 									// reset lowest 3 bits

tc_in_float=((float)t_in*0.0078125)-2.1;		// bit3 represents 0.0625°C 
												// bit0 equals 0.0078125°C 

tc_in_float-=1.3;								// ? 1.3°C decrement for self-heating (not used here)

// tf_in_float=(1.8*tc_in_float)+32;				// convert to °F



// tc77_write(1);					// power-down the tc77 (consumes only 0,1µA in shutdown-mode)
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void tc77_write(short bitfortc77){

char b;
t_unused=0;
SET_TRIS_C(0x06);		// oooo oiio


TC77_CS=0;						// select (chip enable)
tc77_ready=0;
while (!tc77_ready){
	for(b=16;b!=0;b--){  		// 16-bit read
		TC77_SCK=0;
		delay_ms(2);
		TC77_SCK=1;
		if (TC77_SIO){bit_set(t_unused,b-1);}
		delay_ms(2);		
				}

	tc77_ready=bit_test(t_unused,2);				// status of adc (tc77 ready?) takes 300mS after each power up
					}


SET_TRIS_C(0x02);		// oooo ooio

if (!bitfortc77){TC77_SIO=0;}
else if (bitfortc77){TC77_SIO=1;}


for(b=16;b!=0;b--){  			// 16-bit write
		TC77_SCK=0;
		delay_ms(2);
		TC77_SCK=1;
		delay_ms(2);
					}

TC77_SCK=0;
TC77_CS=1;						// deselect (chip disable)
SET_TRIS_C(0x06);		// oooo oiio
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//												The End !!!!

