Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Revision 0:e2e3599e7dba, committed 2021-09-10
- Comitter:
- YROY2004
- Date:
- Fri Sep 10 10:49:37 2021 +0000
- Commit message:
- Draws colored rectangles on a low cost TFT touchscreen that connects to Arduino compatible headers.
Changed in this revision
| main.cpp | Show annotated file Show diff for this revision Revisions of this file |
| mbed.bld | Show annotated file Show diff for this revision Revisions of this file |
diff -r 000000000000 -r e2e3599e7dba main.cpp
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp Fri Sep 10 10:49:37 2021 +0000
@@ -0,0 +1,202 @@
+#include "mbed.h"
+
+#define TFTWIDTH 240
+#define TFTHEIGHT 320
+
+#define TFT_NORTH {SPFD5408_MADCTL_MY | SPFD5408_MADCTL_BGR}
+#define TFT_EAST {SPFD5408_MADCTL_MX | SPFD5408_MADCTL_MY | SPFD5408_MADCTL_MV | SPFD5408_MADCTL_BGR}
+#define TFT_SOUTH {SPFD5408_MADCTL_MX | SPFD5408_MADCTL_BGR}
+#define TFT_WEST {SPFD5408_MADCTL_MV | SPFD5408_MADCTL_BGR}
+
+#define BLACK 0x0000
+#define BLUE 0x001F
+#define RED 0xF800
+#define GREEN 0x07E0
+#define CYAN 0x07FF
+#define MAGENTA 0xF81F
+#define YELLOW 0xFFE0
+#define WHITE 0xFFFF
+
+#define SPFD5408_SOFTRESET 0x01
+#define SPFD5408_SLEEPIN 0x10
+#define SPFD5408_SLEEPOUT 0x11
+#define SPFD5408_NORMALDISP 0x13
+#define SPFD5408_INVERTOFF 0x20
+#define SPFD5408_INVERTON 0x21
+#define SPFD5408_GAMMASET 0x26
+#define SPFD5408_DISPLAYOFF 0x28
+#define SPFD5408_DISPLAYON 0x29
+#define SPFD5408_COLADDRSET 0x2A
+#define SPFD5408_PAGEADDRSET 0x2B
+#define SPFD5408_MEMORYWRITE 0x2C
+#define SPFD5408_PIXELFORMAT 0x3A
+#define SPFD5408_FRAMECONTROL 0xB1
+#define SPFD5408_DISPLAYFUNC 0xB6
+#define SPFD5408_ENTRYMODE 0xB7
+#define SPFD5408_POWERCONTROL1 0xC0
+#define SPFD5408_POWERCONTROL2 0xC1
+#define SPFD5408_VCOMCONTROL1 0xC5
+#define SPFD5408_VCOMCONTROL2 0xC7
+#define SPFD5408_MEMCONTROL 0x36
+#define SPFD5408_MADCTL 0x36
+#define SPFD5408_MADCTL_MY 0x80
+#define SPFD5408_MADCTL_MX 0x40
+#define SPFD5408_MADCTL_MV 0x20
+#define SPFD5408_MADCTL_ML 0x10
+#define SPFD5408_MADCTL_RGB 0x00
+#define SPFD5408_MADCTL_BGR 0x08
+#define SPFD5408_MADCTL_MH 0x04
+
+#define IDLE 1
+#define ACTIVE 0
+#define COMMAND 0
+#define DATA 1
+
+#define TEMPS 40
+
+#define LCD_CS A3
+#define LCD_CD A2
+#define LCD_WR A1
+#define LCD_RD A0
+#define LCD_RESET A4
+
+DigitalOut pinRD(LCD_RD); //PA_0;
+DigitalOut pinWR(LCD_WR); //PA_1;
+DigitalOut pinCD(LCD_CD); //PA_4;
+DigitalOut pinCS(LCD_CS); //PB_0;
+DigitalOut pinReset(LCD_RESET);//PC_1;
+
+
+BusInOut portTFT(D8, D9, D2, D3, D4, D5, D6, D7);
+
+void WriteCommand(uint8_t c)
+{
+ pinCD = COMMAND;
+ pinWR = ACTIVE;
+ portTFT = c;
+ pinWR = IDLE;
+}
+
+void WriteData(uint8_t d)
+{
+ pinCD = DATA;
+ wait_ms(1);
+ wait_ms(1);
+ pinWR = ACTIVE;
+ wait_ms(1);
+ portTFT = d;
+ wait_ms(1);
+ pinWR = IDLE;
+ wait_ms(1);
+}
+
+Serial pc(SERIAL_TX, SERIAL_RX);
+
+DigitalOut myled(LED1);
+
+
+
+void begin(void) {
+
+ pinCS = IDLE;
+ pinCD = DATA;
+ pinWR = IDLE;
+ pinRD = IDLE;
+ portTFT.output();
+
+ pinReset = ACTIVE;
+ wait_ms(100);
+ pinReset = IDLE;
+ wait_ms(100);
+ pinCS = ACTIVE;
+
+ WriteCommand(SPFD5408_SOFTRESET);
+ WriteData(0);
+ wait_ms(50);
+
+ WriteCommand(SPFD5408_MEMCONTROL);
+ WriteData(SPFD5408_MADCTL_MY | SPFD5408_MADCTL_BGR);
+
+ WriteCommand(SPFD5408_PIXELFORMAT);
+ WriteData(0x55);
+
+ WriteCommand(SPFD5408_FRAMECONTROL);
+ WriteData(0x00);
+ WriteData(0x1B);
+
+ WriteCommand(SPFD5408_SLEEPOUT);
+ WriteData(0);
+
+ WriteCommand(SPFD5408_DISPLAYON);
+ WriteData(0);
+}
+
+void setAddrWindow(int x1, int y1, int x2, int y2) {
+ pinCS = ACTIVE;
+ wait_us(TEMPS);
+ WriteCommand(SPFD5408_COLADDRSET);
+ WriteData(x1 >> 8);
+ WriteData(x1);
+ WriteData(x2 >> 8);
+ WriteData(x2);
+ wait_us(TEMPS);
+ pinCS = IDLE;
+
+ pinCS = ACTIVE;
+ wait_us(TEMPS);
+ WriteCommand(SPFD5408_PAGEADDRSET);
+ WriteData(y1 >> 8);
+ WriteData(y1);
+ WriteData(y2 >> 8);
+ WriteData(y2);
+ wait_us(TEMPS);
+ pinCS = IDLE;
+}
+
+
+void fillRect(uint16_t x1, uint16_t y1, uint16_t w, uint16_t h, uint16_t fillcolor) {
+ uint8_t hi, lo;
+ uint16_t x2, y2;
+ uint16_t i, j;
+
+ x2 = x1 + w - 1;
+ y2 = y1 + h - 1;
+ setAddrWindow(x1, y1, x2, y2);
+
+ hi = fillcolor >> 8;
+ lo = fillcolor;
+
+ pinCS = ACTIVE;
+
+ WriteCommand(SPFD5408_MEMORYWRITE);
+ pinCD = DATA;
+ for (i = h; i > 0; i--)
+ {
+ for (j = w; j > 0; j--)
+
+ {
+ pinWR = ACTIVE;
+ portTFT = hi;
+ pinWR = IDLE;
+ pinWR = ACTIVE;
+ portTFT = lo;
+ pinWR = IDLE;
+ }
+ }
+ pinCS = IDLE;
+}
+
+
+int main()
+{
+ begin();
+ fillRect(0, 0, 240, 320, BLACK);
+ fillRect(75, 100, 100, 150, RED);
+ fillRect(80, 105, 90, 140, YELLOW);
+
+ while(1)
+ {
+ }
+}
+
+
diff -r 000000000000 -r e2e3599e7dba mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Fri Sep 10 10:49:37 2021 +0000 @@ -0,0 +1,1 @@ +https://mbed.org/users/mbed_official/code/mbed/builds/fd96258d940d \ No newline at end of file