static unsigned short XPT2046_ReadCMD(void) 
{
    unsigned short buf=0,temp;
    unsigned char i;
    TP_DIN(0);
    TP_DCLK(1);
    for(i=0;i<12;i++) 
    {
        TP_DCLK(0);          
        temp= (TP_DOUT) ? 1:0;
        buf|=(temp<<(11-i));
        TP_DCLK(1);
    }
    buf&=0x0fff;

    return(buf);
}
//====================================================================================
static void WR_CMD (unsigned char cmd) 
{
    unsigned char buf;
    unsigned char i;
    TP_CS(1);
    TP_DIN(0);
    TP_DCLK(0);
    TP_CS(0);
    for(i=0;i<8;i++) 
    {
        buf=(cmd>>(7-i))&0x1;
        TP_DIN(buf);
        Delayus(5);
        TP_DCLK(1);
        Delayus(5);
        TP_DCLK(0);
    }
}
/*
*********************************************************************************************************
*	函 数 名: XPT2046_WriteCMD
*	功能说明: 写命令
*	形    参:CHX 	0x90 	//通道Y+的选择控制字 CHY 	0xd0	//通道X+的选择控制字
*	返 回 值: 无
*********************************************************************************************************
*/
static void XPT2046_WriteCMD(unsigned char cmd) 
{
    unsigned char buf;
    unsigned char i;
//     TP_CS(1);
    TP_DIN(0);
    TP_DCLK(0);
//     TP_CS(0);
    for(i=0;i<8;i++) 
    {
        buf=(cmd>>(7-i))&0x1;
        TP_DIN(buf);
        //Delayus(5);
      DelayUS(5);
        TP_DCLK(1);
        //Delayus(5);
      DelayUS(5);
        TP_DCLK(0);
    }
}
//====================================================================================
static unsigned short RD_AD(void) 
{
    unsigned short buf=0,temp;
    unsigned char i;
    TP_DIN(0);
    TP_DCLK(1);
    for(i=0;i<12;i++) 
    {
        Delayus(5);
        TP_DCLK(0);         
        Delayus(5);   
        temp= (TP_DOUT) ? 1:0;
        buf|=(temp<<(11-i));
        
        Delayus(5);
        TP_DCLK(1);
    }
    TP_CS(1);
    buf&=0x0fff;
    return(buf);
}
//====================================================================================
void TSC_Init(void)
{
	/* Enable SPI1 and GPIO clocks */
	RCC->AHB1ENR    |=((1UL <<  0) |      /* Enable GPIOA clock                 */
	                 (1UL <<  1) |      /* Enable GPIOB clock                 */
	                 (1UL <<  2));      /* Enable GPIOC clock                 */

	//PA.15 as CS OUTPUT
	GPIOA->MODER    &= ~0xC0000000;       /* Clear Bits                         */
	GPIOA->MODER    |=  0x40000000;       /* Alternate Function mode            */
	GPIOA->OSPEEDR  &= ~0xC0000000;       /* Clear Bits                         */
	GPIOA->OSPEEDR  |=  0x80000000;       /* 50 MHz Fast speed                  */
	GPIOA->AFR[0]   &= ~0x00000000;       /* Clear Bits                         */
	GPIOA->AFR[0]   |=  0x00000000;       /* Alternate Function mode AF12       */
	GPIOA->AFR[1]   &= ~0xF0000000;       /* Clear Bits                         */
	GPIOA->AFR[1]   |=  0x00000000;       /* Alternate Function mode AF12       */

	//PB.15 as IRQ[I], PB.04 as SCK[O], PB.05 as MISO[I], PB.01 as MOSI[O]
	GPIOB->MODER    &= ~0xC0000F0C;       /* Clear Bits                         */
	GPIOB->MODER    |=  0x00000104;       /* Alternate Function mode            */
	GPIOB->OSPEEDR  &= ~0xC0000F0C;       /* Clear Bits                         */
	GPIOB->OSPEEDR  |=  0x80000A08;       /* 50 MHz Fast speed                  */
	GPIOB->AFR[0]   &= ~0x00FF00F0;       /* Clear Bits                         */
	GPIOB->AFR[0]   |=  0x00000000;       /* Alternate Function mode AF12       */
	GPIOB->AFR[1]   &= ~0xF0000000;       /* Clear Bits                         */
	GPIOB->AFR[1]   |=  0x00000000;       /* Alternate Function mode AF12       */

	//PC.2 as BUSY[I]
	GPIOC->MODER    &= ~0x00000030;       /* Clear Bits                         */
	GPIOC->MODER    |=  0x00000000;       /* Alternate Function mode            */
	GPIOC->OSPEEDR  &= ~0x00000030;       /* Clear Bits                         */
	GPIOC->OSPEEDR  |=  0x00000020;       /* 50 MHz Fast speed                  */
	GPIOC->AFR[0]   &= ~0x00000F00;       /* Clear Bits                         */
	GPIOC->AFR[0]   |=  0x00000000;       /* Alternate Function mode AF12       */
	GPIOC->AFR[1]   &= ~0x00000000;       /* Clear Bits                         */
	GPIOC->AFR[1]   |=  0x00000000;       /* Alternate Function mode AF12       */

	TP_CS(1);
	TP_DCLK(0);
	TP_DIN(0);
}