Forum: Mikrocontroller und Digitale Elektronik Arduino mit Touchscreen verliert Kalibrierung nach Neustart


von Nick N. (nick_hb)


Lesenswert?

Moin zusammen,

da ich hier bereits einige Male wirklich gute Hinweise sammeln konnte 
und nun bei meinem neuen Projekt nicht wirklich weiter komme, wollte ich 
es einmal mit einem eigenen Thread versuchen.

Ich habe mir ein TouchScreen Panel mit passendem Shield für einen 
Arduino Mega 2560 geholt, um allg. Erfahrungen mit Displays zu sammeln.

http://www.sainsmart.com/arduino/arduino-shields/lcd-shields/sainsmart-3-2-tft-lcd-display-touch-panel-pcb-adapter-sd-slot-for-arduino-2560.html

http://www.sainsmart.com/arduino/arduino-shields/lcd-shields/sainsmart-tft-lcd-adjustable-shield-for-arduino-mega-2560-r3-1280-a082-plug.html

Bis Dato habe ich mich an die Bibliotheken

UTFT
http://www.rinkydinkelectronics.com/library.php?id=51

und UTouch
http://www.rinkydinkelectronics.com/library.php?id=55

gehalten.

UTouch enthält einige Beispiele für das Touch Display, welche an sich 
auch alle funktionieren.
Zunächst habe ich das UTouch_Calibration.ino example implementiert, um 
die entsprechenden Kalibrierdaten zu erhalten und in eine Header Datei 
"UTouchCD.h" einzubinden.
Danach funktionierten auch soweit alle anderen Beispiele und auch die 
Touchfunktion.



Nun zu meinem eigentlichen Problem
Wenn ich die Hardware (Arduino) neustarte (einmal von der 
Spannungsversorgung - 12V oder USB - trenne), verliert er die 
Kalibrierung für die Touchfunktion. Das äußert sich so, dass ein Druck 
auf Pos. X an Pos. Y erkannt wird.

Wenn ich das Programm anschließend mit der Arduino Software wieder neu 
implementiere, funktioniert wieder alles wunderbar (Druck auf Pos. X 
wird an Pos. X erkannt) ohne, dass etwas verändert wurde.


Was ich bereits Probiert habe
Es war nicht besonders umfangreich, aber ich habe versucht die 
Kalibrierdaten /CAL_X, CAL_Y, CAL_S/ außerhalb der Header Datei direkt 
in die /void loop()/, /void setup()/,... zu schreiben. Leider ohne 
erfolg.



Hoffe ich habe das Problem und die verwendete Hard-, Software 
ausreichend beschrieben und ihr könnt mir weiterhelfen.

Viele Grüße


UTouch_ButtonTest.ino
1
// UTouch_ButtonTest 
2
// Copyright (C)2015 Rinky-Dink Electronics, Henning Karlsen. All right reserved
3
// web: http://www.RinkyDinkElectronics.com/
4
//
5
// This program is a quick demo of how create and use buttons.
6
//
7
// This program requires the UTFT library.
8
//
9
// It is assumed that the display module is connected to an
10
// appropriate shield or that you know how to change the pin 
11
// numbers in the setup.
12
//
13
14
#include <UTFT.h>
15
#include <UTouch.h>
16
#include <UTouchCD.h>
17
18
// Initialize display
19
// ------------------
20
// Set the pins to the correct ones for your development board
21
// -----------------------------------------------------------
22
// Standard Arduino Uno/2009 Shield            : <display model>,19,18,17,16
23
// Standard Arduino Mega/Due shield            : <display model>,38,39,40,41
24
// CTE TFT LCD/SD Shield for Arduino Due       : <display model>,25,26,27,28
25
// Teensy 3.x TFT Test Board                   : <display model>,23,22, 3, 4
26
// ElecHouse TFT LCD/SD Shield for Arduino Due : <display model>,22,23,31,33
27
//
28
// Remember to change the model parameter to suit your display module!
29
UTFT    myGLCD(ITDB32S,38,39,40,41);
30
31
// Initialize touchscreen
32
// ----------------------
33
// Set the pins to the correct ones for your development board
34
// -----------------------------------------------------------
35
// Standard Arduino Uno/2009 Shield            : 15,10,14, 9, 8
36
// Standard Arduino Mega/Due shield            :  6, 5, 4, 3, 2
37
// CTE TFT LCD/SD Shield for Arduino Due       :  6, 5, 4, 3, 2
38
// Teensy 3.x TFT Test Board                   : 26,31,27,28,29
39
// ElecHouse TFT LCD/SD Shield for Arduino Due : 25,26,27,29,30
40
//
41
UTouch  myTouch( 6, 5, 4, 3, 2);
42
43
// Declare which fonts we will be using
44
extern uint8_t BigFont[];
45
46
int x, y;
47
char stCurrent[20]="";
48
int stCurrentLen=0;
49
char stLast[20]="";
50
51
/*************************
52
**   Custom functions   **
53
*************************/
54
55
56
void drawButtons()
57
{
58
// Draw the upper row of buttons
59
  for (x=0; x<5; x++)
60
  {
61
    myGLCD.setColor(0, 0, 255);
62
    myGLCD.fillRoundRect (10+(x*60), 10, 60+(x*60), 60);
63
    myGLCD.setColor(255, 255, 255);
64
    myGLCD.drawRoundRect (10+(x*60), 10, 60+(x*60), 60);
65
    myGLCD.printNumI(x+1, 27+(x*60), 27);
66
  }
67
// Draw the center row of buttons
68
  for (x=0; x<5; x++)
69
  {
70
    myGLCD.setColor(0, 0, 255);
71
    myGLCD.fillRoundRect (10+(x*60), 70, 60+(x*60), 120);
72
    myGLCD.setColor(255, 255, 255);
73
    myGLCD.drawRoundRect (10+(x*60), 70, 60+(x*60), 120);
74
    if (x<4)
75
      myGLCD.printNumI(x+6, 27+(x*60), 87);
76
  }
77
  myGLCD.print("0", 267, 87);
78
// Draw the lower row of buttons
79
  myGLCD.setColor(0, 0, 255);
80
  myGLCD.fillRoundRect (10, 130, 150, 180);
81
  myGLCD.setColor(255, 255, 255);
82
  myGLCD.drawRoundRect (10, 130, 150, 180);
83
  myGLCD.print("Clear", 40, 147);
84
  myGLCD.setColor(0, 0, 255);
85
  myGLCD.fillRoundRect (160, 130, 300, 180);
86
  myGLCD.setColor(255, 255, 255);
87
  myGLCD.drawRoundRect (160, 130, 300, 180);
88
  myGLCD.print("Enter", 190, 147);
89
  myGLCD.setBackColor (0, 0, 0);
90
}
91
92
void updateStr(int val)
93
{
94
  if (stCurrentLen<20)
95
  {
96
    stCurrent[stCurrentLen]=val;
97
    stCurrent[stCurrentLen+1]='\0';
98
    stCurrentLen++;
99
    myGLCD.setColor(0, 255, 0);
100
    myGLCD.print(stCurrent, LEFT, 224);
101
  }
102
  else
103
  {
104
    myGLCD.setColor(255, 0, 0);
105
    myGLCD.print("BUFFER FULL!", CENTER, 192);
106
    delay(500);
107
    myGLCD.print("            ", CENTER, 192);
108
    delay(500);
109
    myGLCD.print("BUFFER FULL!", CENTER, 192);
110
    delay(500);
111
    myGLCD.print("            ", CENTER, 192);
112
    myGLCD.setColor(0, 255, 0);
113
  }
114
}
115
116
// Draw a red frame while a button is touched
117
void waitForIt(int x1, int y1, int x2, int y2)
118
{
119
  myGLCD.setColor(255, 0, 0);
120
  myGLCD.drawRoundRect (x1, y1, x2, y2);
121
  while (myTouch.dataAvailable())
122
    myTouch.read();
123
  myGLCD.setColor(255, 255, 255);
124
  myGLCD.drawRoundRect (x1, y1, x2, y2);
125
}
126
127
/*************************
128
**  Required functions  **
129
*************************/
130
131
void setup()
132
{
133
// Initial setup
134
  myGLCD.InitLCD();
135
  myGLCD.clrScr();
136
137
  myTouch.InitTouch();
138
  myTouch.setPrecision(PREC_MEDIUM);
139
140
  myGLCD.setFont(BigFont);
141
  myGLCD.setBackColor(0, 0, 255);
142
  drawButtons();  
143
}
144
145
void loop()
146
{
147
  while (true)
148
  {
149
    if (myTouch.dataAvailable())
150
    {
151
      myTouch.read();
152
      x=myTouch.getX();
153
      y=myTouch.getY();
154
      
155
      if ((y>=10) && (y<=60))  // Upper row
156
      {
157
        if ((x>=10) && (x<=60))  // Button: 1
158
        {
159
          waitForIt(10, 10, 60, 60);
160
          updateStr('1');
161
        }
162
        if ((x>=70) && (x<=120))  // Button: 2
163
        {
164
          waitForIt(70, 10, 120, 60);
165
          updateStr('2');
166
        }
167
        if ((x>=130) && (x<=180))  // Button: 3
168
        {
169
          waitForIt(130, 10, 180, 60);
170
          updateStr('3');
171
        }
172
        if ((x>=190) && (x<=240))  // Button: 4
173
        {
174
          waitForIt(190, 10, 240, 60);
175
          updateStr('4');
176
        }
177
        if ((x>=250) && (x<=300))  // Button: 5
178
        {
179
          waitForIt(250, 10, 300, 60);
180
          updateStr('5');
181
        }
182
      }
183
184
      if ((y>=70) && (y<=120))  // Center row
185
      {
186
        if ((x>=10) && (x<=60))  // Button: 6
187
        {
188
          waitForIt(10, 70, 60, 120);
189
          updateStr('6');
190
        }
191
        if ((x>=70) && (x<=120))  // Button: 7
192
        {
193
          waitForIt(70, 70, 120, 120);
194
          updateStr('7');
195
        }
196
        if ((x>=130) && (x<=180))  // Button: 8
197
        {
198
          waitForIt(130, 70, 180, 120);
199
          updateStr('8');
200
        }
201
        if ((x>=190) && (x<=240))  // Button: 9
202
        {
203
          waitForIt(190, 70, 240, 120);
204
          updateStr('9');
205
        }
206
        if ((x>=250) && (x<=300))  // Button: 0
207
        {
208
          waitForIt(250, 70, 300, 120);
209
          updateStr('0');
210
        }
211
      }
212
213
      if ((y>=130) && (y<=180))  // Upper row
214
      {
215
        if ((x>=10) && (x<=150))  // Button: Clear
216
        {
217
          waitForIt(10, 130, 150, 180);
218
          stCurrent[0]='\0';
219
          stCurrentLen=0;
220
          myGLCD.setColor(0, 0, 0);
221
          myGLCD.fillRect(0, 224, 319, 239);
222
        }
223
        if ((x>=160) && (x<=300))  // Button: Enter
224
        {
225
          waitForIt(160, 130, 300, 180);
226
          if (stCurrentLen>0)
227
          {
228
            for (x=0; x<stCurrentLen+1; x++)
229
            {
230
              stLast[x]=stCurrent[x];
231
            }
232
            stCurrent[0]='\0';
233
            stCurrentLen=0;
234
            myGLCD.setColor(0, 0, 0);
235
            myGLCD.fillRect(0, 208, 319, 239);
236
            myGLCD.setColor(0, 255, 0);
237
            myGLCD.print(stLast, LEFT, 208);
238
          }
239
          else
240
          {
241
            myGLCD.setColor(255, 0, 0);
242
            myGLCD.print("BUFFER EMPTY", CENTER, 192);
243
            delay(500);
244
            myGLCD.print("            ", CENTER, 192);
245
            delay(500);
246
            myGLCD.print("BUFFER EMPTY", CENTER, 192);
247
            delay(500);
248
            myGLCD.print("            ", CENTER, 192);
249
            myGLCD.setColor(0, 255, 0);
250
          }
251
        }
252
      }
253
    }
254
  }
255
}

UTouchCD.h
1
// UTouchCD.h
2
// ----------
3
//
4
// Since there are slight deviations in all touch screens you should run a
5
// calibration on your display module. Run the UTouch_Calibration sketch
6
// that came with this library and follow the on-screen instructions to
7
// update this file.
8
//
9
// Remember that is you have multiple display modules they will probably 
10
// require different calibration data so you should run the calibration
11
// every time you switch to another module.
12
// You can, of course, store calibration data for all your modules here
13
// and comment out the ones you dont need at the moment.
14
//
15
16
// These calibration settings works with my ITDB02-3.2S.
17
// They MIGHT work on your 320x240 display module, but you should run the
18
// calibration sketch anyway. If you are using a display with any other
19
// resolution you MUST calibrate it as these settings WILL NOT work.
20
21
22
#define CAL_X 0x0038CF1AUL
23
#define CAL_Y 0x03C60110UL
24
#define CAL_S 0x000EF13FUL

: Verschoben durch Moderator
von Frank E. (Firma: Q3) (qualidat)


Lesenswert?

Um diese Uhrzeit kann ich den Sourcecode nicht mehr nachvollziehen, aber 
ganz allgemein: Man sollte die bei der Kalibrierung gefundenen 
Korrekturdaten ins EEPROM schreiben, dann überstehen sie auch den 
nächsten Neustart.

Siehe: http://arduino.cc/en/Tutorial/EEPROMWrite

von Nick_N (Gast)


Lesenswert?

Hey Frank,

vielen Dank für die schnelle Antwort. Leider bin ich noch nicht dazu 
gekommen mich mit der Lösung zu beschäftigen, werde aber auf jeden Fall 
die Tage Rückmeldung geben! :-)

von Kabookie (Gast)


Lesenswert?

Hallo Nick,
ich habe leider genau das selbe Problem. Hast du inzwischen eine Lösung 
gefunden?
beste Grüße

von Nick N. (nick_hb)


Lesenswert?

Hi Kabookie,

es hat sich rausgestellt, dass in einer der Konfig-Dateien von "UTouch" 
ein Fehler war.

Die Datei selbst findest du unter:

/Users/<Benutzer>/Documents/Arduino/libraries/UTouch/hardware/avr

und dort "HW_AVR.inc".

Was du nun macht ist in der letzten For-Schleife einen Delay einbauen, 
damit er zwischen dem Hochziehen der beiden clk Pins und der erneuten 
Abfrage einen Moment wartet.
1
void UTouch::touch_WriteData(byte data)
2
{
3
  byte temp;
4
5
  temp=data;
6
  cbi(P_CLK, B_CLK);
7
8
  for(byte count=0; count<8; count++)
9
  {
10
    if(temp & 0x80)
11
      sbi(P_DIN, B_DIN);
12
    else
13
      cbi(P_DIN, B_DIN);
14
    temp = temp << 1; 
15
    cbi(P_CLK, B_CLK);                
16
    sbi(P_CLK, B_CLK);
17
  }
18
}
19
20
word UTouch::touch_ReadData()
21
{
22
  word data = 0;
23
24
  for(byte count=0; count<12; count++)
25
  {
26
    data <<= 1;
27
    sbi(P_CLK, B_CLK);
28
    cbi(P_CLK, B_CLK);
29
    delayMicroseconds(1);
30
    if (rbi(P_DOUT, B_DOUT))
31
      data++;
32
  }
33
  return(data);
34
}

Und schon sollte das Ganze funktionieren.
Viel Erfolg

von Kabookie (Gast)


Lesenswert?

Hallo Nick,
hat super funktioniert! Danke für die schnelle Hilfe!
beste Grüße

von Mares (Gast)


Lesenswert?

Hallo zusammen
Habe das gleiche Problem.
Habe IDE 1.8.3 folgende Utouch / URTouch Dateien versucht.

void URTouch::touch_WriteData(byte data)
{
  byte temp;

  temp=data;
  cbi(P_CLK, B_CLK);

  for(byte count=0; count<8; count++)
  {
    if(temp & 0x80)
      sbi(P_DIN, B_DIN);
    else
      cbi(P_DIN, B_DIN);
    temp = temp << 1;
    cbi(P_CLK, B_CLK);
    sbi(P_CLK, B_CLK);
  }
}

word URTouch::touch_ReadData()
{
  word data = 0;

  for(byte count=0; count<12; count++)
  {
    data <<= 1;
    sbi(P_CLK, B_CLK);
    cbi(P_CLK, B_CLK);
    if (rbi(P_DOUT, B_DOUT))
      data++;
  }
  return(data);
}







oder



void UTouch::touch_WriteData(byte data)
{
  byte temp;

  temp=data;
  cbi(P_CLK, B_CLK);

  for(byte count=0; count<8; count++)
  {
    if(temp & 0x80)
      sbi(P_DIN, B_DIN);
    else
      cbi(P_DIN, B_DIN);
    temp = temp << 1;
    cbi(P_CLK, B_CLK);
    sbi(P_CLK, B_CLK);
  }
}

word UTouch::touch_ReadData()
{
  word data = 0;

  for(byte count=0; count<12; count++)
  {
    data <<= 1;
    sbi(P_CLK, B_CLK);
    cbi(P_CLK, B_CLK);
    if (rbi(P_DOUT, B_DOUT))
      data++;
  }
  return(data);
}




Leider ohne Erfolg.

Wer kann helfen?

Gruss

von Nick N. (nick_hb)


Lesenswert?

@Mares

Guck mal zwei Beträge nach oben. In deiner 
/Users/<Benutzer>/Documents/Arduino/libraries/UTouch/hardware/avr/HW_AVR 
.inc
Datei fehlt ein delay.

Hast dir die anderen Beiträge nicht angeguckt?

Gruß Nick

von Erich J. (erich24)


Lesenswert?

Hallo allgemein

Dank Nick hab ich zufällig auch ein Problem mit meinem 7" Display
am Arduino Mega gelöst. Mit einem Mega Clone liefen die Testprogramme 
nur manchmal. Da ich auch original Megas habe, probierte ich diese mit 
dem TFT Touch -> die Programme liefen sofort problemlos.

Dann habe ich obigen Beitrag vom Nick gelesen und die Änderungen 
durchgeführt und siehe -> nun laufen auch die Clone problemlos mit dem 
Touch.

Vielen Dank für den Tip
Erich

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.