| Calibrate touch screen BL-TFT240320PLUS with arduino How to calibrate touch screen BL-TFT240320PLUS with arduino.
BL-TFT240320PLUS contain touch screen and ADS7843 touch screen driver. ADS7843 provide 12 bit adc for touch screen.

Touch screen and display may be not the same location. Calibrate will convert screen point to display point.

Using 3 point calibrate method. Point cover +-10% of display area (24,32), (215,160) and (120,287).
.jpg)
Arduino library
Arduino class is easy to use. Create class and go. It read metrix form eeprom. So first time calibrate is need.
Calibrate touch screen. only for calibrate.
touchscreen TS; // create class and read metrix form eeprom
void Reset(void); // init touch screen chip
int setCalibrationMatrix( POINT * displayPtr, POINT * screenPtr, MATRIX * matrixPtr) // use for calculate metrix
void touch_calibrate(void) // use for 3 point calibrate interface
Using touch screen
touchscreen TS; // create class and read metrix form eeprom
void Reset(void); // init touch screen chip
void readPoint(void); // read display point
Download
Library and example source code www.circuitidea.com/dev-board/BL-TFT240320PLUS-V2.html
Arduino code
#include <f20x19.h>
#include <lcdFont.h>
#include <touch.h>
/*
Calibrate call touch_calibrate(). Routine set calibrate matrix and save to eeprom.
Calibrate using 3 point calibrate.
First point is 24,32.
Second point is 215,160.
Third point is 120,287.
Touch screen api will read matrix at init class. No need to call calibrate again.
*/
lcdFont LCD;
touchscreen TS;
void showPoint(void)
{
char sTemp[8];
TS.readPoint();
if (TS.screenPoint.x > 0)
{
LCD.Color = WHITE;
LCD.FgColor = GREEN; // text foreground color
LCD.BkColor = RED; // text background color
ltoa(TS.screenPoint.x, sTemp, 10);
LCD.TextBox( 0, 280, 59, 310, sTemp, ALINE_CENTER | BORDER_RECT | BORDER_FILL | BEVEL(8));
ltoa(TS.screenPoint.y, sTemp, 10);
LCD.TextBox( 60, 280, 119, 310, sTemp, ALINE_CENTER | BORDER_RECT | BORDER_FILL | BEVEL(8));
ltoa(TS.displayPoint.x, sTemp, 10);
LCD.TextBox(120, 280, 179, 310, sTemp, ALINE_CENTER | BORDER_RECT | BORDER_FILL | BEVEL(8));
ltoa(TS.displayPoint.y, sTemp, 10);
LCD.TextBox(180, 280, 239, 310, sTemp, ALINE_CENTER | BORDER_RECT | BORDER_FILL | BEVEL(8));
}
}
int setCalibrationMatrix( POINT * displayPtr,
POINT * screenPtr,
MATRIX * matrixPtr)
{
int retValue = 1 ;
matrixPtr->Divider = ((screenPtr[0].x - screenPtr[2].x) * (screenPtr[1].y - screenPtr[2].y)) -
((screenPtr[1].x - screenPtr[2].x) * (screenPtr[0].y - screenPtr[2].y)) ;
if( matrixPtr->Divider == 0 )
{
retValue = 0 ;
}
else
{
matrixPtr->An = ((displayPtr[0].x - displayPtr[2].x) * (screenPtr[1].y - screenPtr[2].y)) -
((displayPtr[1].x - displayPtr[2].x) * (screenPtr[0].y - screenPtr[2].y)) ;
matrixPtr->Bn = ((screenPtr[0].x - screenPtr[2].x) * (displayPtr[1].x - displayPtr[2].x)) -
((displayPtr[0].x - displayPtr[2].x) * (screenPtr[1].x - screenPtr[2].x)) ;
matrixPtr->Cn = (screenPtr[2].x * displayPtr[1].x - screenPtr[1].x * displayPtr[2].x) * screenPtr[0].y +
(screenPtr[0].x * displayPtr[2].x - screenPtr[2].x * displayPtr[0].x) * screenPtr[1].y +
(screenPtr[1].x * displayPtr[0].x - screenPtr[0].x * displayPtr[1].x) * screenPtr[2].y ;
matrixPtr->Dn = ((displayPtr[0].y - displayPtr[2].y) * (screenPtr[1].y - screenPtr[2].y)) -
((displayPtr[1].y - displayPtr[2].y) * (screenPtr[0].y - screenPtr[2].y)) ;
matrixPtr->En = ((screenPtr[0].x - screenPtr[2].x) * (displayPtr[1].y - displayPtr[2].y)) -
((displayPtr[0].y - displayPtr[2].y) * (screenPtr[1].x - screenPtr[2].x)) ;
matrixPtr->Fn = (screenPtr[2].x * displayPtr[1].y - screenPtr[1].x * displayPtr[2].y) * screenPtr[0].y +
(screenPtr[0].x * displayPtr[2].y - screenPtr[2].x * displayPtr[0].y) * screenPtr[1].y +
(screenPtr[1].x * displayPtr[0].y - screenPtr[0].x * displayPtr[1].y) * screenPtr[2].y ;
}
return( retValue ) ;
}
void touch_calibrate(void)
{
unsigned char press;
POINT screenSample[3]; //array of input points
POINT displaySample[3] = {{24,32},{215,160},{120,287}}; //array of expected correct answers TS.getMatrix();
POINT capturePoint; //array of valid input points
LCD.Color = WHITE; // graphic color
LCD.FgColor = WHITE; // text foreground color
LCD.BkColor = BLACK; // text background color
LCD.TextBox(0, 0, LCD.getMaxX(), 30, "Calibrate", ALINE_CENTER);
for (uint8_t i=0; i<3; i++)
{
// draw touch point
LCD.Color = RED;
LCD.Circle(displaySample[i].x, displaySample[i].y , 4);
press = 0;
TS.screenPoint.x = 0;
TS.screenPoint.y = 0;
while(!press)
{
capturePoint.x = TS.screenPoint.x;
capturePoint.y = TS.screenPoint.y;
TS.sampling(&TS.screenPoint, 50);
if (TS.screenPoint.x > 0)
{
// Is stable point?
if ((capturePoint.x == TS.screenPoint.x) && (capturePoint.y == TS.screenPoint.y))
press = 1;
}
}
// got a point
screenSample[i].x = TS.screenPoint.x;
screenSample[i].y = TS.screenPoint.y;
// mark complete point
LCD.Circle(displaySample[i].x, displaySample[i].y,3);
delay(20);
LCD.Circle(displaySample[i].x, displaySample[i].y,2);
delay(20);
LCD.Circle(displaySample[i].x, displaySample[i].y,1);
delay(60);
LCD.Color = WHITE;
LCD.Circle(displaySample[i].x, displaySample[i].y,4);
delay(20);
LCD.Circle(displaySample[i].x, displaySample[i].y,3);
delay(20);
LCD.Circle(displaySample[i].x, displaySample[i].y,2);
delay(20);
LCD.Circle(displaySample[i].x, displaySample[i].y,1);
delay(250);
}
LCD.Clear(BLACK);
LCD.TextBox(0, 0, LCD.getMaxX(), 30, "Calibrating", ALINE_CENTER);
setCalibrationMatrix(&displaySample[0], &screenSample[0], &TS.matrix); // calibration
TS.setMatrix(); // save matrix to eeprom
LCD.Clear(BLACK);
LCD.TextBox(0, 0, LCD.getMaxX(), 30, "Calibrated", ALINE_CENTER);
delay(1000);
}
void setup()
{
LCD.Reset();
LCD.Clear(BLACK);
LCD.Font(f20x19);
TS.Reset();
touch_calibrate(); // use for calibrate only
LCD.Color = WHITE; // graphic color
LCD.FgColor = GREEN; // text foreground color
LCD.BkColor = RED; // text background color
LCD.TextBox( 0, 250, 59, 279, "scrX", ALINE_CENTER | BORDER_RECT | BORDER_FILL | BEVEL(8));
LCD.TextBox( 60, 250, 119, 279, "scrY", ALINE_CENTER | BORDER_RECT | BORDER_FILL | BEVEL(8));
LCD.TextBox(120, 250, 179, 279, "lcdX", ALINE_CENTER | BORDER_RECT | BORDER_FILL | BEVEL(8));
LCD.TextBox(180, 250, 239, 279, "lcdY", ALINE_CENTER | BORDER_RECT | BORDER_FILL | BEVEL(8));
}
void loop()
{
showPoint();
}
Reference
How To Calibrate Touch Screens. www.embedded.com/story/OEG20020529S0046 |