ettft240320tp3_2_rev_b.c


1
/**************************************************************************************************
2
 *                                GLCD Treiber Modul;                        *
3
 *                                                         *
4
 *                     GLCD_Touch Screen :ET-TFT240320TP-3.2Rev.B                               *
5
 *                               DISPLAY :PORTRAIT(Ver.)                                        *
6
 *                                  MODE :Interface GLCD Parallel Data 8 Bit                      *
7
 **************************************************************************************************
8
 *                                                                                               *
9
 * Editor         : AVR Studio 4.18 Build 700                                                    *
10
 * Compiler       : WinAVR-20100110                                                              *
11
 * Create By      : Mr. Sittiphol Yooyod (WWW.ETT.CO.TH)                                         *
12
 * Last Update    : 24/August /2009                                                              *
13
 * Modifiziert am : xx/Oktober/2010                                     *
14
 *                                                         *
15
 * DipSw1 (TSC Select)    : On  On  On On : Touch Screen Kommunikation                       *
16
 * DipSw2 (Interface Mode): Off Off On -- : Interface Mode (8Bit Modus)                         *
17
 *                                                     *
18
 * Port Interface :       |   MCU: Mega128/2561    |     LCD: ET-TFT240320TP-3.2 Rev.B          *
19
 *________________________|_______________________|_________________________________________       *
20
 *  - Data GLCD           |    : PF[0..7]         | Connect: DB8..DB15                            *
21
 *  - Control GLCD        |    : PE2           | Connect: CS-H                                *
22
 *  -                     |    : PE3        | Connect: RS-H(0=cmm,1=data)                  *
23
 *  -                     |    : PE4        | Connect: WR-H(Write)                         *
24
 *  -                     |    : PE5          | Connect: RD-H(Read)                          *
25
 *  -                     |    : PE6        | Connect: RES-H(Reset)                        *
26
 *  - Back Light GLCD     |    : PE7          | Connect: BL-H(LCD-Back Light(Active=1))      *
27
 *  - Touch Screen        |    : PD2 (MISO-in)    | Connect: MISO                                *
28
 *  -                     |    : PD3 (MOSI-out)   | Connect: MOSI                                *
29
 *  -                     |    : PD4 (SCLK-out)   | Connect: SCLK                                *
30
 *  -                     |    : PD5 (BUSY-in)    | Connect: BUSY(No_USE)                        *
31
 *  -                     |    : PD6 (PEN-in)     | Connect: PEN                                 *
32
 *  -                     |    : PD7 (TC_CS-out)  | Connect: TC_CS                               *
33
 *  - UART0(BaudRate=9600)|    : PE0 = RXD0       |                                              *
34
 *  -                     |    : PE1 = TXD0       |                                              *
35
 **************************************************************************************************/
36
37
#ifndef ETTFT240320TP3_2_REV_B_C
38
#define ETTFT240320TP3_2_REV_B_C
39
40
// interne Header
41
#include <stdio.h>             // Standard I/O-Funktionen (printf, ...)
42
#include <stdlib.h>            // Deklariert ISO C-Makros/Funktionen & AVR-spezifische Erweiterungen
43
#include <avr/io.h>            // Makros für IO Ports.
44
#include <math.h>              // Mathematische Funktionen: sin, cos, log, gamma, bessel, ...
45
#include <stdint.h>         // Deklariert Datentypen wie int8_t usw.
46
#include <compat/deprecated.h> // Call Function sbi(port,bit),cbi(port,bit) = Set,Clear bit
47
48
// zusätzliche Header
49
#define extern
50
#include "f_cpu.h" // wird für util/delay.h benötigt
51
#include "ettft240320tp3_2_rev_b.h"
52
#include "ettft240320tp3_2_rev_b_font.h"
53
#undef extern
54
55
// interner Header
56
#include <util/delay.h>        // Verzögerungsschleifen für kurze, exakte Verzögerungen
57
58
59
60
long dis_XD[3] = {24,215,120} ; // Value refer Point X at 10% of Display(X=240:0-239) 3 Position
61
long dis_YD[3] = {32,160,287} ; // Value refer Point Y at 10% of Display(Y=320:0-319) 3 Position
62
63
64
65
// *** Function Delay ms ***
66
void delay_ms(uint16_t cnt){for(;cnt>0;cnt--)_delay_ms(1);}
67
68
// *** Function Initial Port ***
69
void init_port(void)
70
  { DDRF  = 0xFF ; // Set PortF(0..7) = Output for data GLCD Byte High
71
    DDRE |= 0xFC ; // Set PortE(2..7) = Output for Control GLCD(CS,RS,WR,RD,Reset,BL)
72
    DDRD  = 0x98 ; // Set PortD = PD7,PD4,PD3:Output ;PD6,PD5,PD2:Input for Touch Screen
73
// --- Set Value Default Port ---
74
    PORTF = 0    ;
75
    PORTE = 0x7C ;
76
    PORTD = 0x80 ;
77
  }
78
79
/*** 1.Function Control of Graphic LCD EL-TFT240320TP-3.2 REV.B(Driver SPFD5408S) ***/
80
81
// --- ControlPin cs (PE2=CS) ---
82
void gp_cs_hi(void) {sbi(PORTE,2) ;} // PE2 = Hi
83
void gp_cs_lo(void) {cbi(PORTE,2) ;} // PE2 = Lo
84
85
// --- ControlPin rs (PE3=RS) ---
86
void gp_rs_hi(void) {sbi(PORTE,3) ;} // PE3 = Hi
87
void gp_rs_lo(void) {cbi(PORTE,3) ;} // PE3 = Lo
88
89
// --- ControlPin wr (PE4=WR) ---
90
void gp_wr_hi(void) {sbi(PORTE,4) ;} // PE4 = Hi
91
void gp_wr_lo(void) {cbi(PORTE,4) ;} // PE4 = Lo
92
93
// --- ControlPin rd (PE5=RD) ---
94
void gp_rd_hi(void) {sbi(PORTE,5) ;} // PE5 = Hi
95
void gp_rd_lo(void) {cbi(PORTE,5) ;} // PE5 = Lo
96
97
// --- ControlPin res(PE6=RES) ---
98
void gp_res_hi(void) {sbi(PORTE,6);} // PE6 = Hi
99
void gp_res_lo(void) {cbi(PORTE,6);} // PE6 = Lo
100
101
// --- Control BackLight(PE7=Back Light) ---
102
void gp_bl_on (void){sbi(PORTE,7) ;} // PE7 = Hi(ON)
103
void gp_bl_off(void){cbi(PORTE,7) ;} // PE7 = Lo(OFF)
104
105
/****************************
106
 Write Address Command to LCD
107
 ****************************/
108
void gp_wr_cmm(uint8_t cmm)
109
  {
110
// --- Sent Command 8 bit High ---
111
    gp_cs_lo()   ;
112
    PORTF = 0x00 ; // Write the command to the data bus
113
    gp_rs_lo()   ; // RS=0, Set the RS signal low, indicating a command.
114
    gp_wr_lo()   ; // wr=0, Assert the write enable signal.
115
    gp_wr_hi()   ; // wr=1, Deassert the write enable signal.
116
// --- Sent Command 8 bit Low ---
117
    PORTF = cmm  ; // Write the command to the data bus
118
    gp_wr_lo()   ; // wr=0, Assert the write enable signal
119
    gp_wr_hi()   ; // wr=1, Deassert the write enable signal
120
    gp_rs_hi()   ; // RS=1, Set the RS signal high, indicating that following writes are data
121
  }
122
/*****************
123
 Write data to LCD
124
 *****************/
125
void gp_wr_data(uint16_t dw)
126
  {
127
// --- Sent Command 8 bit High ---
128
    gp_cs_lo()    ;
129
    PORTF = dw>>8 ; // Write data to data bus. 8 bit PF[8..15]
130
    gp_wr_lo()    ; // wr = 0, Assert the write enable signal.
131
    gp_wr_hi()    ; // wr = 1, Deassert the write enable signal.
132
// --- Sent Command 8 bit Low ---
133
    PORTF = dw    ; // Write data to data bus. 16 bit PF[8..15]
134
    gp_wr_lo()    ; // wr = 0, Assert the write enable signal.
135
    gp_wr_hi()    ; // wr = 1, Deassert the write enable signal.
136
    gp_cs_hi()    ;
137
  }
138
139
/*********************
140
 Function Initial GLCD
141
 *********************/
142
void init_glcd(void)
143
  { long cnt ;
144
  // --- Start GLCD ----
145
    gp_rd_hi()         ;
146
    gp_res_lo()        ; // Reset GLCD
147
    delay_ms(1)        ; // Delay for 1ms.
148
    gp_res_hi()        ; // Stop Signal Reset GLCD
149
    delay_ms(1)      ; // Delay for 1ms while the LCD comes out of reset.
150
    gp_wr_cmm(0x00)    ; // Command Start Oscillater
151
    gp_wr_data(0x0001) ; // Enable the oscillator.
152
    delay_ms(10)       ; // Delay for 10ms while the oscillator stabilizes.
153
154
  // - Display Setting -
155
    gp_wr_cmm(0x01)    ; // Configure the output drivers.
156
    gp_wr_data(0x0100) ;
157
    gp_wr_cmm(0x02)    ; // Configure the LCD A/C drive waveform.
158
    gp_wr_data(0x0700) ; // Line inversion 0x0700
159
    gp_wr_cmm(0x03)    ; // Configure Entry Mode
160
    gp_wr_data(0x1230) ; // Color:RGB ,Incremen Hor.&Ver.address,Address update Hor.
161
162
  // -- Power Control --
163
    gp_wr_cmm(0x07)    ; // Enable internal operation of the LCD controller.
164
    gp_wr_data(0x0101) ; // 0011
165
    gp_wr_cmm(0x10)    ;
166
    gp_wr_data(0x0000) ; // 0x00B0
167
    gp_wr_cmm(0x11)    ;
168
    gp_wr_data(0x0007) ; // 0x0037
169
    gp_wr_cmm(0x12)    ;
170
    gp_wr_data(0x0000) ; // 0x011E
171
    gp_wr_cmm(0x13)    ;
172
    gp_wr_data(0x0000) ; // 0x1A00 
173
    delay_ms(20)       ;
174
    gp_wr_cmm(0x10)    ;
175
    gp_wr_data(0x16B0) ;
176
    gp_wr_cmm(0x11)    ;
177
    gp_wr_data(0x0037) ;
178
    delay_ms(20)       ; // 50
179
    gp_wr_cmm(0x12)    ;
180
    gp_wr_data(0x013E) ;
181
    delay_ms(20)      ; // 50
182
    gp_wr_cmm(0x13)    ;
183
    gp_wr_data(0x1A00) ;
184
    gp_wr_cmm(0x29)    ;
185
    gp_wr_data(0x000F) ;
186
    delay_ms(20)       ; // 50- Delay for 40ms while the power supply stabilizes.
187
188
  // --- Clear the contents of the display buffer. ---
189
    gp_wr_cmm(0x22);
190
    for(cnt = 0;cnt<((long)240*320);cnt++)
191
    gp_wr_data(Black)     ; // Back-Ground Color =Black
192
    gp_wr_cmm(0x20)       ; // Command Horizontal GRAM Address Set
193
    gp_wr_data(0x0000)    ; // Address Start 0x0000
194
    gp_wr_cmm(0x21)       ; // Command Vertical GRAM Address Set
195
    gp_wr_data(0x0000)    ; // Address Start 0x0000
196
    gp_wr_cmm(0x50)       ; // Command Horizontal Address Start Position
197
    gp_wr_data(0x0000)    ;
198
    gp_wr_cmm(0x51)       ; // Command Horizontal Address End Position(239)
199
    gp_wr_data(0x00EF)    ;
200
    gp_wr_cmm(0x52)       ; // Command Vertical Address Start Position
201
    gp_wr_data(0x0000)    ;
202
    gp_wr_cmm(0x53)       ; // Command Verticall Address End Position(239)
203
    gp_wr_data(0x013F)    ;
204
    gp_wr_cmm(0x60)       ; // Set the number of lines to scan.
205
    gp_wr_data(0x2700)    ;
206
    gp_wr_cmm(0x61)       ; // Enable grayscale inversion of the source outputs.
207
    gp_wr_data(0x0001)    ;
208
209
  // -- GAMMA Control --
210
    gp_wr_cmm(0x30)    ;
211
    gp_wr_data(0x0007) ;
212
    gp_wr_cmm(0x31)    ;
213
    gp_wr_data(0x0403) ;
214
    gp_wr_cmm(0x32)    ;
215
    gp_wr_data(0x0404) ;
216
    gp_wr_cmm(0x35)    ;
217
    gp_wr_data(0x0002) ;
218
    gp_wr_cmm(0x36)    ;
219
    gp_wr_data(0x0707) ;
220
    gp_wr_cmm(0x37)    ;
221
    gp_wr_data(0x0606) ;
222
    gp_wr_cmm(0x38)    ;
223
    gp_wr_data(0x0106) ;
224
    gp_wr_cmm(0x39)    ;
225
    gp_wr_data(0x0007) ; 
226
    gp_wr_cmm(0x3C)    ;
227
    gp_wr_data(0x0700) ;
228
    gp_wr_cmm(0x3D)    ;
229
    gp_wr_data(0x0707) ;
230
    gp_wr_cmm(0x07)    ; // Enable the image display.
231
    gp_wr_data(0x0173) ;
232
233
    gp_bl_on() ; // Back Light ON
234
235
    delay_ms(20) ; // Delay for 20ms, which is equivalent to two frames.
236
  }
237
238
239
240
/*** 2. Function Touch Screen (ADS7846:SPI-Interface) ***/
241
242
243
244
// ---------- CS [PD7] --------------
245
void tcs_cs_hi (void) {sbi(PORTD,7);}   //PD7 =1
246
void tcs_cs_lo (void) {cbi(PORTD,7);}   //PD7 =0
247
// ---------- SCK [PD4] -------------
248
void tcs_sck_hi(void) {sbi(PORTD,4);}   //PD4 =1
249
void tcs_sck_lo(void) {cbi(PORTD,4);}   //PD4 =0
250
// ---------- Data MOSI [PD3] -------
251
void tcs_sdo_hi(void) {sbi(PORTD,3);}   //PD3 =1
252
void tcs_sdo_lo(void) {cbi(PORTD,3);}   //PD3 =0
253
254
/*********************************************
255
 Function Check Status Press Touch Screen
256
 *********************************************
257
 Return Value :
258
 Function return value '0'= Not Touched screen
259
 Function return value '1'= Touched screen
260
 *********************************************/
261
uint8_t status_touch(void)
262
    { if((PIND & 0x40)== 0x40) // Read PD6
263
        return 0 ;           // No Touch
264
      else
265
         return 1 ;           // Press Touch screen
266
    }
267
268
/************************************************
269
 Function SPI Write and Read Data 1 Byte from
270
 Touch Screen ADS7846
271
 ************************************************
272
 Parameter :
273
 DataByte =data or command control ADS7846(1Byte)
274
 Return Value: Return value adc from touched
275
               times 1 byte Pass Function
276
 ************************************************/
277
uint8_t tcs_wr(uint8_t DataByte)
278
  {
279
   uint8_t Bit,result=0  ;
280
   for(Bit=0;Bit<8;Bit++)  // 8 Bit Write
281
      {
282
     tcs_sck_hi()      ; // Start Clock  High
283
284
  // --- write Data(PD3) ---
285
       if((DataByte & 0x80)== 0x80)
286
      tcs_sdo_hi()   ; // Set bit data(PD3) = 1
287
    else
288
      tcs_sdo_lo()   ; // Clear bit data(PD3) = 0
289
      tcs_sck_lo()   ; // Clock low
290
      DataByte <<= 1 ; // Shift Next Bit Data
291
292
  // --- Read Data(PD2) ---
293
     result <<= 1      ;
294
       if ((PIND & 0x04)== 0x04) // If SDI = "1"
295
        {
296
         result |= 0x01; // Keep Result of Read-data
297
        }
298
    }
299
   return (result)       ;
300
  }
301
302
/***********************************************
303
 Function Read X-Y-Pos.ADC TouchScreen-12bit adc
304
 ***********************************************
305
 Parameter :  No
306
 Return Value  :
307
 dif_adc_X = Keep Result ADC X-Position(12 bit)
308
 dif_adc_Y = Keep result ADC Y-Position(12 bit)
309
 ***********************************************/
310
void tcs_read(void)
311
  {
312
   uint16_t tcs_adc_X ,tcs_adc_Y ;
313
   uint8_t buf_data[4]           ;
314
   if(status_touch())
315
   {
316
   // --- Set default Pin control Touch Screen ---
317
     tcs_sdo_lo() ;
318
    tcs_sck_lo() ;
319
    tcs_cs_lo () ;
320
      buf_data[0] =tcs_wr(0xD0); // Write Command Measure X-Position
321
    buf_data[0] =tcs_wr(0x00); // Read ADCdata X-Position (7-bit byte High) data: 0ddddddd  (bit)
322
    buf_data[1] =tcs_wr(0x90); // Write Command Measure Y-Pos.;Read ADC data X-Pos.(5-bit byte Low)data:ddddd000(bit)
323
    buf_data[2] =tcs_wr(0x00); // Read ADCdata Y-Position(7-bit byte High) data:0ddddddd (bit)
324
    buf_data[3] =tcs_wr(0x00); // Read ADCdata Y-Position(5-bit byte Low) data:ddddd000 (bit)
325
    tcs_adc_X  = buf_data[0]       ; // Mark ADC Data X-Position 12 bit
326
    tcs_adc_X  = tcs_adc_X << 5    ;
327
    tcs_adc_X |= buf_data[1] >> 3  ;
328
    tcs_adc_X  = tcs_adc_X & 0x0FFF;
329
    tcs_adc_Y  = buf_data[2]       ; // Mark ADC Data Y-Position 12 bit
330
    tcs_adc_Y  = tcs_adc_Y << 5    ; // Shift 7 bit High
331
    tcs_adc_Y |= buf_data[3] >> 3  ; // Shift 5 bit low
332
    tcs_adc_Y  = tcs_adc_Y & 0x0FFF; // total ADC data 12 bit
333
    tcs_cs_hi()                    ;
334
    dif_adc_X = tcs_adc_X          ; // ADC 12 bit :LCD Start Portrait
335
    dif_adc_Y = 4095-tcs_adc_Y   ;
336
     }
337
  }
338
339
/*********************************************************
340
 Function Calculate average of measurements,
341
 by cut value max and min measurement.
342
 *********************************************************
343
 Parameter :
344
 num = number times of measurement adc from touch screen
345
 Return value:
346
 ave_adc_X = Keep result average adc X-Position
347
 ave_adc_Y = Keep result average adc Y-Position
348
 *********************************************************/
349
void ave_x_y(uint8_t num)
350
  {
351
   uint8_t i;
352
   long sum_x, sum_y;
353
   long min_x, min_y, max_x, max_y;
354
   sum_x=0;sum_y=0;min_x=0xffff;min_y=0xffff,max_x=0;max_y=0;
355
   for(i=0;i<num;i++)
356
      {
357
     sum_x += buf_adc_X[i] ;
358
     sum_y += buf_adc_Y[i] ;
359
     if(buf_adc_X[i]< min_x) // search for minimum X value
360
      {
361
      min_x = buf_adc_X[i] ;
362
     }
363
     else if (buf_adc_X[i]> max_x) // search for maximum X value
364
           {
365
          max_x = buf_adc_X[i] ;
366
           }
367
     if(buf_adc_Y[i]< min_y) // search for minimum Y value
368
     {
369
      min_y = buf_adc_Y[i] ;
370
     }
371
     else if(buf_adc_Y[i]> max_y) // search for maximum Y value
372
          {
373
           max_y = buf_adc_Y[i] ;
374
             }
375
    }
376
   ave_adc_X = (sum_x - min_x - max_x ) / (num-2) ; // update x average value
377
   ave_adc_Y = (sum_y - min_y - max_y ) / (num-2) ;  // update y average value
378
  }
379
380
/***********************************************
381
 Function Calculate Matrix Find value Coeffcient
382
 ***********************************************
383
 Parameter    : No
384
 Return Value : Keep value Coeffcient for use
385
                calculate X-Y-Pos.in variable
386
                divider,An,Bn,Cn,Dn,En,Fn
387
 ***********************************************/
388
void set_matrix()
389
  {
390
   divider =    ((tcs_ave_X[0]-tcs_ave_X[2])*(tcs_ave_Y[1]-tcs_ave_Y[2]))-
391
              ((tcs_ave_X[1]-tcs_ave_X[2])*(tcs_ave_Y[0]-tcs_ave_Y[2])) ;
392
393
        An =   ((dis_XD[0]-dis_XD[2])*(tcs_ave_Y[1]-tcs_ave_Y[2]))-
394
        ((dis_XD[1]-dis_XD[2])*(tcs_ave_Y[0]-tcs_ave_Y[2])) ;
395
396
      Bn =   ((tcs_ave_X[0]-tcs_ave_X[2])*(dis_XD[1]-dis_XD[2]))-
397
        ((dis_XD[0]-dis_XD[2])*(tcs_ave_X[1]-tcs_ave_X[2])) ;
398
399
      Cn =    (tcs_ave_X[2]*dis_XD[1]-tcs_ave_X[1]*dis_XD[2])*tcs_ave_Y[0]+
400
            (tcs_ave_X[0]*dis_XD[2]-tcs_ave_X[2]*dis_XD[0])*tcs_ave_Y[1]+
401
          (tcs_ave_X[1]*dis_XD[0]-tcs_ave_X[0]*dis_XD[1])*tcs_ave_Y[2] ;
402
403
      Dn =   ((dis_YD[0]-dis_YD[2])*(tcs_ave_Y[1]-tcs_ave_Y[2]))-
404
        ((dis_YD[1]-dis_YD[2])*(tcs_ave_Y[0]-tcs_ave_Y[2])) ;
405
406
        En =   ((tcs_ave_X[0]-tcs_ave_X[2])*(dis_YD[1]-dis_YD[2]))-
407
        ((dis_YD[0]-dis_YD[2])*(tcs_ave_X[1]-tcs_ave_X[2])) ;
408
409
    Fn =    (tcs_ave_X[2]*dis_YD[1]-tcs_ave_X[1]*dis_YD[2])*tcs_ave_Y[0]+
410
            (tcs_ave_X[0]*dis_YD[2]-tcs_ave_X[2]*dis_YD[0])*tcs_ave_Y[1]+
411
          (tcs_ave_X[1]*dis_YD[0]-tcs_ave_X[0]*dis_YD[1])*tcs_ave_Y[2] ;
412
  }
413
414
/***********************************************
415
 Function Get Position Address real of Display
416
 ***********************************************
417
 Parameter:
418
 num = Number times for Measure adc from Touched
419
 Return Value :
420
   X = Keep value address X-Position
421
   Y = Keep value address Y-Position
422
 ***********************************************/
423
void get_point(uint8_t num)
424
  {
425
   uint8_t nm ;
426
   nm=0;
427
   while(status_touch() && nm<num)  // Measure touch x,y 10 times if the Touch kept pressed,
428
        {
429
         tcs_read()               ; // Read value ADC Touch X-Y
430
         buf_adc_X[nm]=  dif_adc_X ; // keep value ADC Touch-X
431
     buf_adc_Y[nm]=  dif_adc_Y ; // keep value ADC Touch-Y
432
         nm++               ;
433
    }
434
   if(nm==num)                // if the touch xy successfuly collected,
435
     {
436
    ave_x_y(num)          ; // take average out of 10 touch measurements, excluding max and min measurement.
437
    dif_adc_X = ave_adc_X ; // keep average ADC Touch-X
438
    dif_adc_Y = ave_adc_Y ; // keep average ADC Touch-Y
439
      X = ((An*dif_adc_X)+(Bn*dif_adc_Y)+Cn)/divider  ;
440
      Y = ((Dn*dif_adc_X)+(En*dif_adc_Y)+Fn)/divider  ;
441
     }
442
  }
443
444
/*******************************
445
 Function Calibrate Touch Screen
446
 Touch at Point + on Screen
447
 3 Point(3 step) for Caribrate
448
 *******************************
449
 Parameter : No
450
 Return value : No
451
 *******************************/
452
void touch_calibrate(void)
453
  {
454
   uint8_t n,touch_mark_point=0;
455
   plot_mark(17,32,24,25,Red); // Plot Signe + for Position Calibrate Touch Point1
456
   print_Str("  <Touch Screen Calibration>  ",2,0, 20,Green, Black); // Plot Text Color Green
457
   print_Str("         Step 1 of 3          ",2,0, 50,Green, Black);
458
   print_Str("  Click on center of '+'Icon  ",2,0,200,White, Black); // Plot Text Color White
459
   print_Str("  for Touch screen calibrate  ",2,0,220,White, Black);
460
461
   while(status_touch()){;}     // Wait until not Touch screen ,
462
   while(touch_mark_point != 3)  // Number Mark Point Calibrate = 3 Point 
463
      {
464
     if(status_touch())      // return (1) if touch screen is detected.
465
       {
466
      for(n=0;n<50;n++)
467
         {
468
        tcs_read(); // Read portrait(ADC X,Y) by touch value from ADS7846 and update dif_adc_X & dif_adc_Y
469
        buf_adc_X[n] = dif_adc_X; // General buffer for averaging and filter touch screen adc read outs.
470
        buf_adc_Y[n] = dif_adc_Y; // General buffer for averaging and filter touch screen adc read outs.
471
         }
472
      ave_x_y(50);                // Take average out of 50 touch measurements, excluding max and min measurement.
473
      tcs_ave_X[touch_mark_point]=  ave_adc_X;
474
      tcs_ave_Y[touch_mark_point]=  ave_adc_Y;
475
      touch_mark_point++; // touch_cal_status;0=xy1,1=xy2,2=xy3,3=calibration success.0xff=calibrat. not performed.
476
477
      while(status_touch()) {;} // Waite until not Touch screen ,
478
      switch(touch_mark_point)  // Select Caribrate Next Step 2 and 3
479
            {
480
          case 1:
481
            bg_color(Black);             // Clear Screen
482
            plot_mark(208,160,215,153,Red); // Plot Signe + for Position Calibrate Touch Point2
483
                         print_Str("  <Touch Screen Calibration>  ",2,0, 20,Green, Black);
484
                        print_Str("         Step 2 of 3          ",2,0, 50,Green, Black);
485
                        print_Str("  Click on center of '+'Icon  ",2,0,200,White, Black);
486
                       print_Str("  for Touch screen calibrate  ",2,0,220,White, Black);
487
            break;
488
          case 2:
489
            bg_color(Black);             // Clear Screen
490
            plot_mark(113,287,120,280,Red); // Plot Signe + for Position Calibrate Touch Point3
491
            print_Str("  <Touch Screen Calibration>  ",2,0, 20,Green, Black);
492
                        print_Str("         Step 3 of 3          ",2,0, 50,Green, Black);
493
                        print_Str("  Click on center of '+'Icon  ",2,0,200,White, Black);
494
                       print_Str("  for Touch screen calibrate  ",2,0,220,White, Black);
495
            break;
496
            }
497
       }
498
     delay_ms(100);
499
      }
500
   set_matrix(); // Calculate Coefficient for use find Position Address real on Display
501
  }
502
503
504
505
/*** 3.Function Aplication Display Screen; ***/
506
507
/*******************************************
508
 Funktion Background Farbe oder Clear Screen
509
 *******************************************
510
 Parameter:
511
 - bg_color = Hintergrund Farbe
512
 *******************************************/
513
void bg_color(uint16_t bg_color)
514
  {
515
   long cnt             ;
516
   gp_wr_cmm(0x20)      ; // Command Horizontal GRAM Address Set
517
   gp_wr_data(0x0000)   ; // Address +Start 0x0000
518
   gp_wr_cmm(0x21)      ; // Command Vertical GRAM Address Set
519
   gp_wr_data(0x0000)   ; // Address Start 0x0000
520
   gp_wr_cmm(0x22)      ;
521
   for(cnt = 0;cnt<((long)240*320);cnt++)
522
   gp_wr_data(bg_color) ; // Back-Ground Color Black
523
  }
524
525
526
/******************************************************
527
 Function Print Text 1 Charecter
528
 ******************************************************
529
 Parameter :
530
 - row      = Ascii Code (Position buffer keep text)
531
 - adx,ady  = Position X,Y for begin plot text by will
532
              begin plot from bottom left to top left
533
 - fg_clr   = Color of text
534
 - bg_clr   = Color background of text
535
              if bg_clr =no_bg or1=non color background
536
 ******************************************************/
537
void lcd_Ascii(uint8_t row,uint8_t font, uint8_t adx,uint16_t ady,uint16_t fg_clr,uint16_t bg_clr)
538
  {
539
   uint16_t ay        ;
540
   uint8_t ax,m,s,n,tx;
541
   ax = adx         ;
542
   ay = ady         ;
543
   row = row-0x20   ;
544
545
  // ********** Print Text 1 Charecter(data 14 Byte) **********
546
      if (font == 1) {s = 6;}
547
    if (font == 2) {s = 14;}
548
    for(m=0;m<s;m++)
549
     {
550
      // **** Sent data byte1=8 bit ****
551
    tx = pgm_read_byte(&(ascii_6x8[row][m]));       // Lese Ascii Zeichen Font 1 (6x8)
552
        if (font == 2)
553
       {tx = pgm_read_byte(&(ascii_7x11[row][m]));} // Lese Ascii Zeichen Font 2 (7x11)
554
    for(n=0;n<8;n++)          // Loop Sent data  1 byte(8bit)
555
       {
556
          if(tx & 0x80)        // if data bit7 = 1 ,Plot Color area Charecter
557
         {
558
          gp_wr_cmm(0x20)      ; // Command Set Adddress Hor(X)
559
               gp_wr_data(ax)       ; // Sent X_Address CGRAM
560
               gp_wr_cmm(0x21)      ; // Command Set Address Ver(Y)
561
                 gp_wr_data(ay)   ; // Sent Y_Address CGRAM
562
           gp_wr_cmm(0x22)  ; // Command Write data
563
               gp_wr_data(fg_clr)   ;
564
         }
565
        else            // if data bit7 = 0 ,Plot Color area back ground Charecter
566
        {
567
         if(bg_clr != 1)
568
                 {
569
            gp_wr_cmm(0x20)   ; // Command Set Adddress Hor(X)
570
                  gp_wr_data(ax)    ; // Sent X_Address CGRAM
571
                  gp_wr_cmm(0x21)   ; // Command Set Adddress Ver(Y)
572
                  gp_wr_data(ay)    ; // Sent Y_Address CGRAM
573
            gp_wr_cmm(0x22)   ; // Command Write data
574
                  gp_wr_data(bg_clr); // Sent Data
575
           }
576
        }
577
        tx <<= 1                 ; // Shift Right data 1 bit
578
        ay   = ay-1              ; // Decrement Y-address
579
       }
580
        if (font == 2)  //------- Sent data byte2=3 bit (nur Font 2)-----
581
           {m++                      ; // Increment Next pointter byte Data
582
          tx = pgm_read_byte(&(ascii_7x11[row][m])); // Read data byte2
583
         for(n=0;n<3;n++)         // Loop sent data byte2 = 3 bit
584
           {
585
              if(tx & 0x80)       // if data bit7 = 1 ,Plot Color area Charecter
586
            {
587
             gp_wr_cmm(0x20)   ; // Command Set Adddress Hor(X)
588
                   gp_wr_data(ax)    ; // Sent X_Address CGRAM
589
                   gp_wr_cmm(0x21)   ; // Command Set Adddress Ver(Y)
590
                   gp_wr_data(ay)    ; // Sent Y_Address CGRAM
591
             gp_wr_cmm(0x22)   ; // Command Write data
592
                   gp_wr_data(fg_clr);
593
             }
594
            else              // if data bit7 = 0 ,Plot Color area back ground Charecter
595
            {
596
             if(bg_clr != 1)
597
                     {
598
                gp_wr_cmm(0x20)   ; // Command Set Adddress Hor(X)
599
                      gp_wr_data(ax)    ; // Sent X_Address CGRAM
600
                      gp_wr_cmm(0x21)   ; // Command Set Adddress Ver(Y)
601
                      gp_wr_data(ay)    ; // Sent Y_Address CGRAM
602
                gp_wr_cmm(0x22)   ; // Command Write data
603
                      gp_wr_data(bg_clr);
604
               }
605
            }
606
            tx <<= 1                ; // Shift Right data 1 bit
607
            ay = ay-1               ; // -decrement Y-address
608
           }
609
       }
610
    ax = ax+1; // Complet sent data 1 oder 2 Byte (11bit bei Font 2, 8bit bei Font 1) Increment X-Address
611
    ay = ady ; // Set Position Y-address old
612
      }       ;
613
614
   // **** Fill Back ground Color Position space between Charecter 1 Colum ****
615
     if(font ==2)
616
     {
617
      if(bg_clr != 1)
618
          {
619
         for(n=0;n<11;n++)
620
            {
621
             gp_wr_cmm(0x20)   ; // Command Set Adddress Hor(X)
622
               gp_wr_data(ax)    ; // Sent X_Address CGRAM
623
               gp_wr_cmm(0x21)   ; // Command Set Adddress Ver(Y)
624
               gp_wr_data(ay)    ; // Sent Y_Address CGRAM
625
             gp_wr_cmm(0x22)   ; // Command Write data
626
               gp_wr_data(bg_clr);
627
           ay = ay-1         ; // Incre ment Y-Address
628
            }
629
          }
630
       }
631
  }
632
/************************************************
633
 Function Print String
634
 ************************************************
635
 Parameter :
636
 *str = Charecter ASCII (String)
637
 sx,sy = Position X,Y for begin plot text by will
638
 begin plot from bottom left to top left
639
 fg_color = color of Text
640
 bg_color = color Background of text
641
 ************************************************/
642
void print_Str(char *str,uint8_t font,uint8_t sx,uint16_t sy,uint16_t fg_color,uint16_t bg_color)
643
  {
644
   uint8_t i;
645
  for (i=0; str[i] != '\0'; i++)
646
  {
647
   lcd_Ascii(str[i],font,sx,sy,fg_color,bg_color);
648
   if(font ==1) {sx += 6;} // Abstand zwischen den Zeichen, bei Font1 = 6 pixel)
649
   if(font ==2) {sx += 8;} // Abstand zwischen den Zeichen, bei Font2 = 8 pixel)
650
  }
651
  }
652
653
/*****************************************************
654
 Funktion: LCD Print Numbers
655
 *****************************************************
656
 Formatiert die übergebene "cha" Variable die einen
657
 Sendet über die LCD_Ascii Funktion eine maximal fünf-
658
 stellige Zahlenvariable an das Display.
659
 Parameter :
660
 - num     :die Zahlenvar. die ausgegeben werden soll
661
 - nr      :Wieviele Ziffern? 1-5 Zifferstellen.
662
 - font    :Font für die Ziffern.
663
 - x       :X-Position
664
 - y     :Y-Position
665
 - farbe   :Ziffernfarbe
666
 - bgfarbe :Hintergrundfarbe
667
 *****************************************************/
668
void print_Num(uint16_t num,uint8_t nr,uint8_t font,uint8_t x,uint16_t y,uint16_t fg_color, uint16_t bg_color)
669
670
  {
671
   uint8_t font_x   =0;         // Variable für die Fontbreite
672
   char     zif      =0;         // Variable für die separierte Ziffer
673
674
   if (font == 1) {font_x = 6;}    // X-Pos zur nächsten Ziffer an den Font anpassen
675
   if (font == 2) {font_x = 7;}    // "
676
677
   for(char i=nr; i>0; i--)        // alle Ziffern der Zahl separieren
678
      {
679
     if(num >9)           // mehr als 1 Ziffer in Zahl?
680
      {
681
        zif   =num %10;       // Ziffer separieren und ...
682
        num   =num /10;       // von Zahl entfernen
683
      }
684
        else               // nur 1 Ziffer in Zahl?
685
        {
686
        zif = num; num = 0;      // Ziffer direkt aus Zahl entnehmen, nächste Ziffer =0
687
      }
688
     lcd_Ascii(zif +0x30,font,x+((i *font_x)-font_x),y,fg_color,bg_color); // Ziffer am Display ausgeben
689
      }
690
  }
691
692
/*****************************
693
 Function Plot Dot 1 Pixel
694
 *****************************
695
 Parameter:
696
 dx,dy = Pos. X,Y for Plot Dot
697
 color = Color of dot
698
 *****************************/
699
void plot_dot(uint8_t dx,uint16_t dy,uint16_t color)
700
  {
701
   gp_wr_cmm(0x20)  ; // Command Set Adddress Hor(X)
702
   gp_wr_data(dx)   ; // Sent X_address for CGRAM
703
   gp_wr_cmm(0x21)  ; // Command Set Address Ver(Y)
704
   gp_wr_data(dy)   ; // Sent Y_address for CGRAM
705
   gp_wr_cmm(0x22)  ; // Command Write Data RGB
706
   gp_wr_data(color); // write Data
707
  }
708
709
/*********************************************
710
 Function Draw Line
711
 *********************************************
712
 Function: Line(lx1,ly1,lx2,ly2,line_type);
713
 Input: lx1,ly1   = Start Coordinate Line
714
        lx2,ly2   = End Coordinate Line
715
        line_type = Value Thick of Line(1-100)
716
        color     = Color of line
717
 *********************************************/
718
  void Line(uint8_t lx1,uint16_t ly1,uint8_t lx2,uint16_t ly2,uint8_t line_type,uint16_t color)
719
   {
720
    uint8_t  tick           ;
721
    uint8_t  x1=lx1,xx1=lx1;
722
    uint16_t y1=ly1        ;
723
    uint8_t  x2=lx2,xx2=lx2;
724
    uint16_t y2=ly2        ;
725
  int16_t  dx,dy,inc_dec,two_dx,two_dy,two_dy_dx,two_dx_dy,x,y,p;
726
    if(lx1>lx2) {x1=lx2; y1=ly2; x2=lx1; y2=ly1;}
727
    dx  =abs(x2-x1); dy  =abs(y2-y1);
728
    if(y2>=y1) {inc_dec =1  ;}
729
  else       {inc_dec = -1;}
730
    if(dx>dy)
731
    {
732
     two_dy=(2*dy)         ;
733
     two_dy_dx=(2*(dy-dx)) ;
734
     p=((2*dy)-dx)         ;
735
       x=x1; y=y1;
736
       while(x<=x2)
737
     {
738
        for(tick=0;tick<line_type;tick++)
739
       {
740
        if(xx1==xx2){plot_dot(x+tick,y,color);}
741
        else        {plot_dot(x,y+tick,color);}
742
        }
743
      x++;
744
        if(p<0) {p += two_dy;}
745
        else    {y += inc_dec; p += two_dy_dx;}
746
      } // while
747
      } // if
748
       else
749
      {
750
       two_dx=(2*dx)        ;
751
       two_dx_dy=(2*(dx-dy));
752
       p=((2*dx)-dy)        ;
753
        x=x1                ;
754
        y=y1                ;
755
       while(y!=y2)
756
      {
757
       for(tick=0;tick<line_type;tick++)
758
        {
759
       if(xx1==xx2) {plot_dot(x+tick,y,color);}
760
       else      {plot_dot(x,y+tick,color);}
761
          }
762
         y+=inc_dec;
763
       if(p<0) {p+=two_dx;}
764
       else    {x++; p += two_dx_dy;}
765
     } // while
766
     } // else
767
    }
768
769
/**************************************************************************
770
 Function Draw Circle
771
 **************************************************************************
772
 Function: Circle(cx,cy,Radius,tick,color,fill)
773
 Input   : CX,CY      = Center Point of Circle
774
           Radius     = radius of Circle
775
           tick(1-100)= bold of line
776
            - (have effect when fill=0)
777
           color      = color of Circle
778
           fill       = 0: No fill color in Circle, 1: Fill color in Circle
779
 **************************************************************************/
780
void Circle(uint8_t cx,uint16_t cy,uint8_t Radius,uint8_t tick,uint16_t color,uint8_t fill)
781
  {
782
   int8_t tswitch=0    ;
783
   uint8_t d,x=0,y=0,bd;
784
   d = cy-cx ;
785
   y = Radius;
786
   tswitch = 3-(2*Radius);
787
   while(x<=y)
788
        {
789
         if(fill==1)
790
         {
791
          Line(cx-x,cy+y,cx+x,cy+y,1,color);
792
          Line(cx-x,cy-y,cx+x,cy-y,1,color);
793
          Line(cx-y,cy+x,cx+y,cy+x,1,color);
794
          Line(cx-y,cy-x,cx+y,cy-x,1,color);
795
         }
796
       else
797
         {
798
            for(bd=0;bd<tick;bd++)
799
             {
800
              plot_dot((cx+x)  +bd,(cy+y)+bd,color);
801
              plot_dot((cx+x)  +bd,(cy-y)+bd,color);
802
              plot_dot((cx-x)  +bd,(cy+y)+bd,color);
803
              plot_dot((cx-x)  +bd,(cy-y)+bd,color);
804
              plot_dot((cy+y-d)+bd,(cy+x)+bd,color);
805
              plot_dot((cy+y-d)+bd,(cy-x)+bd,color);
806
              plot_dot((cy-y-d)+bd,(cy+x)+bd,color);
807
              plot_dot((cy-y-d)+bd,(cy-x)+bd,color);
808
             }
809
            }
810
       if(tswitch<0)
811
          tswitch += (4*x)+6;
812
       else
813
           {
814
              tswitch += 4*(x-y)+10;
815
          y--;
816
           }
817
         x++;
818
        }
819
  }
820
821
/************************************************
822
 Function Draw Ellipse
823
 ************************************************
824
 Function: Ellipse(h,k,rx,ry,tick,color,fill)
825
 Input: h,k        =Center Point of Ellipes
826
        rx,ry      =radius of Ellipse in X,Y axis
827
        tick(1-100)=bold of line
828
                   -(have effect when fill=0)
829
        color      =color of Ellipse
830
        fill       =0: No fill color in Ellipes,
831
                    1: Fill color in Ellipes
832
 ************************************************/
833
void Ellipse(uint16_t h,uint16_t k,long rx,long ry,uint8_t tick,uint16_t color,uint8_t fill)
834
    {
835
    unsigned char bd      ;
836
      long  ax  =(rx*rx)   ;
837
      long   by  =(ry*ry)  ;
838
      float  ax2 =(ax*2)   ;
839
      float  by2 =(by*2)   ;
840
      long   x   = 0       ;
841
      long   y   = ry      ;
842
      float  fx  = 0       ;
843
      float  fy  = (ax2*ry);
844
      float     p=(int)(by-(ax*ry)+(0.25*ax)+0.5);
845
     if(fill==1)
846
      {
847
     Line(h-x,k+y,h+x,k+y,1,color);
848
     Line(h-x,k-y,h+x,k-y,1,color);
849
      }
850
     else
851
      {
852
     for(bd=0;bd<tick;bd++)
853
      {
854
           plot_dot((h+x)+bd,(k+y)+bd,color);
855
       plot_dot((h+x)+bd,(k-y)+bd,color);
856
       plot_dot((h-x)+bd,(k-y)+bd,color);
857
       plot_dot((h-x)+bd,(k+y)+bd,color);
858
      }
859
    }
860
861
       while(fx<fy)
862
     {
863
       x++;
864
       fx+=by2;
865
866
       if(p<0)
867
    p+=(fx+by);
868
869
       else
870
    {
871
       y--;
872
       fy-=ax2;
873
       p+=(fx+by-fy);
874
    }
875
876
    if(fill==1)
877
      {
878
     Line(h-x,k+y,h+x,k+y,1,color);
879
     Line(h-x,k-y,h+x,k-y,1,color);
880
      }
881
     else
882
      {
883
      for(bd=0;bd<tick;bd++)
884
       {
885
        plot_dot((h+x)+bd,(k+y)+bd,color);
886
        plot_dot((h+x)+bd,(k-y)+bd,color);
887
        plot_dot((h-x)+bd,(k-y)+bd,color);
888
        plot_dot((h-x)+bd,(k+y)+bd,color);
889
       }
890
    }
891
    }
892
       p=(int)((by*(x+0.5)*(x+0.5))+(ax*(y-1)*(y-1))-(ax*by)+0.5);
893
       while(y>0)
894
    {
895
       y--;
896
       fy-=ax2;
897
898
       if(p>=0)
899
    p+=(ax-fy);
900
       else
901
    {
902
       x++;
903
       fx+=by2;
904
       p+=(fx+ax-fy);
905
    }
906
    if(fill==1)
907
      {
908
     Line(h-x,k+y,h+x,k+y,1,color);
909
     Line(h-x,k-y,h+x,k-y,1,color);
910
      }
911
     else
912
      {
913
      for(bd=0;bd<tick;bd++)
914
       {
915
        plot_dot((h+x)+bd,(k+y)+bd,color);
916
        plot_dot((h+x)+bd,(k-y)+bd,color);
917
        plot_dot((h-x)+bd,(k-y)+bd,color);
918
        plot_dot((h-x)+bd,(k+y)+bd,color);
919
       }
920
    }
921
    }
922
    }
923
924
/************************************************
925
 Funktion Draw a rectangle
926
 ************************************************
927
 Function: Rectan(x1,y1,x2,y2,tick,color,fill)
928
 Inputs  :(x1, y1) =Start Point coordinate Rectan
929
          (x2, y2) =End Point coordinate  Rectan
930
       tick(1-100) =bold of line(fx when fill=0)
931
       color       =color of rectangle
932
       fill        =0:No fill color in rectangle
933
                    1:Fill color in rectangle
934
935
       (x1,y1)+-------------+
936
              |             |
937
              |             |
938
              |             |
939
              |             |
940
              +-------------+  (x2,y2)
941
 ************************************************/
942
void Rectan(uint8_t x1,uint16_t y1,uint8_t x2,uint16_t y2,uint8_t tick,uint16_t color,uint8_t fill)
943
  {
944
   uint16_t xmin,xmax,ymin,ymax,i;
945
   uint8_t tk = tick-1           ;
946
     if(fill==1)
947
    {
948
         if(x1 < x2)        // Find x min and max
949
              {
950
               xmin = x1;
951
               xmax = x2;
952
        }
953
       else
954
        {
955
          xmin = x2;
956
          xmax = x1;
957
         }
958
       if(y1 < y2)      // Find the y min and max
959
         {
960
           ymin = y1;
961
           ymax = y2;
962
         }
963
       else
964
         {
965
          ymin = y2;
966
          ymax = y1;
967
         }
968
       for(; xmin<=xmax;xmin++)
969
         {
970
           for(i=ymin; i<=ymax; i++)
971
           plot_dot(xmin,i,color);
972
         }
973
    }
974
   else  // No-Fill
975
    {
976
      Line(x1,y1,x2,y1,tick,color)      ;
977
      Line(x1,y2-tk,x2,y2-tk,tick,color);
978
      Line(x1,y1,x1,y2,tick,color)      ;
979
      Line(x2-tk,y1,x2-tk,y2,tick,color);
980
    }
981
  }
982
983
/****************************************
984
 Function Plot Mark(+) für Kalibrierung
985
 ****************************************
986
 Parameter:
987
     x1,y1 = Position start Line '-'(Hor)
988
             plot from Left to Right
989
     x2,y2 = Position start Line '|'(Ver)
990
             plot from botton to top
991
 ****************************************/
992
void plot_mark(uint8_t x1,uint16_t y1,uint8_t x2,uint16_t y2,uint16_t color)
993
  {
994
   uint8_t i           ;
995
  // **** Line- ****
996
   gp_wr_cmm(0x20)         ; // Command Set Adddress Hor(X)
997
   gp_wr_data(x1)          ; // Sent X_address  CGRAM
998
   gp_wr_cmm(0x21)         ; // Command Set Address Ver(Y)
999
   gp_wr_data(y1)          ; // Sent Y_address  CGRAM
1000
   gp_wr_cmm(0x22)         ; // Command Write Data RGB
1001
   for(i=0;i<15;i++)
1002
       gp_wr_data(color)   ;
1003
  // ****** Line | *******
1004
   gp_wr_cmm(0x03)         ; // Command Configure Entry Mode
1005
   gp_wr_data(0x1238)      ; // Color:RGB ,Incremen Hor.&Ver.address,Address update Hor.
1006
   gp_wr_cmm(0x20)         ; // Command Set Adddress Hor(X)
1007
   gp_wr_data(x2)          ; // Sent X_Address CGRAM
1008
   gp_wr_cmm(0x21)         ; // Command Set Address Ver(Y)
1009
   gp_wr_data(y2)          ; // Sent Y_Address CGRAM
1010
   gp_wr_cmm(0x22)         ; // Command Write Data RGB
1011
   for(i=0;i<15;i++)
1012
       gp_wr_data(color)   ;
1013
   gp_wr_cmm(0x03)         ; // Command Configure Entry Mode
1014
   gp_wr_data(0x1230)      ; // Color:RGB ,Incremen Hor.&Ver.address,Address update Hor.
1015
  }
1016
1017
#endif