Shish

Dependencies:   mbed USBDevice GLCD

Committer:
pepperu96
Date:
Tue Nov 09 16:04:17 2021 +0000
Revision:
6:3a0fd4eef371
Parent:
0:9ae27a35a771
debug deactivated

Who changed what in which revision?

UserRevisionLine numberNew contents of line
montie97 0:9ae27a35a771 1 /****************************************Copyright (c)****************************************************
montie97 0:9ae27a35a771 2 **
montie97 0:9ae27a35a771 3 ** http://www.powermcu.com
montie97 0:9ae27a35a771 4 **
montie97 0:9ae27a35a771 5 **--------------File Info---------------------------------------------------------------------------------
montie97 0:9ae27a35a771 6 ** File name: TouchPanel.c
montie97 0:9ae27a35a771 7 ** Descriptions: The TouchPanel application function
montie97 0:9ae27a35a771 8 **
montie97 0:9ae27a35a771 9 **--------------------------------------------------------------------------------------------------------
montie97 0:9ae27a35a771 10 ** Created by: AVRman
montie97 0:9ae27a35a771 11 ** Created date: 2010-11-7
montie97 0:9ae27a35a771 12 ** Version: v1.0
montie97 0:9ae27a35a771 13 ** Descriptions: The original version
montie97 0:9ae27a35a771 14 **
montie97 0:9ae27a35a771 15 **--------------------------------------------------------------------------------------------------------
montie97 0:9ae27a35a771 16 ** Modified by:
montie97 0:9ae27a35a771 17 ** Modified date:
montie97 0:9ae27a35a771 18 ** Version:
montie97 0:9ae27a35a771 19 ** Descriptions:
montie97 0:9ae27a35a771 20 **
montie97 0:9ae27a35a771 21 *********************************************************************************************************/
montie97 0:9ae27a35a771 22
montie97 0:9ae27a35a771 23 /* Includes ------------------------------------------------------------------*/
montie97 0:9ae27a35a771 24 #include "../GLCD/GLCD.h"
montie97 0:9ae27a35a771 25 #define CHX 0x90 /* ͨ��Y+��ѡ������� */
montie97 0:9ae27a35a771 26 #define CHY 0xd0 /* ͨ��X+��ѡ������� */
montie97 0:9ae27a35a771 27
montie97 0:9ae27a35a771 28 #define SSPSR_RNE 2
montie97 0:9ae27a35a771 29 #define SSPSR_BSY 4
montie97 0:9ae27a35a771 30
montie97 0:9ae27a35a771 31 /* bit-frequency = PCLK / CPSR */
montie97 0:9ae27a35a771 32 #define SPI_SPEED_4MHz 18 /* 4MHz */
montie97 0:9ae27a35a771 33 #define SPI_SPEED_2MHz 36 /* 2MHz */
montie97 0:9ae27a35a771 34 #define SPI_SPEED_1MHz 72 /* 1MHz */
montie97 0:9ae27a35a771 35 #define SPI_SPEED_500kHz 144 /* 500kHz */
montie97 0:9ae27a35a771 36 #define SPI_SPEED_400kHz 180 /* 400kHz */
montie97 0:9ae27a35a771 37
montie97 0:9ae27a35a771 38
montie97 0:9ae27a35a771 39 #define TP_CS(a) if (a) \
montie97 0:9ae27a35a771 40 LPC_GPIO0->FIOSET = (1<<6);\
montie97 0:9ae27a35a771 41 else \
montie97 0:9ae27a35a771 42 LPC_GPIO0->FIOCLR = (1<<6)
montie97 0:9ae27a35a771 43
montie97 0:9ae27a35a771 44 #define TP_INT_IN ( LPC_GPIO2->FIOPIN & (1<<13) )
montie97 0:9ae27a35a771 45
montie97 0:9ae27a35a771 46
montie97 0:9ae27a35a771 47 typedef struct POINT
montie97 0:9ae27a35a771 48 {
montie97 0:9ae27a35a771 49 uint16_t x;
montie97 0:9ae27a35a771 50 uint16_t y;
montie97 0:9ae27a35a771 51 }Coordinate;
montie97 0:9ae27a35a771 52
montie97 0:9ae27a35a771 53
montie97 0:9ae27a35a771 54 typedef struct Matrix
montie97 0:9ae27a35a771 55 {
montie97 0:9ae27a35a771 56 long double An,
montie97 0:9ae27a35a771 57 Bn,
montie97 0:9ae27a35a771 58 Cn,
montie97 0:9ae27a35a771 59 Dn,
montie97 0:9ae27a35a771 60 En,
montie97 0:9ae27a35a771 61 Fn,
montie97 0:9ae27a35a771 62 Divider ;
montie97 0:9ae27a35a771 63 } Matrix ;
montie97 0:9ae27a35a771 64 /* Private variables ---------------------------------------------------------*/
montie97 0:9ae27a35a771 65 Matrix matrix ;
montie97 0:9ae27a35a771 66 Coordinate display ;
montie97 0:9ae27a35a771 67
montie97 0:9ae27a35a771 68 /* DisplaySample LCD�����϶�Ӧ��ads7843����ADֵ �磺LCD ����45,45 Ӧ�õ�X Y����ADC�ֱ�Ϊ3388,920 */
montie97 0:9ae27a35a771 69 Coordinate ScreenSample[3];
montie97 0:9ae27a35a771 70 /* LCD�ϵ����� */
montie97 0:9ae27a35a771 71 Coordinate DisplaySample[3] = {
montie97 0:9ae27a35a771 72 { 45, 45 },
montie97 0:9ae27a35a771 73 { 45, 270},
montie97 0:9ae27a35a771 74 { 190,190}
montie97 0:9ae27a35a771 75 } ;
montie97 0:9ae27a35a771 76
montie97 0:9ae27a35a771 77
montie97 0:9ae27a35a771 78 void TP_Init(void);
montie97 0:9ae27a35a771 79 Coordinate *Read_Ads7846(void);
montie97 0:9ae27a35a771 80 void TouchPanel_Calibrate(void);
montie97 0:9ae27a35a771 81 void DrawCross(uint16_t Xpos,uint16_t Ypos);
montie97 0:9ae27a35a771 82 void TP_DrawPoint(uint16_t Xpos,uint16_t Ypos);
montie97 0:9ae27a35a771 83 uint8_t setCalibrationMatrix( Coordinate * displayPtr,Coordinate * screenPtr,Matrix * matrixPtr);
montie97 0:9ae27a35a771 84 uint8_t getDisplayPoint(Coordinate * displayPtr,Coordinate * screenPtr,Matrix * matrixPtr );
montie97 0:9ae27a35a771 85
montie97 0:9ae27a35a771 86
montie97 0:9ae27a35a771 87 /* Private define ------------------------------------------------------------*/
montie97 0:9ae27a35a771 88 #define THRESHOLD 2 /* ��ֵ���� */
montie97 0:9ae27a35a771 89
montie97 0:9ae27a35a771 90
montie97 0:9ae27a35a771 91 /*******************************************************************************
montie97 0:9ae27a35a771 92 * Function Name : LPC17xx_SPI_SetSpeed
montie97 0:9ae27a35a771 93 * Description : Set clock speed to desired value
montie97 0:9ae27a35a771 94 * Input : - speed: speed
montie97 0:9ae27a35a771 95 * Output : None
montie97 0:9ae27a35a771 96 * Return : None
montie97 0:9ae27a35a771 97 * Attention : None
montie97 0:9ae27a35a771 98 *******************************************************************************/
montie97 0:9ae27a35a771 99 void LPC17xx_SPI_SetSpeed (uint8_t speed)
montie97 0:9ae27a35a771 100 {
montie97 0:9ae27a35a771 101 speed &= 0xFE;
montie97 0:9ae27a35a771 102 if ( speed < 2 ) {
montie97 0:9ae27a35a771 103 speed = 2 ;
montie97 0:9ae27a35a771 104 }
montie97 0:9ae27a35a771 105 LPC_SSP1->CPSR = speed;
montie97 0:9ae27a35a771 106 }
montie97 0:9ae27a35a771 107
montie97 0:9ae27a35a771 108 /*******************************************************************************
montie97 0:9ae27a35a771 109 * Function Name : ADS7843_SPI_Init
montie97 0:9ae27a35a771 110 * Description : ADS7843 SPI ��ʼ��
montie97 0:9ae27a35a771 111 * Input : None
montie97 0:9ae27a35a771 112 * Output : None
montie97 0:9ae27a35a771 113 * Return : None
montie97 0:9ae27a35a771 114 * Attention : None
montie97 0:9ae27a35a771 115 *******************************************************************************/
montie97 0:9ae27a35a771 116 static void ADS7843_SPI_Init(void)
montie97 0:9ae27a35a771 117 {
montie97 0:9ae27a35a771 118 volatile uint32_t dummy;
montie97 0:9ae27a35a771 119
montie97 0:9ae27a35a771 120 /* Initialize and enable the SSP1 Interface module. */
montie97 0:9ae27a35a771 121 LPC_SC->PCONP |= (1 << 10); /* Enable power to SSPI1 block */
montie97 0:9ae27a35a771 122
montie97 0:9ae27a35a771 123 /* P0.7 SCK, P0.8 MISO, P0.9 MOSI are SSP pins. */
montie97 0:9ae27a35a771 124 LPC_PINCON->PINSEL0 &= ~((3UL<<14) | (3UL<<16) | (3UL<<18)) ; /* P0.7,P0.8,P0.9 cleared */
montie97 0:9ae27a35a771 125 LPC_PINCON->PINSEL0 |= (2UL<<14) | (2UL<<16) | (2UL<<18); /* P0.7 SCK1,P0.8 MISO1,P0.9 MOSI1 */
montie97 0:9ae27a35a771 126
montie97 0:9ae27a35a771 127 /* PCLK_SSP1=CCLK */
montie97 0:9ae27a35a771 128 LPC_SC->PCLKSEL0 &= ~(3<<20); /* PCLKSP0 = CCLK/4 (18MHz) */
montie97 0:9ae27a35a771 129 LPC_SC->PCLKSEL0 |= (1<<20); /* PCLKSP0 = CCLK (72MHz) */
montie97 0:9ae27a35a771 130
montie97 0:9ae27a35a771 131 LPC_SSP1->CR0 = 0x0007; /* 8Bit, CPOL=0, CPHA=0 */
montie97 0:9ae27a35a771 132 LPC_SSP1->CR1 = 0x0002; /* SSP1 enable, master */
montie97 0:9ae27a35a771 133
montie97 0:9ae27a35a771 134 LPC17xx_SPI_SetSpeed ( SPI_SPEED_500kHz );
montie97 0:9ae27a35a771 135
montie97 0:9ae27a35a771 136 /* wait for busy gone */
montie97 0:9ae27a35a771 137 while( LPC_SSP1->SR & ( 1 << SSPSR_BSY ) );
montie97 0:9ae27a35a771 138
montie97 0:9ae27a35a771 139 /* drain SPI RX FIFO */
montie97 0:9ae27a35a771 140 while( LPC_SSP1->SR & ( 1 << SSPSR_RNE ) )
montie97 0:9ae27a35a771 141 {
montie97 0:9ae27a35a771 142 dummy = LPC_SSP1->DR;
montie97 0:9ae27a35a771 143 }
montie97 0:9ae27a35a771 144 }
montie97 0:9ae27a35a771 145
montie97 0:9ae27a35a771 146 /*******************************************************************************
montie97 0:9ae27a35a771 147 * Function Name : TP_Init
montie97 0:9ae27a35a771 148 * Description : ADS7843�˿ڳ�ʼ��
montie97 0:9ae27a35a771 149 * Input : None
montie97 0:9ae27a35a771 150 * Output : None
montie97 0:9ae27a35a771 151 * Return : None
montie97 0:9ae27a35a771 152 * Attention : None
montie97 0:9ae27a35a771 153 *******************************************************************************/
montie97 0:9ae27a35a771 154 void TP_Init(void)
montie97 0:9ae27a35a771 155 {
montie97 0:9ae27a35a771 156 LPC_GPIO0->FIODIR |= (1<<6); /* P0.6 CS is output */
montie97 0:9ae27a35a771 157 LPC_GPIO2->FIODIR |= (0<<13); /* P2.13 TP_INT is input */
montie97 0:9ae27a35a771 158 TP_CS(1);
montie97 0:9ae27a35a771 159 ADS7843_SPI_Init();
montie97 0:9ae27a35a771 160 }
montie97 0:9ae27a35a771 161
montie97 0:9ae27a35a771 162 /*******************************************************************************
montie97 0:9ae27a35a771 163 * Function Name : DelayUS
montie97 0:9ae27a35a771 164 * Description : ��ʱ1us
montie97 0:9ae27a35a771 165 * Input : - cnt: ��ʱֵ
montie97 0:9ae27a35a771 166 * Output : None
montie97 0:9ae27a35a771 167 * Return : None
montie97 0:9ae27a35a771 168 * Attention : None
montie97 0:9ae27a35a771 169 *******************************************************************************/
montie97 0:9ae27a35a771 170 static void DelayUS(uint32_t cnt)
montie97 0:9ae27a35a771 171 {
montie97 0:9ae27a35a771 172 uint32_t i;
montie97 0:9ae27a35a771 173 for(i = 0;i<cnt;i++)
montie97 0:9ae27a35a771 174 {
montie97 0:9ae27a35a771 175 uint8_t us = 12; /* ����ֵΪ12����Լ��1΢�� */
montie97 0:9ae27a35a771 176 while (us--) /* ��1΢�� */
montie97 0:9ae27a35a771 177 {
montie97 0:9ae27a35a771 178 ;
montie97 0:9ae27a35a771 179 }
montie97 0:9ae27a35a771 180 }
montie97 0:9ae27a35a771 181 }
montie97 0:9ae27a35a771 182
montie97 0:9ae27a35a771 183
montie97 0:9ae27a35a771 184 /*******************************************************************************
montie97 0:9ae27a35a771 185 * Function Name : WR_CMD
montie97 0:9ae27a35a771 186 * Description : �� ADS7843���
montie97 0:9ae27a35a771 187 * Input : - cmd: ���������
montie97 0:9ae27a35a771 188 * Output : None
montie97 0:9ae27a35a771 189 * Return : None
montie97 0:9ae27a35a771 190 * Attention : None
montie97 0:9ae27a35a771 191 *******************************************************************************/
montie97 0:9ae27a35a771 192 static uint8_t WR_CMD (uint8_t cmd)
montie97 0:9ae27a35a771 193 {
montie97 0:9ae27a35a771 194 uint8_t byte_r;
montie97 0:9ae27a35a771 195
montie97 0:9ae27a35a771 196 while (LPC_SSP1->SR & (1 << SSPSR_BSY) ); /* Wait for transfer to finish */
montie97 0:9ae27a35a771 197 LPC_SSP1->DR = cmd;
montie97 0:9ae27a35a771 198 while (LPC_SSP1->SR & (1 << SSPSR_BSY) ); /* Wait for transfer to finish */
montie97 0:9ae27a35a771 199 while( !( LPC_SSP1->SR & ( 1 << SSPSR_RNE ) ) ); /* Wait untill the Rx FIFO is not empty */
montie97 0:9ae27a35a771 200 byte_r = LPC_SSP1->DR;
montie97 0:9ae27a35a771 201
montie97 0:9ae27a35a771 202 return byte_r; /* Return received value */
montie97 0:9ae27a35a771 203 }
montie97 0:9ae27a35a771 204
montie97 0:9ae27a35a771 205
montie97 0:9ae27a35a771 206
montie97 0:9ae27a35a771 207 /*******************************************************************************
montie97 0:9ae27a35a771 208 * Function Name : RD_AD
montie97 0:9ae27a35a771 209 * Description : ��ȡADCֵ
montie97 0:9ae27a35a771 210 * Input : None
montie97 0:9ae27a35a771 211 * Output : None
montie97 0:9ae27a35a771 212 * Return : ADS7843���ض��ֽ�����
montie97 0:9ae27a35a771 213 * Attention : None
montie97 0:9ae27a35a771 214 *******************************************************************************/
montie97 0:9ae27a35a771 215 static int RD_AD(void)
montie97 0:9ae27a35a771 216 {
montie97 0:9ae27a35a771 217 unsigned short buf,temp;
montie97 0:9ae27a35a771 218
montie97 0:9ae27a35a771 219 temp = WR_CMD(0x00);
montie97 0:9ae27a35a771 220 buf = temp<<8;
montie97 0:9ae27a35a771 221 DelayUS(1);
montie97 0:9ae27a35a771 222 temp = WR_CMD(0x00);;
montie97 0:9ae27a35a771 223 buf |= temp;
montie97 0:9ae27a35a771 224 buf>>=3;
montie97 0:9ae27a35a771 225 buf&=0xfff;
montie97 0:9ae27a35a771 226 return buf;
montie97 0:9ae27a35a771 227 }
montie97 0:9ae27a35a771 228
montie97 0:9ae27a35a771 229
montie97 0:9ae27a35a771 230 /*******************************************************************************
montie97 0:9ae27a35a771 231 * Function Name : Read_X
montie97 0:9ae27a35a771 232 * Description : ��ȡADS7843ͨ��X+��ADCֵ
montie97 0:9ae27a35a771 233 * Input : None
montie97 0:9ae27a35a771 234 * Output : None
montie97 0:9ae27a35a771 235 * Return : ADS7843����ͨ��X+��ADCֵ
montie97 0:9ae27a35a771 236 * Attention : None
montie97 0:9ae27a35a771 237 *******************************************************************************/
montie97 0:9ae27a35a771 238 int Read_X(void)
montie97 0:9ae27a35a771 239 {
montie97 0:9ae27a35a771 240 int i;
montie97 0:9ae27a35a771 241 TP_CS(0);
montie97 0:9ae27a35a771 242 DelayUS(1);
montie97 0:9ae27a35a771 243 WR_CMD(CHX);
montie97 0:9ae27a35a771 244 DelayUS(1);
montie97 0:9ae27a35a771 245 i=RD_AD();
montie97 0:9ae27a35a771 246 TP_CS(1);
montie97 0:9ae27a35a771 247 return i;
montie97 0:9ae27a35a771 248 }
montie97 0:9ae27a35a771 249
montie97 0:9ae27a35a771 250 /*******************************************************************************
montie97 0:9ae27a35a771 251 * Function Name : Read_Y
montie97 0:9ae27a35a771 252 * Description : ��ȡADS7843ͨ��Y+��ADCֵ
montie97 0:9ae27a35a771 253 * Input : None
montie97 0:9ae27a35a771 254 * Output : None
montie97 0:9ae27a35a771 255 * Return : ADS7843����ͨ��Y+��ADCֵ
montie97 0:9ae27a35a771 256 * Attention : None
montie97 0:9ae27a35a771 257 *******************************************************************************/
montie97 0:9ae27a35a771 258 int Read_Y(void)
montie97 0:9ae27a35a771 259 {
montie97 0:9ae27a35a771 260 int i;
montie97 0:9ae27a35a771 261 TP_CS(0);
montie97 0:9ae27a35a771 262 DelayUS(1);
montie97 0:9ae27a35a771 263 WR_CMD(CHY);
montie97 0:9ae27a35a771 264 DelayUS(1);
montie97 0:9ae27a35a771 265 i=RD_AD();
montie97 0:9ae27a35a771 266 TP_CS(1);
montie97 0:9ae27a35a771 267 return i;
montie97 0:9ae27a35a771 268 }
montie97 0:9ae27a35a771 269
montie97 0:9ae27a35a771 270
montie97 0:9ae27a35a771 271 /*******************************************************************************
montie97 0:9ae27a35a771 272 * Function Name : TP_GetAdXY
montie97 0:9ae27a35a771 273 * Description : ��ȡADS7843 ͨ��X+ ͨ��Y+��ADCֵ
montie97 0:9ae27a35a771 274 * Input : None
montie97 0:9ae27a35a771 275 * Output : None
montie97 0:9ae27a35a771 276 * Return : ADS7843���� ͨ��X+ ͨ��Y+��ADCֵ
montie97 0:9ae27a35a771 277 * Attention : None
montie97 0:9ae27a35a771 278 *******************************************************************************/
montie97 0:9ae27a35a771 279 void TP_GetAdXY(int *x,int *y)
montie97 0:9ae27a35a771 280 {
montie97 0:9ae27a35a771 281 int adx,ady;
montie97 0:9ae27a35a771 282 adx=Read_X();
montie97 0:9ae27a35a771 283 DelayUS(1);
montie97 0:9ae27a35a771 284 ady=Read_Y();
montie97 0:9ae27a35a771 285 *x=adx;
montie97 0:9ae27a35a771 286 *y=ady;
montie97 0:9ae27a35a771 287 }
montie97 0:9ae27a35a771 288
montie97 0:9ae27a35a771 289 /*******************************************************************************
montie97 0:9ae27a35a771 290 * Function Name : TP_DrawPoint
montie97 0:9ae27a35a771 291 * Description : ��ָ�����껭��
montie97 0:9ae27a35a771 292 * Input : - Xpos: Row Coordinate
montie97 0:9ae27a35a771 293 * - Ypos: Line Coordinate
montie97 0:9ae27a35a771 294 * Output : None
montie97 0:9ae27a35a771 295 * Return : None
montie97 0:9ae27a35a771 296 * Attention : None
montie97 0:9ae27a35a771 297 *******************************************************************************/
montie97 0:9ae27a35a771 298 void TP_DrawPoint(uint16_t Xpos,uint16_t Ypos)
montie97 0:9ae27a35a771 299 {
montie97 0:9ae27a35a771 300 LCD_SetPoint(Xpos,Ypos,0xf800); /* ���ĵ� */
montie97 0:9ae27a35a771 301 LCD_SetPoint(Xpos+1,Ypos,0xf800);
montie97 0:9ae27a35a771 302 LCD_SetPoint(Xpos,Ypos+1,0xf800);
montie97 0:9ae27a35a771 303 LCD_SetPoint(Xpos+1,Ypos+1,0xf800);
montie97 0:9ae27a35a771 304 }
montie97 0:9ae27a35a771 305
montie97 0:9ae27a35a771 306 /*******************************************************************************
montie97 0:9ae27a35a771 307 * Function Name : DrawCross
montie97 0:9ae27a35a771 308 * Description : ��ָ�����껭ʮ��׼��
montie97 0:9ae27a35a771 309 * Input : - Xpos: Row Coordinate
montie97 0:9ae27a35a771 310 * - Ypos: Line Coordinate
montie97 0:9ae27a35a771 311 * Output : None
montie97 0:9ae27a35a771 312 * Return : None
montie97 0:9ae27a35a771 313 * Attention : None
montie97 0:9ae27a35a771 314 *******************************************************************************/
montie97 0:9ae27a35a771 315 void DrawCross(uint16_t Xpos,uint16_t Ypos)
montie97 0:9ae27a35a771 316 {
montie97 0:9ae27a35a771 317 LCD_DrawLine(Xpos-15,Ypos,Xpos-2,Ypos,0xffff);
montie97 0:9ae27a35a771 318 LCD_DrawLine(Xpos+2,Ypos,Xpos+15,Ypos,0xffff);
montie97 0:9ae27a35a771 319 LCD_DrawLine(Xpos,Ypos-15,Xpos,Ypos-2,0xffff);
montie97 0:9ae27a35a771 320 LCD_DrawLine(Xpos,Ypos+2,Xpos,Ypos+15,0xffff);
montie97 0:9ae27a35a771 321
montie97 0:9ae27a35a771 322 LCD_DrawLine(Xpos-15,Ypos+15,Xpos-7,Ypos+15,RGB565CONVERT(184,158,131));
montie97 0:9ae27a35a771 323 LCD_DrawLine(Xpos-15,Ypos+7,Xpos-15,Ypos+15,RGB565CONVERT(184,158,131));
montie97 0:9ae27a35a771 324
montie97 0:9ae27a35a771 325 LCD_DrawLine(Xpos-15,Ypos-15,Xpos-7,Ypos-15,RGB565CONVERT(184,158,131));
montie97 0:9ae27a35a771 326 LCD_DrawLine(Xpos-15,Ypos-7,Xpos-15,Ypos-15,RGB565CONVERT(184,158,131));
montie97 0:9ae27a35a771 327
montie97 0:9ae27a35a771 328 LCD_DrawLine(Xpos+7,Ypos+15,Xpos+15,Ypos+15,RGB565CONVERT(184,158,131));
montie97 0:9ae27a35a771 329 LCD_DrawLine(Xpos+15,Ypos+7,Xpos+15,Ypos+15,RGB565CONVERT(184,158,131));
montie97 0:9ae27a35a771 330
montie97 0:9ae27a35a771 331 LCD_DrawLine(Xpos+7,Ypos-15,Xpos+15,Ypos-15,RGB565CONVERT(184,158,131));
montie97 0:9ae27a35a771 332 LCD_DrawLine(Xpos+15,Ypos-15,Xpos+15,Ypos-7,RGB565CONVERT(184,158,131));
montie97 0:9ae27a35a771 333 }
montie97 0:9ae27a35a771 334
montie97 0:9ae27a35a771 335 /*******************************************************************************
montie97 0:9ae27a35a771 336 * Function Name : Read_Ads7846
montie97 0:9ae27a35a771 337 * Description : �õ��˲�֮���X Y
montie97 0:9ae27a35a771 338 * Input : None
montie97 0:9ae27a35a771 339 * Output : None
montie97 0:9ae27a35a771 340 * Return : Coordinate�ṹ���ַ
montie97 0:9ae27a35a771 341 * Attention : None
montie97 0:9ae27a35a771 342 *******************************************************************************/
montie97 0:9ae27a35a771 343 Coordinate *Read_Ads7846(void)
montie97 0:9ae27a35a771 344 {
montie97 0:9ae27a35a771 345 static Coordinate screen;
montie97 0:9ae27a35a771 346 int m0,m1,m2,TP_X[1],TP_Y[1],temp[3];
montie97 0:9ae27a35a771 347 uint8_t count=0;
montie97 0:9ae27a35a771 348 int buffer[2][9]={{0},{0}}; /* ����X��Y���ж�β��� */
montie97 0:9ae27a35a771 349 do /* ѭ������9�� */
montie97 0:9ae27a35a771 350 {
montie97 0:9ae27a35a771 351 TP_GetAdXY(TP_X,TP_Y);
montie97 0:9ae27a35a771 352 buffer[0][count]=TP_X[0];
montie97 0:9ae27a35a771 353 buffer[1][count]=TP_Y[0];
montie97 0:9ae27a35a771 354 count++;
montie97 0:9ae27a35a771 355 }
montie97 0:9ae27a35a771 356 while(!TP_INT_IN&& count<9); /* TP_INT_INΪ�������ж�����,���û����������ʱTP_INT_IN�ᱻ�õ� */
montie97 0:9ae27a35a771 357 if(count==9) /* �ɹ�����9��,�����˲� */
montie97 0:9ae27a35a771 358 {
montie97 0:9ae27a35a771 359 /* Ϊ����������,�ֱ��3��ȡƽ��ֵ */
montie97 0:9ae27a35a771 360 temp[0]=(buffer[0][0]+buffer[0][1]+buffer[0][2])/3;
montie97 0:9ae27a35a771 361 temp[1]=(buffer[0][3]+buffer[0][4]+buffer[0][5])/3;
montie97 0:9ae27a35a771 362 temp[2]=(buffer[0][6]+buffer[0][7]+buffer[0][8])/3;
montie97 0:9ae27a35a771 363 /* ����3�����ݵIJ�ֵ */
montie97 0:9ae27a35a771 364 m0=temp[0]-temp[1];
montie97 0:9ae27a35a771 365 m1=temp[1]-temp[2];
montie97 0:9ae27a35a771 366 m2=temp[2]-temp[0];
montie97 0:9ae27a35a771 367 /* ��������ֵȡ����ֵ */
montie97 0:9ae27a35a771 368 m0=m0>0?m0:(-m0);
montie97 0:9ae27a35a771 369 m1=m1>0?m1:(-m1);
montie97 0:9ae27a35a771 370 m2=m2>0?m2:(-m2);
montie97 0:9ae27a35a771 371 /* �жϾ��Բ�ֵ�Ƿ񶼳�����ֵ���ޣ������3�����Բ�ֵ����������ֵ�����ж���β�����ΪҰ��,���������㣬��ֵ����ȡΪ2 */
montie97 0:9ae27a35a771 372 if( m0>THRESHOLD && m1>THRESHOLD && m2>THRESHOLD ) return 0;
montie97 0:9ae27a35a771 373 /* �������ǵ�ƽ��ֵ��ͬʱ��ֵ��screen */
montie97 0:9ae27a35a771 374 if(m0<m1)
montie97 0:9ae27a35a771 375 {
montie97 0:9ae27a35a771 376 if(m2<m0)
montie97 0:9ae27a35a771 377 screen.x=(temp[0]+temp[2])/2;
montie97 0:9ae27a35a771 378 else
montie97 0:9ae27a35a771 379 screen.x=(temp[0]+temp[1])/2;
montie97 0:9ae27a35a771 380 }
montie97 0:9ae27a35a771 381 else if(m2<m1)
montie97 0:9ae27a35a771 382 screen.x=(temp[0]+temp[2])/2;
montie97 0:9ae27a35a771 383 else
montie97 0:9ae27a35a771 384 screen.x=(temp[1]+temp[2])/2;
montie97 0:9ae27a35a771 385
montie97 0:9ae27a35a771 386 /* ͬ�� ����Y��ƽ��ֵ */
montie97 0:9ae27a35a771 387 temp[0]=(buffer[1][0]+buffer[1][1]+buffer[1][2])/3;
montie97 0:9ae27a35a771 388 temp[1]=(buffer[1][3]+buffer[1][4]+buffer[1][5])/3;
montie97 0:9ae27a35a771 389 temp[2]=(buffer[1][6]+buffer[1][7]+buffer[1][8])/3;
montie97 0:9ae27a35a771 390 m0=temp[0]-temp[1];
montie97 0:9ae27a35a771 391 m1=temp[1]-temp[2];
montie97 0:9ae27a35a771 392 m2=temp[2]-temp[0];
montie97 0:9ae27a35a771 393 m0=m0>0?m0:(-m0);
montie97 0:9ae27a35a771 394 m1=m1>0?m1:(-m1);
montie97 0:9ae27a35a771 395 m2=m2>0?m2:(-m2);
montie97 0:9ae27a35a771 396 if(m0>THRESHOLD&&m1>THRESHOLD&&m2>THRESHOLD) return 0;
montie97 0:9ae27a35a771 397
montie97 0:9ae27a35a771 398 if(m0<m1)
montie97 0:9ae27a35a771 399 {
montie97 0:9ae27a35a771 400 if(m2<m0)
montie97 0:9ae27a35a771 401 screen.y=(temp[0]+temp[2])/2;
montie97 0:9ae27a35a771 402 else
montie97 0:9ae27a35a771 403 screen.y=(temp[0]+temp[1])/2;
montie97 0:9ae27a35a771 404 }
montie97 0:9ae27a35a771 405 else if(m2<m1)
montie97 0:9ae27a35a771 406 screen.y=(temp[0]+temp[2])/2;
montie97 0:9ae27a35a771 407 else
montie97 0:9ae27a35a771 408 screen.y=(temp[1]+temp[2])/2;
montie97 0:9ae27a35a771 409
montie97 0:9ae27a35a771 410 return &screen;
montie97 0:9ae27a35a771 411 }
montie97 0:9ae27a35a771 412 return 0;
montie97 0:9ae27a35a771 413 }
montie97 0:9ae27a35a771 414
montie97 0:9ae27a35a771 415 /* �����Ǵ�������Һ��������任��ת������ */
montie97 0:9ae27a35a771 416 /* ֻ����LCD�ʹ�����������Ƕȷdz�Сʱ,�����������湫ʽ */
montie97 0:9ae27a35a771 417
montie97 0:9ae27a35a771 418
montie97 0:9ae27a35a771 419 /*******************************************************************************
montie97 0:9ae27a35a771 420 * Function Name : setCalibrationMatrix
montie97 0:9ae27a35a771 421 * Description : ����� K A B C D E F
montie97 0:9ae27a35a771 422 * Input : None
montie97 0:9ae27a35a771 423 * Output : None
montie97 0:9ae27a35a771 424 * Return : ����1��ʾ�ɹ� 0ʧ��
montie97 0:9ae27a35a771 425 * Attention : None
montie97 0:9ae27a35a771 426 *******************************************************************************/
montie97 0:9ae27a35a771 427 uint8_t setCalibrationMatrix( Coordinate * displayPtr,
montie97 0:9ae27a35a771 428 Coordinate * screenPtr,
montie97 0:9ae27a35a771 429 Matrix * matrixPtr)
montie97 0:9ae27a35a771 430 {
montie97 0:9ae27a35a771 431
montie97 0:9ae27a35a771 432 uint8_t retTHRESHOLD = 0 ;
montie97 0:9ae27a35a771 433 /* K��(X0��X2) (Y1��Y2)��(X1��X2) (Y0��Y2) */
montie97 0:9ae27a35a771 434 matrixPtr->Divider = ((screenPtr[0].x - screenPtr[2].x) * (screenPtr[1].y - screenPtr[2].y)) -
montie97 0:9ae27a35a771 435 ((screenPtr[1].x - screenPtr[2].x) * (screenPtr[0].y - screenPtr[2].y)) ;
montie97 0:9ae27a35a771 436 if( matrixPtr->Divider == 0 )
montie97 0:9ae27a35a771 437 {
montie97 0:9ae27a35a771 438 retTHRESHOLD = 1;
montie97 0:9ae27a35a771 439 }
montie97 0:9ae27a35a771 440 else
montie97 0:9ae27a35a771 441 {
montie97 0:9ae27a35a771 442 /* A��((XD0��XD2) (Y1��Y2)��(XD1��XD2) (Y0��Y2))��K */
montie97 0:9ae27a35a771 443 matrixPtr->An = ((displayPtr[0].x - displayPtr[2].x) * (screenPtr[1].y - screenPtr[2].y)) -
montie97 0:9ae27a35a771 444 ((displayPtr[1].x - displayPtr[2].x) * (screenPtr[0].y - screenPtr[2].y)) ;
montie97 0:9ae27a35a771 445 /* B��((X0��X2) (XD1��XD2)��(XD0��XD2) (X1��X2))��K */
montie97 0:9ae27a35a771 446 matrixPtr->Bn = ((screenPtr[0].x - screenPtr[2].x) * (displayPtr[1].x - displayPtr[2].x)) -
montie97 0:9ae27a35a771 447 ((displayPtr[0].x - displayPtr[2].x) * (screenPtr[1].x - screenPtr[2].x)) ;
montie97 0:9ae27a35a771 448 /* C��(Y0(X2XD1��X1XD2)+Y1(X0XD2��X2XD0)+Y2(X1XD0��X0XD1))��K */
montie97 0:9ae27a35a771 449 matrixPtr->Cn = (screenPtr[2].x * displayPtr[1].x - screenPtr[1].x * displayPtr[2].x) * screenPtr[0].y +
montie97 0:9ae27a35a771 450 (screenPtr[0].x * displayPtr[2].x - screenPtr[2].x * displayPtr[0].x) * screenPtr[1].y +
montie97 0:9ae27a35a771 451 (screenPtr[1].x * displayPtr[0].x - screenPtr[0].x * displayPtr[1].x) * screenPtr[2].y ;
montie97 0:9ae27a35a771 452 /* D��((YD0��YD2) (Y1��Y2)��(YD1��YD2) (Y0��Y2))��K */
montie97 0:9ae27a35a771 453 matrixPtr->Dn = ((displayPtr[0].y - displayPtr[2].y) * (screenPtr[1].y - screenPtr[2].y)) -
montie97 0:9ae27a35a771 454 ((displayPtr[1].y - displayPtr[2].y) * (screenPtr[0].y - screenPtr[2].y)) ;
montie97 0:9ae27a35a771 455 /* E��((X0��X2) (YD1��YD2)��(YD0��YD2) (X1��X2))��K */
montie97 0:9ae27a35a771 456 matrixPtr->En = ((screenPtr[0].x - screenPtr[2].x) * (displayPtr[1].y - displayPtr[2].y)) -
montie97 0:9ae27a35a771 457 ((displayPtr[0].y - displayPtr[2].y) * (screenPtr[1].x - screenPtr[2].x)) ;
montie97 0:9ae27a35a771 458 /* F��(Y0(X2YD1��X1YD2)+Y1(X0YD2��X2YD0)+Y2(X1YD0��X0YD1))��K */
montie97 0:9ae27a35a771 459 matrixPtr->Fn = (screenPtr[2].x * displayPtr[1].y - screenPtr[1].x * displayPtr[2].y) * screenPtr[0].y +
montie97 0:9ae27a35a771 460 (screenPtr[0].x * displayPtr[2].y - screenPtr[2].x * displayPtr[0].y) * screenPtr[1].y +
montie97 0:9ae27a35a771 461 (screenPtr[1].x * displayPtr[0].y - screenPtr[0].x * displayPtr[1].y) * screenPtr[2].y ;
montie97 0:9ae27a35a771 462 }
montie97 0:9ae27a35a771 463 return( retTHRESHOLD ) ;
montie97 0:9ae27a35a771 464 }
montie97 0:9ae27a35a771 465
montie97 0:9ae27a35a771 466 /*******************************************************************************
montie97 0:9ae27a35a771 467 * Function Name : getDisplayPoint
montie97 0:9ae27a35a771 468 * Description : ͨ�� K A B C D E F ��ͨ��X Y��ֵת��ΪҺ��������
montie97 0:9ae27a35a771 469 * Input : None
montie97 0:9ae27a35a771 470 * Output : None
montie97 0:9ae27a35a771 471 * Return : ����1��ʾ�ɹ� 0ʧ��
montie97 0:9ae27a35a771 472 * Attention : None
montie97 0:9ae27a35a771 473 *******************************************************************************/
montie97 0:9ae27a35a771 474 uint8_t getDisplayPoint(Coordinate * displayPtr,
montie97 0:9ae27a35a771 475 Coordinate * screenPtr,
montie97 0:9ae27a35a771 476 Matrix * matrixPtr )
montie97 0:9ae27a35a771 477 {
montie97 0:9ae27a35a771 478 uint8_t retTHRESHOLD = 0 ;
montie97 0:9ae27a35a771 479
montie97 0:9ae27a35a771 480 if( matrixPtr->Divider != 0 )
montie97 0:9ae27a35a771 481 {
montie97 0:9ae27a35a771 482 /* XD = AX+BY+C */
montie97 0:9ae27a35a771 483 displayPtr->x = ( (matrixPtr->An * screenPtr->x) +
montie97 0:9ae27a35a771 484 (matrixPtr->Bn * screenPtr->y) +
montie97 0:9ae27a35a771 485 matrixPtr->Cn
montie97 0:9ae27a35a771 486 ) / matrixPtr->Divider ;
montie97 0:9ae27a35a771 487 /* YD = DX+EY+F */
montie97 0:9ae27a35a771 488 displayPtr->y = ( (matrixPtr->Dn * screenPtr->x) +
montie97 0:9ae27a35a771 489 (matrixPtr->En * screenPtr->y) +
montie97 0:9ae27a35a771 490 matrixPtr->Fn
montie97 0:9ae27a35a771 491 ) / matrixPtr->Divider ;
montie97 0:9ae27a35a771 492 }
montie97 0:9ae27a35a771 493 else
montie97 0:9ae27a35a771 494 {
montie97 0:9ae27a35a771 495 retTHRESHOLD = 1;
montie97 0:9ae27a35a771 496 }
montie97 0:9ae27a35a771 497 return(retTHRESHOLD);
montie97 0:9ae27a35a771 498 }
montie97 0:9ae27a35a771 499
montie97 0:9ae27a35a771 500 /*******************************************************************************
montie97 0:9ae27a35a771 501 * Function Name : TouchPanel_Calibrate
montie97 0:9ae27a35a771 502 * Description : У׼������
montie97 0:9ae27a35a771 503 * Input : None
montie97 0:9ae27a35a771 504 * Output : None
montie97 0:9ae27a35a771 505 * Return : None
montie97 0:9ae27a35a771 506 * Attention : None
montie97 0:9ae27a35a771 507 *******************************************************************************/
montie97 0:9ae27a35a771 508 void TouchPanel_Calibrate(void)
montie97 0:9ae27a35a771 509 {
montie97 0:9ae27a35a771 510 uint8_t i;
montie97 0:9ae27a35a771 511 Coordinate * Ptr;
montie97 0:9ae27a35a771 512
montie97 0:9ae27a35a771 513 for(i=0;i<3;i++)
montie97 0:9ae27a35a771 514 {
montie97 0:9ae27a35a771 515 LCD_Clear(Black);
montie97 0:9ae27a35a771 516 //GUI_Text(10,10,"Touch crosshair to calibrate",0xffff,Black);
montie97 0:9ae27a35a771 517 DelayUS(1000 * 500);
montie97 0:9ae27a35a771 518 DrawCross(DisplaySample[i].x,DisplaySample[i].y);
montie97 0:9ae27a35a771 519 do
montie97 0:9ae27a35a771 520 {
montie97 0:9ae27a35a771 521 Ptr = Read_Ads7846();
montie97 0:9ae27a35a771 522 }
montie97 0:9ae27a35a771 523 while( Ptr == (void*)0 );
montie97 0:9ae27a35a771 524 ScreenSample[i].x = Ptr->x; ScreenSample[i].y = Ptr->y;
montie97 0:9ae27a35a771 525 }
montie97 0:9ae27a35a771 526 setCalibrationMatrix( &DisplaySample[0],&ScreenSample[0],&matrix ) ; /* ����ֵ�õ����� */
montie97 0:9ae27a35a771 527 LCD_Clear(Black);
montie97 0:9ae27a35a771 528 }
montie97 0:9ae27a35a771 529
montie97 0:9ae27a35a771 530 /*********************************************************************************************************
montie97 0:9ae27a35a771 531 END FILE
montie97 0:9ae27a35a771 532 *********************************************************************************************************/
montie97 0:9ae27a35a771 533