ILI9340 Library

1. Overview

The library allows Ansteron Board to control TFT LCD modules that have ILI9340 controller. The interface is based on SPI bus with one additional signal line. There are also some basic graphic functions as well as raster font renderer.

There are two raster fonts 5x7 and 12x16 come with the library. Other fonts can be added by user.

Another small library name "bmp_loader" can be used with this library to load bitmap from SD card and plot onto LCD module.

2. Circuit

These LCD modules usually operate at 2.8V to 3.3V while Ansteron Board runs at 5V. Voltage translators or voltage dividers are required for the interface. Most modules can be purchased on a carrier breakout with built-in power regulator and signal voltage translator. Some may have an optional micro SD card connector as well.

Note: One pull-up resistor is needed on /SS signal if the module doesn't have one built-in. SPI bus is also used for downloading program.

3. Example

In this example, Ansteron Board will setup LCD module and draw "Hello World" to the top-left corner. The module is 240x320 pixels. Font data is attached into program memory right after program code.

Library file and font data are added in Compiler setting dialog:

4. Functions

setup_ILI9340(cs_pin,dc_pin);

Initialize ILI9340 controller connected to SPI bus. Input cs_pin and dc_pin take values similar to other IO functions (PIN_Bx, PIN_Cx, PIN_Dx). Those are pins that /SS signal and DC signal connected. This function must be called before other functions of the library can work. Note that function does not return any value. Detection of controller present is not available.

set_raster_font(width,height,data_offset,r,g,b);

Apply font settings for renderer. Inputs width and height are the size of font data per character (in pixel). Note that height must be the multiple of 8. data_offset is the offset of font data in data attachment file. Color values r, g, and b are for background color. Font data structures are described below.

raster_char(x,y,c,r,g,b);

Draw character "c" with color given by r, g, b inputs at (x,y) position. Starting position is top-left corner of the character.

raster_string(x,y,string,r,g,b);

Draw a string starting from (x,y) position with color given by r, g, and b. Starting position is top-left corner of the first character.

set_pixel(x,y,r,g,b);

Set color of pixel at (x,y). Color values r, g, and b range from 0 to 255.

line(x1,y1,x2,y2,r,g,b);

Draw a line from (x1,y1) to (x2,y2) with color given by r, g, and b.

horizontal_line(x,y,length,r,g,b);

Draw horizontal line with given length starting from (x,y) position. Inputs r, g, and b are color values for the line.

vertical_line(x,y,length,r,g,b);

Draw vertical line with given length starting from (x,y) position. Inputs r, g, and b are color values for the line.

rectangle(x,y,w,h,r,g,b);

Draw a rectangle of w width, h height with top-left corner at position (x,y) and color given by r, g, and b.

filled_rectangle(x,y,w,h,r,g,b);

Draw filled rectangle of w width, h height with top-left corner at position (x,y) and color given by r, g, and b.

transfer_bitmap(x,y,w,h,buffer);

Send draw bitmap data to display. Data will be placed in a window given by x, y, w, and h. Each pixel takes 3 bytes with blue-green-red data order. The entire buffer is arranged by h number of lines, each line has w number of pixels.

5. Raster font data

Font data is stored on flat memory, each byte represent a vertical line of 8 pixels. Bits with value equal 1 will be rendered with character color; zero bits will be rendered with given background color.

The format of font data for each character is illustrated as below. The layout is number of byte per column by number of columns per character. Below is 2x16 font data, 2 columns, 16 pixels per column. Every two bytes hold data for a column.

Font data only contains 95 printable characters according to ASCII table. The first character in font data is space (0x20).

6. Loading bitmap from SD card

A small library can be added on top of ILI9340 library and SD_card Library to load bitmap image on to the LCD. All bitmaps are in 24bit .BMP file format and their dimensions must be known to the program.

A buffer is needed to temporally store data as it is being transferred from SD card to LCD. The buffer must be big enough to store 1 line of the bitmap. For example, 128x128 pixels bitmap will need a buffer at least 128x3=384 bytes to be loaded. There is only one function:

load_bmp_file(file_name,buffer,x,y,w,h);

Input file_name is the name of file (a full path if file is located in sub folder). The bitmap will be placed in a window at given x, y position and sizes.

The bitmap will not be loaded properly if given dimensions are incorrect. BMP files must be in standard format (24 bit) as Microsoft Windows BMP files.

7. Note

Ansteron Board does not have available memory to store a copy of data on the display, modify current data is not possible unless data is known to the program.

SPI interface between Ansteron Board and ILI9340 is set to run at 8MHz (CPU clock/2) by default.