# Authored by Jonathan Scott James the great On WOKWI
# LCD 1602 8 bit bitbang For general purpose interface as car tachometer voltage meter chronometer frequency counter waveform monitor for generator
machine.Pin(0, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit0
machine.Pin(1, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit1 
machine.Pin(2, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit2
machine.Pin(3, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit3
machine.Pin(4, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit4
machine.Pin(5, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit5
machine.Pin(6, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit6
machine.Pin(7, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit7
machine.Pin(8, machine.Pin.OUT, machine.Pin.PULL_DOWN) #enable
machine.Pin(9, machine.Pin.OUT, machine.Pin.PULL_DOWN) #read/weite 
machine.Pin(10, machine.Pin.OUT, machine.Pin.PULL_DOWN) #register select
machine.Pin(26, machine.Pin.IN) #ADC
machine.Pin(27, machine.Pin.IN, machine.Pin.PULL_DOWN) #key 2 bottom key
machine.Pin(28, machine.Pin.IN, machine.Pin.PULL_DOWN) #key 1 top key
ttt=1;t=3600*24*366+28;t=3600*24*22-9 # python sets the precisio level when you first assign the number according to what will fit in the value you assign it to.
def tickjsj():
    global t
    t=t+1
machine.Timer().init(period=1000, callback=lambda t:tickjsj())
#instructions abd their formats
#*****************************
#  ClearDisplay
# rs rw d7 d6 d5 d4 d3 d2 d1 d0
# 0  0  0  0  0  0  0  0  0  1 
#  Clears entire display and returns the cursor to home position(address0).
#****************************
#  ReturnHome
# rs rw d7 d6 d5 d4 d3 d2 d1 d0
# 0  0  0  0  0  0  0  0  1  x 
#  Return the cursor to the home position.Also returns the
#  display being shifted to the original position.DDRAM
#  contents remain unchanged.
#****************************
#  Entrymode set
# rs rw d7 d6 d5 d4 d3 d2 d1 d0
# 0  0  0  0  0  0  0  1 I/D S
#  Set cursor move direction and specifies display shift.These
#  operations are preformed during data write/read.For normal operation,set S to zero.
#  I/D=1:increment;I/D=0:decrement;S=1:shift insert text mode ,for normal operation,sett ozero.
#****************************
#  Display ON/OFF control
# rs rw d7 d6 d5 d4 d3 d2 d1 d0
# 0  0  0  0  0  0  1  D  C  B
#  (D)display ON/OFF  (C)cursorON/OFF  (B)blink cursor
#   D=1:display ON;D=0:display OFF.C=1:cursor ON;C=0:cursor OFF.B=1:blink cursor ON;B=0:blink cursor OFF.
#****************************
#  Cursoror Display shift
# rs rw d7 d6 d5 d4 d3 d2 d1 d0
# 0  0  0  0  0  1 S/C R/L x x
#  Move the cursor and shift the display without changing DDRAM contents.
#  S/C=1:Display shift;S/C=0:Cursor move.R/L=1:shift to right;R/L=0:shift to left.
#****************************
#  Function  Set 
# rs rw d7 d6 d5 d4 d3 d2 d1 d0
# 0  0  0  0  1  DL N  F  x  x
#  Set the interface data length(DL). Number of display lines(N). character font(F). data buss bits (DL)
#  DL=1: 8 bits ; DL=0: 4bits.
#  N=1: 2 lines ; N=0: 1 lines.
#  F=1: 5dot x 10dot characters ; F=0: 5dot x 7dot characters.
#****************************
#  Set CGRAM address
# rs  rw  d7  d6  d5  d4  d3  d2  d1  d0
# 0   0   0   1   cA0 cA1 cA2 cA3 cA4 cA5
#  custom character(CGRAM) address. Custom characters patterns are stored in these bytes. Is how to define a custom character.
#****************************
#  Set DDRAM Address 
# rs  rw  d7  d6  d5  d4  d3  d2  d1  d0
# 0   0   1   AC6 AC5 AC4 AC3 AC2 AC1 AC0 
#  Set position in display where next character will ve written DDRAM address in address counter
#****************************
#  Readbusy flag & address
# rs  rw  d7  d6  d5  d4  d3  d2  d1  d0
# 0   1   BF  AC6 AC5 AC4 AC3 AC2 AC1 AC0 
#  Reads BusyFlag(BF) indicating internal operationis being performed and reads address counter contents.
#  BF=1:internally operating. 0:can accept instructions
#****************************
#  Write character to dispay at current position then incrimnent/dectiment CG/DDRAM
# rs  rw  d7  d6  d5  d4  d3  d2  d1  d0
# 1   0   d0  d1  d2  d3  d4  d5  d6  d7
#  Writes character code to dispay then incrimnent/dectiment position
#****************************
#  Reads character code from dispay at current position ,, then maybe incrimnent/dectiment CG/DDRAM
# rs  rw  d7  d6  d5  d4  d3  d2  d1  d0
# 1   1   d0  d1  d2  d3  d4  d5  d6  d7
#  reads character code from current dispay Position ,, then maybe incrimnent/dectiment position
#****************************
#****************************
# https://hades.mech.northwestern.edu/images/f/f7/LCD16x2_HJ1602A.pdf
# SEARCH FOR "15.24-bitoperation(4-bit,1line)""
# *********************************************
# LCD1602 initialization sequence
# put pins in output mode
machine.Pin(8, machine.Pin.OUT, machine.Pin.PULL_DOWN) #(e)enable
machine.Pin(10, machine.Pin.OUT, machine.Pin.PULL_DOWN) #(rs)register select
machine.Pin(9, machine.Pin.OUT, machine.Pin.PULL_DOWN) #(r/w)read/weite 
machine.Pin(7, machine.Pin.OUT, machine.Pin.PULL_DOWN) #(d7)data bit7
machine.Pin(6, machine.Pin.OUT, machine.Pin.PULL_DOWN) #(d6)data bit6
machine.Pin(5, machine.Pin.OUT, machine.Pin.PULL_DOWN) #(d5)data bit5
machine.Pin(4, machine.Pin.OUT, machine.Pin.PULL_DOWN) #(d4)data bit4
machine.Pin(3, machine.Pin.OUT, machine.Pin.PULL_DOWN) #(d3)data bit3
machine.Pin(2, machine.Pin.OUT, machine.Pin.PULL_DOWN) #(d2)data bit2
machine.Pin(1, machine.Pin.OUT, machine.Pin.PULL_DOWN) #(d1)data bit1 
machine.Pin(0, machine.Pin.OUT, machine.Pin.PULL_DOWN) #(d0)data bit0
#
#send initialization sequence
# rs rw d0 d1 d2 d3 d4 d5 d6 d7   # R/W 1=R 0=W
# 0  0  x  x  x  x  1  1  0  0
# rs rw d0 d1 d2 d3 d4 d5 d6 d7   # R/W 1=R 0=W
# 0  0  x  x  x  x  1  1  0  0
# rs rw d0 d1 d2 d3 d4 d5 d6 d7   # R/W 1=R 0=W
# 0  0  x  x  x  x  1  1  0  0
# rs rw d0 d1 d2 d3 d4 d5 d6 d7   # R/W 1=R 0=W

# rs rw d0 d1 d2 d3 d4 d5 d6 d7   # R/W 1=R 0=W
# 0  0  0  0  1  1  1  1  0  0  #LARGE FONT 2 LINES
# 0  0  0  0  0  0  0  1  0  0  #
# 0  0  0  0  0  0  1  0  0  0  #
# 0  0  0  0  0  1  0  0  0  0  #
# 0  0  1  0  0  0  0  0  0  0  #
# 0  0  0  1  1  0  0  0  0  0  #

# rs rw d0 d1 d2 d3 d4 d5 d6 d7   # R/W 1=R 0=W
# 0  0  0  0  1  0  x  x  x  x
machine.Pin(9).value(0)  # R/W 1=R 0=W
machine.Pin(10).value(0) # RS rs=1
machine.Pin(0).value(0) #d0
machine.Pin(1).value(0) #d1
machine.Pin(2).value(0) #d2
machine.Pin(3).value(0) #d3
machine.Pin(4).value(1) #d4
machine.Pin(5).value(1) #d5
machine.Pin(6).value(0) #d6
machine.Pin(7).value(0) #d7
machine.Pin(8).value(1)  # Enable 1=Enable
machine.Pin(8).value(0)  # Enable 1=Enable
#
# rs rw d0 d1 d2 d3 d4 d5 d6 d7   # R/W 1=R 0=W
# 0  0  0  0  1  0  x  x  x  x
machine.Pin(0, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit0
machine.Pin(1, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit1 
machine.Pin(2, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit2
machine.Pin(3, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit3
machine.Pin(4, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit4
machine.Pin(5, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit5
machine.Pin(6, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit6
machine.Pin(7, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit7
machine.Pin(8, machine.Pin.OUT, machine.Pin.PULL_DOWN) #enable
machine.Pin(9, machine.Pin.OUT, machine.Pin.PULL_DOWN) #read/weite 
machine.Pin(10, machine.Pin.OUT, machine.Pin.PULL_DOWN) #register select
machine.Pin(9).value(0)  # R/W 1=R 0=W
machine.Pin(10).value(0) # RS
machine.Pin(0).value(0)
machine.Pin(1).value(0)
machine.Pin(2).value(1)
machine.Pin(3).value(0)
machine.Pin(8).value(1)  # Enable 1=Enable
machine.Pin(8).value(0)  # Enable 1=Enable

# rs rw d0 d1 d2 d3 d4 d5 d6 d7   # R/W 1=R 0=W
# 0  0  n  f  x  x  x  x  x  x (#N=1:2lines;N=0:1lines  .F=1:5x11dots;F=0:5x8dots.)
machine.Pin(0, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit0
machine.Pin(1, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit1 
machine.Pin(2, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit2
machine.Pin(3, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit3
machine.Pin(4, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit4
machine.Pin(5, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit5
machine.Pin(6, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit6
machine.Pin(7, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit7
machine.Pin(8, machine.Pin.OUT, machine.Pin.PULL_DOWN) #enable
machine.Pin(9, machine.Pin.OUT, machine.Pin.PULL_DOWN) #read/weite 
machine.Pin(10, machine.Pin.OUT, machine.Pin.PULL_DOWN) #register select
machine.Pin(9).value(0)  # R/W 1=R 0=W
machine.Pin(10).value(0) # RS
machine.Pin(0).value(1)
machine.Pin(1).value(1)
machine.Pin(8).value(1)  # Enable 1=Enable
machine.Pin(8).value(0)  # Enable 1=Enable
#
# rs rw d0 d1 d2 d3 d4 d5 d6 d7   # R/W 1=R 0=W
# 0  0  0  0  0  0  x  x  x  x
machine.Pin(0, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit0
machine.Pin(1, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit1 
machine.Pin(2, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit2
machine.Pin(3, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit3
machine.Pin(4, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit4
machine.Pin(5, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit5
machine.Pin(6, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit6
machine.Pin(7, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit7
machine.Pin(8, machine.Pin.OUT, machine.Pin.PULL_DOWN) #enable
machine.Pin(9, machine.Pin.OUT, machine.Pin.PULL_DOWN) #read/weite 
machine.Pin(10, machine.Pin.OUT, machine.Pin.PULL_DOWN) #register select
machine.Pin(9).value(0)  # R/W 1=R 0=W
machine.Pin(10).value(0) # RS
machine.Pin(0).value(0)
machine.Pin(1).value(0)
machine.Pin(2).value(0)
machine.Pin(3).value(0)
machine.Pin(8).value(1)  # Enable 1=Enable
machine.Pin(8).value(0)  # Enable 1=Enable
# rs rw d0 d1 d2 d3 d4 d5 d6 d7   # R/W 1=R 0=W
# 0  1  d  c  b  x  x  x  x  x ((D)Display, (C)Cursor, (B)Blink
machine.Pin(0, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit0
machine.Pin(1, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit1 
machine.Pin(2, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit2
machine.Pin(3, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit3
machine.Pin(4, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit4
machine.Pin(5, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit5
machine.Pin(6, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit6
machine.Pin(7, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit7
machine.Pin(8, machine.Pin.OUT, machine.Pin.PULL_DOWN) #enable
machine.Pin(9, machine.Pin.OUT, machine.Pin.PULL_DOWN) #read/weite 
machine.Pin(10, machine.Pin.OUT, machine.Pin.PULL_DOWN) #register select
machine.Pin(9).value(0)  # R/W 1=R 0=W
machine.Pin(10).value(1) # RS
machine.Pin(0).value(1)
machine.Pin(1).value(1)
machine.Pin(2).value(8)
machine.Pin(8).value(1)  # Enable
machine.Pin(8).value(0)  # Enable
#
# rs rw d0 d1 d2 d3 d4 d5 d6 d7   # R/W 1=R 0=W
# 0  0  0  0  0  0  x  x  x  x
machine.Pin(0, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit0
machine.Pin(1, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit1 
machine.Pin(2, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit2
machine.Pin(3, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit3
machine.Pin(4, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit4
machine.Pin(5, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit5
machine.Pin(6, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit6
machine.Pin(7, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit7
machine.Pin(8, machine.Pin.OUT, machine.Pin.PULL_DOWN) #enable
machine.Pin(9, machine.Pin.OUT, machine.Pin.PULL_DOWN) #read/weite 
machine.Pin(10, machine.Pin.OUT, machine.Pin.PULL_DOWN) #register select
machine.Pin(9).value(0)  # R/W 1=R 0=W
machine.Pin(10).value(0) # RS
machine.Pin(0).value(0)
machine.Pin(1).value(0)
machine.Pin(2).value(0)
machine.Pin(8).value(1)  # Enable
machine.Pin(8).value(0)  # Enable
# rs rw d0 d1 d2 d3 d4 d5 d6 d7   # R/W 1=R 0=W
# 0  0  0  0  0  1  x  x  x  x (clear display instruction)
machine.Pin(0, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit0
machine.Pin(1, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit1 
machine.Pin(2, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit2
machine.Pin(3, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit3
machine.Pin(4, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit4
machine.Pin(5, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit5
machine.Pin(6, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit6
machine.Pin(7, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit7
machine.Pin(8, machine.Pin.OUT, machine.Pin.PULL_DOWN) #enable
machine.Pin(9, machine.Pin.OUT, machine.Pin.PULL_DOWN) #read/weite 
machine.Pin(10, machine.Pin.OUT, machine.Pin.PULL_DOWN) #register select
machine.Pin(9).value(0)  # R/W 1=R 0=W
machine.Pin(10).value(0) # RS
machine.Pin(0).value(0)
machine.Pin(1).value(0)
machine.Pin(2).value(1)
machine.Pin(8).value(1)  # Enable
machine.Pin(8).value(0)  # Enable
#
# rs rw d0 d1 d2 d3 d4 d5 d6 d7   # R/W 1=R 0=W
# 0  0  0  0  0  0  x  x  x  x
machine.Pin(0, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit0
machine.Pin(1, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit1 
machine.Pin(2, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit2
machine.Pin(3, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit3
machine.Pin(4, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit4
machine.Pin(5, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit5
machine.Pin(6, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit6
machine.Pin(7, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit7
machine.Pin(8, machine.Pin.OUT, machine.Pin.PULL_DOWN) #enable
machine.Pin(9, machine.Pin.OUT, machine.Pin.PULL_DOWN) #read/weite 
machine.Pin(10, machine.Pin.OUT, machine.Pin.PULL_DOWN) #register select
machine.Pin(9).value(0)  # R/W 1=R 0=W
machine.Pin(10).value(0) # RS
machine.Pin(0).value(0)
machine.Pin(1).value(0)
machine.Pin(2).value(0)
machine.Pin(8).value(1)  # Enable
machine.Pin(8).value(0)  # Enable
# rs rw d0 d1 d2 d3 d4 d5 d6 d7   # R/W 1=R 0=W
# 0  0  1 I/D SH X  X  X  X  x
machine.Pin(0, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit0
machine.Pin(1, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit1 
machine.Pin(2, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit2
machine.Pin(3, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit3
machine.Pin(4, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit4
machine.Pin(5, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit5
machine.Pin(6, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit6
machine.Pin(7, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit7
machine.Pin(8, machine.Pin.OUT, machine.Pin.PULL_DOWN) #enable
machine.Pin(9, machine.Pin.OUT, machine.Pin.PULL_DOWN) #read/weite 
machine.Pin(10, machine.Pin.OUT, machine.Pin.PULL_DOWN) #register select
machine.Pin(9).value(0)  # R/W 1=R 0=W
machine.Pin(10).value(0) # RS
machine.Pin(0).value(0)
machine.Pin(1).value(0)
machine.Pin(2).value(0)
machine.Pin(8).value(1)  # Enable
machine.Pin(8).value(0)  # Enable
#******************************************
# LCD1602 initialization sequence
# rs rw d0 d1 d2 d3 d4 d5 d6 d7
# 0  0  0  0  1  1  x  x  x  x
#
# 0  0  0  0  1  0  x  x  x  x
# 0  0  n  f  x  x  x  x  x  x 
#
# 0  0  0  0  1  0  x  x  x  x
# 0  0  n  f  x  x  x  x  x  x (#N=1:2lines;N=0:1lines  .F=1:5x11dots;F=0:5x8dots.)
#
# 0  0  0  0  0  0  x  x  x  x
# 0  1  d  c  b  x  x  x  x  x ((D)Display, (C)Cursor, (B)Blink
#
# 0  0  0  0  0  0  x  x  x  x
# 0  0  0  0  0  1  x  x  x  x (clear display instruction)
#
# 0  0  0  0  0  0  x  x  x  x
# 0  0  1 I/D SH X  X  X  X  x

# nn=3600*24*366*28;m=nn;d=nn;s=nn;nn=1;b2b=0;d=12;k=0;b=0;kk=0;bb=0;sevenSeg="1111110011000011011011111001011001110110111011111111000011111111111011";
tt=" "
eightbitsevensegreversed="00111111000001100101101101001111011001100110110101111101000001110111111101101111"
ssebr=eightbitsevensegreversed
k=0;b=0;k2=0;b2=0
puter=bytearray(1)
while 1:
  k=machine.Pin(28).value()
  if (k>b):
    k1=k1+1-4*(k1>3)
  b=k
  k2=machine.Pin(27).value()
  if (k2>b2):
    k1=k1+1-4*(k1>3)
  b2=k2
  rvdc=machine.ADC(26).read_u16()
  if ttt==t:
    print(end="")
    #machine.lightsleep(100)
  if ttt!=t:
    ttt=t
    s=str(int((t/60-t//60)*60));m=str(int((t/3600-t//3600)*60));h=str(int((t/86400-t//86400)*24));d=str(t//86400);w=str((t//86400*7))    # Extrapolate seconds hours minutes and days from raw uptime seconds
    tt=("00"+d)[-2:]+("00"+h)[-2:]+("00"+m)[-2:]+("00"+s)[-2:]+"77" # compile the output buffer 
    for nn in range(8):
      n=int(tt[nn])
      machine.SPI(0,baudrate=1024,sck=machine.Pin(2),mosi=machine.Pin(3),miso=machine.Pin(4)).readinto(puter,int(ssebr[n*8:n*8+8],2))
      chrout=str(puter)[12]
      if chrout==chr(92):
        print(ord(chr(int("0x"+(str(puter)[14:16])))),"$",end="")
      if chrout!=chr(92):
        print(ord(str(puter)[12]),"@",end="")
    machine.Pin(0).value(0)    # low latched the data into the display
    machine.Pin(0).value(1)    # high latched the data into the display
    print("")
BOOTSELLED1239USBRaspberryPiPico©2020RP2-8020/21P64M15.00TTT