Index: core/board_config.h
===================================================================
--- /YC3121_SDK/fw/core/board_config.h	(revision 801)
+++ /YC3121_SDK/fw/core/board_config.h	(working copy)
@@ -221,7 +221,7 @@
 #define QR_TFT_MOSI            (39)                  /*mosi pin*/
 #define QR_TFT_A0              (10)                  /*a0 pin*/
 #define QR_TFT_BL              (12 | (1 << 7))       /*bl pin*/
-#define QR_TFT_START_COLUMN    ( (320 - 236) / 2 )   /*display center*/   
+#define QR_TFT_START_COLUMN    ( (320 - 236) / 2 )   /*display center*/
 
 //key io
 #define QR_KEY_T_MATRIX        ( 0)                  /*key mode :0: T matrix key,1:matrix key*/
@@ -237,13 +237,13 @@
 
 /*QRdecode lcd */
 //lcd io
-#define QR_LCD_RST             (14) 
+#define QR_LCD_RST             (14)
 #define QR_LCD_CS              (15)
-#define QR_LCD_CLK             (10) 
+#define QR_LCD_CLK             (10)
 #define QR_LCD_MOSI            ( 3)
-#define QR_LCD_A0              (11) 
+#define QR_LCD_A0              (11)
 #define QR_LCD_BL              (39 | (1 << 7))
-#define QR_LCD_SPI_BOUDSPEED   (0xFF)  
+#define QR_LCD_SPI_BOUDSPEED   (0xFF)
 #define QR_LCD_BLANK_LINE_NUM  ( 0)                   /*Value range 0~7*/
 #define QR_LCD_COLUMN_NUM      (64)                   /*Value range 0~7*/
 #define QR_LCD_START_COLUMN    ((128 - 64)/2)
@@ -260,7 +260,16 @@
 #define QR_LCD_CAMERA_DATA       ( 2)
 #define QR_LCD_CAMERA_CS         (42)
 
+/*touch*/
+#define TP_Y_HIGH_PORT          GPIOA
+#define TP_Y_HIGH_IO_PIN        GPIO_Pin_4
+#define TP_Y_LOW_PORT           GPIOC
+#define TP_Y_LOW_IO_PIN         GPIO_Pin_12
+#define TP_X_HIGH_PORT          GPIOA
+#define TP_X_HIGH_IO_PIN        GPIO_Pin_13
+#define TP_X_LOW_PORT           GPIOC
+#define TP_X_LOW_IO_PIN         GPIO_Pin_11
 
-#endif  
+#endif
 
 #endif
Index: sdk/yc_touch.h
===================================================================
--- /YC3121_SDK/fw/sdk/yc_touch.h	(revision 799)
+++ /YC3121_SDK/fw/sdk/yc_touch.h	(working copy)
@@ -5,44 +5,96 @@
 Date         : 2020/1/14
 Description  :
 */
+
 #ifndef __YC_TOUCH_H__
 #define __YC_TOUCH_H__
 
 #include "yc_st7789.h"
+#include "board_config.h"
 #include "yc_adc.h"
-#define X 0
-#define Y 1
 
-//extern struct TOUCH _touch;
-//extern struct TOUCH_PANEl _touchpanel;
+//#define TP_DEBUG
+#if defined(TP_DEBUG)
+#define TP_PRINT        MyPrintf
+#else
+#define TP_PRINT(x,...)
+#endif
+
+#define TP_LEVEL_DIFF  (30)
+#define Tp_AbsolouteDifference(val_a,val_b) (val_a > val_b ? val_a - val_b : val_b - val_a)
+#define Tp_Get_Diff_Result(diff_value,com_value)    (diff_value > com_value ? FALSE : TRUE)
 
-struct TOUCH
+typedef enum
 {
-    uint16_t Adc_Max;
-    uint8_t PressAgainFalg;
-};
+    X_AXIS = 0,
+    Y_AXIS
+}TP_Axis_TypeDef;
 
+typedef struct
+{
+    uint16_t x;
+    uint16_t y;
+}Tp_Calibrate_CB;
+
+typedef struct
+{
+    uint16_t kx;
+    uint16_t ky;
+}Tp_CalibrateSlope_CB;
 
-//存放触摸屏信息的结构体
-struct TOUCH_PANEl
+typedef struct
 {
-    uint16_t x0; //逻辑坐标x
-    uint16_t y0; //逻辑坐标y
-    uint16_t ltx;  //上个点像素坐标x
-    uint16_t lty;  //上个点像素坐标y
-    uint16_t x;  //像素坐标x
-    uint16_t y;  //像素坐标y
-    uint16_t xbase; //x基物理坐标
-    uint16_t ybase; //y基物理坐标
-    uint16_t xlogic; //x基逻辑坐标
-    uint16_t ylogic; //y基逻辑坐标
-    uint16_t xfac;//X轴方向比例因子
-    uint16_t yfac;//Y轴方向比例因子
-};
-
-
-void TP_ReadXY(void);
-uint8_t TP_UpdateCoordVal(void);
-uint8_t TP_DetectPressing(void);
+    uint16_t nx; /*now x value*/
+    uint16_t ny; /*now y value*/
+    uint16_t lx; /*last x value*/
+    uint16_t ly; /*last y value*/
+    int pressflag; /*Press the tag*/
+}Tp_Sign_Para_TypeDef;
+
+#define REGULT_ADC_MAX      (0x7fe)
+
+#define TOP_SIZE_X (220)
+#define TOP_SIZE_Y (190)
+
+#define LEFT_UP_X (50)
+#define LEFT_UP_Y (50)
+
+#define RIGHT_UP_X  (LEFT_UP_X + TOP_SIZE_X)
+#define RIGHT_UP_Y  LEFT_UP_Y
+
+#define LEFT_DOWN_X  LEFT_UP_X
+#define LEFT_DOWN_Y  TOP_SIZE_Y
+
+#define RIGHT_DOWN_X  (LEFT_UP_X + TOP_SIZE_X)
+#define RIGHT_DOWN_Y  TOP_SIZE_Y
+
+#define MIDDLE_X  (LEFT_UP_X + TOP_SIZE_X/2)
+#define MIDDLE_Y  (LEFT_UP_Y + TOP_SIZE_Y/2)
+
+
+#define TP_CALIPONT_X  (160)
+#define TP_CALIPONT_Y  (145)
+#define TP_FAC_KX      (270)
+#define TP_FAC_KY      (363)
+#define TP_PEGPONT_X   (0x613)
+#define TP_PEGPONT_Y   (0x5a8)
+#define TP_CONSTANT_X  (1987)
+#define TP_CONSTANT_Y  (1974)
+
+extern Tp_Calibrate_CB DisplaySample[5];
+extern Tp_Calibrate_CB ScreenSample[5];
+extern Tp_Calibrate_CB PegPont;
+extern Tp_Calibrate_CB CaliPont;
+extern Tp_CalibrateSlope_CB fac;
+extern Tp_CalibrateSlope_CB Constant;
+
+#define PRESS_DETECT_CHANNEL    ADC_CHANNEL_6   /*The GPIO corresponding channel with pull-up mode needs to be selected to detect touch*/
+#define Y_DETECT_CHANNEL        ADC_CHANNEL_6   /*Y axis AD detection channel*/
+#define X_DETECT_CHANNEL        ADC_CHANNEL_7   /*X axis AD detection channel*/
+
+Boolean Tp_Calibrate(void);
+Boolean Tp_DetectPressing(void);
+void Tp_GetAdXY(uint16_t *adx,uint16_t *ady);
+void Tp_GetPoint(uint16_t *Disx,uint16_t*Disy);
 
 #endif
Index: sdk/yc_touch.c
===================================================================
--- /YC3121_SDK/fw/sdk/yc_touch.c	(revision 799)
+++ /YC3121_SDK/fw/sdk/yc_touch.c	(working copy)
@@ -5,321 +5,364 @@
 Date         : 2020/1/14
 Description  :
 */
-#include "yc_touch.h"
 
-struct TOUCH _touch;
-struct TOUCH_PANEl _touchpanel;
+#include "yc_touch.h"
 
-//058电签板
-//uint16_t TP_YHigh_Pin  = GPIO_Pin_10;
-//uint16_t TP_YLow_Pin   = GPIO_Pin_11;
-//uint16_t TP_XHigh_Pin  = GPIO_Pin_11;
-//uint16_t TP_XLow_Pin   = GPIO_Pin_10;
-
-//GPIO_TypeDef TP_YHigh_Port  = GPIOB;
-//GPIO_TypeDef TP_YLow_Port   = GPIOC;
-//GPIO_TypeDef TP_XHigh_Port  = GPIOB;
-//GPIO_TypeDef TP_XLow_Port   = GPIOC;
-
-//#define PRESS_DETECT_CHANNEL  ADC_CHANNEL_5 //需要选择配置上拉模式的GPIO对应通道，用于检测是否触摸
-//#define Y_DETECT_CHANNEL  	  ADC_CHANNEL_5 //Y轴AD检测通道
-//#define X_DETECT_CHANNEL  	  ADC_CHANNEL_6	//X轴AD检测通道
-
-//109电签板
-uint16_t TP_YHigh_Pin  = GPIO_Pin_4;
-uint16_t TP_YLow_Pin   = GPIO_Pin_12;
-uint16_t TP_XHigh_Pin  = GPIO_Pin_13;
-uint16_t TP_XLow_Pin   = GPIO_Pin_11;
-
-GPIO_TypeDef TP_YHigh_Port  = GPIOA;
-GPIO_TypeDef TP_YLow_Port   = GPIOC;
-GPIO_TypeDef TP_XHigh_Port  = GPIOA;
-GPIO_TypeDef TP_XLow_Port   = GPIOC;
-
-#define PRESS_DETECT_CHANNEL  ADC_CHANNEL_6 //需要选择配置上拉模式的GPIO对应通道，用于检测是否触摸
-#define Y_DETECT_CHANNEL  	  ADC_CHANNEL_6 //Y轴AD检测通道
-#define X_DETECT_CHANNEL  	  ADC_CHANNEL_7	//X轴AD检测通道
-
-
-/*
-函数功能:  得到逻辑坐标
-参数：		Y：纵轴
-			X：横轴
-返回值:  返回的逻辑坐标
-*/
-#define SAMP_CNT      8  //采样次数
-#define SAMP_CNT_DIV2 4   //采样数据中间值
-#define THRESHOLD 1  //滤波阈值
+Tp_Calibrate_CB DisplaySample[5];
+Tp_Calibrate_CB ScreenSample[5];
+Tp_Calibrate_CB PegPont;
+Tp_Calibrate_CB CaliPont;
+Tp_CalibrateSlope_CB fac;
+Tp_CalibrateSlope_CB Constant;
+
+/**
+  * @brief  Gets the level difference result
+  * @param  diff_value : value1
+  *         com_value  : value2
+  * @retval TRUE : in range ,FALSE: not in range
+  */
+static Boolean Tp_Get_Level_Result(uint32_t diff_value,uint32_t com_value)
+{
+    if((diff_value > (com_value + TP_LEVEL_DIFF)) || (diff_value < (com_value - TP_LEVEL_DIFF)))
+    {
+        return FALSE;
+    }
+    return TRUE;
+}
 
-#define Yad_min 0x480
-#define Xad_min 0x4a0
+/**
+  * @brief  Get the ad value
+  * @param  Axis :
+  *         X_AXIS  : x axis
+  *         Y_AXIS  : y axis
+  * @retval  ad value
+  */
 
-uint16_t TP_GetCoordVal(uint8_t x_or_y)
+static uint16_t Tp_GetCoordVal(TP_Axis_TypeDef Axis)
 {
-    uint16_t CoordValAdc[SAMP_CNT] = {0}, CoordVal[SAMP_CNT] = {0}, tempxy = 0;
-    uint8_t i = 0, min = 0, j = 0;
-    if (x_or_y == X)
-    {
-        // 配置采样Y轴IO
-        GPIO_Config(TP_YLow_Port, TP_YLow_Pin, OUTPUT_LOW);
-        GPIO_Config(TP_XHigh_Port, TP_XHigh_Pin, ANALOG);
-        GPIO_Config(TP_XLow_Port, TP_XLow_Pin, ANALOG);
-        GPIO_Config(TP_YHigh_Port, TP_YHigh_Pin, OUTPUT_HIGH);
-
-        // 采样SAMP_CNT组数据，并计算出逻辑坐标
-        for (i = 0; i < SAMP_CNT; i++)
-        {
-            CoordValAdc[i] = ADC_GetResult(Y_DETECT_CHANNEL);
-            if (CoordValAdc[i] < Yad_min)CoordValAdc[i] = Yad_min;
-            CoordVal[i] = 320 - ((CoordValAdc[i] - Yad_min) * 0.36);
-            if (CoordVal[i] > 320) CoordVal[i] = 0;
-            delay_us(10);
-        }
-    }
-    else if (x_or_y == Y)
+    uint8_t detect_chancl = 0;
+    if(Axis == X_AXIS)
     {
-        //配采样X轴IO
-        GPIO_Config(TP_YLow_Port, TP_YLow_Pin, ANALOG);
-        GPIO_Config(TP_XHigh_Port, TP_XHigh_Pin, OUTPUT_HIGH);
-        GPIO_Config(TP_XLow_Port, TP_XLow_Pin, OUTPUT_LOW);
-        GPIO_Config(TP_YHigh_Port, TP_YHigh_Pin, ANALOG);
-
-        //采样SAMP_CNT组数据，并计算出逻辑坐标
-        for (i = 0; i < SAMP_CNT; i++)
-        {
-            CoordValAdc[i] = ADC_GetResult(X_DETECT_CHANNEL);
-            if (CoordValAdc[i] < Xad_min)CoordValAdc[i] = Xad_min;
-            CoordVal[i] = 240 - (CoordValAdc[i] - Xad_min) * 0.28;
-            if (CoordVal[i] > 240) CoordVal[i] = 0;
-            delay_us(10);
-        }
+        GPIO_Config(TP_Y_LOW_PORT, TP_Y_LOW_IO_PIN, OUTPUT_LOW);
+        GPIO_Config(TP_Y_HIGH_PORT, TP_Y_HIGH_IO_PIN, OUTPUT_HIGH);
+        GPIO_Config(TP_X_HIGH_PORT, TP_X_HIGH_IO_PIN, ANALOG);
+        GPIO_Config(TP_X_LOW_PORT, TP_X_LOW_IO_PIN, ANALOG);
+        detect_chancl = Y_DETECT_CHANNEL;
     }
-
-    //滤波
-    for (i = 0; i < SAMP_CNT - 1; i++)
+    else if(Axis == Y_AXIS)
     {
-        min = i;
-        for (j = i + 1; j < SAMP_CNT; j++)
-        {
-            if (CoordVal[min] > CoordVal[j]) min = j;
-        }
-        tempxy = CoordVal[i];
-        CoordVal[i] = CoordVal[min];
-        CoordVal[min] = tempxy;
+        GPIO_Config(TP_Y_LOW_PORT, TP_Y_LOW_IO_PIN, ANALOG);
+        GPIO_Config(TP_Y_HIGH_PORT, TP_Y_HIGH_IO_PIN, ANALOG);
+        GPIO_Config(TP_X_HIGH_PORT, TP_X_HIGH_IO_PIN, OUTPUT_HIGH);
+        GPIO_Config(TP_X_LOW_PORT, TP_X_LOW_IO_PIN, OUTPUT_LOW);
+        detect_chancl = X_DETECT_CHANNEL;
     }
-
-    if ((CoordVal[SAMP_CNT_DIV2] - CoordVal[SAMP_CNT_DIV2 - 1]) > THRESHOLD) return 0;
-    tempxy = (CoordVal[SAMP_CNT_DIV2] + CoordVal[SAMP_CNT_DIV2 - 1]) / 2;
-    return tempxy;
+    delay_ms(1);
+    return ADC_GetResult(detect_chancl);
 }
 
-/*
-函数功能: 检测触摸屏是否按下
-返回值:  0:按下
-		 1:未按下
-*/
-uint8_t TP_DetectPressing(void)
+/**
+  * @brief  Get the x axis and  y axis  ad value
+  * @param  adx : The address to which the x axis AD value is stored
+  *         ady : The address to which the y axis AD value is stored
+  * @retval  None
+  */
+
+void Tp_GetAdXY(uint16_t *adx,uint16_t *ady)
 {
-    uint16_t AdcPress = 0;
-    GPIO_Config(TP_YLow_Port, TP_YLow_Pin, OUTPUT_LOW);
-    GPIO_Config(TP_XHigh_Port, TP_XHigh_Pin, ANALOG);
-    GPIO_Config(TP_XLow_Port, TP_XLow_Pin, PULL_UP);
-    GPIO_Config(TP_YHigh_Port, TP_YHigh_Pin, OUTPUT_HIGH);
-    AdcPress = ADC_GetResult(PRESS_DETECT_CHANNEL);
-    if ((AdcPress + 3)  < _touch.Adc_Max)return 0;
-    else return 1;
+    *adx = Tp_GetCoordVal(X_AXIS);
+    *ady = Tp_GetCoordVal(Y_AXIS);
+    return ;
 }
-/*
-函数功能:  由逻辑坐标得到物理坐标
-*/
-void TP_ReadXY(void)
-{
-    uint16_t diff_x = 0, diff_y = 0;
 
-    /*1. 得到逻辑坐标*/
-
-    _touchpanel.x0 = TP_GetCoordVal(X);
-    _touchpanel.y0 = TP_GetCoordVal(Y);
+/**
+  * @brief  Get the x axis and  y axis displayed coordinates
+  * @param  Disx : The address to which the x axis  displayed coordinates is stored
+  *         Disy : The address to which the y axis displayed coordinates is stored
+  * @retval  None
+  */
 
+void Tp_GetPoint(uint16_t *Disx,uint16_t*Disy)
+{
+    /*get adc*/
+    Tp_GetAdXY(Disx,Disy);
 
+    /*get display coordinate*/
+    *Disx = Tp_AbsolouteDifference(*Disx ,Constant.kx) * 100 / fac.kx;
+    *Disy = Tp_AbsolouteDifference(*Disy ,Constant.ky) * 100 / fac.ky;
+}
 
-    /*2. 得到物理坐标*/
+/**
+  * @brief  Check whether the touch screen is pressed
+  * @param  None
+  * @retval TRUE : press FALSE : no press
+  */
+Boolean Tp_DetectPressing(void)
+{
+    uint16_t AdcPress = 0;
+    GPIO_Config(TP_Y_LOW_PORT, TP_Y_LOW_IO_PIN, OUTPUT_LOW);
+    GPIO_Config(TP_Y_HIGH_PORT, TP_Y_HIGH_IO_PIN, OUTPUT_HIGH);
+    GPIO_Config(TP_X_HIGH_PORT, TP_X_HIGH_IO_PIN, ANALOG);
+    GPIO_Config(TP_X_LOW_PORT, TP_X_LOW_IO_PIN, PULL_UP);
+    AdcPress = ADC_GetResult(PRESS_DETECT_CHANNEL);
 
-    if (_touchpanel.x0 > _touchpanel.xlogic)
-        _touchpanel.x = _touchpanel.xbase + ((_touchpanel.x0 - _touchpanel.xlogic) * 100) / _touchpanel.xfac;
-    else
+    if((AdcPress + 3)  < REGULT_ADC_MAX)
     {
-        diff_x = _touchpanel.xlogic - _touchpanel.x0;
-        if (diff_x < _touchpanel.xbase)
-            _touchpanel.x = _touchpanel.xbase - (diff_x * 100) / _touchpanel.xfac;
-        else
-            _touchpanel.x = 1;
+        return TRUE;
     }
-
-
-    if (_touchpanel.y0 > _touchpanel.ylogic)
-        _touchpanel.y = _touchpanel.ybase + ((_touchpanel.y0 - _touchpanel.ylogic) * 100) / _touchpanel.yfac;
     else
     {
-        diff_y = _touchpanel.ylogic - _touchpanel.y0 ;
-        if (diff_y < _touchpanel.ybase)
-            _touchpanel.y = _touchpanel.ybase - (diff_y * 100) / _touchpanel.yfac;
-        else
-        {
-            _touchpanel.y = 1;
-        }
-
+        return FALSE;
     }
 }
 
-/*
-函数功能:  触摸屏校准、更新坐标
-返回值:  0 ：校准失败
-		 1 ：校准成功
-*/
-#define UPDTATE_DIFFERENCE 20
-uint8_t TP_UpdateCoordVal(void)
+/**
+  * @brief  Draw cross 
+  * @param  x: x axis
+  *         y: y axis
+  *         c: color
+  * @retval None
+  */
+static void Tp_Draw_Cross(uint16_t x,uint16_t y,uint16_t c)
 {
-    uint8_t cnt = 0, difference_x1 = 0, difference_x2 = 0, difference_y1 = 0, difference_y2 = 0;
-    uint32_t time_cnt = 0;
-    uint16_t x, y;
-    uint16_t x1, y1, x2, y2, x3, y3, x4, y4, x5, y5;
-
-    /*1. 判断是否校准过*/
-
-    //画第一个圆
-    St7789_tft_DrawCircle(30, 30, 20, RED);
-    while (1)
+    uint8_t s_offect = 10;
+    /*get screen type tft or lcd*/
+    St7899_tft_DrawLine(x - s_offect,y,x + s_offect,y,c);
+    St7899_tft_DrawLine(x,y - s_offect,x ,y+s_offect,c);
+}
+
+/**
+  * @brief  Gets the filtered AD value
+  * @param  None
+  * @retval The x and y coordinates returned
+  */
+static Tp_Calibrate_CB *Read_Adc_Result(void)
+{
+    uint8_t t = 0;
+    uint8_t t1 = 0;
+    uint8_t count = 0;
+    uint16_t temp = 0;
+    uint16_t Tpx = 0;
+    uint16_t Tpy = 0;
+    static Tp_Calibrate_CB screen;
+    int databuffer[2][10] = {{0},{0}};
+    while(count < 6)
     {
-        if (TP_DetectPressing() == 0) //表示触摸屏按下
+        if(Tp_DetectPressing())
         {
-            x = TP_GetCoordVal(X);
-            y = TP_GetCoordVal(Y);
-            while (TP_DetectPressing() == 0) {} //等待松开
-            cnt++; //记录按下的次数
+            Tp_GetAdXY(&Tpx,&Tpy);
+            databuffer[0][count] = Tpx;
+            databuffer[1][count] = Tpy;
+            TP_PRINT("0x%x,0x%x\n", databuffer[0][count], databuffer[1][count]);
+            count ++;
         }
-        switch (cnt)
+    }
+    if(count == 6)
+    {
+        do
         {
-        case 1:
-            x1 = x;
-            y1 = y;
-            //清除第一个圆
-            St7789_tft_DrawCircle(30, 30, 20, WHITE);
-            //画第二个圆
-            St7789_tft_DrawCircle(30, 240 - 30, 20, RED);
-            break;
-        case 2:
-            x2 = x;
-            y2 = y;
-            //清除第二个圆
-            St7789_tft_DrawCircle(30, 240 - 30, 20, WHITE);
-            //画第三个圆
-            St7789_tft_DrawCircle(320 - 30, 30, 20, RED);
-            break;
-        case 3:
-            x3 = x;
-            y3 = y;
-            //清除第三个圆
-            St7789_tft_DrawCircle(320 - 30, 30, 20, WHITE);
-            //画第四个圆
-            St7789_tft_DrawCircle(320 - 30, 240 - 30, 20, RED);
-            break;
-        case 4:
-            x4 = x;
-            y4 = y;
-            //清除第四个圆
-            St7789_tft_DrawCircle(320 - 30, 240 - 30, 20, WHITE);
-            //画第五个个圆
-            St7789_tft_DrawCircle(160, 120, 20, RED);
-            break;
-        case 5:
-            x5 = x;
-            y5 = y;
-            //清除第五个圆
-            St7789_tft_DrawCircle(160, 120, 20, WHITE);
-
-            //检测坐标差值
-            if (x2 > x1) difference_x1 = x2 - x1;
-            else difference_x1 = x1 - x2;
-            if (x3 > x4) difference_x2 = x3 - x4;
-            else difference_x2 = x4 - x3;
-
-            if (y3 > y1) difference_y1 = y3 - y1;
-            else difference_y1 = y1 - y3;
-            if (y2 > y4) difference_y2 = y2 - y4;
-            else difference_y2 = y4 - y2;
-
-            if ((difference_x1 < UPDTATE_DIFFERENCE) && (difference_x2 < UPDTATE_DIFFERENCE) \
-                    && (difference_y1 < UPDTATE_DIFFERENCE) && (difference_y2 < UPDTATE_DIFFERENCE))
+            t1 = 0;
+            for(t = 0; t < count -1 ; t++)
             {
-                //计算KX与Ky
-                _touchpanel.xfac = (((x3 - x1) * 100) / (320 - 30 - 30) + ((x4 - x2) * 100) / (320 - 30 - 30)) / 2;
-                _touchpanel.yfac = (((y2 - y1) * 100) / (240 - 30 - 30) + ((y4 - y3) * 100) / (240 - 30 - 30)) / 2;
-
-
-                // 以第一个圆的逻辑坐标 用Kx与ky计算出E点的逻辑坐标
-                x = x1 + (_touchpanel.xfac * (160 - 30)) / 100;
-                y = y1 + (_touchpanel.yfac * (120 - 30)) / 100;
-
-
-                // 比较触摸得到E点逻辑坐标与计算出E点逻辑坐标的差
-                if (x > x5) difference_x1 = x - x5;
-                else difference_x1 = x5 - x;
-                if (y > y5) difference_y1 = y - y5;
-                else difference_y1 = y5 - y;
-                if (difference_x1 < UPDTATE_DIFFERENCE && difference_y1 < UPDTATE_DIFFERENCE)
+                if(databuffer[0][t] > databuffer[0][t + 1])
                 {
-                    //保存基坐标
-                    _touchpanel.xbase = 30;
-                    _touchpanel.ybase = 30;
-                    _touchpanel.xlogic = x1;
-                    _touchpanel.ylogic = y1;
-
-                    MyPrintf(" _touchpanel.xfac : %d \n",  _touchpanel.xfac);
-                    MyPrintf(" _touchpanel.yfac : %d \n",  _touchpanel.yfac);
-                    MyPrintf(" _touchpanel.ybase : %d \n",  _touchpanel.ybase);
-                    MyPrintf(" _touchpanel.ybase : %d \n",  _touchpanel.ybase);
-                    MyPrintf(" _touchpanel.xlogic : %d \n", _touchpanel.xlogic);
-                    MyPrintf(" _touchpanel.ylogic : %d \n", _touchpanel.ylogic);
-
-                    return 1;
-                }
-                else
-                {
-                    cnt = 0;
+                    temp=databuffer[0][t+1];
+                    databuffer[0][t+1]=databuffer[0][t];
+                    databuffer[0][t]=temp;
+                    t1=1;
                 }
+            } 
+        }while(t1); // x sort
+        do
+        {
+            t1=0;
+			for(t=0;t<count-1;t++)
+			{
+				if(databuffer[1][t]>databuffer[1][t+1])
+				{
+					temp=databuffer[1][t+1];
+					databuffer[1][t+1]=databuffer[1][t];
+					databuffer[1][t]=temp;
+					t1=1;
+				}
+			}
+        }while(t1); // y sort
+        screen.x = 0;
+        screen.y = 0;
+        for( count = 2; count < 5; count ++)
+        {
+            screen.x += databuffer[0][count];
+            screen.y += databuffer[1][count];
+        }
+        screen.x /= 3;
+        screen.y /= 3;
+        TP_PRINT("screen.x : 0x%x,screen.y : 0x%x \n",screen.x,screen.y);
+        for(count = 2; count < 5; count ++)
+        {
+            if((((screen.x-databuffer[0][count])<4)
+            &&((databuffer[0][count]-screen.x)<4))
+            &&(((screen.y-databuffer[1][count])<4)
+            &&((databuffer[1][count]-screen.y)<4)))
+            {
+                ;/*continue detection*/
             }
             else
             {
-                cnt = 0;
+                return 0;/*data exception*/
             }
-            time_cnt++;
-            if (time_cnt >= 3)
-            {
-#if 1
-                _touchpanel.xfac = 102;
-                _touchpanel.yfac = 84;
-                _touchpanel.xbase = 30;
-                _touchpanel.ybase = 30;
-                _touchpanel.xlogic = 29;
-                _touchpanel.ylogic = 83;
-#else
-                _touchpanel.xfac = 96;
-                _touchpanel.yfac = 114;
-                _touchpanel.xbase = 30;
-                _touchpanel.ybase = 30;
-                _touchpanel.xlogic = 51;
-                _touchpanel.ylogic = 26;
-#endif
-
-                return 0;
+        }
+        return &screen;
+    }
+    return 0;
+}
 
-            }
-            St7789_tft_DrawCircle(30, 30, 20, RED);
-            break;
+/**
+  * @brief  Touch the calibration
+  * @param  None
+  * @retval TRUE : calibration success, FALSE : calibration fail
+  */
 
+Boolean Tp_Calibrate(void)
+{
+    uint8_t i = 0;
+    int level_diff_buff[4] = {0};
+    int dis_diff_buff[4] = {0};
+    Tp_Calibrate_CB *ptr;
+    /* read calibrate data to flash */
+    /* get screen type tft or lcd */
+    DisplaySample[0].x = LEFT_UP_X;
+    DisplaySample[0].y = LEFT_UP_Y;
+    DisplaySample[1].x = RIGHT_UP_X;
+    DisplaySample[1].y = RIGHT_UP_Y;
+    DisplaySample[2].x = LEFT_DOWN_X;
+    DisplaySample[2].y = LEFT_DOWN_Y;
+    DisplaySample[3].x = RIGHT_DOWN_X;
+    DisplaySample[3].y = RIGHT_DOWN_Y;
+    DisplaySample[4].x = MIDDLE_X;
+    DisplaySample[4].y = MIDDLE_Y;
+
+    uint8_t size_sample =  sizeof(DisplaySample)/sizeof(Tp_Calibrate_CB);
+    TP_PRINT("szie_d : %d\n",sizeof(DisplaySample)/sizeof(Tp_Calibrate_CB));
+    
+    for(i = 0; i< size_sample ; i++)
+    {
+        /*Please align the calibration with the cross, otherwise the calibration will not work*/
+        Tp_Draw_Cross(DisplaySample[i].x,DisplaySample[i].y,BLACK);
+        
+        do
+        {
+            ptr = Read_Adc_Result();
+        }
+        while(ptr == (void*)0);
+        
+        ScreenSample[i].x = ptr->x;
+        ScreenSample[i].y = ptr->y;
+        
+        Tp_Draw_Cross(DisplaySample[i].x,DisplaySample[i].y,WHITE);
+        
+        if(i< (size_sample - 1))
+        {
+            delay_ms(500);
+        }
+    }
+    
+    /*Calculate the ADC difference*/
+    /*x0 ~ x2*/
+    level_diff_buff[0] = Tp_AbsolouteDifference(ScreenSample[0].x,ScreenSample[2].x);
+    
+    /*x1 ~ x3*/
+    level_diff_buff[1] = Tp_AbsolouteDifference(ScreenSample[1].x,ScreenSample[3].x);
+    
+    /*y0 ~ y1*/
+    level_diff_buff[2] = Tp_AbsolouteDifference(ScreenSample[0].y,ScreenSample[1].y);
+    
+    /*y2 ~ y3*/
+    level_diff_buff[3] = Tp_AbsolouteDifference(ScreenSample[2].y,ScreenSample[3].y);
+    
+    for(i = 0; i < 4 ; i ++)
+    {
+        if(Tp_Get_Diff_Result(level_diff_buff[i],TP_LEVEL_DIFF) == FALSE)
+        {
+            TP_PRINT("level_diff_buff[%d] = %d  err \n",i,level_diff_buff[i] );
+            return FALSE;
         }
+    }
+    
+    /*x0 ~ x1*/
+    dis_diff_buff[0] = Tp_AbsolouteDifference(ScreenSample[0].x,ScreenSample[1].x);
+    
+    /*x2 ~ x3*/
+    dis_diff_buff[1] = Tp_AbsolouteDifference(ScreenSample[2].x,ScreenSample[3].x);
+    
 
+    if(Tp_Get_Level_Result(dis_diff_buff[0],dis_diff_buff[1]) == FALSE || dis_diff_buff[0] < TOP_SIZE_X || dis_diff_buff[1] < TOP_SIZE_X)
+    {
+        TP_PRINT("dis_diff_buffx[0] : %d,dis_diff_buffx[1] : %d err \n",dis_diff_buff[0],dis_diff_buff[1]);
+        return FALSE;
     }
-    return 0;
+    
+    
+    /*y0 ~ y2*/
+    dis_diff_buff[2] = Tp_AbsolouteDifference(ScreenSample[0].y,ScreenSample[2].y);
+    
+    /*y1 ~ y3*/
+    dis_diff_buff[3] = Tp_AbsolouteDifference(ScreenSample[1].y,ScreenSample[3].y);
+    
+    if(Tp_Get_Level_Result(dis_diff_buff[2],dis_diff_buff[3]) == FALSE || dis_diff_buff[2] < TOP_SIZE_Y || dis_diff_buff[3] < TOP_SIZE_Y)
+    {
+        TP_PRINT("dis_diff_buffy[%d] : %d err \n",i,dis_diff_buff[i] );
+        return FALSE;
+    }
+    
+    
+    /*Calculation of kx ky*/
+    
+     fac.kx = (((dis_diff_buff[0] * 100) /  (Tp_AbsolouteDifference(DisplaySample[0].x,DisplaySample[1].x))) + \
+               ((dis_diff_buff[1] * 100) /  (Tp_AbsolouteDifference(DisplaySample[2].x,DisplaySample[3].x))))/2 ;
+    
+        
+     fac.ky = (((dis_diff_buff[2] * 100) /  (Tp_AbsolouteDifference(DisplaySample[0].y,DisplaySample[2].y))) + \
+           ((dis_diff_buff[3] * 100) /  (Tp_AbsolouteDifference(DisplaySample[1].y,DisplaySample[3].y))))/2 ;
+
+    Constant.kx = ScreenSample[0].x + ((DisplaySample[0].x)* fac.kx)/100;// y = -kx + b
+    Constant.ky = ScreenSample[0].y + ((DisplaySample[0].y)* fac.ky)/100;
+
+    TP_PRINT("fac.kx : %d __ fac.ky: %d\n",fac.kx,fac.ky);
+    TP_PRINT("constant.kx : %d __ constant.ky: %d\n",Constant.kx,Constant.ky);
+    
+    /*Calculate the coordinate of the center point from kx, ky,DisplaySample[4].x,DisplaySample[4].y*/
+    
+    PegPont.x = Tp_AbsolouteDifference(fac.kx*DisplaySample[4].x/100,Constant.kx);
+    PegPont.y = Tp_AbsolouteDifference(fac.ky*DisplaySample[4].y/100,Constant.ky);
+    
+    TP_PRINT("PegPont.x : %x __ PegPont.y: %x\n",PegPont.x,PegPont.y);
+    
+    /*Check the calculation coordinates and measurement coordinates of the center point*/
+    
+    
+    uint16_t center_x = Tp_AbsolouteDifference(ScreenSample[4].x,PegPont.x);
+    uint16_t center_y = Tp_AbsolouteDifference(ScreenSample[4].y,PegPont.y);
+    
+    TP_PRINT("center_x: %d __ center_y: %d\n",center_x,center_y);
+    
+    CaliPont.x = DisplaySample[4].x;
+    CaliPont.y = DisplaySample[4].y;
+
+    TP_PRINT(" CaliPont.x = %d\n",CaliPont.x);
+    TP_PRINT(" CaliPont.y = %d\n",CaliPont.y);
+    
+    if(Tp_Get_Diff_Result(center_x,TP_LEVEL_DIFF) ==  FALSE || Tp_Get_Diff_Result(center_y,TP_LEVEL_DIFF) == FALSE)
+    {
+        TP_PRINT("Tp_Get_Diff_Result center err \n");
+        return FALSE;
+    }
+    
+    /*Record calibration value*/
+    
+    TP_PRINT(" fac.kx : %d \n",  fac.kx);
+    TP_PRINT(" fac.ky : %d \n", fac.ky);
+    TP_PRINT(" PegPont.x : %x \n", PegPont.x);
+    TP_PRINT(" PegPont.y : %x \n", PegPont.y);
+    
+    return TRUE;
 }
