'***********************************************************************
'*                 <PWM- Duty Cycle motor controller>                  *
'*                                 by                                  *
'*                        Dr.Godfried-Willem RAES                      *
'*                      Stamp1 Hardware version 1.4                    *
'*                        Filename : <vohumot.bs1>                     *
'*                           Code version 1.4                          *
'***********************************************************************
' 17.05.2003: designed for Laukhuff compressor motor speed control.
'             This code runs on a BS1 stamp.
' 19.05.2003: code optimized.
' 20.05.2003: further debug. Switch off not working for some reason...
'             Solved: bad solder connection on bit D3. Version 1.3
' 21.05.2003: NOT yet uploaded in piperola: double refresh frequency : Version 1.4
'             should give smoother operation of motor.
' 22.05.2003: Version for Vox Humanola motor.

' INITIALISATION:
                    ' parallel input bits 0 to 6
                    ' these bits are mapped on midi notes 0-6
                    ' make these bits inputs:
DIR0 = 0
DIR1 = 0
DIR2 = 0
DIR3 = 0
DIR4 = 0
DIR5 = 0
DIR6 = 0
DIR7 = 1            ' switching output, so must be output.
LOW 7			  ' motor OFF on start.
' stated in its most compact form:
'DIRS = %10000000
SYMBOL Motor = b10
Motor = 0

ReadInput:
      IF pin6 = 1 then SwitchOn   ' motor will be ON when bit 6 is set ( >= 64)
      b2 = pins & %01111111       ' isolate lowest 7 bits
      IF b2 < 1 then SwitchOff
      w2 = b2         ' byte is incoming info 0-127
                      '  w2 now 1-63 range
      w3 = w2 * 8	    ' = 16 to 504 milliseconds
      w3 = w3 - 4     ' = 12 to 500 milliseconds
      w4 = 504 - w3   ' thus the refresh period will allways be 500ms, or 2 Hz
PWMod:
      HIGH 7
      Pause w3        ' ontime
      LOW 7
      Pause w4        ' offtime
goto ReadInput


SwitchOn:
      HIGH 7
      Motor = 1
      IF pin6 = 1 THEN SwitchOn 
      IF pin6 = 0 THEN ReadInput

SwitchOff:
      LOW 7
      Motor = 0
      b2 = pins & %01111111
      if b2 > 0 then ReadInput
      GOTO SwitchOff 
END

bsave               ' creates an OBJ file to disk
