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: 169
  • ST7789_240X240.bas
    15 KB · Views: 153
  • _SPI_Library.bas
    6 KB · Views: 162
  • _Pic18F26K22Library.bas
    46.4 KB · Views: 152
  • _SetUpAmicus18.bas
    2.9 KB · Views: 142
Still extreme at work!! I have an ST7735 and a ST7789 and a IL9431. I will do all three once I get a break.

Unfortunately Work puts food on MY table and new shoes and handbags for the missus. LOL..
 
This is the corrected SPI library that is currently used (MSSP1 module), I leave it in case you don't want to start from scratch.

Example: Call _SPI1_Init(_SPI_MODE0, _SPI_MASTER_SPEED1 | _SPI_ENABLE) 'CLK 4Mhz (64mHZ)

And of course use the latest version of the IDE which has important improvements.
 

Attachments

  • _SPI_Library.bas
    5.8 KB · Views: 66
Last edited:
OKAY!!!! I have the ST7789 working... I have a skeletal program... Only seems to work in Mode 2

Also the red and green are in the wrong positions fill white works and green but blue and red are reversed.

DogFlu66 The init has 9 commands and each command has or doesn't have some parameters some have to have a delay after the command and data has been sent.

Here be the skeletal code..

Note its on a pic18f4620 as I didn't have the K22 here at home hence it runs at 8Mhz max

Code:
'*****************************************************
'lcd driver


'Fuses definition.

Define CONFIG1L = 0x00
Define CONFIG1H = 0x08
Define CONFIG2L = 0x1e
Define CONFIG2H = 0x00
Define CONFIG3L = 0x00
Define CONFIG3H = 0x83
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  'Clock 64Mhz
#define STRING_MAX_LENGTH = 60
'#define SIMULATION_WAITMS_VALUE = 1
'************************************************
Const ST_CMD_DELAY = 0x80  //special signifier for command lists

Const ST77XX_NOP = 0x00
Const ST77XX_SWRESET = 0x01
Const ST77XX_RDDID = 0x04
Const ST77XX_RDDST = 0x09

Const ST77XX_SLPIN = 0x10
Const ST77XX_SLPOUT = 0x11
Const ST77XX_PTLON = 0x12
Const ST77XX_NORON = 0x13

Const ST77XX_INVOFF = 0x20
Const ST77XX_INVON = 0x21
Const ST77XX_DISPOFF = 0x28
Const ST77XX_DISPON = 0x29
Const ST77XX_CASET = 0x2a
Const ST77XX_RASET = 0x2b
Const ST77XX_RAMWR = 0x2c
Const ST77XX_RAMRD = 0x2e

Const ST77XX_PTLAR = 0x30
Const ST77XX_COLMOD = 0x3a
Const ST77XX_MADCTL = 0x36

Const ST77XX_MADCTL_MY = 0x80
Const ST77XX_MADCTL_MX = 0x40
Const ST77XX_MADCTL_MV = 0x20
Const ST77XX_MADCTL_ML = 0x10
Const ST77XX_MADCTL_RGB = 0x00

Const ST77XX_RDID1 = 0xda
Const ST77XX_RDID2 = 0xdb
Const ST77XX_RDID3 = 0xdc
Const ST77XX_RDID4 = 0xdd

Const ST7789_240x240_XSTART = 0
Const ST7789_240x240_YSTART = 80

Const BLACK = 0x0000
Const BLUE = 0x001f
Const RED = 0xf800
Const GREEN = 0x07e0
Const CYAN = 0x07ff
Const MAGENTA = 0xf81f
Const YELLOW = 0xffe0
Const WHITE = 0xffff

Dim _width As Byte  ///< Display width as modified by current rotation
Dim _height As Byte  ///< Display height as modified by current rotation
Dim _xstart As Byte  ///< Internal framebuffer X offset
Dim _ystart As Byte  ///< Internal framebuffer Y offset
Dim _colstart As Byte  ///< Some displays need this changed to offset
Dim _rowstart As Byte  ///< Some displays need this changed to offset
Dim 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 x As Byte
OSCCON = 0x70

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

    x = 1
    While x = 1
    WaitMs 500
    LATA.1 = 1
    Call fillScreen(WHITE)
    WaitMs 500
    LATA.1 = 0
    Call fillScreen(GREEN)
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 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(ST77XX_SWRESET)
    WaitMs 150
    Call SPI_sendcmd(ST77XX_SLPOUT)
    WaitMs 10
    Call SPI_sendcmd(ST77XX_COLMOD)
    Call SPI_senddat(0x55)
    WaitMs 10
    Call SPI_sendcmd(ST77XX_MADCTL)
    Call SPI_senddat(0x8)
    Call SPI_sendcmd(ST77XX_CASET)
    Call SPI_senddat(0x0)
    Call SPI_senddat(0x0)
    Call SPI_senddat(0x0)
    Call SPI_senddat(240)
    Call SPI_sendcmd(ST77XX_RASET)
    Call SPI_senddat(0x0)
    Call SPI_senddat(0x0)
    Call SPI_senddat(0x0)
    Call SPI_senddat(240)
    Call SPI_sendcmd(ST77XX_INVON)
    WaitMs 10
    Call SPI_sendcmd(ST77XX_NORON)
    WaitMs 10
    Call SPI_sendcmd(ST77XX_DISPON)
    WaitMs 10

    
    _colstart = ST7789_240x240_XSTART
    _rowstart = ST7789_240x240_YSTART
    _height = 240
    _width = 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 + _xstart
y = y + _ystart

Call SPI_sendcmd(ST77XX_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(ST77XX_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 >= _width Or y >= _height Then Exit
tmp = x + _W - 1
If tmp >= _width Then _W = _width - x
tmp = x + h - 1
If tmp >= _height Then h = _height - 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, _width, _height, color)
End Proc
 
Const BLACK = 0x0000
Const RED = 0x001f
Const BLUE = 0xf800
Const GREEN = 0x07e0
Const YELLOW = 0x07ff
Const MAGENTA = 0xf81f
Const CYAN = 0xffe0
Const WHITE = 0xffff


These work..
 
Congratulations on getting it up and running!. As soon as possible, I will mount the display again on the test PCB and test it. In the latest IDE updates, they increased the number of input arguments that functions can accept, which makes writing the code for the libraries to work with the display much more convenient now.
 
Remember, The one I have here has no CS so I didn't bother coding it.

Also I saw in the C code RASET was 320, well mine's a 240x240 so that was wrong as well.
I just couldn't follow the Arduino code.. All that BS about a bazillion commands.. Only 9!! much easier to just do "Old School"

I'll be doing the ST7735 next, However! I'm nt even sure my other LCD is a st7735... We'll see.

Please look at the MADCTL I have the BGR format on... BUT I think the author wanted to select ROW bottom to top which is D4 not D3 as in this code.
 
I modified the 240x240 C library to work with 320x320 some time ago, so that shouldn't be an issue. The key is to establish communication with the display and initialize it to ensure it responds. Everything else will come together with a bit of work.
 
Going nuts here!! I have an LCD here and it's 320 x 240.. The ad said it has an ST7735R controller, but it cannot have!! The systronics datasheet for that controller only reaches 160 x 132, so it must be something else..

AND!! I doesn't appear to be a ST7789...
 
Cool!

I spoke via email to Vladimir.. He has given me a new set of licences as I couldn't renew any software as my email was wayyy out.

So I have the latest gear now. . I am also getting to grips with "External Modules", according to Vlad I can use C to program them... He's given me the links.
 
That's fantastic! I was also researching external modules, but in the end, I decided to focus on suggesting improvements to the language, which were added some time ago. That's why it's important to use an updated version of the IDE. If the language is used in a general way, these improvements might not be very noticeable, but when it's used more deeply or in complex scenarios, the new enhancements become evident, especially when writing libraries.
 
Also!! 320 x 240 is a lot..
In the fillrect function you find pix which is a word.. ergo max 65536..
BUTT!! 320 x 240 is 86.4 k so it'll need to be a long.
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…