;Program: DCMOTOR2.BAS ;This program utilizes the VERSATECH TICKit to vary and control the ;speed and direction of a DC motor using UNITRODE L293D Driver. If pin_D6 ;is an active low then the motor is goes slow, otherwise the motor goes ;fast. If pin_D5 is an active low then the motor goes in one direction, ;otherwise it runs in the other direction. ;TICKit ---------------------------------> L293d (Driver) ;pin_D7 (term 38) -----------------------> Input 1 (term 2) ;pin_D6 (term 37) -----------------------> Input 2 (term 7) ;Copyright, Locksley Haynes, Morgan State University, Oct 11, '97 DEF tic62_b ;define the device in use LIB fbasic.lib ;define library for basic routines DEF dc_control1 pin_D7 ;output to the motor for one direction DEF dc_control2 pin_D6 ;output to motor for the other direction DEF speed_pin pin_D5 ;input for speed of motor DEF direct_pin pin_D4 ;input for direction of motor GLOBAL word duty 0w ;the duty cycle ;Function p_w_modulate1 send the output to the driver to control ;the motor in one direction. FUNC none p_w_modulate1 LOCAL word accum 0w LOCAL word n 0w LOCAL word carry 0w BEGIN WHILE <(n, 256w) =(accum, +(accum, duty)) =(carry, and(accum, 0x0100w)) IF <>( carry, 0w) pin_low( dc_control1 ) ELSE pin_high( dc_control1 ) ENDIF =(accum, and(accum, 0x00ffw)) ++( n ) LOOP ENDFUN ;Function p_w_modulate2 send the output to the driver to control ;the motor in the other direction. FUNC none p_w_modulate2 LOCAL word accum 0w LOCAL word n 0w LOCAL word carry 0w BEGIN WHILE <(n, 256w) =(accum, +(accum, duty)) =(carry, and(accum, 0x0100w)) IF <>( carry, 0w) pin_low( dc_control2 ) ELSE pin_high( dc_control2 ) ENDIF =(accum, and(accum, 0x00ffw)) ++( n ) LOOP ENDFUN ;***********************Main function************************** FUNC none main() LOCAL byte speed 0b LOCAL byte direction 0b BEGIN pin_low(dc_control1) ;start with the motor off pin_low(dc_control2) REP =(speed, and(pin_in(speed_pin), 1b)) ;get pin input =(direction, and(pin_in(direct_pin), 1b)) ;get pin input IF ==(speed, 0b) ;if speed pin is active low, =(duty, 128w) ;go slow ELSE ;else go fast =(duty, 300w) ENDIF IF ==(direction, 0b) ;if direction pin is active low pin_high(dc_control2) p_w_modulate1() ;run in one direction ELSE ;else pin_high(dc_control1) p_w_modulate2() ;run in other direction ENDIF LOOP ENDFUN ;****************************************************************