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:
Sat Apr 25 15:07:44 2020 +0000
Revision:
3:61f96984ff5c
Parent:
2:8ab51242fa0e
Bluepill driving an ILI9341 SPI TFT display.

Who changed what in which revision?

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