Share My Creation TFT Voltmeter

The code doesn't use any library, the SPI is set by inline C. Using 1.8" TFT with ST7735 driver, displaying universal 5x7 fonts for 0 to 9. The code works only for ATMEGA328, I used Arduino Nano.
Voltage input at A0. Bitmap is BMP of 16 (5:6:5) bit color, it uses about 85% of the RAM.
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

'  analog input pin A0

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 in As Pin
    Private result=0 As Byte
    Private ib As UInt
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)
    in.Initialize(in.A0, in.MODE_INPUT)
    RunNative ("set_spi",Null)
    TFTinit
    rectan(0,0,159,127,WHITE)    'clear screen

    bitmap
    rectan(35,74,39,78,BLUE)    'dot
    drawC(135,50,10,BLUE,4)    'V
   
End Sub

Sub bitmap
    Dim c1, c2 As Byte
    Dim i As UInt
   
    rectan(66,6,99,39,MAGENTA)
    rectan(68,8,97,37,WHITE)
    area(70,10,95,35)
    For i=0 To 1351 Step 2
        ib=i
        RunNative("load", Null)
        c1=result
        ib=i+1
        RunNative("load", Null)
        c2=result

        DC.DigitalWrite(True)  
        CS.DigitalWrite(False)
        spi(c2)
        spi(c1)
        CS.DigitalWrite(True)

    Next
End Sub

Sub tmr_Tick  
    Dim volt  As Int
    Dim d(4) As Byte
   
    volt=in.AnalogRead
    volt=volt*5
    rectan(0,105,159,115,WHITE)    'bar
    rectan(0,105,volt/32,115,GREEN)    'bar
        'digital dvm
    For i=0 To 2
        d(i)=volt Mod 10
        rectan(105-i*30,50,128-i*30,80,WHITE)
        drawC(105-i*30,50,d(i),RED,4)
        volt=volt/10
    Next
    d(3)=volt Mod 10
    rectan(10,50,33,80,WHITE)
    drawC(10,50,d(3),RED,4)
   
End Sub
        'draw char
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 , 0x1F, 0x20, 0x40, 0x20, 0x1F)
  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(0x60)    '0B1100000=0x60
    'send_data(0x08)    '0x48=left to right,BRG,horizontal   0x40=vertical,RGB
      '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
    }
    #End if
   
    #if C
    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
   

    #if C

    const unsigned char bmp[] = {
0xBE,0xF7,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF
,0xBF,0xC7,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF
,0xFF,0xFF,0x7A,0xB6,0xDF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF
,0xFF,0xFF,0xBE,0xF7,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF
,0xFF,0xFF,0xFF,0xF7,0xBD,0x1E,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF
,0xFF,0xFF,0xFF,0xFF,0xFC,0xCE,0x17,0x75,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF
,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF
,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x1E,0x7F,0x3C,0x06,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF
,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xF3,0x3B,0xF9,0x9D,0xFF,0xFF,0xFF,0xFF
,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xBF,0xC7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F,0x07,0x3F,0x07,0x3F,0x07,0x1F,0x07,0x1F,0x0F,0xFF,0xE7,0xFF,0xFF,0xFF,0xFF
,0xBE,0xF7,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF
,0xFF,0xFF,0x19,0xA6,0x32,0x4C,0xEF,0x1A,0x8D,0x02,0x6D,0x02,0x2C,0x02,0x6C,0x0A
,0x13,0x0C,0xDA,0x05,0x9C,0x06,0xFE,0x06,0x1F,0x07,0x1F,0x07,0x7F,0x57,0xFF,0xFF
,0xFF,0xFF,0xDF,0xFF,0xBE,0xF7,0xBE,0xF7,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF
,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x7E,0xE7,0xDB,0xC6,0xF9,0xA5
,0x56,0x85,0xB4,0x64,0x32,0x5C,0xF2,0x5B,0x73,0x5C,0x36,0x6D,0xF9,0x85,0xFD,0x9E
,0xFF,0xF7,0xFF,0xFF,0xDF,0xFF,0x9E,0xF7


    };

    void load (B4R::Object* o) {
       b4r_main::_result = bmp[b4r_main::_ib];    //b4r_main::_n1 + b4r_main::_n2;
    }
    #End if
 

Attachments

  • tft_dvm.zip
    3.8 KB · Views: 370
  • tft_dvm405.jpg
    20.3 KB · Views: 1,251
Last edited:
Cookies are required to use this site. You must accept them to continue using the site. Learn more…