Driving ILI9341 TFT Display with STM32F103C8T6 board.

Dependencies:   mbed mbed-STM32F103C8T6 UniGraphic

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "stm32f103c8t6.h"
00002 #include "mbed.h"
00003 #include "Arial12x12.h"
00004 #include "Arial24x23.h"
00005 #include "Arial43x48_numb.h"
00006 #include "pict.h"
00007 #include "pavement_48x34.h"
00008 #include "ILI9341.h"
00009  
00010 const unsigned short  FOREGROUND_COLORS[] = {White, Cyan, Red, Magenta, Yellow, Orange, GreenYellow};
00011 const unsigned short  BACKGROUND_COLORS[] = {Black, Navy, DarkGreen, DarkCyan, Maroon};
00012  
00013 Serial          pc(PA_2, PA_3);     // serial interface with PC
00014 ILI9341*        tft;                // ILI9341 driver
00015 Timer           t;
00016 unsigned short  backgroundColor;
00017 unsigned short  foregroundColor;
00018 unsigned short  colorIndex = 0;
00019 char            orient = 3;
00020 DigitalOut      backlight(PA_10);   // Display backlight 
00021  
00022 int main()
00023 {    
00024     confSysClock();     //Configure system clock (72MHz HSE clock, 48MHz USB clock)
00025     backlight = 1;
00026  
00027     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
00028     tft->set_orientation(orient);
00029     int time, time2;
00030     pc.baud (115200);
00031     pc.printf("\n\nSystem Core Clock = %.3f MHZ\r\n",(float)SystemCoreClock/1000000);
00032     t.start();
00033  
00034     while(1) {
00035         foregroundColor = FOREGROUND_COLORS[colorIndex++ % 7];
00036         tft->foreground(foregroundColor);    // set chars to white
00037         backgroundColor = BACKGROUND_COLORS[colorIndex % 5];
00038         tft->background(backgroundColor);    // set background to black
00039         tft->set_orientation((orient++) % 4);
00040         tft->cls();                     // clear the screen
00041         tft->locate(0,30);
00042         tft->printf("Display ID: %.8X\r\n", tft->tftID);
00043         pc.printf("Display ID: %.8X\r\n", tft->tftID);
00044         // mem write/read test
00045         unsigned short readback;
00046         unsigned short colorstep = (0x10000/tft->width());
00047         for(unsigned short i=0; i<tft->width(); i++) {
00048             tft->pixel(i,0,i*colorstep); // write line
00049         }
00050         bool readerror=false;
00051         for(unsigned short i=0; i<tft->width(); i++) { // verify line
00052             readback = tft->pixelread(i,0);
00053             if(readback!=i*colorstep) {
00054                 readerror=true;
00055                 pc.printf("pix %.4X readback %.4X\r\n", i*colorstep, readback);
00056             }
00057         }
00058         tft->locate(0,10);
00059         tft->printf("pixelread test %s\r\n", readerror ? "FAIL":"PASS");
00060         wait(2);
00061  
00062         tft->cls();
00063         tft->set_font((unsigned char*) Terminal6x8,32,127,false); //variable width disabled
00064         tft->locate(0,0);
00065         tft->printf("Display Test\r\nSome text just to see if auto carriage return works correctly");
00066         tft->set_font((unsigned char*) Terminal6x8);
00067         tft->printf("\r\nDisplay Test\r\nSome text just to see if auto carriage return works correctly");
00068         pc.printf("  Display Test \r\n");
00069         wait(3);
00070         t.reset();
00071         tft->cls();
00072         time=t.read_us();
00073         tft->locate(2,55);
00074         tft->printf("cls: %.3fms", (float)time/1000);
00075         pc.printf("cls: %.3fms\r\n", (float)time/1000);
00076         wait(3);
00077  
00078         tft->cls();
00079         t.reset();
00080         // draw some graphics
00081         tft->set_font((unsigned char*) Arial24x23);
00082         tft->locate(10,10);
00083         tft->printf("Test");
00084  
00085         tft->line(0,0,tft->width()-1,0,foregroundColor);
00086         tft->line(0,0,0,tft->height()-1,foregroundColor);
00087         tft->line(0,0,tft->width()-1,tft->height()-1,foregroundColor);
00088  
00089         tft->rect(10,30,50,40,foregroundColor);
00090         tft->fillrect(60,30,100,40,foregroundColor);
00091  
00092         tft->circle(150,32,30,foregroundColor);
00093         tft->fillcircle(140,20,10,foregroundColor);
00094  
00095         double s;
00096  
00097         for (unsigned short i=0; i<tft->width(); i++) {
00098             s =10 * sin((long double) i / 10 );
00099             tft->pixel(i,40 + (int)s ,foregroundColor);
00100         }
00101  
00102  
00103         time=t.read_us();
00104         tft->locate(2,55);
00105         tft->set_font((unsigned char*) Terminal6x8);
00106         tft->printf("plot: %.3fms", (float)time/1000);
00107         pc.printf("plot: %.3fms\r\n", (float)time/1000);
00108         wait(3);
00109         tft->cls();
00110         t.reset();
00111         Bitmap_s pic = {
00112             64,     // XSize
00113             64,     // YSize
00114             8,      // Bytes in Line
00115             burp,   // Pointer to picture data
00116         };
00117         tft->Bitmap_BW(pic,tft->width()-64,0);
00118         time=t.read_us();
00119         tft->locate(2,55);
00120         tft->printf("bmp: %.3fms", (float)time/1000);
00121         pc.printf("bmp: %.3fms\r\n", (float)time/1000);
00122         wait(3);
00123         tft->cls();
00124         tft->set_font((unsigned char*) Arial43x48_numb, 46, 58, false); //only numbers, variable-width disabled
00125         t.reset();
00126         tft->locate(0,0);
00127         tft->printf("%d", 12345);
00128         time=t.read_us();
00129         tft->locate(2,55);
00130         tft->set_font((unsigned char*) Terminal6x8);
00131         tft->printf("Big Font: %.3fms", (float)time/1000);
00132         pc.printf("Big Font: %.3fms\r\n", (float)time/1000);
00133         wait(3);
00134         // sparse pixels test
00135         tft->cls();
00136         tft->FastWindow(true);
00137         t.reset();
00138         for(unsigned int i=0; i<20000; i++) {
00139             tft->pixel((i+(i*89)%tft->width()), (i+(i*61)%tft->height()), White);
00140         }
00141         tft->copy_to_lcd();
00142         time=t.read_us();
00143         tft->cls();
00144         tft->FastWindow(false);
00145         t.reset();
00146         for(unsigned int i=0; i<20000; i++) {
00147             tft->pixel((i+(i*89)%tft->width()), (i+(i*61)%tft->height()), White);
00148         }
00149         tft->copy_to_lcd();
00150         time2=t.read_us();
00151         tft->locate(2,55);
00152         tft->printf("std:%.3fms fastw:%.3fms", (float)time2/1000, (float)time/1000);
00153         pc.printf("std: %.3fms fastw: %.3fms\r\n", (float)time2/1000, (float)time/1000);
00154         wait(3);
00155         // scroll test, only for TFT
00156         tft->cls();
00157         tft->set_font((unsigned char*) Arial24x23);
00158         tft->locate(2,10);
00159         tft->printf("Scrolling");
00160         tft->rect(0,0,tft->width()-1,tft->height()-1,White);
00161         tft->rect(1,1,tft->width()-2,tft->height()-2,Blue);
00162         tft->setscrollarea(0,tft->sizeY());
00163         wait(1);
00164         tft->scroll(1); //up 1
00165         wait(1);
00166         tft->scroll(0); //center
00167         wait(1);
00168         tft->scroll(tft->sizeY()-1); //down 1
00169         wait(1);
00170         tft->scroll(tft->sizeY()); // same as 0, center
00171         wait(1);
00172         tft->scroll(tft->sizeY()>>1); // half screen
00173         wait(1);
00174         tft->scrollreset(); // center
00175         wait(1);
00176         for(unsigned short i=1; i<=tft->sizeY(); i++) {
00177             tft->scroll(i);
00178             wait_ms(2);
00179         }
00180         wait(2);
00181         // color inversion
00182         for(unsigned short i=0; i<=8; i++) {
00183             tft->invert(i&1);
00184             wait_ms(200);
00185         }
00186         wait(2);
00187         // bmp 16bit test
00188         tft->cls();
00189         t.reset();
00190         for(int y=0; y<tft->height(); y+=34) {
00191             for(int x=0; x<tft->width(); x+=48) tft->Bitmap(x,y,48,34,(unsigned char *)pavement_48x34);
00192         }
00193         time=t.read_us();
00194         tft->locate(2,55);
00195         tft->set_font((unsigned char*) Terminal6x8);
00196         tft->printf("Bmp speed: %.3fms", (float)time/1000);
00197         pc.printf("Bmp speed: %.3fms\r\n", (float)time/1000);
00198         wait(2);
00199     }
00200 }