Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

has anyone used ST7789 2.4inch TFT display ?

Status
Not open for further replies.

aliyesami

Full Member level 6
Joined
Jan 7, 2010
Messages
369
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,296
Location
USA
Activity points
4,190
I was using ILI9341 2.4inch TFT and its obsolete and replaced by ST7789 controller but the code iam getting from newheaven display is not working .
has anyone used this display and can please share the initialization code.
I am attaching my initialization code and the program code. its displaying text in jagged form and also displaying blue instead of yellow.

- - - Updated - - -

https://obrazki.elektroda.pl/8255400600_1462902194.jpg

- - - Updated - - -


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
/*
 * NewController.cpp
 *
 * Created: 2/4/2016 4:05:33 PM
 * Converted the C project into C++
 *  Author: SAMI
 */
 
/*
 * controller.c
 *
 * Created: 1/28/2014 12:39:18 AM
 *  Author: Sami Ahmad
 
header file location:
 
C:\Program Files (x86)\Atmel\Atmel Toolchain\AVR8 GCC\Native\3.4.2.1002\avr8-gnu-toolchain\avr\include\avr
C:\Program Files (x86)\Atmel\Atmel Toolchain\AVR8 GCC\Native\3.4.2.1002\avr8-gnu-toolchain\avr\include\util
 */
 
 
#define F_CPU 11059200UL                                    // CPU frequency
#define F_SCL 100000L                                       // I2C clock speed 100 kHz
 
#include <avr/io.h>
#include <util/delay.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <avr/interrupt.h>
#include <avr/eeprom.h>
#include "controller_displayshutoff.h"
#include <inttypes.h>
#include <ctype.h>
#include <compat/twi.h>
#include <math.h>
#include <stdint.h>
 
 
volatile int       _height=320,x=0,y=0,h=0;
volatile int       _width=240;
char            buffer2[12] ,xmtbuff[5],recvd_chars[100],recvd_buff[100],cmdbuff[100],buffer[12];
int         i=0;
 
 
int main(void)
{
    TFT_setup();                            // Initialize the TFT display
    _delay_ms(100);
 
 disp_color(YELLOW);
while(1) {
 disp_color(YELLOW);
 TFT_String(100,10,"TESTING ST7789",BLACK,YELLOW,3);
 TFT_String(140,10,"TESTING ST7789",BLACK,YELLOW,2);
 _delay_ms(10000);
}
 
} // end Main program
 
/*
 Procedure: make a command packet to send the current OCR register values across.
*/
void disp_text()
{
    int i,j,k,a,b;
    a=5,b=5,i=0;
        for (j=0; j<5; ++j)
        {
            for(k=0; k<10; ++k)
            {
//              TFT_String(a,b,itoa(recvd_chars[i],buffer,10),txt_bg_color,txt_bg_color,2);
                TFT_String(a,b,itoa(recvd_buff[i],buffer,10),txt_bg_color,txt_bg_color,2);
//              TFT_String(a,b,itoa(recvd[i],buffer,10),txt_bg_color,txt_bg_color,2);
 
                b=b+30;
                i++;
            }
            b=5,a=a+20;
        }
        a=5,b=5,i=0;
        for (j=0; j<10; ++j)
        {
            for(k=0; k<10; ++k)
            {
                TFT_String(a,b,itoa(recvd_buff[i],buffer,10),BLACK,txt_bg_color,2);
                b=b+30;
                i++;
            }
            b=5,a=a+20;
        }
}
 
 
void DrawCircle(int x0, int y0, int radius)
{
    int x = radius, y = 0;
    int radiusError = 1-x;
 
    while(x >= y)
    {
        setPixel(x + x0, y + y0, BLACK);
        setPixel(y + x0, x + y0, BLACK);
        setPixel(-x + x0, y + y0, BLACK);
        setPixel(-y + x0, x + y0, BLACK);
        setPixel(-x + x0, -y + y0, BLACK);
        setPixel(-y + x0, -x + y0, BLACK);
        setPixel(x + x0, -y + y0, BLACK);
        setPixel(y + x0, -x + y0, BLACK);
        y++;
        if (radiusError<0)
        {
            radiusError += 2 * y + 1;
            } else {
            x--;
            radiusError+= 2 * (y - x + 1);
        }
    }
}
 
void DrawFilledCircle(int x0, int y0, int radius, uint16_t color)
{
    int x = radius;
    int y = 0;
    int xChange = 1 - (radius << 1);
    int yChange = 0;
    int radiusError = 0;
 
    while (x >= y)
    {
        for (int i = x0 - x; i <= x0 + x; i++)
        {
            setPixel(i, y0 + y, color);
            setPixel(i, y0 - y, color);
        }
        for (int i = x0 - y; i <= x0 + y; i++)
        {
            setPixel(i, y0 + x, color);
            setPixel(i, y0 - x, color);
        }
 
        y++;
        radiusError += yChange;
        yChange += 2;
        if (((radiusError << 1) + xChange) > 0)
        {
            x--;
            radiusError += xChange;
            xChange += 2;
        }
    }
}
 
void setPixel(int16_t x, int16_t y, uint16_t color)
{
    setAddrWindow(x,x+1,y,y+1);
    data_out(color >> 8);
    data_out(color);
 
}
 
void menu_button()
{
    fillRect(1,1,199,320,txt_bg_color);
    fillRect(200,1,40,320,header_color);
    fillRect(4,270,40,40,AQUA);
    TFT_String(8,275,"F1",WHITE,AQUA,3);
    TFT_String(50,270,"MENU",txt_color,txt_bg_color,2);
}
 
void fillRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color)
{
    setAddrWindow(x, x+w-1, y, y+h-1);
 
    uint8_t hi = color >> 8, lo = color;
 
    for(y=h; y>0; y--) {
        for(x=w; x>0; x--) {
            data_out(hi);
            data_out(lo);
        }
    }
}
 
 
 
void TFT_Text(int16_t x, int16_t y, unsigned char letter, uint16_t color, uint16_t BackGrnd ,uint8_t font)
{
    int i, j, k ,font_width, count;
    if (font==1) {font_width=5;}
    if (font==2) {font_width=10;}
    if (font==3) {font_width=15;}
 
    char mask = 0x01;                           //start looking at the bytes LSB first, then increment up to the MSB
    for (i=0;i<8;i++)                           //each character is 8 pix tall
    {
        for (k=0;k<font;k++)
        {
            setAddrWindow(x+font-1,x+k,y,y+font_width); //set address to the first column and prepare to write 8 pixels down
            for (count=0;count<5;count++) //each character is 5px wide
            {
                if((Ascii_TFT[letter][count] & mask) == mask) //if the bit we're looking for is true
                {
                    for (j=0;j<font;j++)
                    {
                        data_out(color >> 8); //write the color we want
                        data_out(color);
                    }
                }
                else
                {
                    for (j=0;j<font;j++)
                    {
                        data_out(BackGrnd >> 8); //write the color we want
                        data_out(BackGrnd & 0x00FF);
                    }
                }
            }
         }
         x+=font; //increment to the next column
         mask = mask << 1; //increment the mask to the next-most significant bit
    }
}
 
void TFT_String(int16_t x, int16_t y,  const char *pointer, uint16_t color, uint16_t BackGrnd, uint8_t font)
{
    int char_gap;
    char_gap=10;
    if (font==1){char_gap=6;}
    if (font==2){char_gap=11;}
    if (font==3){char_gap=18;}
 
    const char *Src_Pointer;
    Src_Pointer=pointer;
    while(1)
    {
        TFT_Text(x, y,*Src_Pointer, color, BackGrnd, font);     //write the first character in the string
        Src_Pointer++;                                          //increment to the next character
        y+=char_gap;                                        //set the address 6 pixels to the right (one character is 5px wide)
        if(*Src_Pointer == 0) break;                            //break if end of string
    }
}
 
 
void setAddrWindow(uint16_t StartCol,uint16_t EndCol, uint16_t StartRow, uint16_t EndRow)
{
    comm_out(0x002A);                                               //colum address set
    data_out((StartCol >> 8) & 0x00FF);
    data_out(StartCol & 0x00FF);
    data_out((EndCol >> 8) & 0x00FF);
    data_out(EndCol & 0x00FF);
    comm_out(0x002B);                                               //row address set
    data_out((StartRow >> 8) & 0x00FF);
    data_out(StartRow & 0x00FF);
    data_out((EndRow >> 8) & 0x00FF);
    data_out(EndRow & 0x00FF);
    comm_out(0x002c);                                               // write to RAM
}
 
 
void ForwardDiagonalLine(int x, int y, int h)
{
    for (i=1;i<h;i++)
    {
 
        setAddrWindow(x, x, y,y);
        data_out(0x00);
        data_out(0x00);
        x--;
        y++;
    }
    for (i=1;i<h;i++)
    {
 
        setAddrWindow(x+1, x+1, y+1,y+1);
        data_out(0x00);
        data_out(0x00);
        x--;
        y++;
    }
}
 
void BackwardDiagonalLine(int x, int y, int h)
{
    for (i=1;i<h;i++)
    {
 
        setAddrWindow(x, x, y,y);
        data_out(0x00);
        data_out(0x00);
        x--;
        y--;
    }
    for (i=1;i<h;i++)
    {
 
        setAddrWindow(x+1, x+1, y+1,y+1);
        data_out(0x00);
        data_out(0x00);
        x--;
        y--;
    }
}
 
void draw_triangle(int x, int y, int h)
{
    for (i=1;i<h;i++)
    {
        drawFastHLine(x+h-i,y,i,RED);
        y=y+1;
        TFT_String(180,10,itoa(i,buffer,10),txt_color,txt_bg_color,2);
        _delay_ms(200);
    }
}
 
void drawFastVLine(int16_t x, int16_t y, int16_t h,uint16_t color)
{
    // Rudimentary clipping
    if((x >= _width) || (y >= _height)) return;
    if((y+h-1) >= _height)
    h = _height-y;
    setAddrWindow(x, x, y, y+h-1);
    uint8_t hi = color >> 8, lo = color;
    while (h--) {
        data_out(hi);
        data_out(lo);
    }
}
 
void drawFastHLine(int16_t x, int16_t y, int16_t w,uint16_t color)
{
    // Rudimentary clipping
    if((x >= _width) || (y >= _height)) return;
    if((x+w-1) >= _width)  w = _width-x;
//  setAddrWindow(x, y, x+w-1, y);
    setAddrWindow(x, x+w-1, y, y);
    uint8_t hi = color >> 8, lo = color;
 
    while (w--) {
        data_out(hi);
        data_out(lo);
    }
}
 
void disp_color(uint16_t Color)
{
    fillRect(0,0,240,320,Color);
}
 
 
void write_8(unsigned char c)
{
//  PORTB = (PORTB & ~DMSK)|(c & DMSK);
//  PORTC = (PORTC & DMSK)|(c & ~DMSK);
    PORTB = (PORTB & 0xF8)|(c & 0x07);  // Read PORTB and mask to keep upper 5 bits, Mask c to keep lower 3 bits,
                                      //OR both together and write back to PORTB
    PORTC = (PORTC & 0x07)|(c & 0xF8);    // Read PORTC and mask to keep lower 3 bits, mask c to keep upper 5 bits,
                                      //OR both and write back to PORTC
 
    WRLO;
    WRHI;
}
 
void TFT_setup(void)
{
 
    DDRB |= ((1<<PORTB0)|(1<<PORTB1)|(1<<PORTB2)|(1<<PORTB5));
    DDRC |= ((1<<PORTC2)|(1<<PORTC3)|(1<<PORTC4)|(1<<PORTC5)|(1<<PORTC6)|(1<<PORTC7));
    DDRD |= (1<<PORTD3);
    DDRA |= ((1<<PORTA7)|(1<<PORTA6)|(1<<PORTA5));
    RDHI;
    WRHI;
    init_TFT();
}
 
void comm_out(unsigned char data)
{
    RSLO;
    write_8(data);
}
 
void data_out(unsigned char data)
{
    RSHI;
    write_8(data);
}
 
void init_TFT()
{
    RSTLO;                      // pull the RES pin low
    _delay_ms(50);
    RSTHI;                      // pull the RES pin high
    _delay_ms(50);
 
 
  comm_out(0x28);                                  //display off
  comm_out(0x11);  //exit SLEEP mode
  comm_out(0x36);
  data_out(0x88);  // BGR Format
 
  comm_out(0x3A);
  data_out(0x55); // 18bits / pixel @ 262k colors
 
  comm_out(0xB2); // Porch Setting
  data_out(0x0C);
  data_out(0x0C);
  data_out(0x00);
  data_out(0x33);
  data_out(0x33);
 
  comm_out(0xB7);    // Gate Control
  data_out(0x35);
 
  comm_out(0xBB); //VCOM Setting
  data_out(0x2B);
  comm_out(0xC0);
  data_out(0x2C);
  comm_out(0xC2);
  data_out(0x01);
  data_out(0xFF);
  comm_out(0xC3);
  data_out(0x11);
  comm_out(0xC4);
  data_out(0x20);
  comm_out(0xC6);
  data_out(0x0F);
  comm_out(0xD0);
  data_out(0xA4);
  data_out(0xA1);
 
  //////// WINDOW SET ///////////////////////////////////////////
  comm_out(0x2A);
  data_out(0x00);                  //column address set
  data_out(0x00);                  //start 0x0000
  data_out(0x00);
  data_out(0xEF);                  //end 0x00EF
 
  comm_out(0x2B);
  data_out(0x00);                  //page address set
  data_out(0x00);                  //start 0x0000
  data_out(0x01);
  data_out(0x3F);                  //end 0x013F
 
  comm_out(0x29);                  //display ON
    _delay_ms(10);
}
 
// END TFT Display routines

 
Last edited by a moderator:

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top