'*****************************************************
'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