TFT display of digital temperature and humidity. Sensor DHT11 , TFT module is a 1.8" 128 X160 pixel and has the driver ST7735 is driven by SPI (C code).
B4X:
Sub Process_Globals
Public Serial1 As Serial
Public dht1 As dht
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=0x000
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
Public ORANGE As UInt=0xFC00
Public bgr As UInt=0x0000
Public sc As UInt=0xFFFF
Private spires, spidata As Byte
Private in As Pin
Private yt=2, yh=140, ys=26, xf=83, xc=53 As Byte
Private humid,temp As Byte
Private font() As Byte = Array As Byte( _
0x3E, 0x51, 0x49, 0x45, 0x3E, _ ' 0
0x00, 0x42, 0x7F, 0x40, 0x00, _ ' 1
0x72, 0x49, 0x49, 0x49, 0x46, _
0x21, 0x41, 0x49, 0x4D, 0x33, _
0x18, 0x14, 0x12, 0x7F, 0x10, _
0x27, 0x45, 0x45, 0x45, 0x39, _ ' 5
0x3C, 0x4A, 0x49, 0x49, 0x31, _
0x41, 0x21, 0x11, 0x09, 0x07, _
0x36, 0x49, 0x49, 0x49, 0x36, _
0x46, 0x49, 0x49, 0x29, 0x1E, _ ' 9
0x00, 0x06, 0x09, 0x09, 0x06, _ ' degree symbol
0x3E, 0x41, 0x41, 0x41, 0x22, _ ' C
0x00, 0x00, 0x00, 0x00, 0x00, _ ' (space)
0x00, 0x60, 0x60, 0x00, 0x00, _ ' .
0x7F, 0x09, 0x09, 0x01, 0x01, _ ' F
0x23, 0x13, 0x08, 0x64, 0x62) ' %
End Sub
Private Sub AppStart
Serial1.Initialize(115200)
in.Initialize(4,in.MODE_INPUT)
tmr.Initialize("tmr_Tick", 5000)
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,127,159,bgr) 'bacbground
ball(52,ys+100,RED)
draw(26,yt,10,GREEN,2) 'deg
draw(38,yt,11,GREEN,2) 'C
draw(103,yt,10,GREEN,2) 'deg
draw(115,yt,14,GREEN,2) 'F
draw(38,yh,15,GREEN,2) '%
'scale C
rectan(xc,ys,xc,ys+100,sc)
For i=0 To 5
rectan(xc-5,ys+i*20,xc,ys+i*20,sc)
draw(xc-20,ys+i*20-2,5-i,sc,1)
draw(xc-14,ys+i*20-2,0,sc,1)
Next
rectan(xc-20,ys+98,xc-15,ys+106,bgr)
'scale F
rectan(xf-9,ys,xf-9,ys+100,sc)
For i=0 To 5
rectan(xf-9,ys+i*20,xf-4,ys+i*20,sc)
Next
draw(xf,ys-2,1,sc,1)
draw(xf+6,ys-2,2,sc,1)
draw(xf+12,ys-2,2,sc,1) '122
draw(xf,ys+18,1,sc,1)
draw(xf+6,ys+18,0,sc,1)
draw(xf+12,ys+18,4,sc,1) '104
draw(xf,ys+38,8,sc,1)
draw(xf+6,ys+38,6,sc,1)
draw(xf,ys+58,6,sc,1)
draw(xf+6,ys+58,8,sc,1)
draw(xf,ys+78,5,sc,1)
draw(xf+6,ys+78,0,sc,1)
draw(xf,ys+98,3,sc,1)
draw(xf+6,ys+98,2,sc,1) '32
End Sub
Sub tmr_Tick
Dim d, d1 As Byte
Dim df As UInt
dht1.Read11(in.PinNumber) 'Reading the DHT11 measure
humid=dht1.GetHumidity 'Get humidity from readed measure
temp=dht1.GetTemperature 'Get temperature from readed measure
' Log("Humidity = ",humid, " %", " Temperature =",temp, " Cº")
'deg C
d1 = temp / 10
d = d1 Mod 10
rectan(2,yt,12,yt+16,bgr)
draw(2,yt,d,CYAN,2)
d = temp Mod 10
rectan(14,yt,24,yt+16,bgr)
draw(14,yt,d,CYAN,2)
rectan(59,ys,69,ys+100,bgr)
rectan(59,ys+100-temp*2,69,ys+100,RED)
'deg F
df=temp*90
df=df/5+320
d1=df/1000
d=d1 Mod 10
If d>0 Then draw(70,yt,d,MAGENTA,2) Else rectan(70,yt,80,yt+16,bgr)
d1=df/100
d=d1 Mod 10
rectan(80,yt,90,yt+16,bgr)
draw(80,yt,d,MAGENTA,2)
d1=df/10
d=d1 Mod 10
rectan(92,yt,102,yt+16,bgr)
draw(92,yt,d,MAGENTA,2)
'humid
d1 = humid / 10
d = d1 Mod 10
rectan(12,yh,22,yh+16,bgr)
draw(12,yh,d,ORANGE,2)
d = humid Mod 10
rectan(24,yh,34,yh+16,bgr)
draw(24,yh,d,ORANGE,2)
End Sub
Sub ball(x As Byte, y As Byte, hue As UInt)
Dim i As Byte
rectan(x+9,y,x+14,y+1,hue) 'top
For i=0 To 4
rectan(x+6-i,y+i+1,x+i+17,y+i+2,hue)
Next
rectan(x+1,y+6,x+22,y+9,hue)
rectan(x,y+9,x+23,y+15,hue)
rectan(x+1,y+15,x+22,y+17,hue)
For i=0 To 4
rectan(x+2+i,y+i+17,x+21-i,y+i+18,hue)
Next
rectan(x+9,y+23,x+14,y+23,hue) 'bottom
End Sub
Sub draw(x As Byte, y As Byte, c As Byte, color As UInt, size As Byte)
Dim i, j, line As Byte
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
'send_data(0x06) '18 bits
command(0x36) 'Memory access ctrl (directions)
send_data(0x10) 'horizontal-0B1100000=0x60, vertical-0B00010000=0x10
'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
b4r_main::_spires = SPDR; // received byte
}
void set_spi(B4R::Object* o)
{
SPCR = 0B1011100; // Enable SPI, Master, mode3, set clock rate fck/4 = 4MHz
//SPSR = 1; //set clock rate fck/2=2MHz
}
#End if