Share My Creation OLED Voltmeter

Oled display of text and bitmap and analog bar. The code uses wire lib only to drive the I2C, the text and bitmap and analog bar are generated by functions in the code.
The bitmap code is for size 48x16 for other sizes the code has to be modified.
Connections: voltage input - pin A0, SCL - pin A5, SDA - pin A4
To convert image to code:
On a PC (MS Paint) resize image to 48x16 pixels and save as monochrom bmp.
Download LCD Assistant app from http://en.radzio.dxp.pl/bitmap_converter/
Set it to Byte Orientation - Vertical, Pixels/byte - 8. Save file as text file and copy the bitmap array to the code page.
B4X:
Sub Process_Globals
    Public Serial1 As Serial
    Private master As WireMaster
    Private const addr As Byte = 0x3C
    Private tmr As Timer
    Private in As Pin  
    Private font() As Byte = Array As Byte( _
    0x3E, 0x41, 0x41, 0x41, 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
    0x1F, 0x20, 0x40, 0x20, 0x1F, _ 'V
    0x00, 0x00, 0x60, 0x60, 0x00)    ':  
   
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    tmr.Initialize("tmr_Tick", 1000)
    in.Initialize(in.A0, in.MODE_INPUT)
    tmr.Enabled = True
    master.Initialize
    oled_init
    Delay(1000)
    clr_screen
    drawChar2(10,3,7)    'V
    drawChar2(11,3,2)    '.
    drawBMP(0,40)

End Sub

Sub tmr_Tick
    Dim volt As Int
    Dim d(4), i, j As Byte
    Dim c() As Byte = Array As Byte(0x40,0xF,0xF,0xF,0xF)
    Dim e() As Byte = Array As Byte(0x40,0,0,0,0)
    volt=in.AnalogRead
        'draw the bar
    For j=0 To volt/32
        For i=0 To 3
            command(0x21)     'col addr
            command(0+j*4) 'col start
            command(4+j*4)  'col End
            command(0x22)    '0x22
            command(7) ' Page start
            command(7) ' Page End
            master.WriteTo(addr,c)
        Next
    Next
    For j=0 To 32-volt/32
        For i=0 To 3
            command(0x21)     'col addr
            command(volt/8+j*4) 'col start
            command(volt/8+4+j*4)  'col End
            command(0x22)    '0x22
            command(7) ' Page start
            command(7) ' Page End
            master.WriteTo(addr,e)
        Next
    Next
        'digital DVM
    volt=volt*5
    For i=0 To 2
        d(i)=volt Mod 10
        drawChar2(d(i),3,5-i)
        volt=volt/10
    Next
    d(3)=volt Mod 10
    drawChar2(d(3),3,1)
   
End Sub

    'size 1 chars
Sub drawChar(fig As Byte, y As Byte, x As Byte)
    Dim fb(6), i As Byte

    command(0x21)    'col addr
    command(7 * x)    'col start
    command(7 * x + 4)    'col End
    command(0x22)
    command(y)    ' Page start
    command(y+1)    ' Page End
    For i=0 To 4
        fb(i+1)=(font(5*fig+i))
    Next
    fb(0)=0x40
    master.WriteTo(addr,fb)
 End Sub

   'size 2 chars
Sub drawChar2(fig As Byte, y As Byte, x As Byte)
    Dim i, line As Byte
    Dim btm, top As Byte
       Dim fb(21) As Byte
   
    command(0x20)    ' vert mode
    command(0x01)

    command(0x21)    'col addr
    command(13 * x)    'col start
    command(13 * x + 9)    'col End
    command(0x22)
    command(y)    ' Page start
    command(y+1)    ' Page End

    For    i=0 To 4
        line=font(5*fig+i)
        btm=0
        top=0
 
        If line Mod 2=1 Then top=top+12
        line=line/2
        If line Mod 2=1 Then top=top+48
        line=line/2
        If line Mod 2=1 Then top=top+192
        line=line/2
        If line Mod 2=1 Then btm=btm+3
        line=line/2
        If line Mod 2=1 Then btm=btm+12
        line=line/2
        If line Mod 2=1 Then btm=btm+48
        line=line/2
        If line Mod 2=1 Then btm=btm+192
       
        fb(1+i*4)=top
        fb(2+i*4)=btm
        fb(3+i*4)=top
        fb(4+i*4)=btm
         
    Next
    fb(0)=0x40
    master.WriteTo(addr,fb)

    command(0x20)    'horizontal mode
    command(0x00)
       
End Sub

Sub command(cmnd As Byte)
    Dim cb(2) As Byte
    cb(0)=0
    cb(1)=cmnd
    master.WriteTo(addr,cb)
End Sub

Sub oled_init
    command(0xAE)   'DISPLAYOFF
    command(0x8D)   '       CHARGEPUMP *
    command(0x14)    ' 0x14-pump on
    command(0x20)    '      MEMORYMODE
    command(0x0)     ' 0x0=horizontal, 0x01=vertical, 0x02=page
    command(0xA1)    '    SEGREMAP * A0/A1=top/bottom
    command(0xC8)    ' COMSCANDEC * C0/C8=left/right
    command(0xDA)     '     SETCOMPINS *
    command(0x12)   '0x22=4rows, 0x12=8rows
    command(0x81)     '    SETCONTRAST
    command(0x9F)     '0x8F
   
    command(0xAF)       '   DISPLAYON
End Sub

Sub clr_screen
    Dim x, y As Byte
    Dim c() As Byte = Array As Byte(0x40,0,0,0,0,0,0,0,0)
   
    For  y = 0 To 7
        For x = 0 To 15
            command(0x21)     'col addr
            command(8 * x) 'col start
            command(8 * x + 7)  'col End
            command(0x22)    '0x22
            command(y) ' Page start
            command(y) ' Page End
            master.WriteTo(addr,c)

        Next
    Next
End Sub

Sub drawBMP(y As Byte, x As Byte)
    Dim page, h, i As Byte
    Dim bmp() As Byte = Array As Byte( _
    0xFF, 0xFF, 0xFF, 0x3F, 0x1F, 0x0F, 0x07, 0x07, 0x03, 0x03, 0x03, 0x01, 0x01, 0x11, 0xF1, 0x91, _
    0x90, 0x90, 0xF0, 0x60, 0x00, 0x00, 0x80, 0xC0, 0x60, 0xF0, 0xF0, 0x00, 0x00, 0x10, 0xF0, 0xF1, _
    0x11, 0x11, 0x11, 0xF1, 0xE1, 0x03, 0x03, 0x03, 0x07, 0x07, 0x0F, 0x1F, 0x3F, 0xFF, 0xFF, 0xFF, _
    0xFF, 0xFF, 0xFF, 0xFC, 0xF8, 0xF0, 0xE0, 0xE0, 0xC0, 0xC0, 0xC0, 0x80, 0x80, 0x88, 0x8F, 0x88, _
    0x08, 0x08, 0x0D, 0x07, 0x00, 0x03, 0x03, 0x02, 0x02, 0x0F, 0x0F, 0x02, 0x02, 0x08, 0x0F, 0x0F, _
    0x89, 0x83, 0x87, 0x8F, 0x8C, 0xC8, 0xC0, 0xC0, 0xE0, 0xE0, 0xF0, 0xF8, 0xFC, 0xFF, 0xFF, 0xFF)
   
    For page=0 To 1
        For h=0 To 2  
            Dim c(17) As Byte
            c(0)=0x40
            For i=1 To 16
                c(i)=bmp((i-1)+16*h+48*page)
            Next

            command(0x21)     'col addr
            command(h*16+x) 'col start
            command(h*16+x+16)  'col End
            command(0x22)    '0x22
            command(y+page) ' Page start
            command(y+1+page) ' Page End
            master.WriteTo(addr,c)

        Next
    Next
End Sub
 

Attachments

  • oled_dvm.zip
    2.1 KB · Views: 74
  • oled_dvm400.jpg
    oled_dvm400.jpg
    28.4 KB · Views: 190
Last edited:
Top