$regfile = "M32def.dat"
$crystal = 8000000
'------------------------------------------------------------------------------------------------------------
Config Lcdpin = Pin , Db4 = Pina.2 , Db5 = Pina.3 , Db6 = Pina.4 , Db7 = Pina.5 , Rs = Pina.0 , E = Pina.1
Config Lcd = 20 * 4
Config Kbd = Portc
Config Portd = Output
Config Timer1 = Counter , Edge = Rising
Config Timer0 = Timer , Prescale = 1024
'------------------------------------------------------------------------------------------------------------
Declare Sub Main
Declare Sub Key_scan
Declare Sub Lcd_show
Declare Sub Auto
Declare Sub Manual_1
Declare Sub Manual_2
Declare Sub Feed_back
Declare Sub Alarm
'------------------------------------------------------------------------------------------------------------
Dim Key As Byte
Dim Auto1 As Byte
Dim I As Byte
Dim Manual_1 As Byte
Dim Manual_2 As Byte
Dim Delay1 As Word
Dim Delay2 As Word
Dim F As Byte
Dim Delay3 As Word
Dim A As Long
Dim N As Long
Dim B As Byte


'------------------------------------------------------------------------------------------------------------
Beep Alias Portd.7

Vin1 Alias Portd.0
Vin2 Alias Portd.1
Vin3 Alias Portd.2
Vin4 Alias Portd.3
Vin5 Alias Portd.4
Vin6 Alias Portd.5

'------------------------------------------------------------------------------------------------------------
Init:
Enable Counter1
Enable Interrupts
Enable Timer0
Enable Timer1

On Ovf1 Pulsecount
On Ovf0 Ovf0occures
B = 0
Start Timer0

Cls
Cursor Off

Locate 1 , 2
Lcd "2-CH MOTOR DRIVER"

Locate 3 , 3
Lcd "PLEASE WAIT ..."

Set Beep
Waitms 200
Reset Beep
Waitms 200
Set Beep
Waitms 200
Reset Beep
Wait 2

Call Main



End
'------------------------------------------------------------------------------------------------------------

Sub Main
Cls
Locate 1 , 1
Lcd "1:Manual start1"
Locate 2 , 1
Lcd "2:Manual start2"
Locate 3 , 1
Lcd "3:Auto start"
Locate 4 , 1
Lcd "Sp1:"
Locate 4 , 13
Lcd "Sp2:"

Do
   Call Key_scan
   Call Lcd_show
Loop
End Sub Main
'------------------------------------------------------------------------------------------------------------
Sub Key_scan
Key = Getkbd()
Select Case Key
   Case 0 :
     Call Alarm
     Call Manual_1

   Case 4 :
     Call Alarm
     Call Manual_2

   Case 8 :
     Call Alarm
     Call Auto
End Select

End Sub Key_scan

'------------------------------------------------------------------------------------------------------------
Sub Lcd_show
End Sub Lcd_show

'------------------------------------------------------------------------------------------------------------
Sub Auto
Auto1 = 0
Cls
Locate 1 , 4
Lcd "Auto Starting"

For I = 1 To 51


   Locate 3 , 4
   Lcd "Frequency:  " ; Auto1

   Incr Auto1
Dim J As Word
J = I * 6
Delay1 = 1000 / J
            Set Vin6
         For J = 1 To 2
            Set Vin1

            Waitms Delay1
            Reset Vin6
            Set Vin2

               Waitms Delay1
            Reset Vin1
            Set Vin3
               Waitms Delay1
            Reset Vin2
            Set Vin4

               Waitms Delay1
            Reset Vin3
            Set Vin5

               Waitms Delay1
            Reset Vin4
            Set Vin6
               Waitms Delay1
         Next
Next I
Wait 1
  Set Vin6

    F = 50
   Delay3 = 6 * F
   Delay2 = 1000 / Delay3

    Do
  Key = Getkbd()

            Set Vin1
               Waitms Delay2
            Reset Vin6
            Set Vin2

               Waitms Delay2
            Reset Vin1
            Set Vin3
               Waitms Delay2
            Reset Vin2
            Set Vin4

               Waitms Delay2
            Reset Vin3
            Set Vin5

               Waitms Delay2
            Reset Vin4
            Set Vin6
               Waitms Delay2
                Locate 3 , 4

         Lcd "Frequency:  " ; F

     Select Case Key
        Case 3 :
        Call Alarm
        Call Main

        Case 12:
        Call Alarm
        Waitms 400
        Incr F
        Delay3 = 6 * F
        Delay2 = 1000 / Delay3

        Case 13:
        Call Alarm
        Waitms 400
        Decr F
        Delay3 = 6 * F
        Delay2 = 1000 / Delay3

    End Select
      Loop

End Sub Auto

'------------------------------------------------------------------------------------------------------------
Sub Manual_1
Cls
Manual_1 = 0
Locate 1 , 1
Lcd "Manual Starting 1"
    F = 50
   Delay3 = 6 * F
   Delay2 = 1000 / Delay3

    Do
  Key = Getkbd()

            Set Vin1
               Waitms Delay2
            Reset Vin6
            Set Vin2

               Waitms Delay2
            Reset Vin1
            Set Vin3
               Waitms Delay2
            Reset Vin2
            Set Vin4

               Waitms Delay2
            Reset Vin3
            Set Vin5

               Waitms Delay2
            Reset Vin4
            Set Vin6
               Waitms Delay2
                Locate 3 , 4

         Lcd "Frequency:  " ; F

     Select Case Key
        Case 3 :
        Call Alarm
        Call Main

        Case 12:
        Call Alarm
        Waitms 400
        Incr F
        Delay3 = 6 * F
        Delay2 = 1000 / Delay3

        Case 13:
        Call Alarm
        Waitms 400
        Decr F
        Delay3 = 6 * F
        Delay2 = 1000 / Delay3

    End Select
      Loop


End Sub Manual_1
'------------------------------------------------------------------------------------------------------------
Sub Manual_2
Cls
Manual_2 = 0
Locate 1 , 1
Lcd "Manual Starting 2"
Do
   Key = Getkbd()
            Set Vin1
               Waitms Delay2
            Reset Vin6
            Set Vin2

               Waitms Delay2
            Reset Vin1
            Set Vin3
               Waitms Delay2
            Reset Vin2
            Set Vin4

               Waitms Delay2
            Reset Vin3
            Set Vin5

               Waitms Delay2
            Reset Vin4
            Set Vin6
               Waitms Delay2
                Locate 3 , 4
   Select Case Key
       Case 12:
        Call Alarm
        Waitms 250
        Incr F
        Delay3 = 6 * F
        Delay2 = 1000 / Delay3

        Case 13:
        Call Alarm
        Waitms 250
        Decr F
        Delay3 = 6 * F
        Delay2 = 1000 / Delay3

      Case 3 :
      Waitms 250
         Call Alarm
         Call Main
   End Select
   Locate 3 , 1
   Lcd "Frequency:  " ; F ; "  "

   Loop

End Sub Manual_2

'------------------------------------------------------------------------------------------------------------
Sub Feed_back
End Sub Feed_back
'------------------------------------------------------------------------------------------------------------
Sub Alarm
     Set Beep
     Waitms 10
     Reset Beep

End Sub Alarm

'______________________________________________________________________________

Ovf0occures:
Incr N
If N > 30 Then
Stop Timer0
A = B * 65536
A = A + Counter1
Locate 4 , 5
Lcd A ;"   "
B = 0
N = 0 : Counter1 = 0
Start Timer0
End If
Return

'______________________________________________________________________________

Pulsecount:
Incr B
Counter1 = 0
Return

