3.5" inch TFT LCD Display Module 480X320 driven with FSMC.
TFT LCD Display Module 480X320 driven with FSMC
I have recently bought a 3.5" inch TFT LCD Touch Screen Display Module 480X320 with a www.mcufriend.com
label on the back side. The display was equipped with an 8bit parallel interface. First I decided to test it with the UniGraphic library using the BUS_8
protocol. The display was very slow but improved when I switched to the PAR_8
protocol. Because I heard about the possibility to use a Flexible Static Memory Controller (FSMC), built into some STM MCU's, to drive LCD's (read/write to LCD's memory rather than to an external SRAM) I thought it would be a fun to try it out.
Below is the brief story of what I did:
- Created a project for my STM32F407VE board in the STM32CubeIDE
- Set the
Clock Configuration
to match the one used by Mbed for the Seeed Arch Max board:
- Selected
FSMC
in theConnectivity
category and configured it as below: - Let the
STM32CubeIDE
generate the code (files). - Created a new program for the Seeed Arch Max target in the Mbed Online Compiler by selecting a
mbed os blinky
template. - Replaced the
main.cpp
with themain.c
content of theSTM32CubeIDE
project. Copy & Pasted
the other files with codes from theSTM32CubeIDE
project to the online compiler project.- Renamed and modified:
"stm32f4xx_it.h" to "stm32f4xx_it_msp.h"
"stm32f4xx_it.c" to "stm32f4xx_it_msp.c" - Added the UniGraphic library to the online compiler project.
- Extended the
UniGraphic
library with aFSMC_8
protocol and replaced theTFT::set_orientation(int orient)
function with the one used bymcufriend
for arduino. - Modified the
main.cpp
as needed.
Wiring
STM32F407VE | TFT LCD module |
---|---|
+3.3V | 3V3 |
GND | GND |
PB_12 | LCD_RST |
GND | LCD_CS |
PD_13 (RS) | LCD_RS |
PD_5 (WR) | LCD_WR |
PD_4 (RD) | LCD_RD |
PD_14 (DB00) | LCD_D0 |
PD_15 (DB01) | LCD_D1 |
PD_0 (DB02) | LCD_D2 |
PD_1 (DB03) | LCD_D3 |
PE_7 (DB04) | LCD_D4 |
PE_8 (DB05) | LCD_D5 |
PE_9 (DB06) | LCD_D6 |
PE_10 (DB07) | LCD_D7 |
Results
Execution times | ||
---|---|---|
Used protocol | BUS_8 | FSMC_8 |
Operation \ Time | ms | ms |
Clear | 2283.980 | 38.454 |
Plot | 192.066 | 11.365 |
8bit BMP | 63.805 | 41.338 |
Large Font | 163.872 | 7.895 |
Sparce pixels | 2072.265/1458.051 | 74.107/52.168 |
16bit BMP | 2288.589 | 59.904 |
UniGraphic/Inits/SSD1306.cpp@1:47c996032a9e, 2020-09-25 (annotated)
- Committer:
- hudakz
- Date:
- Fri Sep 25 14:52:27 2020 +0000
- Revision:
- 1:47c996032a9e
- Parent:
- 0:fa952828e34c
3.5" inch TFT LCD Display Module 480X320 driven with FSMC.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hudakz | 0:fa952828e34c | 1 | /* mbed UniGraphic library - Device specific class |
hudakz | 0:fa952828e34c | 2 | * Copyright (c) 2015 Peter Drescher |
hudakz | 0:fa952828e34c | 3 | * Released under the MIT License: http://mbed.org/license/mit |
hudakz | 0:fa952828e34c | 4 | */ |
hudakz | 0:fa952828e34c | 5 | |
hudakz | 0:fa952828e34c | 6 | #include "Protocols.h" |
hudakz | 0:fa952828e34c | 7 | #include "SSD1306.h" |
hudakz | 0:fa952828e34c | 8 | |
hudakz | 0:fa952828e34c | 9 | ////////////////////////////////////////////////////////////////////////////////// |
hudakz | 0:fa952828e34c | 10 | // display settings /////////////////////////////////////////////////////// |
hudakz | 0:fa952828e34c | 11 | ///////////////////////////////////////////////////////////////////////// |
hudakz | 0:fa952828e34c | 12 | |
hudakz | 0:fa952828e34c | 13 | |
hudakz | 0:fa952828e34c | 14 | #define IC_X_SEGS 128 // UC1608 SEG has range 0-239 (239-0 if MX=1), check your datasheet, important for the orientation |
hudakz | 0:fa952828e34c | 15 | #define IC_Y_COMS 64 // UC1608 COM has range 0-127 (127-0 if MY=1), check your datasheet, important for the orientation |
hudakz | 0:fa952828e34c | 16 | |
hudakz | 0:fa952828e34c | 17 | #define SSD1306_SETCONTRAST 0x81 |
hudakz | 0:fa952828e34c | 18 | #define SSD1306_DISPLAYALLON_RESUME 0xA4 |
hudakz | 0:fa952828e34c | 19 | #define SSD1306_DISPLAYALLON 0xA5 |
hudakz | 0:fa952828e34c | 20 | #define SSD1306_NORMALDISPLAY 0xA6 |
hudakz | 0:fa952828e34c | 21 | #define SSD1306_INVERTDISPLAY 0xA7 |
hudakz | 0:fa952828e34c | 22 | #define SSD1306_DISPLAYOFF 0xAE |
hudakz | 0:fa952828e34c | 23 | #define SSD1306_DISPLAYON 0xAF |
hudakz | 0:fa952828e34c | 24 | #define SSD1306_SETDISPLAYOFFSET 0xD3 |
hudakz | 0:fa952828e34c | 25 | #define SSD1306_SETCOMPINS 0xDA |
hudakz | 0:fa952828e34c | 26 | #define SSD1306_SETVCOMDETECT 0xDB |
hudakz | 0:fa952828e34c | 27 | #define SSD1306_SETDISPLAYCLOCKDIV 0xD5 |
hudakz | 0:fa952828e34c | 28 | #define SSD1306_SETPRECHARGE 0xD9 |
hudakz | 0:fa952828e34c | 29 | #define SSD1306_SETMULTIPLEX 0xA8 |
hudakz | 0:fa952828e34c | 30 | #define SSD1306_SETLOWCOLUMN 0x00 |
hudakz | 0:fa952828e34c | 31 | #define SSD1306_SETHIGHCOLUMN 0x10 |
hudakz | 0:fa952828e34c | 32 | #define SSD1306_SETSTARTLINE 0x40 |
hudakz | 0:fa952828e34c | 33 | #define SSD1306_MEMORYMODE 0x20 |
hudakz | 0:fa952828e34c | 34 | #define SSD1306_COMSCANINC 0xC0 |
hudakz | 0:fa952828e34c | 35 | #define SSD1306_COMSCANDEC 0xC8 |
hudakz | 0:fa952828e34c | 36 | #define SSD1306_SEGREMAP 0xA0 |
hudakz | 0:fa952828e34c | 37 | #define SSD1306_CHARGEPUMP 0x8D |
hudakz | 0:fa952828e34c | 38 | |
hudakz | 0:fa952828e34c | 39 | SSD1306::SSD1306(proto_t displayproto, PortName port, PinName CS, PinName reset, PinName DC, PinName WR, PinName RD, const char *name , unsigned int LCDSIZE_X, unsigned int LCDSIZE_Y) |
hudakz | 0:fa952828e34c | 40 | : LCD(displayproto, port, CS, reset, DC, WR, RD, LCDSIZE_X, LCDSIZE_Y, IC_X_SEGS, IC_Y_COMS, name) |
hudakz | 0:fa952828e34c | 41 | { |
hudakz | 0:fa952828e34c | 42 | hw_reset(); |
hudakz | 0:fa952828e34c | 43 | BusEnable(true); |
hudakz | 0:fa952828e34c | 44 | init(); |
hudakz | 0:fa952828e34c | 45 | cls(); |
hudakz | 0:fa952828e34c | 46 | set_orientation(1); |
hudakz | 0:fa952828e34c | 47 | locate(0,0); |
hudakz | 0:fa952828e34c | 48 | } |
hudakz | 0:fa952828e34c | 49 | SSD1306::SSD1306(proto_t displayproto, int Hz, PinName mosi, PinName miso, PinName sclk, PinName CS, PinName reset, PinName DC, const char *name, unsigned int LCDSIZE_X, unsigned int LCDSIZE_Y) |
hudakz | 0:fa952828e34c | 50 | : LCD(displayproto, Hz, mosi, miso, sclk, CS, reset, DC, LCDSIZE_X, LCDSIZE_Y, IC_X_SEGS, IC_Y_COMS, name) |
hudakz | 0:fa952828e34c | 51 | { |
hudakz | 0:fa952828e34c | 52 | hw_reset(); |
hudakz | 0:fa952828e34c | 53 | BusEnable(true); |
hudakz | 0:fa952828e34c | 54 | init(); |
hudakz | 0:fa952828e34c | 55 | cls(); |
hudakz | 0:fa952828e34c | 56 | set_orientation(1); |
hudakz | 0:fa952828e34c | 57 | locate(0,0); |
hudakz | 0:fa952828e34c | 58 | } |
hudakz | 0:fa952828e34c | 59 | |
hudakz | 0:fa952828e34c | 60 | SSD1306::SSD1306(proto_t displayproto, int Hz, int address, PinName sda, PinName scl, const char* name , unsigned int LCDSIZE_X, unsigned int LCDSIZE_Y) |
hudakz | 0:fa952828e34c | 61 | : LCD(displayproto, Hz, address, sda, scl, LCDSIZE_X, LCDSIZE_Y, IC_X_SEGS, IC_Y_COMS, name) |
hudakz | 0:fa952828e34c | 62 | { |
hudakz | 0:fa952828e34c | 63 | init(); |
hudakz | 0:fa952828e34c | 64 | cls(); |
hudakz | 0:fa952828e34c | 65 | set_orientation(1); |
hudakz | 0:fa952828e34c | 66 | locate(0,0); |
hudakz | 0:fa952828e34c | 67 | } |
hudakz | 0:fa952828e34c | 68 | |
hudakz | 0:fa952828e34c | 69 | |
hudakz | 0:fa952828e34c | 70 | // reset and init the lcd controller |
hudakz | 0:fa952828e34c | 71 | void SSD1306::init() |
hudakz | 0:fa952828e34c | 72 | { |
hudakz | 0:fa952828e34c | 73 | /* Start Initial Sequence ----------------------------------------------------*/ |
hudakz | 0:fa952828e34c | 74 | |
hudakz | 0:fa952828e34c | 75 | // wr_cmd8(0xE2); // sw reset |
hudakz | 0:fa952828e34c | 76 | wait_ms(15); |
hudakz | 0:fa952828e34c | 77 | |
hudakz | 0:fa952828e34c | 78 | wr_cmd8(SSD1306_DISPLAYOFF); // no problem in SPI_16 for single byte cmds |
hudakz | 0:fa952828e34c | 79 | wr_cmd16((SSD1306_SETDISPLAYCLOCKDIV<<8)|0x80); // wr_cmd16 for multibyte cmds issue in SPI16 mode |
hudakz | 0:fa952828e34c | 80 | // wr_cmd8(0x80); // in SPI_16 it would become 0xE380 and will break things up |
hudakz | 0:fa952828e34c | 81 | wr_cmd16((SSD1306_SETMULTIPLEX<<8)|63); |
hudakz | 0:fa952828e34c | 82 | // wr_cmd8(63); |
hudakz | 0:fa952828e34c | 83 | |
hudakz | 0:fa952828e34c | 84 | wr_cmd16((SSD1306_SETDISPLAYOFFSET<<8)|0x00); |
hudakz | 0:fa952828e34c | 85 | // wr_cmd8(0x0); |
hudakz | 0:fa952828e34c | 86 | |
hudakz | 0:fa952828e34c | 87 | wr_cmd8(SSD1306_SETSTARTLINE | 0x0); // line #0 |
hudakz | 0:fa952828e34c | 88 | |
hudakz | 0:fa952828e34c | 89 | wr_cmd16((SSD1306_CHARGEPUMP<<8)|0x14); |
hudakz | 0:fa952828e34c | 90 | // wr_cmd8(0x14); // 0x10 |
hudakz | 0:fa952828e34c | 91 | |
hudakz | 0:fa952828e34c | 92 | wr_cmd16((SSD1306_MEMORYMODE<<8)|0x00); |
hudakz | 0:fa952828e34c | 93 | // wr_cmd8(0x00); // 0x0 act like ks0108 |
hudakz | 0:fa952828e34c | 94 | |
hudakz | 0:fa952828e34c | 95 | wr_cmd8(SSD1306_SEGREMAP ); //| 0x1); |
hudakz | 0:fa952828e34c | 96 | |
hudakz | 0:fa952828e34c | 97 | wr_cmd8(SSD1306_COMSCANDEC); |
hudakz | 0:fa952828e34c | 98 | |
hudakz | 0:fa952828e34c | 99 | wr_cmd16((SSD1306_SETCOMPINS<<8)|0x12); |
hudakz | 0:fa952828e34c | 100 | // wr_cmd8(0x12); // LCDSIZE_Y == 32 ? 0x02 : 0x12); |
hudakz | 0:fa952828e34c | 101 | |
hudakz | 0:fa952828e34c | 102 | wr_cmd16((SSD1306_SETCONTRAST<<8)|0xCF); |
hudakz | 0:fa952828e34c | 103 | // wr_cmd8(0xCF); // _rawHeight == 32 ? 0x8F : ((vccstate == SSD1306_EXTERNALVCC) ? 0x9F : 0xCF) ); |
hudakz | 0:fa952828e34c | 104 | |
hudakz | 0:fa952828e34c | 105 | wr_cmd16((SSD1306_SETPRECHARGE<<8)|0xF1); |
hudakz | 0:fa952828e34c | 106 | // wr_cmd8(0xF1); // : 0x22); |
hudakz | 0:fa952828e34c | 107 | |
hudakz | 0:fa952828e34c | 108 | wr_cmd16((SSD1306_SETVCOMDETECT<<8)|0x40); |
hudakz | 0:fa952828e34c | 109 | // wr_cmd8(0x40); |
hudakz | 0:fa952828e34c | 110 | |
hudakz | 0:fa952828e34c | 111 | wr_cmd8(SSD1306_DISPLAYALLON_RESUME); |
hudakz | 0:fa952828e34c | 112 | |
hudakz | 0:fa952828e34c | 113 | //wr_cmd8(SSD1306_NORMALDISPLAY); |
hudakz | 0:fa952828e34c | 114 | wr_cmd8(SSD1306_INVERTDISPLAY); |
hudakz | 0:fa952828e34c | 115 | |
hudakz | 0:fa952828e34c | 116 | wr_cmd8(SSD1306_DISPLAYON); |
hudakz | 0:fa952828e34c | 117 | } |
hudakz | 0:fa952828e34c | 118 | |
hudakz | 0:fa952828e34c | 119 | //////////////////////////////////////////////////////////////////// |
hudakz | 0:fa952828e34c | 120 | // functions that overrides the standard ones implemented in LCD.cpp |
hudakz | 0:fa952828e34c | 121 | //////////////////////////////////////////////////////////////////// |
hudakz | 0:fa952828e34c | 122 | |
hudakz | 0:fa952828e34c | 123 | void SSD1306::mirrorXY(mirror_t mode) |
hudakz | 0:fa952828e34c | 124 | { |
hudakz | 0:fa952828e34c | 125 | switch (mode) |
hudakz | 0:fa952828e34c | 126 | { |
hudakz | 0:fa952828e34c | 127 | case(NONE): |
hudakz | 0:fa952828e34c | 128 | wr_cmd16(0xA0C0); |
hudakz | 0:fa952828e34c | 129 | break; |
hudakz | 0:fa952828e34c | 130 | case(X): |
hudakz | 0:fa952828e34c | 131 | wr_cmd16(0xA1C0); |
hudakz | 0:fa952828e34c | 132 | break; |
hudakz | 0:fa952828e34c | 133 | case(Y): |
hudakz | 0:fa952828e34c | 134 | wr_cmd16(0xA0C8); |
hudakz | 0:fa952828e34c | 135 | break; |
hudakz | 0:fa952828e34c | 136 | case(XY): |
hudakz | 0:fa952828e34c | 137 | wr_cmd16(0xA1C8); |
hudakz | 0:fa952828e34c | 138 | break; |
hudakz | 0:fa952828e34c | 139 | } |
hudakz | 0:fa952828e34c | 140 | } |
hudakz | 0:fa952828e34c | 141 | |
hudakz | 0:fa952828e34c | 142 | void SSD1306::set_contrast(int o) |
hudakz | 0:fa952828e34c | 143 | { |
hudakz | 0:fa952828e34c | 144 | contrast = o; |
hudakz | 0:fa952828e34c | 145 | |
hudakz | 0:fa952828e34c | 146 | wr_cmd16(0x8100|(o&0xFF)); |
hudakz | 0:fa952828e34c | 147 | } |
hudakz | 0:fa952828e34c | 148 | |
hudakz | 0:fa952828e34c | 149 | //////////////////////////////////////////////////////////////////// |
hudakz | 0:fa952828e34c | 150 | // functions that overrides the standard ones implemented in LCD.cpp |
hudakz | 0:fa952828e34c | 151 | //////////////////////////////////////////////////////////////////// |
hudakz | 0:fa952828e34c | 152 | |
hudakz | 0:fa952828e34c | 153 | |
hudakz | 0:fa952828e34c | 154 | const uint8_t scroll_speed[8]={3,2,1,6,0,5,4,7}; |
hudakz | 0:fa952828e34c | 155 | |
hudakz | 0:fa952828e34c | 156 | //////////////////////////////////////////////////////////////////// |
hudakz | 0:fa952828e34c | 157 | // functions addon to LCD.cpp |
hudakz | 0:fa952828e34c | 158 | //////////////////////////////////////////////////////////////////// |
hudakz | 0:fa952828e34c | 159 | void SSD1306::horizontal_scroll(int l_r,int s_page,int e_page,int speed){ |
hudakz | 0:fa952828e34c | 160 | wr_cmd8(0x2E); // deactivate scroll before change |
hudakz | 0:fa952828e34c | 161 | if(l_r == 1){ |
hudakz | 0:fa952828e34c | 162 | wr_cmd16(0x2700); // horizontal scroll left |
hudakz | 0:fa952828e34c | 163 | } |
hudakz | 0:fa952828e34c | 164 | else { |
hudakz | 0:fa952828e34c | 165 | wr_cmd16(0x2600); |
hudakz | 0:fa952828e34c | 166 | } |
hudakz | 0:fa952828e34c | 167 | wr_cmd16((s_page & 0x07)<<8 | (scroll_speed[speed & 0x07])); |
hudakz | 0:fa952828e34c | 168 | wr_cmd16((e_page & 0x07)<<8 ); |
hudakz | 0:fa952828e34c | 169 | wr_cmd16(0xFF2F); |
hudakz | 0:fa952828e34c | 170 | } |
hudakz | 0:fa952828e34c | 171 | |
hudakz | 0:fa952828e34c | 172 | void SSD1306::horiz_vert_scroll(int l_r,int s_page,int e_page,int v_off,int speed){ |
hudakz | 0:fa952828e34c | 173 | wr_cmd8(0x2E); // deactivate scroll before change |
hudakz | 0:fa952828e34c | 174 | if(l_r == 1){ |
hudakz | 0:fa952828e34c | 175 | wr_cmd16(0x2A00); // horizontal scroll left |
hudakz | 0:fa952828e34c | 176 | } |
hudakz | 0:fa952828e34c | 177 | else { |
hudakz | 0:fa952828e34c | 178 | wr_cmd16(0x2900); |
hudakz | 0:fa952828e34c | 179 | } |
hudakz | 0:fa952828e34c | 180 | wr_cmd16((s_page & 0x07)<<8 | (scroll_speed[speed & 0x07])); |
hudakz | 0:fa952828e34c | 181 | wr_cmd16((e_page & 0x07)<<8 | (v_off & 0x3F) ); |
hudakz | 0:fa952828e34c | 182 | wr_cmd8(0x2F); |
hudakz | 0:fa952828e34c | 183 | |
hudakz | 0:fa952828e34c | 184 | } |
hudakz | 0:fa952828e34c | 185 | |
hudakz | 0:fa952828e34c | 186 | void SSD1306::end_scroll(){ |
hudakz | 0:fa952828e34c | 187 | wr_cmd8(0x2E); |
hudakz | 0:fa952828e34c | 188 | } |