Display TFT ST7789 (OshonSoft Basic).

DogFlu66

Member
I leave the library to use ST7789 TFT display, I have translated one that is in C language. But unfortunately I have not been able to get it to work yet. I leave the files in case someone with more experience with these displays can review it.
 

Attachments

  • Main_ST7789.bas
    1.5 KB · Views: 168
  • ST7789_240X240.bas
    15 KB · Views: 152
  • _SPI_Library.bas
    6 KB · Views: 161
  • _Pic18F26K22Library.bas
    46.4 KB · Views: 149
  • _SetUpAmicus18.bas
    2.9 KB · Views: 140
Right!! I can draw characters to the screen... I have used that lookup function ( quite a few times )

I can print the words with forecolour and a backcolour... Double sized printing.. ie four pixels for each bit.

Code:
'Fuses definition.

#define CONFIG1L = 0x00
#define CONFIG1H = 0x38
#define CONFIG2L = 0x1E
#define CONFIG2H = 0x3C
#define CONFIG3L = 0x00
#define CONFIG3H = 0x3D
#define CONFIG4L = 0x80
#define CONFIG4H = 0x00
#define CONFIG5L = 0x0F
#define CONFIG5H = 0xC0
#define CONFIG6L = 0x0F
#define CONFIG6H = 0xE0
#define CONFIG7L = 0x0F
#define CONFIG7H = 0x40

#define CLOCK_FREQUENCY = 64  'Clock 64Mhz
#define STRING_MAX_LENGTH = 60

'#define SIMULATION_WAITMS_VALUE = 1
'************************************************


'*****************************************************
'lcd driver

Const ILI9341_NOP = 0x00
Const ILI9341_SWRESET = 0x01
Const ILI9341_RDDID = 0x04
Const ILI9341_RDDST = 0x09

Const ILI9341_SLPIN = 0x10
Const ILI9341_SLPOUT = 0x11
Const ILI9341_PTLON = 0x12
Const ILI9341_NORON = 0x13

Const ILI9341_INVOFF = 0x20
Const ILI9341_INVON = 0x21
Const ILI9341_DISPOFF = 0x28
Const ILI9341_DISPON = 0x29
Const ILI9341_CASET = 0x2a
Const ILI9341_RASET = 0x2b
Const ILI9341_RAMWR = 0x2c
Const ILI9341_RAMRD = 0x2e

Const ILI9341_PTLAR = 0x30
Const ILI9341_COLMOD = 0x3a
Const ILI9341_MADCTL = 0x36
Const ILI9341_DISP_FUNC = 0xB6


Const ILI9341_MADCTL_MY = 0x80
Const ILI9341_MADCTL_MX = 0x40
Const ILI9341_MADCTL_MV = 0x20
Const ILI9341_MADCTL_ML = 0x10
Const ILI9341_MADCTL_RGB = 0x00

Const ILI9341_RDID1 = 0xda
Const ILI9341_RDID2 = 0xdb
Const ILI9341_RDID3 = 0xdc
Const ILI9341_RDID4 = 0xdd


Const LCD_BLACK = 0x0000
Const LCD_RED = 0x001f
Const LCD_BLUE = 0xf800
Const LCD_GREEN = 0x07E0
Const LCD_YELLOW = 0x07ff
Const LCD_MAGENTA = 0xf81f
Const LCD_CYAN = 0xffe0
Const LCD_WHITE = 0xffff

Dim LCD_width As Word  ///< Display width as modified by current rotation
Dim LCD_height As Word  ///< Display height as modified by current rotation

Dim LCD_rotation As Byte  ///< Display rotation (0 thru 3)

Symbol TFT_RST = PORTC.0  'RST RESET pin
Symbol TFT_DC = PORTC.1  'DC (SS) Data/Command pin
Symbol TFT_CS = PORTC.2  'CS Chip Select pin
Symbol TFT_SCK = PORTC.3  'SCK as output
Symbol TFT_SDI = PORTC.4  'SDI as input -> SDO
Symbol TFT_SDO = PORTC.5  'SDO as output -> SDI
'*************************************************
Dim str As String
Dim x As Byte
OSCCON = 0x70

OSCTUNE.PLLEN = 1
TRISA.1 = 0
TRISC = 0
ADCON1 = 15
Call SPI_init()
Call LCD_init()

str = "MAXIMUM!"
Main:

    x = 1
    While x = 1
    WaitMs 300
    LATA.1 = 1
    Call fillScreen(LCD_RED)

    WaitMs 300
    LATA.1 = 0
    Call fillwindow(10,10,310,230, LCD_BLUE)
    WaitMs 300
    LATA.1 = 1
    Call fillwindow(10,10,310,230, LCD_GREEN)
    WaitMs 300
    LATA.1 = 0
    Call fillScreen(LCD_WHITE)
    Call LCD_DrawLine(10,10,310,230,LCD_BLACK)
    Call LCD_DrawLine(310,10,10,230,LCD_BLACK)
    Call LCD_DrawLine(10,120,310,120,LCD_BLACK)
    Call LCD_DrawLine(160,10,160,230,LCD_BLACK)

    Call LCD_Circle(160,120,100,LCD_BLUE)
    Call fillwindow(100,50,220,190, LCD_WHITE)
    Call LCD_DrawRectangle(100,50,220,190,LCD_BLUE)
    Call LCD_String( str,90,110, LCD_BLACK, LCD_WHITE)

    WaitMs 2000

Wend
End

Proc SPI_init()

SSPSTAT = 0x40
SSPCON1 = 0x31


End Proc

Proc LCD_WRITE_COM(dat As Byte)
    TFT_CS = 0
    TFT_DC = 0
    SSPBUF = dat
    While SSPSTAT.BF = 0
    Wend
    TFT_DC = 1
    TFT_CS = 1

End Proc

Proc LCD_WRITE_DATA(dat As Byte)
    TFT_CS = 0
    TFT_DC = 1
    SSPBUF = dat
    While SSPSTAT.BF = 0
    Wend
    TFT_DC = 1
    TFT_CS = 1

End Proc

Proc LCD_init()

Dim idx As Byte
Dim cmd As Byte
    High TFT_RST
    'ConfigPin TFT_RST = Output
    WaitMs 100
    Low TFT_RST
    WaitMs 100
    High TFT_RST
    WaitMs 200

    Call LCD_WRITE_COM(ILI9341_SWRESET)
    WaitMs 150
    Call LCD_WRITE_COM(ILI9341_DISPOFF)
    Call LCD_WRITE_COM(0xCB)     //Power control a
    Call LCD_WRITE_DATA(0x39)
    Call LCD_WRITE_DATA(0x2C)
    Call LCD_WRITE_DATA(0x00)
    Call LCD_WRITE_DATA(0x34)
    Call LCD_WRITE_DATA(0x02)
    WaitMs 1
    Call LCD_WRITE_COM(0xCF)    //Power control b
    Call LCD_WRITE_DATA(0x00)
    Call LCD_WRITE_DATA(0XC1)
    Call LCD_WRITE_DATA(0X30)
    WaitMs 1
    Call LCD_WRITE_COM(0xE8)    //Timing control a
    Call LCD_WRITE_DATA(0x85)
    Call LCD_WRITE_DATA(0x10)
    Call LCD_WRITE_DATA(0x78)
    WaitMs 1
    Call LCD_WRITE_COM(0xEA)    //Timing control b
    Call LCD_WRITE_DATA(0x00)
    Call LCD_WRITE_DATA(0x00)
    WaitMs 1
    Call LCD_WRITE_COM(0xED)    //Power on seq control
    Call LCD_WRITE_DATA(0x64)
    Call LCD_WRITE_DATA(0x03)
    Call LCD_WRITE_DATA(0X12)
    Call LCD_WRITE_DATA(0X81)

    //WaitMs 1
    Call LCD_WRITE_COM(0xC0)    //Power control 1
    Call LCD_WRITE_DATA(0x26)  //VRH[5:0]
    //WaitMs 1
    Call LCD_WRITE_COM(0xC1)    //Power control 2
    Call LCD_WRITE_DATA(0x11)   //SAP[2:0];BT[3:0]
    //WaitMs 1
    Call LCD_WRITE_COM(0xC5)    //VCM control
    Call LCD_WRITE_DATA(0x3e)   //Contrast
    Call LCD_WRITE_DATA(0x28)
   // WaitMs 1
    Call LCD_WRITE_COM(0xF7)
    Call LCD_WRITE_DATA(0x20)
    //WaitMs 1
    Call LCD_WRITE_COM(ILI9341_MADCTL)
    Call LCD_WRITE_DATA(0x8)
    Call LCD_WRITE_COM(ILI9341_INVOFF)
    //WaitMs 1
    Call LCD_WRITE_COM(ILI9341_NORON)
    WaitMs 1
    Call LCD_WRITE_COM(ILI9341_COLMOD )
    Call LCD_WRITE_DATA(0x55)
    //WaitMs 1
    Call LCD_WRITE_COM(0xB7) //emtry mode
    Call LCD_WRITE_DATA(0x07)
    Call LCD_WRITE_COM(0x3A) //pixel format
    Call LCD_WRITE_DATA(0x55) //16bit
    Call LCD_WRITE_COM(ILI9341_DISP_FUNC)    // Display Function Control
    Call LCD_WRITE_DATA(0x08)
    Call LCD_WRITE_DATA(0x82)
    Call LCD_WRITE_DATA(0x27)
    WaitMs 1
    Call LCD_WRITE_COM(ILI9341_SLPOUT)
    WaitMs 120
    Call LCD_WRITE_COM(ILI9341_DISPON)
    WaitMs 1

    LCD_height = 240
    LCD_width = 320

End Proc

'/**************************************************************************/
'@brief  SPI displays set an address window rectangle For blitting pixels
'@param  x  Top left corner x coordinate
'@param  y  Top left corner x coordinate
'@param  W  Width of window
'@param  h  Height of window
'/**************************************************************************/
Proc setAddrWindow(x1 As Word, y1 As Word, x2 As Word, y2 As Word)

Call LCD_WRITE_COM(ILI9341_CASET)  //Column addr set
Call LCD_WRITE_DATA(x1.HB)
Call LCD_WRITE_DATA(x1.LB)
Call LCD_WRITE_DATA(x2.HB)
Call LCD_WRITE_DATA(x2.LB)
Call LCD_WRITE_COM(ILI9341_RASET)  //Row addr set
Call LCD_WRITE_DATA(y1.HB)
Call LCD_WRITE_DATA(y1.LB)
Call LCD_WRITE_DATA(y2.HB)
Call LCD_WRITE_DATA(y2.LB)

Call LCD_WRITE_COM(ILI9341_RAMWR)  //write to RAM
End Proc
Proc LCD_DrawLine(x1 As Word, y1 As Word, x2 As Word, y2 As Word, color As Word)

    Dim t As Word
    Dim xerr, yerr, delta_x, delta_y, distance As Integer
    Dim incx, incy, xPix, yPix As Integer
    xerr = 0
    yerr = 0

    delta_x = x2 - x1
    delta_y = y2 - y1
    xPix = x1
    yPix = y1

    If delta_x > 0 Then    incx = 1
    If delta_x = 0 Then incx = 0
    If delta_x < 0 Then
        incx = -1
        delta_x = -delta_x
    Endif

    If delta_y > 0 Then    incy = 1
    If delta_y = 0 Then incy = 0
    If delta_y < 0 Then
        incy = -1
        delta_y = -delta_y
    Endif


    If delta_x > delta_y Then
        distance = delta_x
    Else
        distance = delta_y
    Endif

    For t = 0 To distance + 1 Step 1
         Call LCD_DrawPoint(xPix, yPix, color)
        xerr = xerr + delta_x
        yerr = yerr + delta_y
        If xerr > distance Then
            xerr = xerr - distance
            xPix = xPix + incx
        Endif
        If  yerr > distance Then
            yerr = yerr - distance
            yPix = yPix + incy
        Endif
    Next t
End Proc
Proc LCD_Char( char As Byte, x As Word, y As Word, fcol As Word , bcol As Word)

    Dim  x0 As Integer
    Dim idx, idx2 As Byte
    Dim mask As Byte

    For idx = 0 To 9 Step 1
        mask = _LCDFnt(char, idx)
        For idx2 = 0 To 13 Step 2
            If mask And 0x80 Then
                Call LCD_DrawPoint(x+idx2, y, fcol)
                Call LCD_DrawPoint(x+idx2+1, y, fcol)
                Call LCD_DrawPoint(x+idx2, y+1, fcol)
                Call LCD_DrawPoint(x+idx2+1, y+1, fcol)
            Else
                Call LCD_DrawPoint(x+idx2, y, bcol)
                Call LCD_DrawPoint(x+idx2+1, y, bcol)
                Call LCD_DrawPoint(x+idx2, y+1, bcol)
                Call LCD_DrawPoint(x+idx2+1, y+1, bcol)
            Endif
            mask = ShiftLeft(mask,1)
        Next idx2
        y = y + 2
    Next idx

End Proc
Proc LCD_String( str As String, x As Word, y As Word, fcol As Word , bcol As Word)

Dim idx As Byte
Dim x0 As Word
idx = 0

    While str(idx) <> 0
        Call LCD_Char(str(idx), x, y,fcol,bcol)
        x = x+18
        idx = idx + 1
    Wend

End Proc
// Rectangle

Proc LCD_DrawRectangle( x1 As Integer, y1 As Integer, x2 As Integer, y2 As Integer, color As Word)

    Call LCD_DrawLine(x1, y1, x2, y1, color)
    Call LCD_DrawLine(x1, y1, x1, y2, color)
    Call LCD_DrawLine(x1, y2, x2, y2, color)
    Call LCD_DrawLine(x2, y1, x2, y2, color)

End Proc
//   Circle routine

Proc LCD_Circle(x1 As Word, y1 As Word, rad As Word, color As Word)
Dim xPix, yPix, di As Integer
xPix = 0
yPix = rad
di = 3 - ShiftLeft(rad,1)
    While xPix <= yPix

    Call LCD_DrawPoint(x1+xPix, y1-yPix, color)
    Call LCD_DrawPoint(x1+yPix, y1-xPix, color)
    Call LCD_DrawPoint(x1+yPix, y1+xPix, color)
    Call LCD_DrawPoint(x1+xPix, y1+yPix, color)
    Call LCD_DrawPoint(x1-xPix, y1+yPix, color)
    Call LCD_DrawPoint(x1-yPix, y1+xPix, color)
    Call LCD_DrawPoint(x1-xPix, y1-yPix, color)
    Call LCD_DrawPoint(x1-yPix, y1-xPix, color)
    xPix = xPix + 1

    If di < 0 Then
        di= (4 * xPix + 6) + di
    Else
        di= (10 + 4 * (xPix-yPix)) + di
        yPix = yPix - 1
    Endif
    Wend

End Proc
// line drawing

Proc LCD_DrawPoint(x1 As Word, y1 As Word ,color As Word)

    Call LCD_WRITE_COM(ILI9341_CASET)  //Column addr set
    Call LCD_WRITE_DATA(x1.HB)
    Call LCD_WRITE_DATA(x1.LB)
    Call LCD_WRITE_COM(ILI9341_RASET)  //Row addr set
    Call LCD_WRITE_DATA(y1.HB)
    Call LCD_WRITE_DATA(y1.LB)
    Call LCD_WRITE_COM(ILI9341_RAMWR)  //write to RAM

    Call LCD_WRITE_DATA(color.HB)
    Call LCD_WRITE_DATA(color.LB)

End Proc

'/**************************************************************************/
Proc fillRect(x1 As Word, y1 As Word, x2 As Word, y2 As Word, color As Word)
Dim px As Long
Dim tmp As Word
If x2 > 0 And y2 > 0 Then      //Nonzero width and height?

    Call setAddrWindow(x1, y1, x2, y2)

    px = x2
    px = px * y2
    TFT_CS = 0
    TFT_DC = 1
    While px > 1
        SSPBUF = color.HB
        While SSPSTAT.BF = 0
        Wend
        SSPBUF = color.LB
        While SSPSTAT.BF = 0
        Wend
        px = px - 1
    Wend
    TFT_DC = 1
    TFT_CS = 1
    Endif
End Proc

'/**************************************************************************/
'@brief    Fill the screen completely with one color. Update in subclasses If desired!
'@param    color 16-Bit 5-6-5 Color To fill with
'/**************************************************************************/
Proc fillScreen(color As Word)
Call fillRect(0, 0, LCD_width, LCD_height, color)
End Proc
Proc fillwindow(x1 As Word,y1 As Word,x2 As Word,y2 As Word,color As Word)
Call fillRect(x1, y1, x2, y2, color)
End Proc
Function _LCDFnt(ch As Byte, idx As Byte) As Byte
    Symbol Retval = _LCDFnt

            Select Case ch
                Case " "
                    Retval = LookUp(0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00), idx  //SPACE
                Case "!"
                    Retval = LookUp(0x00, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x00, 0x08, 0x00), idx  //!
                Case 34
                    Retval = LookUp(0x00, 0x14, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00), idx  // "
                Case "#"
                    Retval = LookUp(0x00, 0x12, 0x12, 0x7f, 0x24, 0x24, 0xfe, 0x48, 0x48, 0x00), idx  //#
                Case "$"
                    Retval = LookUp(0x00, 0x10, 0x7c, 0x92, 0x70, 0x1c, 0x92, 0x7c, 0x10, 0x00), idx  //$
                Case "%"
                    Retval = LookUp(0x00, 0x41, 0xa2, 0x44, 0x08, 0x10, 0x22, 0x45, 0x82, 0x00), idx  //%
                Case "&"
                    Retval = LookUp(0x00, 0x10, 0x28, 0x10, 0x28, 0x44, 0x43, 0x42, 0x39, 0x00), idx  //&
                Case "'"
                    Retval = LookUp(0x00, 0x10, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00), idx  //'
                Case "("
                    Retval = LookUp(0x00, 0x08, 0x10, 0x20, 0x20, 0x20, 0x20, 0x10, 0x08, 0x00), idx  //(
                Case ")"
                    Retval = LookUp(0x00, 0x08, 0x04, 0x02, 0x02, 0x02, 0x20, 0x04, 0x08, 0x00), idx  //)
                Case "*"
                    Retval = LookUp(0x00, 0x08, 0x2a, 0x1c, 0x7f, 0x1c, 0x2a, 0x08, 0x00, 0x00), idx  //*
                Case "+"
                    Retval = LookUp(0x00, 0x00, 0x00, 0x00, 0x00, 0x3e, 0x08, 0x08, 0x00, 0x00), idx  //+
                Case ","
                    Retval = LookUp(0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x08, 0x10, 0x00), idx  //,
                Case "-"
                    Retval = LookUp(0x00, 0x00, 0x00, 0x00, 0x00, 0x3e, 0x00, 0x00, 0x00, 0x00), idx  //-
                Case "."
                    Retval = LookUp(0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x08, 0x00), idx  //.
                Case "/"
                    Retval = LookUp(0x00, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x00), idx  ///
                Case "0"
                    Retval = LookUp(0x00, 0x3c, 0x46, 0x4a, 0x4a, 0x52, 0x52, 0x62, 0x3c, 0x00), idx  //0
                Case "1"
                    Retval = LookUp(0x00, 0x08, 0x18, 0x08, 0x08, 0x08, 0x08, 0x08, 0x3e, 0x00), idx  //1
                Case "2"
                    Retval = LookUp(0x00, 0x3c, 0x42, 0x02, 0x0c, 0x30, 0x40, 0x40, 0x7e, 0x00), idx  //2
                Case "3"
                    Retval = LookUp(0x00, 0x3c, 0x42, 0x02, 0x0c, 0x02, 0x02, 0x42, 0x3c, 0x00), idx  //3
                Case "4"
                    Retval = LookUp(0x00, 0x09, 0x18, 0x28, 0x48, 0x48, 0x7e, 0x08, 0x08, 0x00), idx  //4
                Case "5"
                    Retval = LookUp(0x00, 0x7e, 0x40, 0x40, 0x7c, 0x02, 0x02, 0x42, 0x3c, 0x00), idx  //5
                Case "6"
                    Retval = LookUp(0x00, 0x3c, 0x42, 0x40, 0x7c, 0x42, 0x42, 0x42, 0x3c, 0x00), idx  //6
                Case "7"
                    Retval = LookUp(0x00, 0x7e, 0x02, 0x02, 0x04, 0x08, 0x10, 0x10, 0x10, 0x00), idx  //7
                Case "8"
                    Retval = LookUp(0x00, 0x3c, 0x42, 0x42, 0x3c, 0x42, 0x42, 0x42, 0x3c, 0x00), idx  //8
                Case "9"
                    Retval = LookUp(0x00, 0x3c, 0x42, 0x42, 0x3e, 0x02, 0x02, 0x42, 0x3c, 0x00), idx  //9
                Case ":"
                    Retval = LookUp(0x00, 0x00, 0x00, 0x08, 0x08, 0x00, 0x08, 0x08, 0x00, 0x00), idx  //:
                Case ";"
                    Retval = LookUp(0x00, 0x00, 0x00, 0x08, 0x08, 0x00, 0x08, 0x08, 0x10, 0x00), idx  //;
                Case "<"
                    Retval = LookUp(0x00, 0x04, 0x08, 0x10, 0x20, 0x20, 0x10, 0x08, 0x04, 0x00), idx  //<
                Case "="
                    Retval = LookUp(0x00, 0x00, 0x00, 0x00, 0x3e, 0x00, 0x3e, 0x00, 0x00, 0x00), idx  //=
                Case ">"
                    Retval = LookUp(0x00, 0x10, 0x08, 0x04, 0x02, 0x02, 0x04, 0x08, 0x10, 0x00), idx  //>
                Case "?"
                    Retval = LookUp(0x00, 0x3c, 0x42, 0x02, 0x04, 0x08, 0x08, 0x00, 0x08, 0x00), idx  //?
                Case "@"
                    Retval = LookUp(0x00, 0x7e, 0x81, 0x9b, 0xa5, 0xa6, 0x9c, 0x81, 0x7e, 0x00), idx  //@
                Case "A"
                    Retval = LookUp(0x00, 0x18, 0x24, 0x42, 0x42, 0x7e, 0x42, 0x42, 0x42, 0x00), idx  //A
                Case "B"
                    Retval = LookUp(0x00, 0x7c, 0x42, 0x42, 0x7c, 0x42, 0x42, 0x42, 0x7c, 0x00), idx  //B
                Case "C"
                    Retval = LookUp(0x00, 0x3c, 0x42, 0x40, 0x40, 0x40, 0x40, 0x42, 0x3c, 0x00), idx  //C
                Case "D"
                    Retval = LookUp(0x00, 0x7c, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x7c, 0x00), idx  //D
                Case "E"
                    Retval = LookUp(0x00, 0x7e, 0x40, 0x40, 0x78, 0x40, 0x40, 0x40, 0x7e, 0x00), idx  //E
                Case "F"
                    Retval = LookUp(0x00, 0x7e, 0x40, 0x40, 0x78, 0x40, 0x40, 0x40, 0x40, 0x00), idx  //F
                Case "G"
                    Retval = LookUp(0x00, 0x3c, 0x42, 0x40, 0x40, 0x46, 0x42, 0x42, 0x3c, 0x00), idx  //G
                Case "H"
                    Retval = LookUp(0x00, 0x42, 0x42, 0x42, 0x7e, 0x42, 0x42, 0x42, 0x42, 0x00), idx  //H
                Case "I"
                    Retval = LookUp(0x00, 0x7e, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x7e, 0x00), idx  //I
                Case "J"
                    Retval = LookUp(0x00, 0x7e, 0x08, 0x08, 0x08, 0x08, 0x08, 0x48, 0x30, 0x00), idx  //J
                Case "K"
                    Retval = LookUp(0x00, 0x42, 0x44, 0x48, 0x70, 0x48, 0x44, 0x42, 0x42, 0x00), idx  //K
                Case "L"
                    Retval = LookUp(0x00, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x7e, 0x00), idx  //L
                Case "M"
                    Retval = LookUp(0x00, 0x42, 0x42, 0x66, 0x5a, 0x42, 0x42, 0x42, 0x42, 0x00), idx  //M
                Case "N"
                    Retval = LookUp(0x00, 0x42, 0x42, 0x62, 0x52, 0x4a, 0x46, 0x42, 0x42, 0x00), idx  //N
                Case "O"
                    Retval = LookUp(0x00, 0x3c, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x3c, 0x00), idx  //O
                Case "P"
                    Retval = LookUp(0x00, 0x7c, 0x42, 0x42, 0x7c, 0x40, 0x40, 0x40, 0x40, 0x00), idx  //P
                Case "Q"
                    Retval = LookUp(0x00, 0x3c, 0x42, 0x42, 0x42, 0x42, 0x4a, 0x46, 0x3e, 0x00), idx  //Q
                Case "R"
                    Retval = LookUp(0x00, 0x7c, 0x42, 0x42, 0x7c, 0x48, 0x44, 0x42, 0x42, 0x00), idx  //R
                Case "S"
                    Retval = LookUp(0x00, 0x3c, 0x42, 0x40, 0x30, 0x0c, 0x02, 0x42, 0x3c, 0x00), idx  //S
                Case "T"
                    Retval = LookUp(0x00, 0x7e, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x00), idx  //T
                Case "U"
                    Retval = LookUp(0x00, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x3c, 0x00), idx  //U
                Case "V"
                    Retval = LookUp(0x00, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x24, 0x18, 0x00), idx  //V
                Case "W"
                    Retval = LookUp(0x00, 0x42, 0x42, 0x42, 0x42, 0x42, 0x5a, 0x66, 0x42, 0x00), idx  //W
                Case "X"
                    Retval = LookUp(0x00, 0x42, 0x42, 0x24, 0x18, 0x18, 0x24, 0x42, 0x42, 0x00), idx  //X
                Case "Y"
                    Retval = LookUp(0x00, 0x42, 0x42, 0x24, 0x18, 0x05, 0x08, 0x08, 0x08, 0x00), idx  //Y
                Case "Z"
                    Retval = LookUp(0x00, 0x7e, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x7e, 0x00), idx  //Z
                Case "["
                    Retval = LookUp(0x00, 0x3c, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x3c, 0x00), idx  //[
                Case "\"
                    Retval = LookUp(0x00, 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01, 0x00), idx  //\
                Case "]"
                    Retval = LookUp(0x00, 0x3c, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x3c, 0x00), idx  //]
                Case "^"
                    Retval = LookUp(0x00, 0x08, 0x14, 0x22, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00), idx  //^
                Case "_"
                    Retval = LookUp(0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7f, 0x00), idx  //_
                Case "`"
                    Retval = LookUp(0x00, 0x10, 0x10, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00), idx  //`
                Case "a"
                    Retval = LookUp(0x00, 0x00, 0x00, 0x1c, 0x02, 0x1e, 0x22, 0x22, 0x1e, 0x00), idx  //a
                Case "b"
                    Retval = LookUp(0x00, 0x20, 0x20, 0x3c, 0x22, 0x22, 0x22, 0x22, 0x3c, 0x00), idx  //b
                Case "c"
                    Retval = LookUp(0x00, 0x00, 0x00, 0x1c, 0x22, 0x20, 0x20, 0x22, 0x1c, 0x00), idx  //c
                Case "d"
                    Retval = LookUp(0x00, 0x02, 0x02, 0x1e, 0x22, 0x22, 0x22, 0x22, 0x1e, 0x00), idx  //d
                Case "e"
                    Retval = LookUp(0x00, 0x00, 0x00, 0x1c, 0x22, 0x3e, 0x20, 0x22, 0x1c, 0x00), idx  //e
                Case "f"
                    Retval = LookUp(0x00, 0x0c, 0x10, 0x10, 0x3c, 0x10, 0x10, 0x10, 0x10, 0x00), idx  //f
                Case "g"
                    Retval = LookUp(0x00, 0x00, 0x00, 0x1e, 0x22, 0x22, 0x22, 0x1e, 0x02, 0x3c), idx  //g
                Case "h"
                    Retval = LookUp(0x00, 0x20, 0x20, 0x3c, 0x22, 0x22, 0x22, 0x22, 0x22, 0x00), idx  //h
                Case "i"
                    Retval = LookUp(0x00, 0x08, 0x00, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x00), idx  //i
                Case "j"
                    Retval = LookUp(0x00, 0x08, 0x00, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x30), idx  //j
                Case "k"
                    Retval = LookUp(0x00, 0x20, 0x20, 0x22, 0x24, 0x38, 0x24, 0x22, 0x22, 0x00), idx  //k
                Case "l"
                    Retval = LookUp(0x00, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x18, 0x00), idx  //l
                Case "m"
                    Retval = LookUp(0x00, 0x00, 0x00, 0x22, 0x36, 0x2a, 0x22, 0x22, 0x22, 0x00), idx  //m
                Case "n"
                    Retval = LookUp(0x00, 0x00, 0x00, 0x3c, 0x22, 0x22, 0x22, 0x22, 0x22, 0x00), idx  //n
                Case "o"
                    Retval = LookUp(0x00, 0x00, 0x00, 0x1c, 0x22, 0x22, 0x22, 0x22, 0x1c, 0x00), idx  //o
                Case "p"
                    Retval = LookUp(0x00, 0x00, 0x00, 0x7c, 0x14, 0x14, 0x14, 0x08, 0x00, 0x00), idx  //p
                Case "q"
                    Retval = LookUp(0x00, 0x00, 0x00, 0x3c, 0x22, 0x22, 0x22, 0x3c, 0x20, 0x20), idx  //q
                Case "r"
                    Retval = LookUp(0x00, 0x00, 0x00, 0x2e, 0x30, 0x20, 0x20, 0x20, 0x20, 0x00), idx  //r
                Case "s"
                    Retval = LookUp(0x00, 0x00, 0x00, 0x1c, 0x22, 0x10, 0x0c, 0x22, 0x1c, 0x00), idx  //s
                Case "t"
                    Retval = LookUp(0x00, 0x10, 0x10, 0x38, 0x10, 0x10, 0x10, 0x12, 0x0c, 0x00), idx  //t
                Case "u"
                    Retval = LookUp(0x00, 0x00, 0x00, 0x22, 0x22, 0x22, 0x22, 0x22, 0x1c, 0x00), idx  //u
                Case "v"
                    Retval = LookUp(0x00, 0x00, 0x00, 0x22, 0x22, 0x22, 0x22, 0x14, 0x08, 0x00), idx  //v
                Case "w"
                    Retval = LookUp(0x00, 0x00, 0x00, 0x22, 0x22, 0x22, 0x2a, 0x36, 0x22, 0x00), idx  //w
                Case "x"
                    Retval = LookUp(0x00, 0x00, 0x00, 0x22, 0x14, 0x08, 0x14, 0x22, 0x22, 0x00), idx  //x
                Case "y"
                    Retval = LookUp(0x00, 0x00, 0x00, 0x22, 0x22, 0x22, 0x22, 0x1e, 0x02, 0x3c), idx  //y
                Case "z"
                    Retval = LookUp(0x00, 0x00, 0x00, 0x3e, 0x02, 0x04, 0x08, 0x10, 0x3e, 0x00), idx  //z
                Case "{"
                    Retval = LookUp(0x00, 0x04, 0x08, 0x08, 0x08, 0x10, 0x08, 0x08, 0x08, 0x04), idx  //{
                Case "|"
                    Retval = LookUp(0x00, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x00), idx  //|
                Case "}"
                    Retval = LookUp(0x00, 0x10, 0x08, 0x08, 0x08, 0x04, 0x08, 0x08, 0x08, 0x10), idx  //}
                Case "~"
                    Retval = LookUp(0x00, 0x00, 0x00, 0x00, 0xcc, 0x33, 0x00, 0x00, 0x00, 0x00), idx  //~
                Case 165 '"Ñ" + chr(165) +
                    Retval = LookUp(0x00, 0x00, 0x00, 0x7e, 0x05, 0x09, 0x11, 0x7e), idx  //Ñ
                Case 164'"ñ" + chr(164) +
                    Retval = LookUp(0x00, 0x00, 0x00, 0x78, 0x12, 0x0a, 0x0a, 0x70), idx  //ñ
            EndSelect

End Function
 
Hi I,
Well done!
I would like to go along with both of your progress, but I am so far behind in undertsanding.
Am I correct that if I use this CODE as a program on it's own (e,g,no includes) it should work with myset up/ screen?
C.
 
Should do.. You need to swap the config defines to suit a pic18f4620..
If you are running at 32Mhz just change the SSPCON1 = 0x31 to 0x30 or it will be super slow.

I haven't printed every character so if one looks wrong, let me know and I'll give you the changes
Once I have all aspects done, I'll try to marry it into the library that Dog has done..

If you use a different screen all you will need to do is include that LCD's init..
 
Hi C and Dog..

I have put the large font into some sort of grid Just needs the select case putting on..

Using the double height routine you can get 32 x 32 font.. ( the actual size is a tad smaller as not the whole 16 bits are used... The font is more like 10 x 14 which will view as 20 x 28. which is very useable.
 

Attachments

  • large font.bas
    17.3 KB · Views: 26
Excellent work; it will be really interesting to see everything working together as a whole. I used a function in C for large characters that allows specifying width and height independently for a specific list of characters. I tweaked it because I wasn't entirely satisfied with it, and over time, I ported it to the PIC18 just for fun. I’ll look it up to see if it might be useful. In fact, in the simulator section, the PIC18 emulates it very well (GLCD128x64).
 
Last edited:
The display system I designed at work, uses the toshiba T6963C chipset which is the same orientation as the ones were using. The KS0108 style is a horizontal refresh type and I only ever used it for hobbies.

The font set I have made will all port to this TFT.. Albeit much bigger in size..

You can always quadruple the height by maniplulating the LCD_Char routine...

I have started the tidy up and "out of bounds" checking.. It would be nice to make a few widgets to use as well.
 
I have been using the KS0108 controller for nearly 15 years in an automated system for which I produce small series, around 30 units per year. The system works so well that I will likely continue using it as long as I can source LCDs based on the KS0108, until I retire.
 
I did a C library once over.. I found that many manufacturers use the same system,

Apart from the KS0107 and KS0108, there was the NT7108, the HD61202, the S6B0102 and S6B0108
I believe ( may be wrong) but the ST7920 was also the same..

The unit I used to get from MikroE was a Toshiba with a touch screen.. Worked really well.
 
Hi,
I used the CODE from #121 and used my set up. Your SPI is called [ spi_init ], and mines called [ init_spi ]

The LED flashes and the screen goes white when it is switched on, so I know the CLK is working, and I can see it with a digital analyser. The SPI CLK looks erratic, but earlier it was ok. With your CODE there's no clock.

If it easy, perhaps you could scan my CODE in case there's something wrong you can spot.
C.
Code:
'18F46K20 32MHz XTL PCB_9 BASE_MASTER TFT ME #121 081124 0900

Define CONFIG1L = 0x00
Define CONFIG1H = 0x06  '6= EXT8 MHz XPLL = 32 MHZ
Define CONFIG2L = 0x06  'BORV Set to 3.0V  (SBOREN?)
Define CONFIG2H = 0x00
Define CONFIG3L = 0x00
Define CONFIG3H = 0x81  'Set for HVP
Define CONFIG4L = 0x80
Define CONFIG4H = 0x00
Define CONFIG5L = 0x0f
Define CONFIG5H = 0xc0
Define CONFIG6L = 0x0f
Define CONFIG6H = 0xe0
Define CONFIG7L = 0x0f
Define CONFIG7H = 0x40
Define CLOCK_FREQUENCY = 32

'OSH config
Define SINGLE_DECIMAL_PLACES = 2
Define STRING_MAX_LENGTH = 20

'IO MAP
'SET BITS ON/OFF before TRIS!
Const LATAinit = %00000000  'ON/OFF
Const LATBinit = %00000000
Const LATCinit = %00000000
Const LATDinit = %00000000
Const LATEinit = %00000000

'SET PIN IN/OUT
Const TRISAinit = %00000000
Const TRISBinit = %00000000
Const TRISCinit = %00010000
Const TRISDinit = %00000000
Const TRISEinit = %00000000

'Define SIMULATION_WAITMS_VALUE = 1  'Comment in for SIM out for PIC

Define SEROUT_DELAYUS = 1000

Disable High
Disable Low

'################################################

Symbol rled = PORTE.2
Symbol TFT_LED = PORTD.2 'SCREEN LED
Symbol SPI_SCLK = PORTC.3 'Clock
Symbol SPI_CS = PORTC.2   'Chip Select
Symbol SPI_DIN = PORTC.5  'Data Input to MAX7219 [IN orOUT??] |
'Initialize hardware SPI
'Proc SPI_Init() [Ians]
    ''Configure pin directions
    'ConfigPin SPI_SCLK = Output
    'ConfigPin SPI_CS = Output
    'ConfigPin SPI_DIN = Output ??|

    'Initialize pins
    'SPI_CS = 1 'Deselect the chip

    'Configure MSSP module
    'SSP1STAT = 0x40 'Data sampled at the rising edge of the clock
    'SSP1CON1 = 0x21 'SPI Master mode, Fosc/16 = 4Mhz for system clock at 64Mhz
    'SSP1CON1 = 0x22 'SPI Master mode, Fosc/64 = 1Mhz for system clock at 64Mhz
    'SSP1CON1.SSPEN = 1 'Enable MSSP module
'End Proc

'Symbol TFT_RST = PORTC.0  'RST RESET pin
'Symbol TFT_DC = PORTC.1  'DC (SS) Data/Command pin

'Symbol TFT_CS = PORTC.2  'CS Chip Select pin
'Symbol TFT_SCLK = PORTC.3  'SCK as output
'Symbol TFT_SDI = PORTC.4  'SDI as input -> SDO
'Symbol TFT_SDO = PORTC.5  'SDO as output -> SDI [Is there no MOSI?] |

'Init LATx flip flops before TRIS regs
LATA = LATAinit
LATB = LATBinit
LATC = LATCinit
LATD = LATDinit
LATE = LATEinit

TRISA = TRISAinit
TRISB = TRISBinit
TRISC = TRISCinit
TRISD = TRISDinit
TRISE = TRISEinit


'START UP LEDS
rled = 1
WaitMs 1000
rled = 0
WaitMs 1000
rled = 1
WaitMs 1000
rled = 0
WaitMs 1000

TFT_LED = 1 'Switch screen light on


'*****************************************************
'lcd driver

Const ILI9341_NOP = 0x00
Const ILI9341_SWRESET = 0x01
Const ILI9341_RDDID = 0x04
Const ILI9341_RDDST = 0x09

Const ILI9341_SLPIN = 0x10
Const ILI9341_SLPOUT = 0x11
Const ILI9341_PTLON = 0x12
Const ILI9341_NORON = 0x13

Const ILI9341_INVOFF = 0x20
Const ILI9341_INVON = 0x21
Const ILI9341_DISPOFF = 0x28
Const ILI9341_DISPON = 0x29
Const ILI9341_CASET = 0x2a
Const ILI9341_RASET = 0x2b
Const ILI9341_RAMWR = 0x2c
Const ILI9341_RAMRD = 0x2e

Const ILI9341_PTLAR = 0x30
Const ILI9341_COLMOD = 0x3a
Const ILI9341_MADCTL = 0x36
Const ILI9341_DISP_FUNC = 0xB6


Const ILI9341_MADCTL_MY = 0x80
Const ILI9341_MADCTL_MX = 0x40
Const ILI9341_MADCTL_MV = 0x20
Const ILI9341_MADCTL_ML = 0x10
Const ILI9341_MADCTL_RGB = 0x00

Const ILI9341_RDID1 = 0xda
Const ILI9341_RDID2 = 0xdb
Const ILI9341_RDID3 = 0xdc
Const ILI9341_RDID4 = 0xdd


Const LCD_BLACK = 0x0000
Const LCD_RED = 0x001f
Const LCD_BLUE = 0xf800
Const LCD_GREEN = 0x07E0
Const LCD_YELLOW = 0x07ff
Const LCD_MAGENTA = 0xf81f
Const LCD_CYAN = 0xffe0
Const LCD_WHITE = 0xffff

Dim LCD_width As Word  ///< Display width as modified by current rotation
Dim LCD_height As Word  ///< Display height as modified by current rotation

Dim LCD_rotation As Byte  ///< Display rotation (0 thru 3)

Symbol TFT_RST = PORTC.0  'RST RESET pin
Symbol TFT_DC = PORTC.1  'DC (SS) Data/Command pin
Symbol TFT_CS = PORTC.2  'CS Chip Select pin
Symbol TFT_SCK = PORTC.3  'SCK as output
Symbol TFT_SDI = PORTC.4  'SDI as input -> SDO
Symbol TFT_SDO = PORTC.5  'SDO as output -> SDI
'*************************************************
Dim str As String
Dim x As Byte
OSCCON = 0x70

OSCTUNE.PLLEN = 1
TRISA.1 = 0
TRISC = 0
ADCON1 = 15
'Call SPI_init() [IAN]
Call init_spi() '[MINE]
Call LCD_init()

str = "MAXIMUM!"
Main:
    x = 1
    While x = 1
    WaitMs 300
    LATA.1 = 1
    Call fillScreen(LCD_RED)

    WaitMs 300
    LATA.1 = 0
    Call fillwindow(10,10,310,230, LCD_BLUE)
    WaitMs 300
    LATA.1 = 1
    Call fillwindow(10,10,310,230, LCD_GREEN)
    WaitMs 300
    LATA.1 = 0
    Call fillScreen(LCD_WHITE)
    Call LCD_DrawLine(10,10,310,230,LCD_BLACK)
    Call LCD_DrawLine(310,10,10,230,LCD_BLACK)
    Call LCD_DrawLine(10,120,310,120,LCD_BLACK)
    Call LCD_DrawLine(160,10,160,230,LCD_BLACK)

    Call LCD_Circle(160,120,100,LCD_BLUE)
    Call fillwindow(100,50,220,190, LCD_WHITE)
    Call LCD_DrawRectangle(100,50,220,190,LCD_BLUE)
    Call LCD_String( str,90,110, LCD_BLACK, LCD_WHITE)

    WaitMs 2000


Wend
End

'Proc SPI_init()

'SSPSTAT = 0x40 '01000000 6=CKE Enable SMBus specific inputs
'SSPCON1 = 0x30 '0x31 30=0011000 5=SSPEN Serial port EN. 4=Idle High

'End Proc

Proc LCD_WRITE_COM(dat As Byte)
    TFT_CS = 0
    TFT_DC = 0
    SSPBUF = dat
    While SSPSTAT.BF = 0
    Wend
    TFT_DC = 1
    TFT_CS = 1

End Proc

Proc LCD_WRITE_DATA(dat As Byte)
    TFT_CS = 0
    TFT_DC = 1
    SSPBUF = dat
    While SSPSTAT.BF = 0
    Wend
    TFT_DC = 1
    TFT_CS = 1

End Proc

Proc LCD_init()

Dim idx As Byte
Dim cmd As Byte
    High TFT_RST
    'ConfigPin TFT_RST = Output
    WaitMs 100
    Low TFT_RST
    WaitMs 100
    High TFT_RST
    WaitMs 200
rled = 0

    Call LCD_WRITE_COM(ILI9341_SWRESET)
    WaitMs 150
    Call LCD_WRITE_COM(ILI9341_DISPOFF)
    Call LCD_WRITE_COM(0xCB)     //Power control a
    Call LCD_WRITE_DATA(0x39)
    Call LCD_WRITE_DATA(0x2C)
    Call LCD_WRITE_DATA(0x00)
    Call LCD_WRITE_DATA(0x34)
    Call LCD_WRITE_DATA(0x02)
    WaitMs 1
    Call LCD_WRITE_COM(0xCF)    //Power control b
    Call LCD_WRITE_DATA(0x00)
    Call LCD_WRITE_DATA(0XC1)
    Call LCD_WRITE_DATA(0X30)
    WaitMs 1
    Call LCD_WRITE_COM(0xE8)    //Timing control a
    Call LCD_WRITE_DATA(0x85)
    Call LCD_WRITE_DATA(0x10)
    Call LCD_WRITE_DATA(0x78)
    WaitMs 1
    Call LCD_WRITE_COM(0xEA)    //Timing control b
    Call LCD_WRITE_DATA(0x00)
    Call LCD_WRITE_DATA(0x00)
    WaitMs 1
    Call LCD_WRITE_COM(0xED)    //Power on seq control
    Call LCD_WRITE_DATA(0x64)
    Call LCD_WRITE_DATA(0x03)
    Call LCD_WRITE_DATA(0X12)
    Call LCD_WRITE_DATA(0X81)

    //WaitMs 1
    Call LCD_WRITE_COM(0xC0)    //Power control 1
    Call LCD_WRITE_DATA(0x26)  //VRH[5:0]
    //WaitMs 1
    Call LCD_WRITE_COM(0xC1)    //Power control 2
    Call LCD_WRITE_DATA(0x11)   //SAP[2:0];BT[3:0]
    //WaitMs 1
    Call LCD_WRITE_COM(0xC5)    //VCM control
    Call LCD_WRITE_DATA(0x3e)   //Contrast
    Call LCD_WRITE_DATA(0x28)
   // WaitMs 1
    Call LCD_WRITE_COM(0xF7)
    Call LCD_WRITE_DATA(0x20)
    //WaitMs 1
    Call LCD_WRITE_COM(ILI9341_MADCTL)
    Call LCD_WRITE_DATA(0x8)
    Call LCD_WRITE_COM(ILI9341_INVOFF)
    //WaitMs 1
    Call LCD_WRITE_COM(ILI9341_NORON)
    WaitMs 1
    Call LCD_WRITE_COM(ILI9341_COLMOD )
    Call LCD_WRITE_DATA(0x55)
    //WaitMs 1
    Call LCD_WRITE_COM(0xB7) //emtry mode
    Call LCD_WRITE_DATA(0x07)
    Call LCD_WRITE_COM(0x3A) //pixel format
    Call LCD_WRITE_DATA(0x55) //16bit
    Call LCD_WRITE_COM(ILI9341_DISP_FUNC)    // Display Function Control
    Call LCD_WRITE_DATA(0x08)
    Call LCD_WRITE_DATA(0x82)
    Call LCD_WRITE_DATA(0x27)
    WaitMs 1
    Call LCD_WRITE_COM(ILI9341_SLPOUT)
    WaitMs 120
    Call LCD_WRITE_COM(ILI9341_DISPON)
    WaitMs 1

    LCD_height = 240
    LCD_width = 320

End Proc

'/**************************************************************************/
'@brief  SPI displays set an address window rectangle For blitting pixels
'@param  x  Top left corner x coordinate
'@param  y  Top left corner x coordinate
'@param  W  Width of window
'@param  h  Height of window
'/**************************************************************************/
Proc setAddrWindow(x1 As Word, y1 As Word, x2 As Word, y2 As Word)

Call LCD_WRITE_COM(ILI9341_CASET)  //Column addr set
Call LCD_WRITE_DATA(x1.HB)
Call LCD_WRITE_DATA(x1.LB)
Call LCD_WRITE_DATA(x2.HB)
Call LCD_WRITE_DATA(x2.LB)
Call LCD_WRITE_COM(ILI9341_RASET)  //Row addr set
Call LCD_WRITE_DATA(y1.HB)
Call LCD_WRITE_DATA(y1.LB)
Call LCD_WRITE_DATA(y2.HB)
Call LCD_WRITE_DATA(y2.LB)

Call LCD_WRITE_COM(ILI9341_RAMWR)  //write to RAM
End Proc
Proc LCD_DrawLine(x1 As Word, y1 As Word, x2 As Word, y2 As Word, color As Word)

    Dim t As Word
    Dim xerr, yerr, delta_x, delta_y, distance As Integer
    Dim incx, incy, xPix, yPix As Integer
    xerr = 0
    yerr = 0

    delta_x = x2 - x1
    delta_y = y2 - y1
    xPix = x1
    yPix = y1

    If delta_x > 0 Then    incx = 1
    If delta_x = 0 Then incx = 0
    If delta_x < 0 Then
        incx = -1
        delta_x = -delta_x
    Endif

    If delta_y > 0 Then    incy = 1
    If delta_y = 0 Then incy = 0
    If delta_y < 0 Then
        incy = -1
        delta_y = -delta_y
    Endif


    If delta_x > delta_y Then
        distance = delta_x
    Else
        distance = delta_y
    Endif

    For t = 0 To distance + 1 Step 1
         Call LCD_DrawPoint(xPix, yPix, color)
        xerr = xerr + delta_x
        yerr = yerr + delta_y
        If xerr > distance Then
            xerr = xerr - distance
            xPix = xPix + incx
        Endif
        If  yerr > distance Then
            yerr = yerr - distance
            yPix = yPix + incy
        Endif
    Next t
End Proc
Proc LCD_Char( char As Byte, x As Word, y As Word, fcol As Word , bcol As Word)

    Dim  x0 As Integer
    Dim idx, idx2 As Byte
    Dim mask As Byte

    For idx = 0 To 9 Step 1
        mask = _LCDFnt(char, idx)
        For idx2 = 0 To 13 Step 2
            If mask And 0x80 Then
                Call LCD_DrawPoint(x+idx2, y, fcol)
                Call LCD_DrawPoint(x+idx2+1, y, fcol)
                Call LCD_DrawPoint(x+idx2, y+1, fcol)
                Call LCD_DrawPoint(x+idx2+1, y+1, fcol)
            Else
                Call LCD_DrawPoint(x+idx2, y, bcol)
                Call LCD_DrawPoint(x+idx2+1, y, bcol)
                Call LCD_DrawPoint(x+idx2, y+1, bcol)
                Call LCD_DrawPoint(x+idx2+1, y+1, bcol)
            Endif
            mask = ShiftLeft(mask,1)
        Next idx2
        y = y + 2
    Next idx

End Proc
Proc LCD_String( str As String, x As Word, y As Word, fcol As Word , bcol As Word)

Dim idx As Byte
Dim x0 As Word
idx = 0

    While str(idx) <> 0
        Call LCD_Char(str(idx), x, y,fcol,bcol)
        x = x+18
        idx = idx + 1
    Wend

End Proc
// Rectangle

Proc LCD_DrawRectangle( x1 As Integer, y1 As Integer, x2 As Integer, y2 As Integer, color As Word)

    Call LCD_DrawLine(x1, y1, x2, y1, color)
    Call LCD_DrawLine(x1, y1, x1, y2, color)
    Call LCD_DrawLine(x1, y2, x2, y2, color)
    Call LCD_DrawLine(x2, y1, x2, y2, color)

End Proc
//   Circle routine

Proc LCD_Circle(x1 As Word, y1 As Word, rad As Word, color As Word)
Dim xPix, yPix, di As Integer
xPix = 0
yPix = rad
di = 3 - ShiftLeft(rad,1)
    While xPix <= yPix

    Call LCD_DrawPoint(x1+xPix, y1-yPix, color)
    Call LCD_DrawPoint(x1+yPix, y1-xPix, color)
    Call LCD_DrawPoint(x1+yPix, y1+xPix, color)
    Call LCD_DrawPoint(x1+xPix, y1+yPix, color)
    Call LCD_DrawPoint(x1-xPix, y1+yPix, color)
    Call LCD_DrawPoint(x1-yPix, y1+xPix, color)
    Call LCD_DrawPoint(x1-xPix, y1-yPix, color)
    Call LCD_DrawPoint(x1-yPix, y1-xPix, color)
    xPix = xPix + 1

    If di < 0 Then
        di= (4 * xPix + 6) + di
    Else
        di= (10 + 4 * (xPix-yPix)) + di
        yPix = yPix - 1
    Endif
    Wend

End Proc
// line drawing

Proc LCD_DrawPoint(x1 As Word, y1 As Word ,color As Word)

    Call LCD_WRITE_COM(ILI9341_CASET)  //Column addr set
    Call LCD_WRITE_DATA(x1.HB)
    Call LCD_WRITE_DATA(x1.LB)
    Call LCD_WRITE_COM(ILI9341_RASET)  //Row addr set
    Call LCD_WRITE_DATA(y1.HB)
    Call LCD_WRITE_DATA(y1.LB)
    Call LCD_WRITE_COM(ILI9341_RAMWR)  //write to RAM

    Call LCD_WRITE_DATA(color.HB)
    Call LCD_WRITE_DATA(color.LB)

End Proc

'/**************************************************************************/
Proc fillRect(x1 As Word, y1 As Word, x2 As Word, y2 As Word, color As Word)
Dim px As Long
Dim tmp As Word
If x2 > 0 And y2 > 0 Then      //Nonzero width and height?

    Call setAddrWindow(x1, y1, x2, y2)

    px = x2
    px = px * y2
    TFT_CS = 0
    TFT_DC = 1
    While px > 1
        SSPBUF = color.HB
        While SSPSTAT.BF = 0
        Wend
        SSPBUF = color.LB
        While SSPSTAT.BF = 0
        Wend
        px = px - 1
    Wend
    TFT_DC = 1
    TFT_CS = 1
    Endif
End Proc

'/**************************************************************************/
'@brief    Fill the screen completely with one color. Update in subclasses If desired!
'@param    color 16-Bit 5-6-5 Color To fill with
'/**************************************************************************/
Proc fillScreen(color As Word)
Call fillRect(0, 0, LCD_width, LCD_height, color)
End Proc
Proc fillwindow(x1 As Word,y1 As Word,x2 As Word,y2 As Word,color As Word)
Call fillRect(x1, y1, x2, y2, color)
End Proc
Function _LCDFnt(ch As Byte, idx As Byte) As Byte
    Symbol Retval = _LCDFnt

            Select Case ch
                Case " "
                    Retval = LookUp(0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00), idx  //SPACE
                Case "!"
                    Retval = LookUp(0x00, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x00, 0x08, 0x00), idx  //!
                Case 34
                    Retval = LookUp(0x00, 0x14, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00), idx  // "
                Case "#"
                    Retval = LookUp(0x00, 0x12, 0x12, 0x7f, 0x24, 0x24, 0xfe, 0x48, 0x48, 0x00), idx  //#
                Case "$"
                    Retval = LookUp(0x00, 0x10, 0x7c, 0x92, 0x70, 0x1c, 0x92, 0x7c, 0x10, 0x00), idx  //$
                Case "%"
                    Retval = LookUp(0x00, 0x41, 0xa2, 0x44, 0x08, 0x10, 0x22, 0x45, 0x82, 0x00), idx  //%
                Case "&"
                    Retval = LookUp(0x00, 0x10, 0x28, 0x10, 0x28, 0x44, 0x43, 0x42, 0x39, 0x00), idx  //&
                Case "'"
                    Retval = LookUp(0x00, 0x10, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00), idx  //'
                Case "("
                    Retval = LookUp(0x00, 0x08, 0x10, 0x20, 0x20, 0x20, 0x20, 0x10, 0x08, 0x00), idx  //(
                Case ")"
                    Retval = LookUp(0x00, 0x08, 0x04, 0x02, 0x02, 0x02, 0x20, 0x04, 0x08, 0x00), idx  //)
                Case "*"
                    Retval = LookUp(0x00, 0x08, 0x2a, 0x1c, 0x7f, 0x1c, 0x2a, 0x08, 0x00, 0x00), idx  //*
                Case "+"
                    Retval = LookUp(0x00, 0x00, 0x00, 0x00, 0x00, 0x3e, 0x08, 0x08, 0x00, 0x00), idx  //+
                Case ","
                    Retval = LookUp(0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x08, 0x10, 0x00), idx  //,
                Case "-"
                    Retval = LookUp(0x00, 0x00, 0x00, 0x00, 0x00, 0x3e, 0x00, 0x00, 0x00, 0x00), idx  //-
                Case "."
                    Retval = LookUp(0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x08, 0x00), idx  //.
                Case "/"
                    Retval = LookUp(0x00, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x00), idx  ///
                Case "0"
                    Retval = LookUp(0x00, 0x3c, 0x46, 0x4a, 0x4a, 0x52, 0x52, 0x62, 0x3c, 0x00), idx  //0
                Case "1"
                    Retval = LookUp(0x00, 0x08, 0x18, 0x08, 0x08, 0x08, 0x08, 0x08, 0x3e, 0x00), idx  //1
                Case "2"
                    Retval = LookUp(0x00, 0x3c, 0x42, 0x02, 0x0c, 0x30, 0x40, 0x40, 0x7e, 0x00), idx  //2
                Case "3"
                    Retval = LookUp(0x00, 0x3c, 0x42, 0x02, 0x0c, 0x02, 0x02, 0x42, 0x3c, 0x00), idx  //3
                Case "4"
                    Retval = LookUp(0x00, 0x09, 0x18, 0x28, 0x48, 0x48, 0x7e, 0x08, 0x08, 0x00), idx  //4
                Case "5"
                    Retval = LookUp(0x00, 0x7e, 0x40, 0x40, 0x7c, 0x02, 0x02, 0x42, 0x3c, 0x00), idx  //5
                Case "6"
                    Retval = LookUp(0x00, 0x3c, 0x42, 0x40, 0x7c, 0x42, 0x42, 0x42, 0x3c, 0x00), idx  //6
                Case "7"
                    Retval = LookUp(0x00, 0x7e, 0x02, 0x02, 0x04, 0x08, 0x10, 0x10, 0x10, 0x00), idx  //7
                Case "8"
                    Retval = LookUp(0x00, 0x3c, 0x42, 0x42, 0x3c, 0x42, 0x42, 0x42, 0x3c, 0x00), idx  //8
                Case "9"
                    Retval = LookUp(0x00, 0x3c, 0x42, 0x42, 0x3e, 0x02, 0x02, 0x42, 0x3c, 0x00), idx  //9
                Case ":"
                    Retval = LookUp(0x00, 0x00, 0x00, 0x08, 0x08, 0x00, 0x08, 0x08, 0x00, 0x00), idx  //:
                Case ";"
                    Retval = LookUp(0x00, 0x00, 0x00, 0x08, 0x08, 0x00, 0x08, 0x08, 0x10, 0x00), idx  //;
                Case "<"
                    Retval = LookUp(0x00, 0x04, 0x08, 0x10, 0x20, 0x20, 0x10, 0x08, 0x04, 0x00), idx  //<
                Case "="
                    Retval = LookUp(0x00, 0x00, 0x00, 0x00, 0x3e, 0x00, 0x3e, 0x00, 0x00, 0x00), idx  //=
                Case ">"
                    Retval = LookUp(0x00, 0x10, 0x08, 0x04, 0x02, 0x02, 0x04, 0x08, 0x10, 0x00), idx  //>
                Case "?"
                    Retval = LookUp(0x00, 0x3c, 0x42, 0x02, 0x04, 0x08, 0x08, 0x00, 0x08, 0x00), idx  //?
                Case "@"
                    Retval = LookUp(0x00, 0x7e, 0x81, 0x9b, 0xa5, 0xa6, 0x9c, 0x81, 0x7e, 0x00), idx  //@
                Case "A"
                    Retval = LookUp(0x00, 0x18, 0x24, 0x42, 0x42, 0x7e, 0x42, 0x42, 0x42, 0x00), idx  //A
                Case "B"
                    Retval = LookUp(0x00, 0x7c, 0x42, 0x42, 0x7c, 0x42, 0x42, 0x42, 0x7c, 0x00), idx  //B
                Case "C"
                    Retval = LookUp(0x00, 0x3c, 0x42, 0x40, 0x40, 0x40, 0x40, 0x42, 0x3c, 0x00), idx  //C
                Case "D"
                    Retval = LookUp(0x00, 0x7c, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x7c, 0x00), idx  //D
                Case "E"
                    Retval = LookUp(0x00, 0x7e, 0x40, 0x40, 0x78, 0x40, 0x40, 0x40, 0x7e, 0x00), idx  //E
                Case "F"
                    Retval = LookUp(0x00, 0x7e, 0x40, 0x40, 0x78, 0x40, 0x40, 0x40, 0x40, 0x00), idx  //F
                Case "G"
                    Retval = LookUp(0x00, 0x3c, 0x42, 0x40, 0x40, 0x46, 0x42, 0x42, 0x3c, 0x00), idx  //G
                Case "H"
                    Retval = LookUp(0x00, 0x42, 0x42, 0x42, 0x7e, 0x42, 0x42, 0x42, 0x42, 0x00), idx  //H
                Case "I"
                    Retval = LookUp(0x00, 0x7e, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x7e, 0x00), idx  //I
                Case "J"
                    Retval = LookUp(0x00, 0x7e, 0x08, 0x08, 0x08, 0x08, 0x08, 0x48, 0x30, 0x00), idx  //J
                Case "K"
                    Retval = LookUp(0x00, 0x42, 0x44, 0x48, 0x70, 0x48, 0x44, 0x42, 0x42, 0x00), idx  //K
                Case "L"
                    Retval = LookUp(0x00, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x7e, 0x00), idx  //L
                Case "M"
                    Retval = LookUp(0x00, 0x42, 0x42, 0x66, 0x5a, 0x42, 0x42, 0x42, 0x42, 0x00), idx  //M
                Case "N"
                    Retval = LookUp(0x00, 0x42, 0x42, 0x62, 0x52, 0x4a, 0x46, 0x42, 0x42, 0x00), idx  //N
                Case "O"
                    Retval = LookUp(0x00, 0x3c, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x3c, 0x00), idx  //O
                Case "P"
                    Retval = LookUp(0x00, 0x7c, 0x42, 0x42, 0x7c, 0x40, 0x40, 0x40, 0x40, 0x00), idx  //P
                Case "Q"
                    Retval = LookUp(0x00, 0x3c, 0x42, 0x42, 0x42, 0x42, 0x4a, 0x46, 0x3e, 0x00), idx  //Q
                Case "R"
                    Retval = LookUp(0x00, 0x7c, 0x42, 0x42, 0x7c, 0x48, 0x44, 0x42, 0x42, 0x00), idx  //R
                Case "S"
                    Retval = LookUp(0x00, 0x3c, 0x42, 0x40, 0x30, 0x0c, 0x02, 0x42, 0x3c, 0x00), idx  //S
                Case "T"
                    Retval = LookUp(0x00, 0x7e, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x00), idx  //T
                Case "U"
                    Retval = LookUp(0x00, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x3c, 0x00), idx  //U
                Case "V"
                    Retval = LookUp(0x00, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x24, 0x18, 0x00), idx  //V
                Case "W"
                    Retval = LookUp(0x00, 0x42, 0x42, 0x42, 0x42, 0x42, 0x5a, 0x66, 0x42, 0x00), idx  //W
                Case "X"
                    Retval = LookUp(0x00, 0x42, 0x42, 0x24, 0x18, 0x18, 0x24, 0x42, 0x42, 0x00), idx  //X
                Case "Y"
                    Retval = LookUp(0x00, 0x42, 0x42, 0x24, 0x18, 0x05, 0x08, 0x08, 0x08, 0x00), idx  //Y
                Case "Z"
                    Retval = LookUp(0x00, 0x7e, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x7e, 0x00), idx  //Z
                Case "["
                    Retval = LookUp(0x00, 0x3c, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x3c, 0x00), idx  //[
                Case "\"
                    Retval = LookUp(0x00, 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01, 0x00), idx  //\
                Case "]"
                    Retval = LookUp(0x00, 0x3c, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x3c, 0x00), idx  //]
                Case "^"
                    Retval = LookUp(0x00, 0x08, 0x14, 0x22, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00), idx  //^
                Case "_"
                    Retval = LookUp(0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7f, 0x00), idx  //_
                Case "`"
                    Retval = LookUp(0x00, 0x10, 0x10, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00), idx  //`
                Case "a"
                    Retval = LookUp(0x00, 0x00, 0x00, 0x1c, 0x02, 0x1e, 0x22, 0x22, 0x1e, 0x00), idx  //a
                Case "b"
                    Retval = LookUp(0x00, 0x20, 0x20, 0x3c, 0x22, 0x22, 0x22, 0x22, 0x3c, 0x00), idx  //b
                Case "c"
                    Retval = LookUp(0x00, 0x00, 0x00, 0x1c, 0x22, 0x20, 0x20, 0x22, 0x1c, 0x00), idx  //c
                Case "d"
                    Retval = LookUp(0x00, 0x02, 0x02, 0x1e, 0x22, 0x22, 0x22, 0x22, 0x1e, 0x00), idx  //d
                Case "e"
                    Retval = LookUp(0x00, 0x00, 0x00, 0x1c, 0x22, 0x3e, 0x20, 0x22, 0x1c, 0x00), idx  //e
                Case "f"
                    Retval = LookUp(0x00, 0x0c, 0x10, 0x10, 0x3c, 0x10, 0x10, 0x10, 0x10, 0x00), idx  //f
                Case "g"
                    Retval = LookUp(0x00, 0x00, 0x00, 0x1e, 0x22, 0x22, 0x22, 0x1e, 0x02, 0x3c), idx  //g
                Case "h"
                    Retval = LookUp(0x00, 0x20, 0x20, 0x3c, 0x22, 0x22, 0x22, 0x22, 0x22, 0x00), idx  //h
                Case "i"
                    Retval = LookUp(0x00, 0x08, 0x00, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x00), idx  //i
                Case "j"
                    Retval = LookUp(0x00, 0x08, 0x00, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x30), idx  //j
                Case "k"
                    Retval = LookUp(0x00, 0x20, 0x20, 0x22, 0x24, 0x38, 0x24, 0x22, 0x22, 0x00), idx  //k
                Case "l"
                    Retval = LookUp(0x00, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x18, 0x00), idx  //l
                Case "m"
                    Retval = LookUp(0x00, 0x00, 0x00, 0x22, 0x36, 0x2a, 0x22, 0x22, 0x22, 0x00), idx  //m
                Case "n"
                    Retval = LookUp(0x00, 0x00, 0x00, 0x3c, 0x22, 0x22, 0x22, 0x22, 0x22, 0x00), idx  //n
                Case "o"
                    Retval = LookUp(0x00, 0x00, 0x00, 0x1c, 0x22, 0x22, 0x22, 0x22, 0x1c, 0x00), idx  //o
                Case "p"
                    Retval = LookUp(0x00, 0x00, 0x00, 0x7c, 0x14, 0x14, 0x14, 0x08, 0x00, 0x00), idx  //p
                Case "q"
                    Retval = LookUp(0x00, 0x00, 0x00, 0x3c, 0x22, 0x22, 0x22, 0x3c, 0x20, 0x20), idx  //q
                Case "r"
                    Retval = LookUp(0x00, 0x00, 0x00, 0x2e, 0x30, 0x20, 0x20, 0x20, 0x20, 0x00), idx  //r
                Case "s"
                    Retval = LookUp(0x00, 0x00, 0x00, 0x1c, 0x22, 0x10, 0x0c, 0x22, 0x1c, 0x00), idx  //s
                Case "t"
                    Retval = LookUp(0x00, 0x10, 0x10, 0x38, 0x10, 0x10, 0x10, 0x12, 0x0c, 0x00), idx  //t
                Case "u"
                    Retval = LookUp(0x00, 0x00, 0x00, 0x22, 0x22, 0x22, 0x22, 0x22, 0x1c, 0x00), idx  //u
                Case "v"
                    Retval = LookUp(0x00, 0x00, 0x00, 0x22, 0x22, 0x22, 0x22, 0x14, 0x08, 0x00), idx  //v
                Case "w"
                    Retval = LookUp(0x00, 0x00, 0x00, 0x22, 0x22, 0x22, 0x2a, 0x36, 0x22, 0x00), idx  //w
                Case "x"
                    Retval = LookUp(0x00, 0x00, 0x00, 0x22, 0x14, 0x08, 0x14, 0x22, 0x22, 0x00), idx  //x
                Case "y"
                    Retval = LookUp(0x00, 0x00, 0x00, 0x22, 0x22, 0x22, 0x22, 0x1e, 0x02, 0x3c), idx  //y
                Case "z"
                    Retval = LookUp(0x00, 0x00, 0x00, 0x3e, 0x02, 0x04, 0x08, 0x10, 0x3e, 0x00), idx  //z
                Case "{"
                    Retval = LookUp(0x00, 0x04, 0x08, 0x08, 0x08, 0x10, 0x08, 0x08, 0x08, 0x04), idx  //{
                Case "|"
                    Retval = LookUp(0x00, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x00), idx  //|
                Case "}"
                    Retval = LookUp(0x00, 0x10, 0x08, 0x08, 0x08, 0x04, 0x08, 0x08, 0x08, 0x10), idx  //}
                Case "~"
                    Retval = LookUp(0x00, 0x00, 0x00, 0x00, 0xcc, 0x33, 0x00, 0x00, 0x00, 0x00), idx  //~
                Case 165 '"Ñ" + chr(165) +
                    Retval = LookUp(0x00, 0x00, 0x00, 0x7e, 0x05, 0x09, 0x11, 0x7e), idx  //Ñ
                Case 164'"ñ" + chr(164) +
                    Retval = LookUp(0x00, 0x00, 0x00, 0x78, 0x12, 0x0a, 0x0a, 0x70), idx  //ñ
            EndSelect

End Function
'*******************************************************
'*                                                     *
'*            START UP SPI ROUTINE                     *
'*                                                     *
'******************************************************

Proc init_spi()  'PIC 18F46K20
SPI_CS = 1 'Deselect the chip [IAN]

TRISC.3 = 0  'SCK to Slave ****** output *******
TRISC.4 = 1  'MISO  ****** input *******
TRISC.5 = 0  'MOSI  ****** output *******

'MODE 0,0???
SSPSTAT.SMP = 0  '7=Input data sampled at middle of data output time ** not about clock generation **
SSPSTAT.CKE = 1  '6=Output data changes on clock transition from active to idle
SSPSTAT.5 = 0  'I2C only
SSPSTAT.4 = 0  'I2C only
SSPSTAT.3 = 0  'I2C only
SSPSTAT.2 = 0  'I2C only
SSPSTAT.1 = 0  'I2C only

SSPCON1 = 0  'RESET THE CONTROL REGISTER
SSPCON1.WCOL = 0  '7=Collision detect
SSPCON1.SSPOV = 0  '6=Overflow
SSPCON1.SSPEN = 1  '5=Configure SCK,SD0,SDI,/SS
SSPCON1.CKP = 1  '4== idle state For clock is A HIGH level
SSPCON1.SSPM3 = 0  '0010 = SPI Master mode, clock = FOSC/64
SSPCON1.SSPM2 = 0
SSPCON1.SSPM1 = 1  'mASTER MODE
SSPCON1.SSPM0 = 0
End Proc
 
The only difference

Your code:-

SSPSTAT = 0x40 and SSPCON1 = 0x32 //<<< slow..

Mine:-

SSPSTAT = 0x40 and SSPCON1 = 0x30

But you set the FOSC / 64 That's going to be sssllloooww 125khz.. mines running at 4Mhz

Can I ask? are you using the ILI9341? as this code is for that device If you are using the ST7789 I'll need to send you the init() routine for that device..
 
Hi I,
I'm using the ST7789.
C
 
Like I said... I haven't a ST7789 in that size.. You'll need the other INIT from the first code and the other set xy routine... I'll sort it out for you
 
Also!! The picture in a post way back You had several colours displayed.

Can you post me THAT code?
Hi I,
Here's the CODE from then, but it is very intermittent, I have to keep WRITEing it for it to work, and sometimes it's just the scrambled screen.
As you can see on this and many iterations, I did remember to change SSPCON1 to 0X30.
C.
Code:
'18F46K20 32MHz XTL PCB_9 BASE_MASTER TFT 240x320  141024 1700
'********************************************************
'*                                                      *
'*    Set up the PIC18F46K20 Microcontroller            *
'*                                                      *
'********************************************************

Define CONFIG1L = 0x00
Define CONFIG1H = 0x06  '6= EXT8 MHz XPLL = 32 MHZ
Define CONFIG2L = 0x06  'BORV Set to 3.0V  (SBOREN?)
Define CONFIG2H = 0x00
Define CONFIG3L = 0x00
Define CONFIG3H = 0x81  'Set for HVP
Define CONFIG4L = 0x80
Define CONFIG4H = 0x00
Define CONFIG5L = 0x0f
Define CONFIG5H = 0xc0
Define CONFIG6L = 0x0f
Define CONFIG6H = 0xe0
Define CONFIG7L = 0x0f
Define CONFIG7H = 0x40
Define CLOCK_FREQUENCY = 32

'OSH config
Define SINGLE_DECIMAL_PLACES = 2
Define STRING_MAX_LENGTH = 20

'IO MAP
'SET BITS ON/OFF before TRIS!
Const LATAinit = %00000000  'ON/OFF
Const LATBinit = %00000000
Const LATCinit = %00000000
Const LATDinit = %00000000
Const LATEinit = %00000000

'SET PIN IN/OUT
Const TRISAinit = %00000000
Const TRISBinit = %00000000
Const TRISCinit = %00000000
Const TRISDinit = %00000000
Const TRISEinit = %00000000

'Define SIMULATION_WAITMS_VALUE = 1  'Comment in for SIM out for PIC

Define SEROUT_DELAYUS = 1000

Disable High
Disable Low

Symbol rled = PORTE.2
Symbol TFT_LED = PORTD.2
TFT_LED = 1

Symbol TFT_RST = PORTC.0  'RST RESET pin
Symbol TFT_DC = PORTC.1  'DC (SS) Data/Command pin
Symbol TFT_CS = PORTC.2  'CS Chip Select pin
Symbol TFT_SCK = PORTC.3  'SCK as output
Symbol TFT_SDI = PORTC.4  'SDI as input -> SDO
Symbol TFT_SDO = PORTC.5  'SDO as output -> SDI

'Init LATx flip flops before TRIS regs
LATA = LATAinit
LATB = LATBinit
LATC = LATCinit
LATD = LATDinit
LATE = LATEinit

TRISA = TRISAinit
TRISB = TRISBinit
TRISC = TRISCinit
TRISD = TRISDinit
TRISE = TRISEinit


'START UP LEDS
rled = 1
WaitMs 1000
rled = 0
WaitMs 1000
rled = 1
WaitMs 1000
rled = 0
WaitMs 1000


'INITIALIZE SPI
Call init_spi()  'initialise spi -here SSPEN=1 sets the pin directions for SPI


'main:'/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
'Toggle rled
'WaitMs 1000

'Toggle TFT_LED
'WaitMs 1000


'Goto main
'End

'************************************************
'#ifndef _ADAFRUIT_ILI9341H_
'#define _ADAFRUIT_ILI9341H_

'#include "Adafruit_GFX.h"
'#include "Arduino.h"
'#include "Print.h"
'#include <Adafruit_SPITFT.h>
'#include <SPI.h>

Const ILI9341_TFTWIDTH = 240  ///< ILI9341 max TFT width
Const ILI9341_TFTHEIGHT = 255 '[TEMP NOT BYTE ?]'320 ///< ILI9341 max TFT height

Const ILI9341_NOP = 0x00     ///< No-op register
Const ILI9341_SWRESET = 0x01 ///< Software reset register
Const ILI9341_RDDID = 0x04   ///< Read display identification information
Const ILI9341_RDDST = 0x09   ///< Read Display Status

Const ILI9341_SLPIN = 0x10  ///< Enter Sleep Mode
Const ILI9341_SLPOUT = 0x11 ///< Sleep Out
Const ILI9341_PTLON = 0x12  ///< Partial Mode ON
Const ILI9341_NORON = 0x13  ///< Normal Display Mode ON

Const ILI9341_RDMODE = 0x0A     ///< Read Display Power Mode
Const ILI9341_RDMADCTL = 0x0B   ///< Read Display MADCTL
Const ILI9341_RDPIXFMT = 0x0C   ///< Read Display Pixel Format
Const ILI9341_RDIMGFMT = 0x0D   ///< Read Display Image Format
Const ILI9341_RDSELFDIAG = 0x0F ///< Read Display Self-Diagnostic Result

Const ILI9341_INVOFF = 0x20   ///< Display Inversion OFF
Const ILI9341_INVON = 0x21    ///< Display Inversion ON
Const ILI9341_GAMMASET = 0x26 ///< Gamma Set
Const ILI9341_DISPOFF = 0x28  ///< Display OFF
Const ILI9341_DISPON = 0x29   ///< Display ON

Const ILI9341_CASET = 0x2A ///< Column Address Set
Const ILI9341_PASET = 0x2B ///< Page Address Set
Const ILI9341_RAMWR = 0x2C ///< Memory Write
Const ILI9341_RAMRD = 0x2E ///< Memory Read

Const ILI9341_PTLAR = 0x30    ///< Partial Area
Const ILI9341_VSCRDEF = 0x33  ///< Vertical Scrolling Definition
Const ILI9341_MADCTL = 0x36   ///< Memory Access Control
Const ILI9341_VSCRSADD = 0x37 ///< Vertical Scrolling Start Address
Const ILI9341_PIXFMT = 0x3A   ///< COLMOD: Pixel Format Set

Const ILI9341_FRMCTR1 = 0x81 ///< Frame Rate Control (In Normal Mode/Full Colors)
Const ILI9341_FRMCTR2 = 0xB2 ///< Frame Rate Control (In Idle Mode/8 colors)
Const ILI9341_FRMCTR3 = 0xB3 ///< Frame Rate control (In Partial Mode/Full Colors)
Const ILI9341_INVCTR = 0xB4  ///< Display Inversion Control
Const ILI9341_DFUNCTR = 0xB6 ///< Display Function Control

Const ILI9341_PWCTR1 = 0xC0 ///< Power Control 1
Const ILI9341_PWCTR2 = 0xC1 ///< Power Control 2
Const ILI9341_PWCTR3 = 0xC2 ///< Power Control 3
Const ILI9341_PWCTR4 = 0xC3 ///< Power Control 4
Const ILI9341_PWCTR5 = 0xC4 ///< Power Control 5
Const ILI9341_VMCTR1 = 0xC5 ///< VCOM Control 1
Const ILI9341_VMCTR2 = 0xC7 ///< VCOM Control 2

Const ILI9341_RDID1 = 0xDA ///< Read ID 1
Const ILI9341_RDID2 = 0xDB ///< Read ID 2
Const ILI9341_RDID3 = 0xDC ///< Read ID 3
Const ILI9341_RDID4 = 0xDD ///< Read ID 4

Const ST7789_240x360_XSTART = 0'ST7789_240x240_XSTART = 0 'ADDED |
Const ST7789_240x360_YSTART = 80 'ST7789_240x240_YSTART = 80 'ADDED |

Const ILI9341_GMCTRP1 = 0xE0 ///< Positive Gamma Correction
Const ILI9341_GMCTRN1 = 0xE1 ///< Negative Gamma Correction
//#const ILI9341_PWCTR6     = 0xFC

// Color definitions
Const ILI9341_BLACK = 0x0000       ///<   0,   0,   0
Const ILI9341_NAVY = 0x000F        ///<   0,   0, 123
Const ILI9341_DARKGREEN = 0x03E0   ///<   0, 125,   0
Const ILI9341_DARKCYAN = 0x03EF    ///<   0, 125, 123
Const ILI9341_MAROON = 0x7800      ///< 123,   0,   0
Const ILI9341_PURPLE = 0x780F      ///< 123,   0, 123
Const ILI9341_OLIVE = 0x7BE0       ///< 123, 125,   0
Const ILI9341_LIGHTGREY = 0xC618   ///< 198, 195, 198
Const ILI9341_DARKGREY = 0x7BEF    ///< 123, 125, 123
Const ILI9341_BLUE = 0x001F        ///<   0,   0, 255
Const ILI9341_GREEN = 0x07E0       ///<   0, 255,   0
Const ILI9341_CYAN = 0x07FF        ///<   0, 255, 255
Const ILI9341_RED = 0xF800         ///< 255,   0,   0
Const ILI9341_MAGENTA = 0xF81F     ///< 255,   0, 255
Const ILI9341_YELLOW = 0xFFE0      ///< 255, 255,   0
Const ILI9341_WHITE = 0xFFFF       ///< 255, 255, 255
Const ILI9341_ORANGE = 0xFD20      ///< 255, 165,   0
Const ILI9341_GREENYELLOW = 0xAFE5 ///< 173, 255,  41
Const ILI9341_PINK = 0xFC18        ///< 255, 130, 198
'*************************************************

Dim x As Byte
'OSCCON = 0x70
'OSCTUNE.PLLEN = 1 'NOTE COMMENTED OUT @
TRISA.1 = 0
TRISC = 0
'ADCON1 = 15 '?
'Call SPI_init()
Call init_spi()
Call LCD_init()

TFT_LED = 1
WaitMs 3000

main:

'WaitMs 1000
'Toggle TFT_LED
'WaitMs 1000
Toggle rled

    'x = 1
    'While x = 1
    WaitMs 500
    LATA.1 = 1
    Call fillScreen(ILI9341_DARKGREEN)
    WaitMs 500
    LATA.1 = 0
    Call fillScreen(ILI9341_RED)
Goto main 'Wend
End

'Proc SPI_init()

'SSPSTAT = 0xc0
'SSPCON1 = 0x30

'End Proc

Proc SPI_sendcmd(dat As Byte)
    TFT_DC = 0
    SSPBUF = dat
    While SSPSTAT.BF = 0
    Wend
    TFT_DC = 1
End Proc
Proc SPI_senddat(dat As Word) '(dat As Byte)
    TFT_DC = 1
    SSPBUF = dat
    While SSPSTAT.BF = 0
    Wend
    TFT_DC = 1
End Proc

Proc LCD_init()

Dim idx As Byte
Dim cmd As Byte
    High TFT_RST
'ConfigPin TFT_RST = Output
    WaitMs 100
    Low TFT_RST
    WaitMs 100
    High TFT_RST
    WaitMs 200

    Call SPI_sendcmd(ILI9341_SWRESET)
    WaitMs 150
    Call SPI_sendcmd(ILI9341_SLPOUT)
    WaitMs 10
    Call SPI_sendcmd(ILI9341_PIXFMT) '?
    Call SPI_senddat(0x55)
    WaitMs 10
    Call SPI_sendcmd(ILI9341_MADCTL)
    Call SPI_senddat(0x8)
    Call SPI_sendcmd(ILI9341_CASET)
    Call SPI_senddat(0x0)
    Call SPI_senddat(0x0)
    Call SPI_senddat(0x0)
    Call SPI_senddat(360) '(240)
    Call SPI_sendcmd(ILI9341_PASET) 'RASET?
    Call SPI_senddat(0x0)
    Call SPI_senddat(0x0)
    Call SPI_senddat(0x0)
    Call SPI_senddat(360) '(240)
    Call SPI_sendcmd(ILI9341_INVON)
    WaitMs 10
    Call SPI_sendcmd(ILI9341_DISPON)
    WaitMs 10
    Call SPI_sendcmd(ILI9341_DISPON)
    WaitMs 10


    '_colstart = ST7789_240x360_XSTART 'ST7789_240x240_XSTART

    'ST7789_240x360_XSTART = 0'ST7789_240x240_XSTART = 0 'ADDED ?

    '_rowstart = ST7789_240x360_YSTART 'ST7789_240x240_YSTART ?
    '_height = 240
    '_width = 360 '240

End Proc

'/**************************************************************************/
'@brief  SPI displays set an address window rectangle For blitting pixels
'@param  x  Top left corner x coordinate
'@param  y  Top left corner x coordinate
'@param  W  Width of window
'@param  h  Height of window
'/**************************************************************************/
Proc setAddrWindow(x As Byte, y As Byte, _W As Byte, h As Byte)
x = x + ST7789_240x360_XSTART
y = y + ST7789_240x360_YSTART

Call SPI_sendcmd(ILI9341_CASET)  //Column addr set
Call SPI_senddat(0)
Call SPI_senddat(x)
Call SPI_senddat(0)
Call SPI_senddat(x + _W - 1)

'Call SPI_sendcmd(ST77XX_RASET)  //Row addr set ?
'Call SPI_senddat(0)
'Call SPI_senddat(y)
'Call SPI_senddat(0)
'Call SPI_senddat(y + h - 1)

Call SPI_sendcmd(ILI9341_RAMWR)  //write to RAM
End Proc



'/**************************************************************************/
Proc fillRect(x As Byte, y As Byte, _W As Byte, h As Byte, color As Word)
Dim hi As Byte
Dim lo As Byte
Dim px As Word
Dim tmp As Byte

If _W > 0 And h > 0 Then  //Nonzero width and height?

hi = color.HB
lo = color.LB

If x >= ILI9341_TFTWIDTH Or y >= ILI9341_TFTHEIGHT Then Exit 'COULD BE REVERED?
tmp = x + _W - 1
If tmp >= ILI9341_TFTWIDTH Then _W = ILI9341_TFTWIDTH - x
tmp = x + h - 1
If tmp >= ILI9341_TFTHEIGHT Then h = ILI9341_TFTHEIGHT - y
Call setAddrWindow(x, y, _W, h)

px = _W
px = px * h
While px > 1
Call SPI_senddat(hi)
Call SPI_senddat(lo)
px = px - 1
Wend

Endif
End Proc

'/**************************************************************************/
'@brief    Fill the screen completely with one color. Update in subclasses If desired!
'@param    color 16-Bit 5-6-5 Color To fill with
'/**************************************************************************/
Proc fillScreen(color As Word)
Call fillRect(0, 0, ILI9341_TFTWIDTH, ILI9341_TFTHEIGHT, color)
End Proc
'*******************************************************
'*                                                     *
'*            START UP SPI ROUTINE                     *
'*                                                     *
'*******************************************************
Proc init_spi()  'PIC 18F46K20
TRISC.3 = 0  'SCK to Slave ****** output *******
TRISC.4 = 1  'MISO  ****** input *******
TRISC.5 = 0  'MOSI  ****** output *******

'MODE 0,0???
SSPSTAT.SMP = 0  'Input data sampled at middle of data output time ** not about clock generation **
SSPSTAT.CKE = 1  'CHANGED to 1 [0 Output data changes on clock transition from active to idle ** AS ABOVE **]
SSPSTAT.5 = 0  'I2C only
SSPSTAT.4 = 0  'I2C only
SSPSTAT.3 = 0  'I2C only
SSPSTAT.2 = 0  'I2C only
SSPSTAT.1 = 0  'I2C only

SSPCON1 = 0  'RESET THE CONTROL REGISTER **************************************ADDED
SSPCON1.WCOL = 0  'Collision detect
SSPCON1.SSPOV = 0  'Overflow
SSPCON1.SSPEN = 1  'Configure SCK,SD0,SDI,/SS ** HAS TO BE 1. To reset or reconfigure SPI mode, clear the SSPEN bit,
'reinitialize the sspcon registers And Then set the SSPEN Bit from the datasheet !!!!!!!!!!!!!!!!
SSPCON1.CKP = 1  '= idle state For clock is A HIGH level
SSPCON1.SSPM3 = 0  '0010 = SPI Master mode, clock = FOSC/64
SSPCON1.SSPM2 = 0
SSPCON1.SSPM1 = 1  'mASTER MODE
SSPCON1.SSPM0 = 0
End Proc
 
Just though of something.............. I first used a pic18f4620 when I got that st7789 code working.

My lil 240x240 sst7789 is at work. so I'll bring it home and make sure it works on YOUR!! platform.

Which I must say I'm confused abut.

I thought you were using the same 4620 as I was.. BUT!! you are using the K series the pic18f46k20 to be exact.. I use the 46K22 as I need the second serial port. Now here is what I found The pic46k20 is a 3v6 max device.. Your Brown out is set to 3v So tell me! what is your power supply? I hope its 3v3 and even then the BOREN is a tad too high.
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…