$regfile = "m32def.dat"
$crystal = 16e6
$baud = 19200
$hwstack = 64
$swstack = 16
$framesize = 48


'Motoren anhalten
'PD4(OC1B)= Motor rechts und
'PD5(OC1A)= Motor links sind PWM-gesteuert
Motor_r_pwm Alias Portd.4 : Config Motor_r_pwm = Output : Motor_r_pwm = 0
Motor_l_pwm Alias Portd.5 : Config Motor_l_pwm = Output : Motor_l_pwm = 0


'Schieberegister 74HC595
'aus Datenblatt:
'Data is shifted on the pos.-going transition of the SHcp-input
'SHcp - Pin 11 - shift register clock input
'Data is transferred to the storage register on a pos.-going transition of the STcp-input
'STcp - Pin 12 - storage register clock input
'gemeinsam

Ser_data Alias Portc.0 : Config Ser_data = Output
Clock_sr_lcd Alias Portc.1 : Config Clock_sr_lcd = Output
Clock_st_lcd Alias Portc.2 : Config Clock_st_lcd = Output

Declare Sub Send_lcd_byte(byval _byte As Byte)

'LCD DEM20485 SBH-PW-N
Lcd_rs Alias Portc.0 : Config Lcd_rs = Output               '1=Daten / 0=Kommando
Lcd_rw Alias Portc.1 : Config Lcd_rw = Output               '1=Read / 0=Write
Lcd_enable Alias Portc.2 : Config Lcd_enable = Output       'fallende Flanke übertragen der Daten / H-Pegel=Lesen

Dim Lcd_string As String * 20
Dim Lcd_byte(21) As Byte At Lcd_string Overlay

Declare Sub Lcd_init()
Declare Sub Lcd_clear()
Declare Sub Lcd_xy(byval _x As Byte , Byval _y As Byte)
Declare Sub Lcd_send_char(byval _wort As String * 20)



'Init
Call Send_lcd_byte(0)
Call Lcd_init()


Do
   Call Lcd_xy(5 , 3)
   Call Lcd_send_char( "Hello world")

   Wait 3
   Call Lcd_clear()
   Wait 1
Loop
End



Sub Send_lcd_byte(byval _byte As Byte)
   Clock_st_lcd = 0
   Clock_sr_lcd = 0 : Ser_data = _byte.7 : Clock_sr_lcd = 1
   Clock_sr_lcd = 0 : Ser_data = _byte.6 : Clock_sr_lcd = 1
   Clock_sr_lcd = 0 : Ser_data = _byte.5 : Clock_sr_lcd = 1
   Clock_sr_lcd = 0 : Ser_data = _byte.4 : Clock_sr_lcd = 1
   Clock_sr_lcd = 0 : Ser_data = _byte.3 : Clock_sr_lcd = 1
   Clock_sr_lcd = 0 : Ser_data = _byte.2 : Clock_sr_lcd = 1
   Clock_sr_lcd = 0 : Ser_data = _byte.1 : Clock_sr_lcd = 1
   Clock_sr_lcd = 0 : Ser_data = _byte.0 : Clock_sr_lcd = 1
   Clock_sr_lcd = 0                                         '/Output Enable=R/W=C.1
   Clock_st_lcd = 1                                         '=Enable=C.2
End Sub


Sub Lcd_init()
   Waitms 41
   'Function set
   Call Send_lcd_byte(&B0011_1000) : Lcd_rs = 0 : !nop : Lcd_enable = 0 : Waitus 38
   'Display on/off control
   Call Send_lcd_byte(&B0000_1100) : Lcd_rs = 0 : !nop : Lcd_enable = 0 : Waitus 38
   'Display clear
   Call Send_lcd_byte(&B0000_0001) : Lcd_rs = 0 : !nop : Lcd_enable = 0 : Waitus 1521
   'Entry mode set
   Call Send_lcd_byte(&B0000_0110) : Lcd_rs = 0 : !nop : Lcd_enable = 0 : Waitus 100
   'letzte Pause verlängert, da sonst das 1. Zeichen nach Init nicht kam
End Sub


Sub Lcd_clear()
   Call Send_lcd_byte(&B00000001) : Lcd_rs = 0 : Lcd_enable = 0 : Waitus 1521
End Sub


Sub Lcd_xy(byval _x As Byte , Byval _y As Byte)
Local _wert As Byte
   _wert = _x + 127
   Select Case _y
      Case 2 : _wert = _wert + &H40
      Case 3 : _wert = _wert + &H14
      Case 4 : _wert = _wert + &H54
   End Select
   Call Send_lcd_byte(_wert) : Lcd_rs = 0 : Lcd_enable = 0 : Waitus 38
End Sub


Sub Lcd_send_char(_wort As String * 20)
Local _laenge As Byte
Local _i As Byte
   Lcd_string = _wort
   _laenge = Len(_wort)
   For _i = 1 To _laenge
      Call Send_lcd_byte(lcd_byte(_i)) : Lcd_rs = 1 : Lcd_enable = 0 : Waitus 38
   Next _i
End Sub