'************************************************************************************ '* This object drives a stepper motor controller that is controlled by a clock and '* direction. The start method is used to initialize the clock and direction pins, '* and start the driver method cog. '* '* The set_speed method is used to set the frequency and direction. If speed is '* zero, clkticks is set to zero and the clock stops toggling. If speed is greater '* than zero the clock frequency is set to speed * SCALE / 40, and the direction '* pin is set to zero. If speed is less than zero the clock frequency is set to '* -speed * SCALE / 40, and the direction pin is set to one. '* '* Author: Dave Hein '* Copyright (c) 2020 '* See end of file for terms of use. '************************************************************************************ con SCALE = 40 dat clkpin long 0 dirpin long 0 clkticks long 0 count long 0 cognum long 0 stack long 0[20] pub start(cpin, dpin) clkpin := cpin dirpin := dpin clkticks := 0 DIRA[dirpin] := 1 cognum := cognew(driver, @stack) + 1 pub stop if cognum cogstop(cognum - 1) longfill(@clkpin, 0, 5) pub set_speed(speed) if speed > 0 clkticks := 20*clkfreq/(speed * SCALE) OUTA[dirpin] := 0 elseif speed < 0 clkticks := 20*clkfreq/(-speed * SCALE) OUTA[dirpin] := 1 else clkticks := 0 pub driver | ticks DIRA[clkpin] := 1 count := cnt repeat ticks := clkticks if ticks waitcnt(count += ticks) !OUTA[clkpin] {{ Terms of Use: MIT License Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. }}