Application and calibration of touch screen of s3c2440

Publisher:anluranLatest update time:2016-05-30 Source: eefocusKeywords:s3c2440 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
Touch screen is the most popular human-computer interaction interface today. It is widely used in consumer electronic products such as mobile phones. At present, this technology has a trend of developing towards PCs. Based on different principles, touch screens can be divided into resistive, capacitive, surface acoustic wave, etc. Resistive is a widely used touch screen. Its principle is to obtain the coordinates of the touch point by measuring the horizontal and vertical resistance values.
 
S3C2440 integrates a 4-wire resistive touch screen interface. The detection of the touch point coordinates is achieved through A/D conversion. S3C2440 has a total of 4 touch screen interface modes, among which the automatic (continuous) XY coordinate conversion mode and the waiting interrupt mode are more commonly used. The waiting interrupt mode generates an interrupt when the stylus falls. In this mode, the value of the A/D touch screen control register ADCTSC should be 0xD3. After the system responds to the interrupt, the measurement mode of the XY coordinates must be no operation mode, that is, the lower two bits of the register ADCTSC must be cleared. The automatic (continuous) XY coordinate conversion mode is that the system converts the X-axis coordinate and Y-axis coordinate of the touch point in sequence, where the X-axis coordinate value is written into the lower 10 bits of the ADCDAT0 register, and the Y-axis coordinate is written into the lower 10 bits of the ADCDAT1 register. In this mode, the system will also generate an interrupt signal. In general, to realize the touch screen function, it is first set to the waiting interrupt mode, and after the interrupt is generated, it is set to the automatic (continuous) XY coordinate conversion mode to read the coordinate values ​​of the touch points in sequence. In the process of realizing the touch screen function, in addition to the several registers introduced above, the following registers will also be used. The 8th bit of the ADCTSC register can realize whether the stylus pen falls or lifts the interrupt. If you have written a Windows-based application, you will be familiar with this. It is like a single-click operation of the mouse. A single click operation includes two actions: pressing and releasing. These two actions can complete different commands. The 3rd bit of the ADCTSC register can select the enable of the pull-up resistor. In the waiting interrupt mode, the pull-up resistor must be valid, and after the interrupt is triggered, the pull-up resistor must be invalid. The second bit of register ADCTSC is used to select the automatic (continuous) XY coordinate conversion mode. The lower two bits of the stylus lift/drop interrupt status register ADCUPDN can determine the state of the stylus causing the interrupt. The A/D delay register ADCDLY can set the delay length from the start of the interrupt to the actual start of the A/D conversion, and its clock source frequency is 3.68MHz.
 
       Before starting to implement the touch screen function, there is another problem that needs to be solved, that is, the calibration of the touch screen. The touch screen and LCD are two different physical devices. For an LCD with a resolution of 320×240, its width is 320 pixels and its height is 240 pixels. The data processed by the touch screen is the physical coordinates of the point, which is collected by the touch screen controller. In order to achieve a one-to-one correspondence between the physical coordinates on the touch screen and the pixel coordinates on the LCD, a certain conversion, that is, calibration, is required between the two. Moreover, the parameters of the resistive touch screen will change due to its own reasons, so it needs to be calibrated frequently. The more common calibration method is the three-point calibration method. Its principle is:
 
       let the coordinates of each point PD on the LCD be [XD, YD], and the coordinates of each point PT on the touch screen be [XT, YT]. To realize the conversion of the coordinates on the touch screen to the coordinates on the LCD, the following formula is required for conversion:
XD=A×XT+B×YT+C
YD=D×XT+E×YT+F
Because there are a total of six parameters (A, B, C, D, E, F), only three sampling points are needed to obtain these six parameters. Once these six parameters are determined, as long as the coordinate point PT on any touch screen is given and substituted into this formula, the coordinate PD of the corresponding pixel on the LCD can be obtained. The specific solution process will not be explained in detail, only the final result will be given. It is known that the three sampling points on the LCD are: PD0, PD1, PD2, and the three points on the touch screen corresponding to them are: PT0, PT1, PT2. The final result of the six parameters A, B, C, D, E, and F is a fraction, and they all have a common denominator, which is:
              K = (XT0-XT2) × (YT1-YT2) - (XT1-XT2) × (YT0-YT2)
Then the six parameters are:
              A = [(XD0-XD2) × (YT1-YT2) - (XD1-XD2) × (YT0-YT2)] / K
              B = [(XT0-XT2) × (XD1-XD2) - (XD0-XD2) × (XT1-XT2)] / K
              C = [YT0 × (XT2 × XD1-XT1 × XD2) + YT1 × (XT0 × XD2-XT2 × XD0) + YT2 × (XT1 × XD0-XT0 × XD1)] / K
              D=[(YD0-YD2)×(YT1-YT2)-(YD1-YD2)×(YT0-YT2)] / K
              E=[(XT0-XT2)×(YD1-YD2)-(YD0-YD2)×(XT1-XT2)] / K
              F=[YT0×(XT2×YD1-XT1×YD2)+YT1×(XT0×YD2-XT2×YD0)+YT2×(XT1×YD0-XT0×YD1)] / K The
 
       following program is a simple example of realizing the touch screen function - drawing a red square with a side length of 10 pixels with the touch point as the center. The coordinates of the touch point are obtained by the following method: when the stylus falls, an interrupt is entered, and then the coordinates of the touch point are read, and the interrupt is not exited until the stylus is lifted. Since the touch screen needs to be calibrated, it needs to be calibrated before use. But it is not necessary to calibrate every time you use it. As long as the coordinates do not drift, there is no need to calibrate again. So after a calibration, just save those parameters and use the parameters saved last time when needed next time. Here, we use EEPROM to save these parameters, that is, A, B, C, D, E, F, K are saved in the continuous 4-byte space of the memory with 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x80 as the first address. In addition, the memory address 0x1F saves an identification information. When it is 0x6A, it means that these parameters have been calculated and saved. Just read the parameters from the above memory address. When it is other values, calibration is required. During calibration, three sampling points are needed. Here we select (32,24), (160,216), and (288,120) on the LCD as the three sampling points. We draw a cross on these three sampling points (as shown in the figure below). You only need to click these three points in sequence to complete the touch screen calibration.

 
…… ……
volatile U32 LCD_BUFFER[LCD_HEIGHT][LCD_WIDTH];
unsigned char iic_buffer[8];
unsigned char devAddr=0xa0;
int A,B,C,D,E,F,K;
 
volatile int xdata, ydata;
 
int flagIIC; //IIC flag
int flagTS; //Touch screen flag
…… ……
 
//Touch screen interrupt
void __irq ADCTs(void)
{
       rADCTSC = (1<<3)|(1<<2); //Pull-up resistor is invalid, automatic continuous XY coordinate conversion mode is turned on
       rADCDLY = 40000; //Delay
      
       rADCCON|=0x1; //Start A/D conversion
 
       while(rADCCON & 0x1)
; //Check whether A/D conversion has started
       while(!(rADCCON & 0x8000))
; //Wait for the end of A/D conversion
             
       while(!(rSRCPND & ((U32)0x1<<31)))     
; //Judge the suspension bit of A/D interrupt
 
       xdata=(rADCDAT0&0x3ff); //Read X-axis coordinate
ydata=(rADCDAT1&0x3ff); //Read Y-axis coordinate
     
       flagTS = 1; //Set flag
     
      rSUBSRCPND|=0x1<<9;
       rSRCPND = 0x1<<31;
       rINTPND = 0x1<<31;         
       rINTSUBMSK=~(0x1<<9);
       rINTMSK=~(0x1<<31); //Clear A/D interrupt, enable A/D interrupt mask
                      
       rADCTSC =0xd3; //Set the wait interrupt mode again, this time to determine whether the stylus is lifted
       rADCTSC=rADCTSC|(1<<8); //Set the stylus lift interrupt
 
       while(1) //Wait for the stylus to lift
       {
              if(rSUBSRCPND & (0x1<<9)) //Check A/D touch screen interrupt suspension
              {
                     break; //If the stylus is lifted, jump out of the loop
              }
       }    
      
       rADCDLY=50000;
       rSUBSRCPND|=0x1<<9;
       rINTSUBMSK=~(0x1<<9);
       rSRCPND = 0x1<<31;
       rINTPND = 0x1<<31; //Clear A/D interrupt again, enable A/D interrupt mask
       rADCTSC =0xd3; //Set the wait interrupt mode to prepare for the next touch pen drop
}
 
//Draw the "cross" shapevoid
drawCross(U32 x,U32 y,U32 color)
{
       int i;
       for(i=x-10;i               PutPixel(i,y, color);
       for(i=y-10;i               PutPixel(x,i, color);
}
 
//Touch screen calibrationvoid
TSCal(void)
{
       int i=0;
       int xt[3],yt[3];
       Brush_Background(0,0,LCD_WIDTH,LCD_HEIGHT,0xFFFFFF);
       drawCross(32,24,0xFF0000);
       Draw_ASCII(36,28,0xFF0000,one);
       drawCross(160,216,0xFF0000);
       Draw_ASCII(164,220,0xFF0000,two);
       drawCross(288,120,0xFF0000);
       Draw_ASCII(292,124,0xFF0000,three);
 
       //Read the coordinate values ​​of three sampling points in sequence
       for(i=0;i<3;i++)
       {    
              while(flagTS==0)
                     delay(500);
              xt[i]=xdata;
              yt[i]=ydata;
              flagTS=0;             
       }
 
//Calculate parameters
       K=(xt[0]-xt[2])*(yt[1]-yt[2])-(xt[1]-xt[2])*(yt[0]-yt[2]);
       A=(32-288)*(yt[1]-yt[2])-(160-288)*(yt[0]-yt[2]);
       B=(xt[0]-xt[2] )*(160-288)-(32-288)*(xt[1]-xt[2]);
       C=yt[0]*(xt[2]*160-xt[1]*288)+yt [1]*(xt[0]*288-xt[2]*32)+yt[2]*(xt[1]*32-xt[0]*160);
       D=(24-120)*(yt[1]-yt[2])-(216-120)*(yt[0]-yt[2]);
       E=(xt[0]-xt[2] )*(216-120)-(24-120)*(xt[1]-xt[2]);
       F=yt[0]*(xt[2]*216-xt[1]*120)+yt [1]*(xt[0]*120-xt[2]*24)+yt[2]*(xt[1]*24-xt[0]*216);
}
 
//Convert a 32-bit integer Convert to 4 8-bit bytes and write to EEPROM
void wrTStoIIC(int coef,unsigned char address)
{
       iic_buffer[0]=(unsigned char)((coef&0xFF000000)>>24);
       iic_buffer[1]=(unsigned char)((coef&0x00FF0000)>>16);
     iic_buffer[2]=(unsigned char)((coef&0x0000FF00)>>8);
     iic_buffer[3 ]=(unsigned char)(coef&0x000000FF);
     wr24c02a(address,iic_buffer,4);
}
 
//Read 4 8-bit bytes from the EEPROM and combine them into a 32-bit integer.
int rdTStoIIC(unsigned char address)
{
       int temp;
       rd24c02a(address,iic_buffer,4);
       temp=(iic_buffer[0]<<24)|(iic_buffer[1]<<16)|(iic_buffer[2]<<8 )|(iic_buffer[3]);
       return temp;
}
 
void Main(void)
{
      
LCD_Init();   
rLCDCON1|=1;
   
rADCDLY=50000; //Set delay
       rADCCON=(1<<14)+(9<< 6); //Set A/D pre-scaling
 
       rADCTSC=0xd3; //Set the touch screen to wait for interrupt mode.
 
       pISR_ADC = (U32)ADCTs;
      
... ...
 
flagTS = 0;
       flagIIC = 1;
 
       //Read the flag address content in EEPROM to determine whether the touch screen calibration parameters have been calculated and saved
       rd24c02a(0x1F,iic_buffer,1 );
 
       if(iic_buffer[0]!=0x6A) //If the calibration parameters of the touch screen are not calculated and saved, recalibrate
{
       TSCal();
              rINTMSK=~((0x1<<31)|(0x1<<27)); //Open IIC interrupt shield
              iic_buffer[0]=0x6A;
              wr24c02a(0x1F,iic_buffer,1); //Set the "touch screen calibration parameters calculated and maintained" flag information
              delay(3000); //Wait for a while, be sure to Yes, otherwise the EEPROM cannot read and write correctly
              wrTStoIIC(A,0x20);
              delay(3000);
            wrTStoIIC(B,0x30);
            delay(3000);
            wrTStoIIC(C,0x40);
            delay(3000);
              wrTStoIIC(D,0x50);
              delay(3000);
              wrTStoIIC(E,0x60);
              delay(3000);
              wrTStoIIC( F,0x70);
              delay(3000);
              wrTStoIIC(K,0x80);
             
       }
       else //If the touch screen calibration parameters are ready, read them directly
       {
              A=rdTStoIIC(0x20);
              delay(3000);
              B=rdTStoIIC(0x30);
              delay(3000);
              C=rdTStoIIC(0x40);
              delay(3000);
              D=rdTStoIIC(0x50);
              delay(3000);
              E =rdTStoIIC(0x60);
              delay(3000);
              F=rdTStoIIC(0x70);
              delay(3000);
              K=rdTStoIIC(0x80);
       }
     
       Brush_Background(0,0,LCD_WIDTH,LCD_HEIGHT,0xFFFFFF);
  
while(1)
{
              if (flagTS)
       {
                     flagTS=0;
              xLcd = (A*xdata+B*ydata+C)/K; //Calculate the X-axis coordinate
                     yLcd = (D*xdata+E*ydata+F)/K; //Calculate the Y-axis coordinate
              Brush_Background(xLcd-5,yLcd-5,xLcd+5,yLcd+5,0xFF0000); //Draw a square
       }
       delay(1000000);
}
}
 
 
       The program introduced in this article is slightly more complicated. Let's summarize the touch screen calibration and interruption:
1. Generally, the touch screen needs to be calibrated once when it is used for the first time, and no calibration is required afterwards unless obvious drift occurs. We store the calibration parameters in EEPROM. The next time you use it, you only need to read this set of data. The 0x1F address in the EEPROM is used to store the calibration parameter identification information. If it has been calibrated, the content of this bit is 0x6A. In this way, the next time you turn on the touch screen, By reading the content of this bit, you can know whether the touch screen has been calibrated. If it has not been calibrated, it needs to be calibrated. If it has been calibrated, there is no need to repeat the calibration.
2. First set the waiting interrupt mode to wait for the touch screen interrupt to occur. When the interrupt occurs and enters the interrupt mode, it will be converted to the automatic continuous XY coordinate conversion mode to correctly read the coordinate value of the touch point. When the pen falls, the interrupt is entered, and when the pen is lifted, the interrupt is exited. The program only records the coordinate value of the touch point when the pen falls.
 
The demonstration diagram of this program is as follows:
 
Keywords:s3c2440 Reference address:Application and calibration of touch screen of s3c2440

Previous article:Application of SD/MMC of s3c2440
Next article:A/D conversion application of s3c2440

Recommended ReadingLatest update time:2024-11-23 10:59

S3C2440 memory initialization
It is divided into three parts:  ①2440 address space  ②Memory and chip hardware connection  ③Storage controller register S3C2440 Address Space S3C2440 provides 27 address lines to the outside. With only 27 pins on the chip, it can only access 128MB of peripheral space.  In order to expand the access range of peripher
[Microcontroller]
Design and implementation of firewall supporting IPv6 protocol based on S3C2440 processor
1 Introduction Among many network security facilities, firewall is an important and effective network security device. It filters and shields network communications to prevent unauthorized access into and out of the computer network. A firewall is a security barrier between a trusted network and an untrusted network.
[Microcontroller]
Design and implementation of firewall supporting IPv6 protocol based on S3C2440 processor
Design of logistics distribution system based on S3C2440 processor Linux platform
  introduction   Modern logistics systems have entered the stage of informatization. The informatization distribution system has an important impact on informatization logistics. The informatization of logistics distribution is to use modern information systems and electronic means to strengthen the management of th
[Microcontroller]
Design of logistics distribution system based on S3C2440 processor Linux platform
s3c2440 header file Def.h
#ifndef __DEF_H__ #define __DEF_H__ #define U32 unsigned int #define U16 unsigned short #define S32 int #define S16 short int #define U8  unsigned char #define S8  char #define TRUE  1    #define FALSE  0 #define OK  1 #define FAIL 0 #define ESC_KEY ('q') // 0x1b #endif /*__DEF_H__*/
[Microcontroller]
ARM-Linux s3c2440 UART Analysis (Part 3)
Looking back at the above, the underlying driver of the s3c2440 serial port revolves around three data structures: UART specific driver structure definition: struct uart_driver s3c24xx_uart_drv; UART port structure definition: struct uart_port s3c24xx_serial_ops; UART related operation function structure definitio
[Microcontroller]
S3C2440 Input Subsystem Study Notes Section 1
  I have been exposed to S3C2440 for a while, but I have never persisted in studying. As a recent graduate, I was deeply affected by my lack of skills and the pressure at work, so I decided to change my past and insist on studying every day after work. Here I don’t I dare to say that I can finish learning 2440, becaus
[Microcontroller]
S3C2440 bare metal experiment (7) ---- LCD driver
LCD is actually quite simple as long as you understand the timing. Now the LCD driver under LINUX has good support for processors like Samsung. You only need to modify some parameters. If it is a chip-level driver, it is more complicated, mainly involving Framebuffer, which will make people crazy. After completing the
[Microcontroller]
Use of S3C2440 timer
#include "mytimer.h" #include "lhg_def.h" #include "uart.h" #include "lhg_def.h" #include "2440addr.h" //Timer input clock Frequency = PCLK / {prescaler value+1} / {divider value} //PCLK=50Mhz //prescaler : 0~255 #define prescaler234 99 //divider : 1/2,1/4,1/8,1/16 select (0,1,2,3) #define divider4 2 //Timer is set t
[Microcontroller]
Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号