Author: Annie
Company: Microsoft

uint16 Init2046(char SPI_ID)     //input: SPI_ID;  output: Dat16bit
{
  
  uint16 SPIData,i,j,Data,Dat16bit;
  uint16 ReadBit;
  
  CLR_DCLK;
  CLR_TPCS;
   
  Data=0x0000;
  
  for(i=0;i<8;i++)		  //clock 1~8
    {
       if(SPI_ID & 0x80)
	   {
	   SET_DI;
	   }
       else
	   {
       CLR_DI; 
	   }

	   CLR_DCLK;	
       Delay_NOP(1);
       SET_DCLK;
	   Delay_NOP(1);
       SPI_ID<<=1;		  
    }   

  CLR_DCLK;                         //clock 9
  Delay_NOP(1);
  SET_DCLK;
  Delay_NOP(1);;
  SPIData=0;

  for(j=0;j<12;j++)		 //clock10~21
   {
       CLR_DCLK;
       Delay_NOP(1);
       SET_DCLK;
       Delay_NOP(1);

       if((ReadDI(ReadBit))>1)
	   {
       SPIData=(SPIData<<1)+1;}
	 else
       {SPIData=(SPIData<<1);}
    } 
  for(i=0;i<3;i++)		  //clock 22~24
    {
     
	 CLR_DCLK;
	   
 Delay_NOP(1);
     SET_DCLK;
 Delay_NOP(1);
     
    }

   SET_DCLK;
   SET_TPCS;

   Dat16bit = SPIData;

   return Dat16bit;
  
 }