    org 100h 
    
    jmp main
    
port    equ 378h    
    
main:                
;    mov direction,0
;    mov al, 5   ;base
;    call motor_number
;    
;    mov ax,100
;l2: call drive
;    dec ax
;    jnz l2  
;    
;    mov direction,1
;    mov al, 5   ;base
;    call motor_number
;    
;    mov ax,100
;l3: call drive
;    dec ax
;    jnz l3
;
    mov direction,0
    mov al, 2   ;gripper
    call motor_number
    
    mov ax,5000
l4: call drive
    dec ax
    jnz l4

    mov direction,1
    mov al, 2   ;gripper
    call motor_number
    
    mov ax,5000
l5: call drive
    dec ax
    jnz l5
  
    jmp main
    
    

;*************************************************************
;Name:          motor_number
;
;Author:        Dan Kohn
;
;Date:          12/14/2006
;
;Function:      select the motor to move 
;
;On Call:       AL contains motor number (0-7) where:
;                    0 -> Aux #1
;                    1 -> Wrist 
;                    2 -> gripper
;                    3 -> wrist
;                    4 -> elbow
;                    5 -> base
;                    6 -> shoulder
;                    7 -> Aux #2
;
;On Return:     All Registers Restored

motor_number:
    push bx
    push ax
    
    mov motor_num,al
    mov bx,offset motor_table
    mov ah,00
    add bx,ax
    mov al,[bx]
    mov motor,al
    
    pop ax
    pop bx
    ret
    
    
motor_table:
    db  0000_0000b
    db  0000_0010b
    db  0001_0000b
    db  0001_0010b
    db  0010_0000b
    db  0010_0010b
    db  0011_0000b
    db  0011_0010b    
      
motor       db 0 
motor_num   db 0   


;*************************************************************
;Name:          drive
;
;Author:        Dan Kohn
;
;Date:          12/14/2006
;
;Function:      Steps motor (selected via "motor_num routine") one time
;
;On Call:       motor contains mask of motor to move
;               motor_num contains motor number to move
;               direction contains 0 for one direction, 1 for reverse direction
;
;On Return:     All Registers Restored

direction   db 0
motor_count db 8 dup(0)    

drive:
    push ax
    push bx
    push cx
    push dx
    
    mov bx,offset motor_count
    mov ah,0
    mov al,motor_num
    add bx,ax
    
    mov cl,[bx]
    
    mov dl,direction
    and dl,1
    
    jnz dr_l1
    
    inc cl
    jmp dr_l2    
    

dr_l1:
    dec cl
    
    
dr_l2:
    mov [bx],cl
    
  
    mov bx,offset step_table
    mov al,cl
    mov ah,0
    mov dl,4
    
    div dl
    
    mov al,ah
    mov ah,0
    add bx,ax
    
l1: mov al,[bx]       
    or al,motor
    mov dx,port
    out dx,al
    call time_delay
    or al,01h                                 
    out dx,al
    call time_delay  
    
    pop dx
    pop cx
    pop bx
    pop ax
    
    ret
             
     
step_table:
    db 0000_0100b
    db 1000_0000b
    db 0000_1000b
    db 0100_0000b
    
     
step_table_end = $   
    

    
;*************************************************************
;Name:          TIME_DELAY
;
;Author:        Dan Kohn
;
;Date:          10/30/2005
;
;Revised:       10/31/2005
;               PUSHA does not work on Toshiba XT Compatable
;               laptop. Replaced with individual push and pops.
;
;Function:      Wait for ??
;
;On Call:       NONE
;
;On Return:     All Registers Restored

time_delay: push ax         ;put all reg on stack for restore
            push bx
            push cx
            push dx

            mov cx,22
td_l2:            
            
            mov dx,10000

td_l1:      dec dx
            jnz td_l1

            dec cx
            jnz td_l2
                        
            pop dx            ;restore all registers
            pop cx
            pop bx
            pop ax
            RET
