tft display interface microcontroller free sample

The ST7789 TFT is a color display that uses SPI protocol. This display is an IPS display, it comes in different sizes (1.3″, 1.54″ …) but all of them should have the same resolution of 240×240 pixel.

The ST7789 display module shown in project circuit diagram has 7 pins: (from right to left): GND (ground), VCC, SCL (serial clock), SDA (serial data), RES (reset), DC (or D/C: data/command) and BLK (back light).

The ST7789 TFT display works with 3.3V only (power supply and control lines). The display module is supplied with 3.3V that comes from the AMS1117 3V3 voltage regulator, this regulator steps down the 5V into 3.3V (supplies the display controller with regulated 3V3).

To connect the PIC18F46K22 with the display module, I used voltage divider for each line. This means there are 4 voltage dividers. Each voltage divider consists of 2.2k and 3.3k resistors, this drops the 5V into 3V which is sufficient.

If the display module has a CS pin (Chip Select) then it should be connected to the PIC18F46K22 microcontroller through another voltage divider (for example connecting it to pin RD2).

In this project SPI1 module is used with SCK1 on pin RC3 (#18) and SDO1 (MOSI) on pin RC5 (#24). SCK1 and SDO1 pins of the PIC18F46K22 MCU are respectively connected to SCL and SDA pins of the ST7789 display module.

The default connection setting of the mikroC ST7789 TFT library is hardware SPI1 module (SPI1 module must be initialized before initiating the display). Instead of hardware SPI1 module, software SPI or hardware SPI2 module can be used.

If TFT data pin (TFT_DIN) and clock pin (TFT_SCK) are defined in the main code (before #include “ST7789.c”) then the library will automatically use software SPI.

If the display module has a CS pin uncomment its related lines (#define TFT_CS and #define TFT_CS_DIR) and connect it to RD2 pin of the microcontroller through voltage divider.

tft display interface microcontroller free sample

The ILI9341 TFT module contains a display controller with the same name: ILI9341. It’s a color display that uses SPI interface protocol and requires 4 or 5 control pins, it’s low cost and easy to use.

The resolution of this TFT display is 240 x 320 pixel which means it has total of 76800 pixels. This module works with 3.3V only and it doesn’t support 5V (not 5V tolerant).

The ILI9341 TFT display board which is shown in project circuit diagram has 14 pins, the first 9 pins are for the display and the other 5 pins are for the touch module.

So, the display pins are numbered from 1 to 9 (from left to right): VCC (5V), GND (ground), CS (chip select), RST (reset), DC (or D/C: data/command), MOSI (or SDI), SCK (clock), BL (back light LED) and MISO (or SDO).

As mentioned above, the ILI9341 TFT display controller works with 3.3V only (power supply and control lines). The display module is supplied with 5V where GND pin is connected to circuit ground, VCC and BL pins are connected to circuit +5V. This module has a built-in 3.3V regulator which supplies the display controller with 3.3V from the 5V source.

All PIC18F46K22 MCU output pins are 5V, connecting a 5V pin directly to the ILI9341 display board may damage its controller circuit. To avoid that, I used voltage divider for each line which means there are 5 voltage dividers. Each voltage divider consists of 2.2k and 3.3k resistors, this drops the 5V into 3V which is sufficient.

Interfacing PIC18F46K22 MCU with ILI9341 TFT display C code:The following C code is for mikroC PRO for PIC compiler, it was tested with version 7.6.0.

The default connection setting of the mikroC ILI9341 TFT library is hardware SPI1 module (SPI1 module must be initialized before initiating the display). Instead of hardware SPI1 module, software SPI or hardware SPI2 module can be used.

If TFT data pin (TFT_SDI) and clock pin (TFT_SCK) are defined in the main code (before #include “ILI9341.c”) then the library will automatically use software SPI.

As mentioned above, the ILI9341 TFT is connected to PIC18F46K22 microcontroller SPI1 module pins (SCK1 and SDO1). Hardware SPI1 module and the ILI9341 TFT display are initialized as:

tft display interface microcontroller free sample

FocusLCDs.com sent me a free sample of a 4x3” TFT LCD (P/N: E43RG34827LW2M300-R) to try out. This is a color active matrix TFT (Thin Film Transistor) LCD (liquid crystal display) that uses amorphous silicon TFT as a switching device. This model is composed of a Transmissive type TFT-LCD Panel, driver circuit, backlight unit. The resolution of a 4.3” TFT-LCD contains 480x272 pixels, and can display up to 16.7M colors.

For this project, you would need the RA8875 driver board (available at AdaFruit for US$35) to interface the TFT display to the Arduino. It comes with a header which you can solder on as needed.

tft display interface microcontroller free sample

With the increasing popularity of smartphones and tablet computers, touch has become one of the most common user interfaces encountered today. For our final project design we have converted a number of open-source C++ libraries to C in order to interface with an LCD and touch screen via the Atmel ATmega644 microcontroller. In addition to these new libraries we included three pieces of software: a free drawing mode, a game called Yellow which pays homage to the arcade game Pac-Man© developed by Namco in 1980, and the classic pencil and paper game Tic-Tac-Toe. Each piece of software serves to demonstrate some of the many capabilities of the LCD and touch screen combination.

Our goal for the final project was to provide the ECE 4760 course with an easy to setup and cheap touch screen interface to be used with the Atmel ATmega644 microcontroller. While researching ways to implement this, we found the tutorial for an Arduino-based touch screen provided by LadyAda. The resulting C libraries that we created for our device are based upon the open-source C++ libraries provided in the tutorial.

We opted to display images with an LCD screen rather than using a CRT monitor due to the onboard memory of LCD screens. This means that the entire screen does not need to be refreshed when displaying a new image. This severely reduces the amount of overhead required by the CPU to update the screen.

The only hardware used for this project is the protoboard with an ATmega644 and a TFT LCD with resistive touch screen purchased from Adafruit. The LCD is a 2.8" 320x240 pixel resolution screen with an attached resistive touch screen. A built in linear regulator allows the screen to be used with either 5V or 3.3V logic. The wiring was done using a tutorial from LadyAda. The LCD screen has four control lines, eight data lines, a reset pin, a backlight pin, four pins for the touch screen, VCC and ground. VCC is connected to 5V from the MCU and ground is connected to MCU ground. The backlight should always be on, so it is simply connected to VCC. It is possible to use a PWM signal to dim the backlight, but that was not necessary for this project.

The four pins for the resistive touch screen are Y+, Y-, X+, and X-. Y+ is connected to pin A0, X- is connected to A1, X+ is connected to A2, and Y- is connected to A3. The X position on the touch is determined by setting Y+, Y-, and X- to ground and X+ to high. The voltage on Y+ is then read. To get the most accurate position, the 10-bit ADC result from the microcontroller is used. This requires reading the low bits first and then reading the high bits and concatenating the results. The final raw x position returned is the 10-bit ADC result subtracted from 1023. The Y position is determined in a similar way. This time the X+, X-, and Y- pins are grounded and the Y+ pin is set high. The voltage on the X- pin is then read.

The software for this project is based off of the open-source libraries released by Adafruit. There are three libraries: TFTLCD, TouchScreen, and Adafruit_GFX. These libraries are written for Arduino microcontrollers and are in C++. Converting the libraries to C involved removing the classes and converting all of the functions to static functions. The libraries also contained a large number of Arduino specific functions. These functions were manually replaced with code that performs the same functionality, but that works on the ATmega644. The touch screen libraries were initially using PORTC as inputs to the ADC. However, since the input to the ADC on the ATmega644 is PORTA, this had to be changed. To see the mapping of all of the pins, refer to the hardware section. To make it easier for future students to use, the converted libraries are located in separate C files from the programs we wrote. The TFT LCD and Adafruit_GFX libraries have been combined into a single C file. A few additional functions were added to the libraries. These include the map function which is included in the Arduino software package. This function remaps a number from one range to another. Additional drawing functions added include the ability to draw half a circle and the ability to draw strings instead of individual characters. Comments were also added to the header files to allow the user to quickly understand what a function does and what the appropriate inputs are.

To showcase the capabilities of the touch screen and LCD, three simple programs were written. The first is a simple drawing program based off an example provided with the TFT LCD library. The second is a Tic-Tac-Toe game which allows the player to draw X"s on their turn. The final program is Yellow. Yellow is a simple demonstration that draws its inspiration from the popular arcade game Pac-Man©, created by Namco in 1980.

There are twenty-four different colors the user can choose from. These colors are split into four groups of six colors. When the free draw program starts, the first set of colors is displayed. The currently selected color is indicated by having a white border around it. When the user touches a different color, the pen color changes and the white border is moved to the selected color. The right two buttons on the screen allow the user to scroll between different sets of colors. The buttons are indicated by the "<" and ">" characters. Pressing one of these buttons changes the state which will then redraw the colored squares at the top of the screen to represent the colors available in the selected set. The colors in each set are generically defined at the top of the C file in the format SXCY where X represents the color set (0-3) and Y represents the color number in the set (1-6). Changing a particular color in the set is as simple as changing the defined value.

The software for this project stressed the space available on the microcontroller for the program. The program takes up 98.6% of the instruction memory space. Most of this is the AI for Tic-Tac-Toe which is over 3000 lines of code in total.

The screen is capable of displaying great 16-bit colors and offers an excellent resolution of 320x240. The only disappointment with the hardware is the lack of accuracy of the touch screen. If constant pressure is not consistently applied, the touch is not always detected. Playing with the pressure threshold helped, but did not completely fix the issue. Our other complaint is with the speed of the microcontroller. Even with a 20MHz crystal, the ATmega644 was not fast enough to do animation without noticeable flickering. The compiler optimization was required to be OS since we used almost all available space on the microcontroller. Compiling with O0 optimization resulted in over 199% usage of program memory space.

The outcome of our final project met the majority of our expectations. We successfully converted the C++ libraries provided byLadyAda to be used with the ATmega644 and have an easy to setup interface between the microcontroller and LCD/touch screen combination. We were also able to develop three different pieces of software to demonstrate the capabilities of the touch screen.

Given more time, we would have liked to refine the included software demonstrations as our code consumes approximately 98% of the instruction memory in the microcontroller. We could have gone through the code to make it more efficient, reducing the instruction count and allowing us to implement some other ideas.

One expansion that could be explored for our free drawing program is being able to save a user"s drawings to memory. If the microcontroller and touch screen were packaged into a portable, battery-powered device, an easy to carry drawing pad could be created. With the ability to save drawings, the user could then upload them to a computer later where they could be further developed and refined.

While working in the lab we made ourselves available to questions from any other students looking for help, and sought help from them and the TAs when necessary. It is our hope that our project may provide a simple and easy to use touch screen interface for the Atmel ATmega644 microcontrollers for use by future versions of ECE 4760 or by any other interested parties.