This is a simple program using the Mikroelectronika TFT resistive touchscreen to create a sketchpad application.

Dependencies:   SPI_TFT_ILI9341 TFT_fonts mbed

Fork of TFT_Mikroelectronika_IL9341 by Oxford CWM Team

Code to make a simple sketchpad using the touchscreen from Mikroelectronika

The Mikroelectronika touch screen is a good sized display we are using for the first time in 2016. It's got a touch panel layer and can be addressed by either SPI or parallel buses.

http://www.mikroe.com/add-on-boards/display/tft-proto/

/media/uploads/cstevens/sketchpad.jpg

Pin connections for use with an mbed

Referring to the pin names on the screen pcb

Pins:

Screenmbed nameKL25Z example
sdimosiPTD2
sdomisoPTD3
rssckPTD1
RDGNDGND
IM0GNDGND
IM13.3V3V3
IM23.3V3V3
IM33.3V3V3
CSdigitalout pinPTD5
RSTdigitaloutpinPTD0
WRdigitaloutpinPTA13

You will need 2 wires to drive the backlight on leda and ledk (anode(connect to +V via a current limiting resistor) and cathode) Link +3.3V to IM1, IM2, IM3 to setup spi communication mode link

Committer:
cstevens
Date:
Wed Jun 15 09:00:27 2016 +0000
Revision:
7:c2bc477a07da
Parent:
6:18c7288b5e00
working versin with a few problems - mostly that the touch identification routine is still not 10% accurATE at telling when an actual touch has occurred.. random noise on the adcs tends to make the occasional spot apear.; fix for thi would be to detec

Who changed what in which revision?

UserRevisionLine numberNew contents of line
cstevens 4:14043cafbec7 1 // example to test the TFT Display from Mikroelectronika
dreschpe 0:7c3b9bfd6ead 2
dreschpe 0:7c3b9bfd6ead 3 #include "stdio.h"
dreschpe 0:7c3b9bfd6ead 4 #include "mbed.h"
dreschpe 0:7c3b9bfd6ead 5 #include "SPI_TFT_ILI9341.h"
dreschpe 0:7c3b9bfd6ead 6 #include "string"
dreschpe 0:7c3b9bfd6ead 7 #include "Arial12x12.h"
dreschpe 0:7c3b9bfd6ead 8 #include "Arial24x23.h"
dreschpe 0:7c3b9bfd6ead 9 #include "Arial28x28.h"
dreschpe 0:7c3b9bfd6ead 10 #include "font_big.h"
cstevens 4:14043cafbec7 11
cstevens 6:18c7288b5e00 12
cstevens 6:18c7288b5e00 13 Serial pc (USBTX,USBRX);
cstevens 4:14043cafbec7 14
dreschpe 0:7c3b9bfd6ead 15
dreschpe 0:7c3b9bfd6ead 16
pegcjs 3:676adf84c914 17
cstevens 6:18c7288b5e00 18 // the display has a backlight switch on board
cstevens 6:18c7288b5e00 19 DigitalOut LCD_LED(PTA13); // may not be needed on mikroelectronika board
cstevens 5:424af4fa03ab 20 DigitalOut pwr(PTD7); // ditto
dreschpe 0:7c3b9bfd6ead 21
dreschpe 0:7c3b9bfd6ead 22 // the TFT is connected to SPI pin 5-7
pegcjs 1:e0479f60cd48 23 //SPI_TFT_ILI9341 TFT(p5, p6, p7, p8, p9, p10,"TFT"); // mosi, miso, sclk, cs, reset, dc for lpc1768
cstevens 5:424af4fa03ab 24 SPI_TFT_ILI9341 TFT(PTD2, PTD3, PTD1, PTD5, PTD0, PTA13,"TFT"); // mosi, miso, sclk, cs, reset, dc for frdmkl25z
cstevens 6:18c7288b5e00 25 //NB better combination to use a coherent 2x4 block for lcd
pegcjs 3:676adf84c914 26 // SPI_TFT_ILI9341 TFT(PTD2, PTD3, PTD1, PTA16, PTA17, PTC16,"TFT"); // mosi, miso, sclk, cs, reset, dc for frdmkl25z
cstevens 6:18c7288b5e00 27 // DigitalOut LCD_LED(PTC17);
cstevens 7:c2bc477a07da 28 int touching=0;
cstevens 6:18c7288b5e00 29
cstevens 6:18c7288b5e00 30 // Subroutine to read the x location of the touch point
cstevens 6:18c7288b5e00 31 // need to set x+ to 3V and ground x- then read analogue voltage on ym
cstevens 6:18c7288b5e00 32 //nb need to add a check for actual touch as opposed to random crap
cstevens 6:18c7288b5e00 33 int readX()
cstevens 6:18c7288b5e00 34 {
cstevens 7:c2bc477a07da 35 int delta=0,xv1=0,xv2=0,k=0;
cstevens 7:c2bc477a07da 36
cstevens 7:c2bc477a07da 37 AnalogIn yp(PTB3);
cstevens 7:c2bc477a07da 38 AnalogIn ym(PTB2);
cstevens 6:18c7288b5e00 39 DigitalOut xp(PTB0);
cstevens 6:18c7288b5e00 40 DigitalOut xm(PTB1);
cstevens 6:18c7288b5e00 41
cstevens 6:18c7288b5e00 42 xp=1; // set positive sdie of x high
cstevens 6:18c7288b5e00 43 xm=0;
cstevens 6:18c7288b5e00 44 // dont need to do anyhting to set low side as it should be fine.
cstevens 6:18c7288b5e00 45 // but do need to disconnect yp
cstevens 6:18c7288b5e00 46 //yp.PinMode(PullNone)
cstevens 7:c2bc477a07da 47 for(k=0; k<10; k++) { // make 10 readings to average
cstevens 7:c2bc477a07da 48 xv1+=(int)ym.read_u16(); // get value
cstevens 7:c2bc477a07da 49 xv2+=(int)yp.read_u16(); // get other value
cstevens 7:c2bc477a07da 50 }
cstevens 7:c2bc477a07da 51 delta=abs(xv2-xv1)/10;
cstevens 7:c2bc477a07da 52 if(delta<300) touching=1;
cstevens 7:c2bc477a07da 53 else touching=0;
cstevens 7:c2bc477a07da 54 pc.printf("delta=%d \t %d\n\r",delta,touching);
cstevens 7:c2bc477a07da 55 xp=0;
cstevens 7:c2bc477a07da 56 xm=0;
cstevens 7:c2bc477a07da 57 return(xv2/10); //maybe better to return the average of both....
cstevens 6:18c7288b5e00 58 }
cstevens 6:18c7288b5e00 59 // subroutine to read y values - has different pin functions ..
cstevens 6:18c7288b5e00 60 int readY()
cstevens 6:18c7288b5e00 61 {
cstevens 7:c2bc477a07da 62 DigitalOut yp(PTB3);
cstevens 7:c2bc477a07da 63 DigitalOut ym(PTB2);
cstevens 6:18c7288b5e00 64 AnalogIn xp(PTB0);
cstevens 6:18c7288b5e00 65 AnalogIn xm(PTB1);
cstevens 6:18c7288b5e00 66
cstevens 6:18c7288b5e00 67 yp=1; // set positive sdie of x high
cstevens 6:18c7288b5e00 68 ym=0;
cstevens 6:18c7288b5e00 69 // dont need to do anyhting to set low side as it should be fine.
cstevens 6:18c7288b5e00 70 // but do need to disconnect yp
cstevens 6:18c7288b5e00 71 //yp.PinMode(PullNone)
cstevens 6:18c7288b5e00 72 int yval=(int)xm.read_u16(); // get value
cstevens 7:c2bc477a07da 73
cstevens 7:c2bc477a07da 74 yp=0;
cstevens 7:c2bc477a07da 75 ym=0;
cstevens 6:18c7288b5e00 76 return(yval);
cstevens 7:c2bc477a07da 77
cstevens 6:18c7288b5e00 78 }
cstevens 6:18c7288b5e00 79
cstevens 7:c2bc477a07da 80 void drawbuttons()
cstevens 7:c2bc477a07da 81 {
cstevens 7:c2bc477a07da 82 TFT.fillrect(0,0,50,50,Red);
cstevens 7:c2bc477a07da 83 TFT.fillrect(50,0,100,50,Green);
cstevens 7:c2bc477a07da 84 TFT.fillrect(100,0,150,50,Blue);
cstevens 7:c2bc477a07da 85 TFT.fillrect(150,0,200,50,White);
cstevens 7:c2bc477a07da 86 TFT.fillrect(200,0,240,50,Black);
cstevens 7:c2bc477a07da 87
cstevens 7:c2bc477a07da 88 TFT.rect(0,0,50,50,White);
cstevens 7:c2bc477a07da 89 TFT.rect(50,0,100,50,White);
cstevens 7:c2bc477a07da 90 TFT.rect(100,0,150,50,White);
cstevens 7:c2bc477a07da 91 TFT.rect(150,0,200,50,White);
cstevens 7:c2bc477a07da 92 TFT.rect(200,0,240,50,White);
cstevens 7:c2bc477a07da 93
cstevens 7:c2bc477a07da 94 }
cstevens 6:18c7288b5e00 95
cstevens 4:14043cafbec7 96
dreschpe 0:7c3b9bfd6ead 97 int main()
dreschpe 0:7c3b9bfd6ead 98 {
cstevens 6:18c7288b5e00 99 pc.baud(115200);
cstevens 7:c2bc477a07da 100 int color=0;
cstevens 7:c2bc477a07da 101 int xpos=0,ypos=0,xp=0,yp=0,sw=0;;
pegcjs 3:676adf84c914 102 pwr=1;
pegcjs 3:676adf84c914 103 wait(0.2);
cstevens 6:18c7288b5e00 104
dreschpe 0:7c3b9bfd6ead 105 int i;
pegcjs 2:25bfb21253a6 106 LCD_LED = 1; // backlight on
cstevens 6:18c7288b5e00 107
dreschpe 0:7c3b9bfd6ead 108 TFT.claim(stdout); // send stdout to the TFT display
dreschpe 0:7c3b9bfd6ead 109 TFT.set_orientation(1);
dreschpe 0:7c3b9bfd6ead 110 TFT.background(Black); // set background to black
dreschpe 0:7c3b9bfd6ead 111 TFT.foreground(White); // set chars to white
dreschpe 0:7c3b9bfd6ead 112 TFT.cls(); // clear the screen
dreschpe 0:7c3b9bfd6ead 113
dreschpe 0:7c3b9bfd6ead 114 //first show the 4 directions
dreschpe 0:7c3b9bfd6ead 115 TFT.set_orientation(0);
dreschpe 0:7c3b9bfd6ead 116 TFT.background(Black);
dreschpe 0:7c3b9bfd6ead 117 TFT.cls();
dreschpe 0:7c3b9bfd6ead 118
dreschpe 0:7c3b9bfd6ead 119 TFT.set_font((unsigned char*) Arial12x12);
dreschpe 0:7c3b9bfd6ead 120 TFT.locate(0,0);
cstevens 6:18c7288b5e00 121 printf(" 0 Hello Mbed 0");
dreschpe 0:7c3b9bfd6ead 122 TFT.set_orientation(1);
dreschpe 0:7c3b9bfd6ead 123 TFT.locate(0,0);
cstevens 6:18c7288b5e00 124 printf(" 1 Hello Mbed 1");
dreschpe 0:7c3b9bfd6ead 125 TFT.set_orientation(2);
dreschpe 0:7c3b9bfd6ead 126 TFT.locate(0,0);
cstevens 6:18c7288b5e00 127 printf(" 2 Hello Mbed 2");
dreschpe 0:7c3b9bfd6ead 128 TFT.set_orientation(3);
dreschpe 0:7c3b9bfd6ead 129 TFT.locate(0,0);
cstevens 6:18c7288b5e00 130 printf(" 3 Hello Mbed 3");
pegcjs 1:e0479f60cd48 131 TFT.set_orientation(3);
dreschpe 0:7c3b9bfd6ead 132 TFT.set_font((unsigned char*) Arial24x23);
dreschpe 0:7c3b9bfd6ead 133 TFT.locate(50,100);
cstevens 6:18c7288b5e00 134 TFT.printf("TFT orientation 3");
cstevens 7:c2bc477a07da 135 TFT.set_orientation(0);
cstevens 7:c2bc477a07da 136
cstevens 6:18c7288b5e00 137 for(i=0; i<10; i++) {
cstevens 7:c2bc477a07da 138 wait(0.2); // wait one seconds
cstevens 6:18c7288b5e00 139 TFT.locate(50,160);
cstevens 6:18c7288b5e00 140 TFT.printf("count %d",i);
cstevens 6:18c7288b5e00 141 }
cstevens 7:c2bc477a07da 142 TFT.set_orientation(0);
cstevens 6:18c7288b5e00 143 TFT.cls();
cstevens 6:18c7288b5e00 144 // LCD_LED = 1;
cstevens 7:c2bc477a07da 145 //cornwer markers
cstevens 7:c2bc477a07da 146 //TFT.fillcircle(10,10,5,0xffff);
cstevens 7:c2bc477a07da 147 //TFT.fillcircle(230,10,5,0xffff);
cstevens 7:c2bc477a07da 148 //TFT.fillcircle(230,310,5,0xffff);
cstevens 7:c2bc477a07da 149 //TFT.fillcircle(10,310,5,0xffff);
cstevens 7:c2bc477a07da 150 drawbuttons();
cstevens 6:18c7288b5e00 151 while(1==1) {
dreschpe 0:7c3b9bfd6ead 152
cstevens 6:18c7288b5e00 153 xpos=readX();
cstevens 6:18c7288b5e00 154 ypos=readY();
cstevens 7:c2bc477a07da 155 // top chunk of the screen is the button area //
cstevens 7:c2bc477a07da 156 // 0<y<50 is palette area //
cstevens 7:c2bc477a07da 157
cstevens 7:c2bc477a07da 158 //pc.printf("xpos=%d\t,\typo=%d",xpos,ypos);
cstevens 7:c2bc477a07da 159 xp=(240*(xpos-5800))/51200;
cstevens 7:c2bc477a07da 160 yp=320-(320*(ypos-3000))/58300;
cstevens 7:c2bc477a07da 161 if(touching==1) pc.printf("\txp=%d\t,\typo=%d\n\r",xp,yp);
cstevens 7:c2bc477a07da 162 if(xp>5 && yp>50 && touching==1) TFT.fillcircle(xp,yp,2,color);
cstevens 7:c2bc477a07da 163 if(yp<50) { // color buttons
cstevens 7:c2bc477a07da 164 sw=(int)xp/50;
cstevens 7:c2bc477a07da 165 switch(sw) {
cstevens 7:c2bc477a07da 166 case 0:
cstevens 7:c2bc477a07da 167 color=0xf800;
cstevens 7:c2bc477a07da 168 break;
cstevens 7:c2bc477a07da 169 case 1:
cstevens 7:c2bc477a07da 170 color=0x07e0;
cstevens 7:c2bc477a07da 171 break;
cstevens 7:c2bc477a07da 172 case 2:
cstevens 7:c2bc477a07da 173 color=0x001f;
cstevens 7:c2bc477a07da 174 break;
cstevens 7:c2bc477a07da 175 case 3:
cstevens 7:c2bc477a07da 176 color=0xffff;
cstevens 7:c2bc477a07da 177 break;
cstevens 7:c2bc477a07da 178 case 4:
cstevens 7:c2bc477a07da 179 color=0x0000;
cstevens 7:c2bc477a07da 180 TFT.cls();
cstevens 7:c2bc477a07da 181 drawbuttons();
cstevens 7:c2bc477a07da 182 break;
cstevens 7:c2bc477a07da 183 }
cstevens 7:c2bc477a07da 184 // if(xp<50) color=0xF800;
cstevens 7:c2bc477a07da 185 // if(50<xp && xp<100) color=0x07e0;
cstevens 7:c2bc477a07da 186 // if(xp>100 && xp<150) color=0x001f;
cstevens 7:c2bc477a07da 187 }
cstevens 7:c2bc477a07da 188
cstevens 6:18c7288b5e00 189 wait(0.1);
cstevens 6:18c7288b5e00 190
pegcjs 1:e0479f60cd48 191 }
cstevens 6:18c7288b5e00 192
dreschpe 0:7c3b9bfd6ead 193 }