The code doesn't use any library, the SPI is set by inline C. There are functions for initializing ST7735 1.8'' module, displaying characters and part of universal 5x7 fonts for 0 to 9 only, letters can be added if needed. The code works only for ATMEGA328, I used Arduino Nano.
B4X:
' TFT module with ST7735 driver
' 1 LED 3.3 V or 5V via 47 ohm
' 2 SCK 13
' 3 SDA 11
' 4 A0 Or DC 9
' 5 RESET 8
' 6 CS 10
' 7 GND GND
' 8 VCC 5 V
Sub Process_Globals
Public Serial1 As Serial
Private tmr As Timer
Public CS As Pin
Public DC As Pin
Public SDA As Pin
Public SCK As Pin
Public RST As Pin
Public BLACK As UInt=0x0000
Public BLUE As UInt=0x01F
Public RED As UInt=0xF800
Public GREEN As UInt=0x07E0
Public CYAN As UInt=0x07FF
Public MAGENTA As UInt=0xF81F
Public YELLOW As UInt=0xFFE0
Public WHITE As UInt=0xFFFF
Private spires, spidata As Byte
Private d(5) As Byte
End Sub
Private Sub AppStart
Serial1.Initialize(115200)
tmr.Initialize("tmr_Tick", 1000)
tmr.Enabled = True
CS.Initialize(10, CS.MODE_OUTPUT)
DC.Initialize(9, DC.MODE_OUTPUT)
SCK.Initialize(13, SCK.MODE_OUTPUT)
RST.Initialize(8, RST.MODE_OUTPUT)
SDA.Initialize(11, SDA.MODE_OUTPUT)
RunNative ("set_spi",Null)
TFTinit
rectan(0,0,159,127,WHITE)
rectan(100,70,104,74,BLUE)
rectan(100,56,104,60,BLUE)
rectan(35,70,39,74,BLUE)
rectan(35,56,39,60,BLUE)
End Sub
Sub tmr_Tick
d(0)=d(0)+1
If d(0)>9 Then
d(0)=0
d(1)=d(1)+1
End If
If d(1)>5 Then
d(1)=0
d(2)=d(2)+1
End If
If d(2)>9 Then
d(2)=0
d(3)=d(3)+1
End If
If d(3)>5 Then
d(3)=0
d(4)=d(4)+1
End If
If d(4)>9 Then
d(4)=0
End If
rectan(135,50,160,80,WHITE)
drawC(135,50,d(0),RED,4)
rectan(110,50,135,80,WHITE)
drawC(110,50,d(1),RED,4)
rectan(70,50,95,80,WHITE)
drawC(70,50,d(2),RED,4)
rectan(45,50,70,80,WHITE)
drawC(45,50,d(3),RED,4)
rectan(5,50,30,80,WHITE)
drawC(5,50,d(4),RED,4)
End Sub
Sub drawC(x As Byte, y As Byte, c As Byte, color As UInt, size As Byte)
Dim i, j, line As Byte
Dim font() As Byte = Array As Byte(0x3E, 0x41, 0x41, 0x41, 0x3E, 0x00, 0x42, 0x7F, 0x40, 0x00, 0x72, 0x49, 0x49, 0x49, 0x46, 0x21, 0x41, 0x49, 0x4D, 0x33,0x18, 0x14, 0x12, 0x7F, 0x10,0x27, 0x45, 0x45, 0x45, 0x39,0x3C, 0x4A, 0x49, 0x49, 0x31,0x41, 0x21, 0x11, 0x09, 0x07,0x36, 0x49, 0x49, 0x49, 0x36,0x46, 0x49, 0x49, 0x29, 0x1E )
For i=0 To 5
If i = 5 Then
line = 0
Else
line = font((c*5)+i)
For j = 0 To 7
If (line Mod 2)=1 Then
If size = 1 Then
pixel(x+i, y+j, color)
Else
rectan(x+(i*size), y+(j*size), x+(i*size)+size, y+(j*size)+size, color)
End If
End If
line = line / 2
Next
End If
Next
End Sub
Sub spi(data As Byte) 'send byte over spi
spidata=data
RunNative ("spi",Null)
Return
End Sub
Sub command(cmd As Byte)
DC.DigitalWrite(False) 'command Mode
CS.DigitalWrite(False) 'Select the LCD (active low)
spi(cmd)'set up data on bus
CS.DigitalWrite(True) 'Deselect LCD (active low)
End Sub
Sub send_data(data As Byte)
DC.DigitalWrite(True) 'data mode
CS.DigitalWrite(False) 'Select the LCD (active low)
spi(data) 'set up data on bus
CS.DigitalWrite(True) 'Deselect LCD (active low)
End Sub
Sub TFTinit
Dim i As Byte
RST.DigitalWrite(True) 'hardware reset
Delay(200)
RST.DigitalWrite(False)
Delay(10)
RST.DigitalWrite(True)
Delay(10)
command(0x01) 'sw reset
Delay(200)
command(0x11) ' Sleep out
Delay(200)
command(0x3A) 'color mode
send_data(0x05) '16 bits
command(0x36) 'Memory access ctrl (directions)
send_data(0x60) '0B1100000
'command(0x21) 'color inversion on
command(0x2D) 'color look up table
send_data(0)
For i = 1 To 31
send_data(i * 2)
Next
For i = 0 To 63
send_data(i)
Next
send_data(0)
For i = 1 To 31
send_data(i * 2)
Next
command(0x13) 'Normal display on
command(0x29) 'Main screen turn on
End Sub
Sub area(x0 As Byte, y0 As Byte, x1 As Byte, y1 As Byte)
command(0x2A) 'Column addr set
send_data(0x00)
send_data(x0) ' XSTART
send_data(0x00)
send_data(x1) ' XEND
command(0x2B) 'Row addr set
send_data(0x00)
send_data(y0) ' YSTART
send_data(0x00)
send_data(y1) ' YEND
command(0x2C) 'write To RAM
End Sub
Sub rectan(x0 As Byte, y0 As Byte, x1 As Byte, y1 As Byte, color As UInt)
Dim i As Int
area(x0,y0,x1,y1)
For i=(y1 - y0 + 1) * (x1 - x0 + 1) To 0 Step -1
DC.DigitalWrite(True) ' data mode
CS.DigitalWrite(False)
spi(color / 256)
spi(color Mod 256)
CS.DigitalWrite(True)
Next
End Sub
Sub pixel(x As Byte, y As Byte, color As UInt)
area(x,y,x+1,y+1)
send_data(color / 256)
send_data(color Mod 256)
End Sub
#if C
void spi(B4R::Object* o)
{
SPDR = b4r_main::_spidata; // Start transmission
while (!(SPSR & _BV(SPIF))); // Wait For transmission To complete
//Return SPDR; // received byte
b4r_main::_spires = SPDR;
}
#End if
#if C
void set_spi(B4R::Object* o)
{
SPCR = 0B1011101; // Enable SPI, Master, mode3, set clock rate fck/16
SPSR = 1; //set clock rate fck/2=2MHz
}
#End if
Attachments
Last edited: