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 Jan 07 18:10:25 2017 +0000
Revision:
1:cf50575d5b7f
Parent:
0:7136ca870582
Child:
2:8ab51242fa0e
Updated

Who changed what in which revision?

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