ILI9341 Library
Inhaltsverzeichnis
Overview
Features:
- SPI (Soft-/Hardware), 8/16 Bit parallel interface
- ATMega/ATXmega
- Fast assembler-optimized routines
- Backlight control (Digital IO or PWM)
- Screen rotation (4 directions), Standby, Color inversion
- Basic graphics, pictures (fast SRAM-pictures), text
The library has been developed and tested using an XMega-A1 Xplained board with a Spansion S25FL032P (32Mbit) dataflash mounted on it. Credits go to Hkipnik for the fixed font text/BGF picture routines and the alternative init sequence from his ILI9341 lib (in V1.1).
Options
Options and settings need to be set before the $include-statement, every setting has a default value that will be used if not specified otherwise. (in the list below the default values for the ATXMega are shown, values for ATMega are written in brackets () if applicable).
Enables the SPI- instead of the 8-Bit paralell interface
Const Lcd_enable_spi = False
Use software SPI
Const Lcd_use_soft_spi = False
Enables the 16-Bit paralell interface
Const Lcd_enable_16bit = False
Enable backlight control
Const Lcd_enable_backlight = False
Use a timer to generate a PWM signal for the backlight instead of switching it on or off
Const Lcd_enable_backlight_pwm = True
ATMega only: select the timer to use for the PWM signal (XMega: automatic)
Lcd_backlight_timer Alias Timer2
Invert backlight logic (1: off, 0: on)
Const Lcd_invert_backlight = False
Select the port of the display control pins (XMega: HW-SPI & PWM Timer fixed on PortC-PortF, ATMega: HW SPI on PortB, for SW SPI or parallel modes any GPIO will work)
Const Lcd_ctrl_port = Portc (Portb)
8 Bit mode data port
Const Lcd_data_port_1 = Portf (Portc)
16 Bit mode data port #2
Const Lcd_data_port_2 = Portd
Port of the display reset signal
Const Lcd_reset_port = Lcd_ctrl_port
Pin of the display reset signal
Const Lcd_reset_pin = 0
SPI mode pin assignments (HW SPI: fixed, SW SPI: any GPIO)
Const Lcd_pin_dc = 1 (4) Const Lcd_pin_cs = 4 (0) Const Lcd_pin_sdo = 5 (2) Const Lcd_pin_clk = 7 (1)
parallel (8/16 Bit) mode pin assignments
Const Lcd_pin_wr = 1 Const Lcd_pin_rd = 5 Const Lcd_pin_dc = 7 Const Lcd_pin_cs = 4
Port of the backlight control signal
Const Lcd_backlight_port = Lcd_ctrl_port
Pin of the backlight control signal
Const Lcd_pin_backlight = 0 (6)
In case the display shows scanlines, you can try an alternative init sequence:
Const Lcd_alternative_init = False
API
Initializes the control interface (paralell/serial), backlight control and the display
Sub Lcd_init()
Inverts the screen's colors
Sub Lcd_invert(byval Inverted As Byte )
Sleep mode
Indicates if the display is in sleep mode.
Dim Lcd_sleepmode As Bit
Set display in sleep mode / wake up
Sub Lcd_sleep(byval Value As Byte)
Backlight
Sets the backlight value (0: BL Off; 1-254: PWM; 255: BL On. 0 puts the display also in standby mode, any other value reenables it)
Sub Lcd_backlight(byval Value As Byte)
Smoothly dims the backlight (PWM only)
Sub Lcd_backlight_dim(byval Startvalue As Byte , Byval Endvalue As Byte , Byval Millisecs As Word)
Screen rotation
Indicates the current rotation
Dim Lcd_rotation As Byte
the actual max X and Y coordinates (after rotation)
Dim Lcd_screen_width As Word Dim Lcd_screen_height As Word
Sets the display rotation (0: 0° Portrait, 1: 90° Landscape, 2: 180° Portrait, 3: 270° Landscape, 4: 0° ...)
Sub Lcd_set_rotation(byval Rotation As Byte)
Basic graphics
Clears the screen.
Sub Lcd_clear(byval Color As Word)
Draws a rectangle
Sub Lcd_rect(byval X1 As Word , Byval Y1 As Word , Byval X2 As Word , Byval Y2 As Word , Byval Color As Word)
Draws a filled rectangle, checks for the display boundaries
Sub Lcd_fill_rect(byval X1 As Word , Byval Y1 As Word , Byval X2 As Word , Byval Y2 As Word , Byval Color As Word)
Sets a single pixel.
Sub Lcd_set_pixel(byval X As Word , Byval Y As Word , Byval Color As Word)
Draws a line between two points
Sub Lcd_line(byval X1 As Word , Byval Y1 As Word , Byval X2 As Word , Byval Y2 As Word , Byval Color As Word)
Draws a line with color transition between two points
Sub Lcd_line_gradient(byval X1 As Word , Byval Y1 As Word , Byval X2 As Word , Byval Y2 As Word , Byval Color1 As Word , Byval Color2 As Word)
Draws a circle
Sub Lcd_circle(byval Xcenter As Word , Byval Ycenter As Word , Byval Radius As Word , Byval Color As Word)
Draws a filled circle
Sub Lcd_fill_circle(byval X As Word , Byval Y As Word , Byval Radius As Word , Byval Color As Word)
Draws an ellipse
Sub Lcd_ellipse(byval X_center As Word , Byval Y_center As Word , Byval X_radius As Word , Byval Y_radius As Word , Byval Color As Word)
Pictures
Draws a BIN picture from the internal program flash memory. Image has to be loaded with Restore before.
Sub Lcd_pic_flash(byval Xs As Word , Byval Ys As Word , Byval Width As Byte , Byval Height As Word)
Draws a BIN picture stored on an external SPI DataFlash [1]
Sub Lcd_pic_spi(byval Xs As Word , Byval Ys As Word , Byval Width As Word , Byval Height As Word , Byval Address As Dword)
Set to the start address of the free SRAM memory section, where BIN pics should be loaded to
Dim Lcd_ram_pics_free_ptr As Dword
Loads a BIN picture from the program flash into the SRAM for faster access. Data will be loaded starting at Lcd_ram_pics_free_ptr, the function returns the start address of the actual picture.
Function Lcd_load_pic_flash2ram(byval Width As Word , Byval Height As Word) As Word
Draws a BIN picture from the SRAM memory.
Sub Lcd_pic_ram(byval Xs As Word , Byval Ys As Word , Byval Width As Word , Byval Height As Word , Byval Address As Dword)
Draws a BGF coded picture from the program flash memory.
Sub Lcd_pic_bgf(byval Xoffset As Word , Byval Yoffset As Word)
Text
For reduced Digit font
Dim Lcd_digit_font As Bit
Normal/Color font format
Dim Lcd_color_font As Bit
Loads the font, following Lcd_text commands will use it. You need to call Restore [Font] before.
Sub Lcd_loadfont()
Draws a fixed-font text on the screen.
Sub Lcd_text(byval S As String , Byval Xoffset As Word , Byval Yoffset As Word , Byval Forecolor As Word , Byval Backcolor As Word)
Sample
$regfile = "m128def.dat" '$regfile = "xm128a1def.dat" $hwstack = 64 $swstack = 64 $framesize = 64 Config Submode = New Const False = 0 Const True = 1 #if _xmega = True $crystal = 32000000 $include "XMegaPLL.inc" #else $crystal = 16000000 #endif 'Const Lcd_enable_spi = True 'Const Lcd_use_soft_spi = True Const Lcd_enable_16bit = True $include "ILI9341.inc" $include "ProportionalFont.inc" Sub Random_color() R = Rnd(255) G = Rnd(255) B = Rnd(255) Color = Color24to16(r , G , B) End Sub Dim Pixels As Word Dim R As Byte , G As Byte , B As Byte Dim X As Word , Y As Word , Color As Word Dim X2 As Word , Y2 As Word , Radius As Word Dim Statustext As String * 30 Restore Color8x8 Lcd_color_font = True Lcd_loadfont Restore Timesnewroman_10pt Lcd_tt_loadfont Enable Interrupts Lcd_init Do ' =========== Clear Screen ============ For Pixels = 1 To 99 Random_color Lcd_clear Color Next Waitms 3000 Lcd_clear &H0000 ' =========== Gradient Fill (Color16 functions) ============ R = 0 G = 0 B = 0 X2 = 0 Color = 0 Y = 0 For Pixels = 0 To 512 Radius = Color Random_color For G = 0 To 32 X2 = Color16alpha(color , Radius , G) Lcd_fill_rect 0 , Y , Lcd_screen_width , Y , X2 Y = Y + 1 If Y = Lcd_height Then Y = 0 Next Next Waitms 3000 Lcd_clear &H0000 ' =========== Pixels ============ For Pixels = 1 To 65535 Random_color X = Rnd(lcd_screen_width) Y = Rnd(lcd_screen_height) Lcd_set_pixel X , Y , Color Next Waitms 3000 Lcd_clear &H0000 ' =========== Lines ============ For Pixels = 1 To 3000 Random_color X = Rnd(lcd_screen_width) Y = Rnd(lcd_screen_height) X2 = Rnd(lcd_screen_width) Y2 = Rnd(lcd_screen_height) Lcd_line X , Y , X2 , Y2 , Color Next Waitms 3000 Lcd_clear &H0000 ' =========== Boxes ============ For Pixels = 1 To 10000 Random_color X = Rnd(lcd_screen_width) Y = Rnd(lcd_screen_height) X2 = Rnd(lcd_screen_width) Y2 = Rnd(lcd_screen_height) Lcd_rect X , Y , X2 , Y2 , Color Next Waitms 3000 Lcd_clear &H0000 ' =========== Circles ============ For Pixels = 1 To 1000 Random_color Y = Rnd(lcd_screen_width) Y2 = Rnd(lcd_screen_height) Radius = Rnd(200) Lcd_circle Y , Y2 , Radius , Color Next Waitms 3000 Lcd_clear &H0000 ' =========== Filled Circles ============ For Pixels = 1 To 50 Random_color Y = Rnd(lcd_screen_width) Y2 = Rnd(lcd_screen_height) Radius = Rnd(80) Lcd_fill_circle Y , Y2 , Radius , Color Next Waitms 3000 Lcd_clear &H0000 ' =========== Flash BIN Pics ============ For Pixels = 1 To 50 X = Rnd(80) Y = Rnd(200) Restore Coffee Lcd_pic_flash X , Y , 160 , 120 Next Waitms 3000 Lcd_clear &H0000 ' =========== Flash BGF Pics ============ For Pixels = 1 To 50 X = Rnd(108) Y = Rnd(188) Restore Animals Lcd_pic_bgf X , Y Next Waitms 3000 Lcd_clear &H0000 ' =========== Fixed Font Text ============ For Pixels = 1 To 1000 Random_color X = Rnd(220) Y = Rnd(310) Lcd_text "Text" , X , Y , Color , Color_black Next Waitms 3000 Lcd_clear &H0000 ' =========== Proportional Font Text ============ For Pixels = 1 To 1000 Random_color X = Rnd(220) Y = Rnd(310) Lcd_tt_text "Text" , X , Y , Color Next Waitms 3000 Lcd_clear &H0000 ' =========== Rotation ============ For Pixels = 0 To 4 Lcd_set_rotation Pixels Lcd_clear Color_white Y = Lcd_screen_height - 20 Lcd_fill_rect 0 , Y , Lcd_screen_width , Lcd_screen_height , Color_black Y = Y + 4 Statustext = "Rotation " + Str(lcd_rotation) Lcd_tt_text Statustext , 10 , Y , Color_white X = Lcd_screen_width - 80 X = X / 2 Y = Lcd_screen_height - 155 Y = Y / 2 Restore Hand Lcd_pic_flash X , Y , 80 , 155 Waitms 1500 Next ' =========== Backlight Dimming ============ X = 0 Lcd_clear Color_white Y = Lcd_screen_height - 20 Lcd_fill_rect X , Y , X2 , Y2 , Color_black Y = Y + 4 Lcd_tt_text "Backlight dimming" , 10 , Y , Color_white Lcd_backlight_dim 255 , 1 , 1500 ' dim to 0 would enable the screen's standby mode Waitms 200 Lcd_backlight_dim 1 , 255 , 1500 ' =========== Screen Standby ============ Lcd_clear Color_black Y = Lcd_screen_height - 16 Lcd_tt_text "Standby" , 10 , Y , Color_white Waitms 2000 Lcd_backlight 0 Waitms 1000 Lcd_backlight 255 Waitms 1000 ' =========== Invert ============ Y = Lcd_screen_height - 20 Lcd_fill_rect X , Y , X2 , Y2 , Color_black Y = Y + 4 Lcd_tt_text "Invert" , 10 , Y , Color_white Waitms 1000 Lcd_invert True Waitms 3000 Lcd_invert False Waitms 1000 Loop End $include "data\color8x8.font" $include "data\TimesNewRoman_10pt.inc" $inc Coffee , Nosize , "data\Coffee.bin" $inc Hand , Nosize , "data\Hand.bin" Animals: $bgf "data\Animals.bgc"
Sample project from the video
The complete project, from which the video has been made, is included in the download. It is based on a XMega XPlained-A1 development board with a Spansion S25FL032P 4Mb DataFlash mounted. The TFT display (MikroE TFT Proto) is mounted on a strip board together with the backlight and touch screen components.
The connections on the front side of the XPlained. On J4 all control signals are connected, the parallel data lines are located on J1 resp. J3, the touch screen is located on J2 (T1..3 are the outputs of the OpAmp).
For 16-Bit mode, two additional connections on the back side the the XPlained have to be added to get an additional 8-bit output port. A drop of instant adhesive holds the connector in place.
Stripboard example
Electrical connection
Change log
1.1:
- Lcd_color_font toggles between normal/color font format, fixes wrong displayed fixed fonts
- Compatibility bug fixes
- Added alternative init sequence for displays showing scanlines, enabled by Lcd_alternative_init
- Fixed bug in HW SPI waitloops
- Updated for Bascom 2.0.8.0
Download
Old versions: