Driving ILI9341 TFT Display with STM32F103C8T6 board.

Dependencies:   mbed mbed-STM32F103C8T6 UniGraphic

TFT Display ILI9341 driven by STM32F103C8T6 board

This is a fork of Geremia's UniGraphic Demo using his UniGraphic library.
For more details see the UniGraphic API documentation and ILI9341 Class Reference.

Schematic

/media/uploads/hudakz/stm32f103c8t6_ili9341_02.png

Wiring

STM32F103C8T6ILI9341
+5V<=>VCC
GND<=>GND
PA_15<=>CS
PA_12<=>RESET
PA_11<=>D/C
PB_5<=>SDI(MOSI)
PB_3<=>SCK
+5V<R1>LED
PB_4<=>SDO(MISO)
Committer:
hudakz
Date:
Tue Feb 05 13:43:36 2019 +0000
Revision:
2:8ab51242fa0e
Parent:
0:7136ca870582
Child:
3:61f96984ff5c
Updated.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hudakz 0:7136ca870582 1 #include "mbed.h"
hudakz 0:7136ca870582 2 #include "Arial12x12.h"
hudakz 0:7136ca870582 3 #include "Arial24x23.h"
hudakz 0:7136ca870582 4 #include "Arial43x48_numb.h"
hudakz 0:7136ca870582 5 #include "pict.h"
hudakz 0:7136ca870582 6 #include "pavement_48x34.h"
hudakz 0:7136ca870582 7 #include "ILI9341.h"
hudakz 2:8ab51242fa0e 8
hudakz 0:7136ca870582 9 const unsigned short FOREGROUND_COLORS[] = {White, Cyan, Red, Magenta, Yellow, Orange, GreenYellow};
hudakz 0:7136ca870582 10 const unsigned short BACKGROUND_COLORS[] = {Black, Navy, DarkGreen, DarkCyan, Maroon};
hudakz 2:8ab51242fa0e 11
hudakz 0:7136ca870582 12 Serial pc(PA_2, PA_3); // serial interface with PC
hudakz 0:7136ca870582 13 ILI9341* tft; // ILI9341 driver
hudakz 0:7136ca870582 14 Timer t;
hudakz 0:7136ca870582 15 unsigned short backgroundColor;
hudakz 0:7136ca870582 16 unsigned short foregroundColor;
hudakz 0:7136ca870582 17 unsigned short colorIndex = 0;
hudakz 0:7136ca870582 18 char orient = 3;
hudakz 2:8ab51242fa0e 19 DigitalOut backlight(PA_10); // Display backlight
hudakz 2:8ab51242fa0e 20
hudakz 0:7136ca870582 21 int main()
hudakz 2:8ab51242fa0e 22 {
hudakz 2:8ab51242fa0e 23 backlight = 1;
hudakz 2:8ab51242fa0e 24
hudakz 2:8ab51242fa0e 25 tft = new ILI9341(SPI_8, 10000000, PB_5, PB_4, PB_3, PA_15, PA_12, PA_11, "tft"); // SPI type, SPI speed, mosi, miso, sclk, cs, reset, dc
hudakz 0:7136ca870582 26 tft->set_orientation(orient);
hudakz 0:7136ca870582 27 int time, time2;
hudakz 0:7136ca870582 28 pc.baud (115200);
hudakz 0:7136ca870582 29 pc.printf("\n\nSystem Core Clock = %.3f MHZ\r\n",(float)SystemCoreClock/1000000);
hudakz 0:7136ca870582 30 t.start();
hudakz 2:8ab51242fa0e 31
hudakz 0:7136ca870582 32 while(1) {
hudakz 2:8ab51242fa0e 33 foregroundColor = FOREGROUND_COLORS[colorIndex++ % 7];
hudakz 0:7136ca870582 34 tft->foreground(foregroundColor); // set chars to white
hudakz 0:7136ca870582 35 backgroundColor = BACKGROUND_COLORS[colorIndex % 5];
hudakz 0:7136ca870582 36 tft->background(backgroundColor); // set background to black
hudakz 2:8ab51242fa0e 37 tft->set_orientation((orient++) % 4);
hudakz 0:7136ca870582 38 tft->cls(); // clear the screen
hudakz 0:7136ca870582 39 tft->locate(0,30);
hudakz 0:7136ca870582 40 tft->printf("Display ID: %.8X\r\n", tft->tftID);
hudakz 0:7136ca870582 41 pc.printf("Display ID: %.8X\r\n", tft->tftID);
hudakz 0:7136ca870582 42 // mem write/read test
hudakz 0:7136ca870582 43 unsigned short readback;
hudakz 0:7136ca870582 44 unsigned short colorstep = (0x10000/tft->width());
hudakz 0:7136ca870582 45 for(unsigned short i=0; i<tft->width(); i++) {
hudakz 0:7136ca870582 46 tft->pixel(i,0,i*colorstep); // write line
hudakz 0:7136ca870582 47 }
hudakz 0:7136ca870582 48 bool readerror=false;
hudakz 0:7136ca870582 49 for(unsigned short i=0; i<tft->width(); i++) { // verify line
hudakz 0:7136ca870582 50 readback = tft->pixelread(i,0);
hudakz 0:7136ca870582 51 if(readback!=i*colorstep) {
hudakz 0:7136ca870582 52 readerror=true;
hudakz 0:7136ca870582 53 pc.printf("pix %.4X readback %.4X\r\n", i*colorstep, readback);
hudakz 0:7136ca870582 54 }
hudakz 0:7136ca870582 55 }
hudakz 0:7136ca870582 56 tft->locate(0,10);
hudakz 0:7136ca870582 57 tft->printf("pixelread test %s\r\n", readerror ? "FAIL":"PASS");
hudakz 0:7136ca870582 58 wait(2);
hudakz 2:8ab51242fa0e 59
hudakz 0:7136ca870582 60 tft->cls();
hudakz 0:7136ca870582 61 tft->set_font((unsigned char*) Terminal6x8,32,127,false); //variable width disabled
hudakz 0:7136ca870582 62 tft->locate(0,0);
hudakz 0:7136ca870582 63 tft->printf("Display Test\r\nSome text just to see if auto carriage return works correctly");
hudakz 0:7136ca870582 64 tft->set_font((unsigned char*) Terminal6x8);
hudakz 0:7136ca870582 65 tft->printf("\r\nDisplay Test\r\nSome text just to see if auto carriage return works correctly");
hudakz 0:7136ca870582 66 pc.printf(" Display Test \r\n");
hudakz 0:7136ca870582 67 wait(3);
hudakz 0:7136ca870582 68 t.reset();
hudakz 0:7136ca870582 69 tft->cls();
hudakz 0:7136ca870582 70 time=t.read_us();
hudakz 0:7136ca870582 71 tft->locate(2,55);
hudakz 0:7136ca870582 72 tft->printf("cls: %.3fms", (float)time/1000);
hudakz 0:7136ca870582 73 pc.printf("cls: %.3fms\r\n", (float)time/1000);
hudakz 0:7136ca870582 74 wait(3);
hudakz 2:8ab51242fa0e 75
hudakz 0:7136ca870582 76 tft->cls();
hudakz 0:7136ca870582 77 t.reset();
hudakz 0:7136ca870582 78 // draw some graphics
hudakz 0:7136ca870582 79 tft->set_font((unsigned char*) Arial24x23);
hudakz 0:7136ca870582 80 tft->locate(10,10);
hudakz 0:7136ca870582 81 tft->printf("Test");
hudakz 2:8ab51242fa0e 82
hudakz 0:7136ca870582 83 tft->line(0,0,tft->width()-1,0,foregroundColor);
hudakz 0:7136ca870582 84 tft->line(0,0,0,tft->height()-1,foregroundColor);
hudakz 0:7136ca870582 85 tft->line(0,0,tft->width()-1,tft->height()-1,foregroundColor);
hudakz 2:8ab51242fa0e 86
hudakz 0:7136ca870582 87 tft->rect(10,30,50,40,foregroundColor);
hudakz 0:7136ca870582 88 tft->fillrect(60,30,100,40,foregroundColor);
hudakz 2:8ab51242fa0e 89
hudakz 0:7136ca870582 90 tft->circle(150,32,30,foregroundColor);
hudakz 0:7136ca870582 91 tft->fillcircle(140,20,10,foregroundColor);
hudakz 2:8ab51242fa0e 92
hudakz 0:7136ca870582 93 double s;
hudakz 2:8ab51242fa0e 94
hudakz 0:7136ca870582 95 for (unsigned short i=0; i<tft->width(); i++) {
hudakz 0:7136ca870582 96 s =10 * sin((long double) i / 10 );
hudakz 0:7136ca870582 97 tft->pixel(i,40 + (int)s ,foregroundColor);
hudakz 0:7136ca870582 98 }
hudakz 2:8ab51242fa0e 99
hudakz 2:8ab51242fa0e 100
hudakz 0:7136ca870582 101 time=t.read_us();
hudakz 0:7136ca870582 102 tft->locate(2,55);
hudakz 0:7136ca870582 103 tft->set_font((unsigned char*) Terminal6x8);
hudakz 0:7136ca870582 104 tft->printf("plot: %.3fms", (float)time/1000);
hudakz 0:7136ca870582 105 pc.printf("plot: %.3fms\r\n", (float)time/1000);
hudakz 0:7136ca870582 106 wait(3);
hudakz 0:7136ca870582 107 tft->cls();
hudakz 0:7136ca870582 108 t.reset();
hudakz 0:7136ca870582 109 Bitmap_s pic = {
hudakz 0:7136ca870582 110 64, // XSize
hudakz 0:7136ca870582 111 64, // YSize
hudakz 0:7136ca870582 112 8, // Bytes in Line
hudakz 0:7136ca870582 113 burp, // Pointer to picture data
hudakz 0:7136ca870582 114 };
hudakz 0:7136ca870582 115 tft->Bitmap_BW(pic,tft->width()-64,0);
hudakz 0:7136ca870582 116 time=t.read_us();
hudakz 0:7136ca870582 117 tft->locate(2,55);
hudakz 0:7136ca870582 118 tft->printf("bmp: %.3fms", (float)time/1000);
hudakz 0:7136ca870582 119 pc.printf("bmp: %.3fms\r\n", (float)time/1000);
hudakz 0:7136ca870582 120 wait(3);
hudakz 0:7136ca870582 121 tft->cls();
hudakz 0:7136ca870582 122 tft->set_font((unsigned char*) Arial43x48_numb, 46, 58, false); //only numbers, variable-width disabled
hudakz 0:7136ca870582 123 t.reset();
hudakz 0:7136ca870582 124 tft->locate(0,0);
hudakz 0:7136ca870582 125 tft->printf("%d", 12345);
hudakz 0:7136ca870582 126 time=t.read_us();
hudakz 0:7136ca870582 127 tft->locate(2,55);
hudakz 0:7136ca870582 128 tft->set_font((unsigned char*) Terminal6x8);
hudakz 0:7136ca870582 129 tft->printf("Big Font: %.3fms", (float)time/1000);
hudakz 0:7136ca870582 130 pc.printf("Big Font: %.3fms\r\n", (float)time/1000);
hudakz 0:7136ca870582 131 wait(3);
hudakz 0:7136ca870582 132 // sparse pixels test
hudakz 0:7136ca870582 133 tft->cls();
hudakz 0:7136ca870582 134 tft->FastWindow(true);
hudakz 0:7136ca870582 135 t.reset();
hudakz 0:7136ca870582 136 for(unsigned int i=0; i<20000; i++) {
hudakz 0:7136ca870582 137 tft->pixel((i+(i*89)%tft->width()), (i+(i*61)%tft->height()), White);
hudakz 0:7136ca870582 138 }
hudakz 0:7136ca870582 139 tft->copy_to_lcd();
hudakz 0:7136ca870582 140 time=t.read_us();
hudakz 0:7136ca870582 141 tft->cls();
hudakz 0:7136ca870582 142 tft->FastWindow(false);
hudakz 0:7136ca870582 143 t.reset();
hudakz 0:7136ca870582 144 for(unsigned int i=0; i<20000; i++) {
hudakz 0:7136ca870582 145 tft->pixel((i+(i*89)%tft->width()), (i+(i*61)%tft->height()), White);
hudakz 0:7136ca870582 146 }
hudakz 0:7136ca870582 147 tft->copy_to_lcd();
hudakz 0:7136ca870582 148 time2=t.read_us();
hudakz 0:7136ca870582 149 tft->locate(2,55);
hudakz 0:7136ca870582 150 tft->printf("std:%.3fms fastw:%.3fms", (float)time2/1000, (float)time/1000);
hudakz 0:7136ca870582 151 pc.printf("std: %.3fms fastw: %.3fms\r\n", (float)time2/1000, (float)time/1000);
hudakz 0:7136ca870582 152 wait(3);
hudakz 0:7136ca870582 153 // scroll test, only for TFT
hudakz 0:7136ca870582 154 tft->cls();
hudakz 0:7136ca870582 155 tft->set_font((unsigned char*) Arial24x23);
hudakz 0:7136ca870582 156 tft->locate(2,10);
hudakz 0:7136ca870582 157 tft->printf("Scrolling");
hudakz 0:7136ca870582 158 tft->rect(0,0,tft->width()-1,tft->height()-1,White);
hudakz 0:7136ca870582 159 tft->rect(1,1,tft->width()-2,tft->height()-2,Blue);
hudakz 0:7136ca870582 160 tft->setscrollarea(0,tft->sizeY());
hudakz 0:7136ca870582 161 wait(1);
hudakz 0:7136ca870582 162 tft->scroll(1); //up 1
hudakz 0:7136ca870582 163 wait(1);
hudakz 0:7136ca870582 164 tft->scroll(0); //center
hudakz 0:7136ca870582 165 wait(1);
hudakz 0:7136ca870582 166 tft->scroll(tft->sizeY()-1); //down 1
hudakz 0:7136ca870582 167 wait(1);
hudakz 0:7136ca870582 168 tft->scroll(tft->sizeY()); // same as 0, center
hudakz 0:7136ca870582 169 wait(1);
hudakz 0:7136ca870582 170 tft->scroll(tft->sizeY()>>1); // half screen
hudakz 0:7136ca870582 171 wait(1);
hudakz 0:7136ca870582 172 tft->scrollreset(); // center
hudakz 0:7136ca870582 173 wait(1);
hudakz 0:7136ca870582 174 for(unsigned short i=1; i<=tft->sizeY(); i++) {
hudakz 0:7136ca870582 175 tft->scroll(i);
hudakz 0:7136ca870582 176 wait_ms(2);
hudakz 0:7136ca870582 177 }
hudakz 0:7136ca870582 178 wait(2);
hudakz 0:7136ca870582 179 // color inversion
hudakz 0:7136ca870582 180 for(unsigned short i=0; i<=8; i++) {
hudakz 0:7136ca870582 181 tft->invert(i&1);
hudakz 0:7136ca870582 182 wait_ms(200);
hudakz 0:7136ca870582 183 }
hudakz 0:7136ca870582 184 wait(2);
hudakz 0:7136ca870582 185 // bmp 16bit test
hudakz 0:7136ca870582 186 tft->cls();
hudakz 0:7136ca870582 187 t.reset();
hudakz 0:7136ca870582 188 for(int y=0; y<tft->height(); y+=34) {
hudakz 0:7136ca870582 189 for(int x=0; x<tft->width(); x+=48) tft->Bitmap(x,y,48,34,(unsigned char *)pavement_48x34);
hudakz 0:7136ca870582 190 }
hudakz 0:7136ca870582 191 time=t.read_us();
hudakz 0:7136ca870582 192 tft->locate(2,55);
hudakz 0:7136ca870582 193 tft->set_font((unsigned char*) Terminal6x8);
hudakz 0:7136ca870582 194 tft->printf("Bmp speed: %.3fms", (float)time/1000);
hudakz 0:7136ca870582 195 pc.printf("Bmp speed: %.3fms\r\n", (float)time/1000);
hudakz 0:7136ca870582 196 wait(2);
hudakz 0:7136ca870582 197 }
hudakz 2:8ab51242fa0e 198 }