ILI9341 Library: Unterschied zwischen den Versionen

Aus ProjectWiki
Wechseln zu:Navigation, Suche
K (Created page with "== Overview == == Options == <pre>' Enables the SPI- instead of the 8-Bit paralell interface #if Not Varexist( "Lcd_enable_spi") Const Lcd_enable_spi = False #endif ' Use s...")
 
K (Overview)
 
(14 dazwischenliegende Versionen desselben Benutzers werden nicht angezeigt)
Zeile 1: Zeile 1:
 
== Overview ==
 
== Overview ==
 +
New version - 1.1 (Aug. 2017)
  
 +
[http://www.youtube.com/watch?v=eGcUwT6J5nE Demo Video]
 +
 +
[http://www.mcselec.com/index2.php?option=com_forum&Itemid=59&page=viewtopic&t=12893 Forum thread]
 +
 +
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 ==
<pre>' Enables the SPI- instead of the 8-Bit paralell interface
+
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).
#if Not Varexist( "Lcd_enable_spi")
+
 
  Const Lcd_enable_spi = False
+
Enables the SPI- instead of the 8-Bit paralell interface
#endif
+
<pre>Const Lcd_enable_spi = False</pre>
 +
 
 +
 
 +
Use software SPI
 +
<pre>Const Lcd_use_soft_spi = False</pre>
 +
 
 +
 
 +
Enables the 16-Bit paralell interface
 +
<pre>Const Lcd_enable_16bit = False</pre>
 +
 
 +
 
 +
Enable backlight control
 +
<pre>Const Lcd_enable_backlight = False</pre>
 +
 
 +
 
 +
Use a timer to generate a PWM signal for the backlight instead of switching it on or off
 +
<pre>Const Lcd_enable_backlight_pwm = True</pre>
 +
 
 +
 
 +
ATMega only: select the timer to use for the PWM signal (XMega: automatic)
 +
<pre>Lcd_backlight_timer Alias Timer2</pre>
 +
 
 +
 
 +
Invert backlight logic (1: off, 0: on)
 +
<pre>Const Lcd_invert_backlight = False</pre>
 +
 
 +
 
 +
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)
 +
<pre>Const Lcd_ctrl_port = Portc (Portb)</pre>
 +
 
 +
 
 +
8 Bit mode data port
 +
<pre>Const Lcd_data_port_1 = Portf (Portc)</pre>
 +
 
 +
 
 +
16 Bit mode data port #2
 +
<pre>Const Lcd_data_port_2 = Portd</pre>
 +
 
  
' Use software SPI
+
Port of the display reset signal
#if Not Varexist( "Lcd_use_soft_spi") And Lcd_enable_spi = True
+
<pre>Const Lcd_reset_port = Lcd_ctrl_port</pre>
  Const Lcd_use_soft_spi = False
 
#endif
 
  
' Enables the 16-Bit paralell interface
 
#if Not Varexist( "Lcd_enable_16bit")
 
  Const Lcd_enable_16bit = False
 
#endif
 
  
' Enable backlight control
+
Pin of the display reset signal
#if Not Varexist( "Lcd_enable_backlight")
+
<pre>Const Lcd_reset_pin = 0</pre>
  Const Lcd_enable_backlight = False
 
#endif
 
  
' Use a timer to generate a PWM signal for the backlight instead of switching it on or off
 
#if Not Varexist( "Lcd_enable_backlight_pwm")
 
  Const Lcd_enable_backlight_pwm = True
 
#endif
 
  
' ATMega only: select the timer to use for the PWM signal (XMega: automatic)
+
SPI mode pin assignments (HW SPI: fixed, SW SPI: any GPIO)
#if Not Varexist( "Lcd_backlight_timer") And Lcd_enable_backlight_pwm = True And _xmega = False
+
<pre>Const Lcd_pin_dc = 1 (4)
  Lcd_backlight_timer Alias Timer2
+
Const Lcd_pin_cs = 4 (0)
#endif
+
Const Lcd_pin_sdo = 5 (2)
 +
Const Lcd_pin_clk = 7 (1)</pre>
  
' invert backlight logic (1: off, 0: on)
 
#if Not Varexist( "Lcd_invert_backlight") And(lcd_enable_backlight = True Or Lcd_enable_backlight_pwm = True)
 
  Const Lcd_invert_backlight = False
 
#endif
 
  
' Select the port of the display control pins (XMega: PortC-PortF, ATMega: for HW SPI)
+
parallel (8/16 Bit) mode pin assignments
#if Not Varexist( "Lcd_ctrl_port")
+
<pre>Const Lcd_pin_wr = 1
  #if _xmega = True
+
Const Lcd_pin_rd = 5
      Const Lcd_ctrl_port = Portc
+
Const Lcd_pin_dc = 7
  #else
+
Const Lcd_pin_cs = 4</pre>
      Const Lcd_ctrl_port = Portb
 
  #endif
 
#endif
 
  
' 8 Bit mode data port
 
#if Not Varexist( "Lcd_data_port_1") And Lcd_enable_spi = False
 
  #if _xmega = True
 
      Const Lcd_data_port_1 = Portf
 
  #else
 
      Const Lcd_data_port_1 = Portc
 
  #endif
 
#endif
 
  
' 16 Bit mode data port #2
+
Port of the backlight control signal
#if Not Varexist( "Lcd_data_port_2") And Lcd_enable_16bit = True
+
<pre>Const Lcd_backlight_port = Lcd_ctrl_port</pre>
  Const Lcd_data_port_2 = Portd
 
#endif
 
  
' Port where the display reset is connected to
 
#if Not Varexist( "Lcd_reset_port")
 
  Const Lcd_reset_port = Lcd_ctrl_port
 
#endif
 
  
' Pin of the display reset signal
+
Pin of the backlight control signal
#if Not Varexist( "Lcd_reset_pin")
+
<pre>Const Lcd_pin_backlight = 0 (6)</pre>
  Const Lcd_reset_pin = 0
 
#endif
 
  
#if Lcd_enable_spi = True
 
  ' SPI mode pin assignments
 
  #if Not Varexist( "Lcd_pin_dc")
 
      #if _xmega = True
 
        Const Lcd_pin_dc = 1
 
      #else
 
        Const Lcd_pin_dc = 4
 
      #endif
 
  #endif
 
  ' (HW: fixed, SW: select any)
 
  #if Not Varexist( "Lcd_pin_cs")
 
      #if _xmega = True
 
        Const Lcd_pin_cs = 4
 
      #else
 
        Const Lcd_pin_cs = 0
 
      #endif
 
  #endif
 
  #if Not Varexist( "Lcd_pin_sdo")
 
      #if _xmega = True
 
        Const Lcd_pin_sdo = 5
 
      #else
 
        Const Lcd_pin_sdo = 2
 
      #endif
 
  #endif
 
  #if Not Varexist( "Lcd_pin_clk")
 
      #if _xmega = True
 
        Const Lcd_pin_clk = 7
 
      #else
 
        Const Lcd_pin_clk = 1
 
      #endif
 
  #endif
 
'  Const Lcd_pin_sdi = 6
 
#else
 
  ' parallel (8/16 Bit) mode pin assignments
 
  #if Not Varexist( "Lcd_pin_wr")
 
      Const Lcd_pin_wr = 1
 
  #endif
 
  #if Not Varexist( "Lcd_pin_rd")
 
      Const Lcd_pin_rd = 5
 
  #endif
 
  #if Not Varexist( "Lcd_pin_dc")
 
      Const Lcd_pin_dc = 7
 
  #endif
 
  #if Not Varexist( "Lcd_pin_cs")
 
      Const Lcd_pin_cs = 4
 
  #endif
 
#endif
 
  
' Port of the backlight control signal
+
In case the display shows scanlines, you can try an alternative init sequence:
#if Not Varexist( "Lcd_backlight_port") And(lcd_enable_backlight = True Or Lcd_enable_backlight_pwm = True)
+
<pre>Const Lcd_alternative_init = False</pre>
  Const Lcd_backlight_port = Lcd_ctrl_port
 
#endif
 
  
' Pin of the backlight control signal
 
#if Not Varexist( "Lcd_pin_backlight" ) And(lcd_enable_backlight = True Or Lcd_enable_backlight_pwm = True)
 
  #if _xmega = True
 
      Const Lcd_pin_backlight = 0
 
  #else
 
      Const Lcd_pin_backlight = 6
 
  #endif
 
#endif</pre>
 
  
 
== API ==
 
== API ==
<pre>Dim Lcd_screen_width As Word                                                    ' the actual max X and Y coordinates (after rotation)
+
Initializes the control interface (paralell/serial), backlight control and the display
Dim Lcd_screen_height As Word</pre>
+
<pre>Sub Lcd_init()</pre>
 +
 
  
<pre>Dim Lcd_digit_font As Bit                                                      'For reduced Digit font</pre>
+
Inverts the screen's colors
 +
<pre>Sub Lcd_invert(byval Inverted As Byte )</pre>
  
<pre>' Initializes the control interface (paralell/serial), backlight control and the display
 
Sub Lcd_init()</pre>
 
  
 +
=== Sleep mode ===
 +
Indicates if the display is in sleep mode.
 
<pre>Dim Lcd_sleepmode As Bit</pre>
 
<pre>Dim Lcd_sleepmode As Bit</pre>
  
<pre>Sub Lcd_backlight(byval Value As Byte) </pre>
 
  
Sets the backlight value (0: Off, Standby On; 1-254: PWM, 255: On, Display Wakeup)
+
Set display in sleep mode / wake up
 +
<pre>Sub Lcd_sleep(byval Value As Byte)</pre>
 +
 
 +
 
 +
=== 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)
 +
<pre>Sub Lcd_backlight(byval Value As Byte)</pre>
 +
 
 +
 
 +
Smoothly dims the backlight (PWM only)
 
<pre>Sub Lcd_backlight_dim(byval Startvalue As Byte , Byval Endvalue As Byte , Byval Millisecs As Word)</pre>
 
<pre>Sub Lcd_backlight_dim(byval Startvalue As Byte , Byval Endvalue As Byte , Byval Millisecs As Word)</pre>
  
  
 +
=== Screen rotation ===
 +
Indicates the current rotation
 
<pre>Dim Lcd_rotation As Byte</pre>
 
<pre>Dim Lcd_rotation As Byte</pre>
  
  
 +
the actual max X and Y coordinates (after rotation)
 +
<pre>Dim Lcd_screen_width As Word
 +
Dim Lcd_screen_height As Word</pre>
 +
 +
 +
Sets the display rotation (0: 0° Portrait, 1: 90° Landscape, 2: 180° Portrait, 3: 270° Landscape, 4: 0° ...)
 
<pre>Sub Lcd_set_rotation(byval Rotation As Byte)</pre>
 
<pre>Sub Lcd_set_rotation(byval Rotation As Byte)</pre>
  
  
 +
=== Basic graphics ===
 +
Clears the screen.
 +
<pre>Sub Lcd_clear(byval Color As Word) </pre>
 +
 +
 +
Draws a rectangle
 +
<pre>Sub Lcd_rect(byval X1 As Word , Byval Y1 As Word , Byval X2 As Word , Byval Y2 As Word , Byval Color As Word)</pre>
 +
 +
 +
Draws a filled rectangle, checks for the display boundaries
 
<pre>Sub Lcd_fill_rect(byval X1 As Word , Byval Y1 As Word , Byval X2 As Word , Byval Y2 As Word , Byval Color As Word)</pre>
 
<pre>Sub Lcd_fill_rect(byval X1 As Word , Byval Y1 As Word , Byval X2 As Word , Byval Y2 As Word , Byval Color As Word)</pre>
  
  
 +
Sets a single pixel.
 
<pre>Sub Lcd_set_pixel(byval X As Word , Byval Y As Word , Byval Color As Word)</pre>
 
<pre>Sub Lcd_set_pixel(byval X As Word , Byval Y As Word , Byval Color As Word)</pre>
  
  
 +
Draws a line between two points
 +
<pre>Sub Lcd_line(byval X1 As Word , Byval Y1 As Word , Byval X2 As Word , Byval Y2 As Word , Byval Color As Word)</pre>
 +
 +
 +
Draws a line with color transition between two points
 +
<pre>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)</pre>
 +
 +
 +
Draws a circle
 +
<pre>Sub Lcd_circle(byval Xcenter As Word , Byval Ycenter As Word , Byval Radius As Word , Byval Color As Word)</pre>
 +
 +
 +
Draws a filled circle
 +
<pre>Sub Lcd_fill_circle(byval X As Word , Byval Y As Word , Byval Radius As Word , Byval Color As Word)</pre>
 +
 +
 +
Draws an ellipse
 +
<pre>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)</pre>
 +
 +
=== Pictures ===
 +
Draws a BIN picture from the internal program flash memory. Image has to be loaded with ''Restore'' before.
 
<pre>Sub Lcd_pic_flash(byval Xs As Word , Byval Ys As Word , Byval Width As Byte , Byval Height As Word)</pre>
 
<pre>Sub Lcd_pic_flash(byval Xs As Word , Byval Ys As Word , Byval Width As Byte , Byval Height As Word)</pre>
  
  
 +
Draws a BIN picture stored on an external SPI DataFlash [http://mat.midlight.eu/wiki/index.php/SPI_DataFlash_Library]
 
<pre>Sub Lcd_pic_spi(byval Xs As Word , Byval Ys As Word , Byval Width As Word , Byval Height As Word , Byval Address As Dword)</pre>
 
<pre>Sub Lcd_pic_spi(byval Xs As Word , Byval Ys As Word , Byval Width As Word , Byval Height As Word , Byval Address As Dword)</pre>
  
  
 +
Set to the start address of the free SRAM memory section, where BIN pics should be loaded to
 
<pre>Dim Lcd_ram_pics_free_ptr As Dword</pre>
 
<pre>Dim Lcd_ram_pics_free_ptr As Dword</pre>
  
  
 +
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.
 
<pre>Function Lcd_load_pic_flash2ram(byval Width As Word , Byval Height As Word) As Word</pre>
 
<pre>Function Lcd_load_pic_flash2ram(byval Width As Word , Byval Height As Word) As Word</pre>
  
  
 +
Draws a BIN picture from the SRAM memory.
 
<pre>Sub Lcd_pic_ram(byval Xs As Word , Byval Ys As Word , Byval Width As Word , Byval Height As Word , Byval Address As Dword)</pre>
 
<pre>Sub Lcd_pic_ram(byval Xs As Word , Byval Ys As Word , Byval Width As Word , Byval Height As Word , Byval Address As Dword)</pre>
  
  
 +
Draws a BGF coded picture from the program flash memory.
 
<pre>Sub Lcd_pic_bgf(byval Xoffset As Word , Byval Yoffset As Word)</pre>
 
<pre>Sub Lcd_pic_bgf(byval Xoffset As Word , Byval Yoffset As Word)</pre>
  
  
<pre>Sub Lcd_clear(byval Color As Word) </pre>
+
=== Text ===
 +
For reduced Digit font
 +
<pre>Dim Lcd_digit_font As Bit</pre>
 +
 
 +
 
 +
Normal/Color font format
 +
<pre>Dim Lcd_color_font As Bit</pre>
  
  
<pre>Sub Lcd_invert(byval Inverted As Byte )</pre>
+
Loads the font, following ''Lcd_text'' commands will use it. You need to call ''Restore [Font]'' before.
 +
<pre>Sub Lcd_loadfont()</pre>
  
  
 +
Draws a fixed-font text on the screen.
 
<pre>Sub Lcd_text(byval S As String , Byval Xoffset As Word , Byval Yoffset As Word , Byval Forecolor As Word , Byval Backcolor As Word)</pre>
 
<pre>Sub Lcd_text(byval S As String , Byval Xoffset As Word , Byval Yoffset As Word , Byval Forecolor As Word , Byval Backcolor As Word)</pre>
  
  
<pre>Sub Lcd_line(byval X1 As Word , Byval Y1 As Word , Byval X2 As Word , Byval Y2 As Word , Byval Color As Word)</pre>
+
== Sample ==
 +
<pre>$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"</pre>
  
<pre>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)</pre>
 
  
 +
== 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 ([http://www.mikroe.com/add-on-boards/display/tft-proto/ MikroE TFT Proto]) is mounted on a strip board together with the backlight and touch screen components.
  
<pre>Sub Lcd_circle(byval Xcenter As Word , Byval Ycenter As Word , Byval Radius As Word , Byval Color As Word)</pre>
 
  
 +
[[File:TFT-XPlained-Frontside.jpg|width|600px|XPlained-A1 connection overview (Front side)]]
  
<pre>Sub Lcd_fill_circle(byval X As Word , Byval Y As Word , Byval Radius As Word , Byval Color As Word)</pre>
+
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).
  
  
<pre>Sub Lcd_rect(byval X1 As Word , Byval Y1 As Word , Byval X2 As Word , Byval Y2 As Word , Byval Color As Word)</pre>
+
[[File:TFT-XPlained-Backside.jpg|width|600px|XPlained-A1 connection overview (Back side, only for 16 Bit mode)]]
  
 +
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.
  
== Sample ==
 
  
 +
[[File:TFT-XPlained-Boardconstruction.jpg|width|600px|XPlained-A1 TFT-Dev board]]
  
===
+
Stripboard example
  
  
Zeile 213: Zeile 472:
 
[[File:lcd-connections.jpg|width|600px|LCD connection schematic]]
 
[[File:lcd-connections.jpg|width|600px|LCD connection schematic]]
  
=== ATXMega-A1 Xplained connections ===
 
  
 +
== 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
 +
* Added bresenham ellipse function
 +
 +
== Download ==
  
== Donate ==
+
* [http://www.braunecker.at/downloads/ili9341/ili9341.zip ILI9341 TFT Display Driver Library 1.1]
This Software is [http://en.wikipedia.org/wiki/Donationware Donationware].
 
<paypal>1</paypal>
 
[[About|See here]] for more informations
 
  
  
== Download ==
+
Old versions:
* [http://www.braunecker.at/downloads/ili9341/ili9341.zip ILI9341 TFT Display Driver Library 1.0]
+
* [http://www.braunecker.at/downloads/ili9341/ili9341-1-0.zip ILI9341 TFT Display Driver Library 1.0]

Aktuelle Version vom 30. August 2017, 21:50 Uhr

Overview

New version - 1.1 (Aug. 2017)

Demo Video

Forum thread

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.


XPlained-A1 connection overview (Front side)

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).


XPlained-A1 connection overview (Back side, only for 16 Bit mode)

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.


XPlained-A1 TFT-Dev board

Stripboard example


Electrical connection

LCD connection schematic


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
  • Added bresenham ellipse function

Download


Old versions: