Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'Public variables can be accessed from all modules.
    Public Serial1 As Serial
    
    Private LCD_CLEAR_SCREEN  As Byte = 0x01    ' Clear screen
    Private LCD_ADDRESS_RESET  As Byte = 0x02    ' The address counter is reset
    Private LCD_BASIC_FUNCTION  As Byte = 0x30    ' Basic instruction set
    Private LCD_EXTEND_FUNCTION  As Byte = 0x34    ' Extended instruction set
    
    Private ST7920_SS As Pin 'data/command pin on LCD is now SS(slave select)
    Private ST7920_RST As Pin 'reset
    
'    Private SPI_MODE0 As Byte = 0
'    Private SPI_MODE1 As Byte = 1
'    Private SPI_MODE2 As Byte = 2
'    Private SPI_MODE3 As Byte = 3
'   
'    Private SPI_LSBFIRST As Byte = 0
'    Private SPI_MSBFIRST As Byte = 1
'   
    Private LOW As Boolean = False
    Private HIGH As Boolean = True
    
End Sub
Private Sub AppStart
    
    Serial1.Initialize(115200)
    Log("AppStart")
    ST7920_SS.Initialize (10,ST7920_SS.MODE_OUTPUT)
    ST7920_RST.Initialize(8,ST7920_RST.MODE_OUTPUT)
    SPI.begin
    ST7920_SS.DigitalWrite(LOW)
    STN7920_init
    SetGraphicsMode(True)
    SetGraphicsMode(False)
'    ST7920_DisplayString("hello world",1)
    ST7920_Write(0x93,True)
    ST7920_Write(0x20,False) 'space
    ST7920_Write(0x41,False) 'letter A
End Sub
private Sub ST7920_Write(LCDdata As Byte, Command As Boolean)
    Dim tArray (3) As Byte
    If Command Then
        tArray(0) = (0xFA)
    Else
        tArray(0)= (0xF8)
    End If
    tArray (1) = (Bit.And(LCDdata,0xF0))
    tArray (2) =(Bit.And(Bit.ShiftLeft(LCDdata,4) ,0xF0))
    SPI.BeginTransaction(2000000,SPI.MSBFIRST,SPI.SPI_MODE(3))
    ST7920_SS.DigitalWrite(HIGH)
    For m = 0 To 2
        SPI.Transfer(tArray(m))
    Next
    ST7920_SS.DigitalWrite(LOW)
    SPI.EndTransaction
    
'    SPI.BeginTransaction(2000000,SPI.MSBFIRST,SPI.SPI_MODE(3))
'    ST7920_SS.DigitalWrite(HIGH)
'    If Command Then
'        SPI.Transfer(0xFA)
'    Else
'        SPI.Transfer(0xF8)
'       
'    End If
'    SPI.Transfer(Bit.And(LCDdata,0xF0))
'    SPI.Transfer(Bit.And(Bit.ShiftLeft(LCDdata,4) ,0xF0))
'    'Log (LCDdata,"-",(Bit.And(LCDdata,0xF0)),"-",(Bit.And(Bit.ShiftLeft(LCDdata,4) ,0xF0)))
'    ST7920_SS.DigitalWrite(LOW)
'    'SPI.EndTransaction
End Sub
    
private Sub STN7920_init()
    ST7920_RST.DigitalWrite(LOW)
    Delay(100)
    ST7920_RST.DigitalWrite(HIGH)
    ST7920_Write(LCD_BASIC_FUNCTION,True)
    ST7920_Write(LCD_CLEAR_SCREEN,True)
    ST7920_Write(0x06,True)
    ST7920_Write(0x0C,True)
End Sub
private Sub SetGraphicsMode(Enabled As Boolean)
    If Enabled Then
        ST7920_Write(LCD_EXTEND_FUNCTION,True)
        ST7920_Write(Bit.Or(LCD_EXTEND_FUNCTION,0x02),True)
    Else
        ST7920_Write(LCD_BASIC_FUNCTION,True)
    End If
End Sub
private Sub ST7920_DisplayString(StrData() As Byte, Row As Byte)
Dim Addr As Byte
Select Row
    Case 1
        Addr = 0x90
    Case 2
        Addr = 0x88
    Case 3
        Addr = 0x98
    Case Else
        Addr = 0x80
End Select
ST7920_Write(Addr,True)
Dim Length As Byte = StrData.Length
For i = 0 To Length - 1
    ST7920_Write(StrData(i),False)
Next
End Sub