OpenCores
URL https://opencores.org/ocsvn/steppermotordrive/steppermotordrive/trunk

Subversion Repositories steppermotordrive

[/] [steppermotordrive/] [trunk/] [StepperMotorDrive.vhd] - Blame information for rev 18

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 6 franksdeve
library IEEE;
2
use IEEE.STD_LOGIC_1164.ALL;
3
use IEEE.STD_LOGIC_ARITH.ALL;
4
use IEEE.STD_LOGIC_UNSIGNED.ALL;
5
 
6
-- c2003 Franks Development, LLC
7
-- http://www.franks-development.com
8
-- !This source is distributed under the terms & conditions specified at opencores.org
9
-- 
10
-- Please see the file "StepperMotorWiring.bmp" for info on connecting 4 & 6 
11
-- wire motors to your device.  This source should drive either type, though connection
12
-- to 4-wire motors requires significantly more FET's to buffer outputs.  The
13 16 franksdeve
-- circuitry for 6-wire motors is more straightforward.  The colors specified are 
14
-- standard to many brands of stepper motors.  If you get 1-2-3-4 on the FET's connected
15
-- to 4-3-2-1 on your logic device (backwards), the motor will simply rotate backwards.
16
-- if out of order, the motor won't rotate at all.  Motors come in many dirrerent ratings
17
-- of degrees rotation per step; some offer exceptional resolution very inexpensively.
18
-- 
19
-- It is important to note that most logic operates at multiple megahertz.  Given
20
-- a steper motor at 100 steps per revolution, and a clock of 10MHz, running the 
21
-- motor at the full clock rate would equal 100,000 rps or 6 million revolutions per minute
22
-- obviously, motors don't do this.  Most steppers are designed for fine resolution at low
23
-- speeds.  Thus we employ a really big clock divider to get things operating at speeds
24
-- of which the motor is capable.  Check your motor ratings for a usefull value.
25
-- in lieu of ratings, 100 rpm is usually achieveable, so plan accordingly.
26 6 franksdeve
--
27
-- Another practical consideration is the threshold voltage of the FET's used to
28
-- buffer the logic outputs & provide current drive to the motor.  Most power
29
-- FET's have a threshold voltage in the 5-12V range (or even higher),
30
-- while logic devices now run in the 3.3V and lower range.  Thus, you must be 
31
-- careful to choose a FET with a low threshold voltage, or a level-converter must
32
-- be utilized between the logic output and the gate of the drive FET's.
33 15 franksdeve
-- Practical tip: FET's with very high currect handling capabilities, in general
34
-- (so be sure to read the datasheet before you buy), tend to handle larger currents
35
-- for any given gate voltage.  This means that in many cases, even if Vgs is rated at 5 volts
36
-- if your stepper uses relatively low current, the FET's may still drive it at 3.3V or lower.
37
-- In the worst case, you are likely going to need a high voltage power supply to drive the
38
-- motors anyway, so you can "double-up" low power FET's to drive the gates of the power FET's
39
-- with a higher voltage.  In effect, the low-power FET's are wired as inverters with a low
40
-- swithcing voltage.  Contact Franks Development for a napkin-sketch if you aren't familiar
41
-- with how to do that.  Good luck.
42 6 franksdeve
--
43
-- One of the most advantageous abilities of stepper motors is the ability to 
44
-- provide static holding force in any position.  Of course this consumes power
45
-- and heats the motor up significantly (though steppers are rated to handle this)
46 16 franksdeve
-- use of the "static holding" input port will specify this behavior.  Be aware,
47
-- however, that the motor will dissipate power if left energized for long periods
48
-- without (or without for that matter) rotating.  This will make them hot!  They are 
49
-- usually designed for it, but it is a consideration, especially if in a small,
50
-- sealed, enclosure, excess heat may make your logic cease functioning correctly at 
51
-- some point.
52 6 franksdeve
 
53
entity StepperMotorPorts is
54 10 franksdeve
    Port (
55 16 franksdeve
                                StepDrive : out std_logic_vector(3 downto 0); -- the 4 output to drive the MOSFETS driving the coils in the motor.
56
                                clock : in std_logic;                                                            -- clock input to logic device
57
                                Direction : in std_logic;                                                        -- spin clockwise or counter-clockwise? (actual direction depends on correct hookup/pin assignements)
58
                                StepEnable : in std_logic;                                                       -- move a single step on next clock divider rollover.  leave high for a single clock to get a single step.  If high across rollover, may get two steps
59
                                ProvideStaticHolding : in std_logic                         -- leave motor coils energized when not rotating, so that counter-torque is applied if attempt to move shaft
60 6 franksdeve
                );
61
end StepperMotorPorts;
62
 
63 17 franksdeve
architecture StepperMotor of StepperMotorPorts is
64 6 franksdeve
 
65 16 franksdeve
        signal state : std_logic_vector(1 downto 0);                             -- simple state machine, 4 states
66
        signal StepCounter : std_logic_vector(31 downto 0);   -- most motors won't spin extrordinarially fast, so this slows the clock input way down
67
        constant StepLockOut : std_logic_vector(31 downto 0) := "00000000000000110000110101000000"; --rollover for the counter, to get a non-binary delay time divider
68
        signal InternalStepEnable : std_logic;                                          -- used to capture a step enable even when we are in the wait loop for the clock divider.
69 6 franksdeve
 
70
begin
71
 
72
        process(clock)
73
        begin
74
 
75 16 franksdeve
                if ( (clock'Event) and (clock = '1') ) then              --on clock
76 6 franksdeve
 
77 16 franksdeve
                        StepCounter <= StepCounter + "0000000000000000000000000000001"; --move the delay counter
78 6 franksdeve
 
79 16 franksdeve
                        if (StepEnable = '1') then
80
 
81
                                InternalStepEnable <= '1';      -- capture any requests for a step, even while we are waiting...
82
 
83
                        end if;
84
 
85 6 franksdeve
                        if (StepCounter >= StepLockOut) then
86
 
87 16 franksdeve
                                StepCounter <= "00000000000000000000000000000000";              -- if we just roll-ed over, then it's time to do something
88 6 franksdeve
 
89 16 franksdeve
                                if (InternalStepEnable = '1') then -- are we supposed to step on this clock?
90 6 franksdeve
 
91 16 franksdeve
                                        InternalStepEnable <= StepEnable;       -- InternalStepEnable togles at the speed of the clock divider rollover, trailing the 
92
                                                                                                                                        -- external StepEnable by less than or equal to one rollover.
93
                                                                                                                                        -- Putting this inside the "if internal=1" makes us wait until after move to turn off,
94
                                                                                                                                        -- so we move at least once for each pulse of external step enable line.
95
 
96
                                        if (Direction = '1') then state <= state + "01"; end if; -- to change the direction of a stepper motor, you energize 
97
                                        if (Direction = '0') then state <= state - "01"; end if; -- the coils in the opposite pattern, so just run states backwards
98
                                                                                                                                                                                                -- this also allows a change of direction at any arbitrary point
99 6 franksdeve
                                        case state is
100
 
101
                                                when "00" =>
102
 
103 16 franksdeve
                                                        StepDrive <= "1010";                    -- these states follow proper pattern of coil energizing for turning steppers
104 6 franksdeve
 
105
                                                when "01" =>
106
 
107
                                                        StepDrive <= "1001";
108
 
109
                                                when "10" =>
110
 
111
                                                        StepDrive <= "0101";
112
 
113
                                                when "11" =>
114
 
115
                                                        StepDrive <= "0110";
116
 
117
                                                when others =>
118
 
119
                                        end case; --state
120 17 franksdeve
 
121
                                else
122
 
123
                                        if (ProvideStaticHolding = '0') then --should we leave coils in energized state by defaul or not?
124
 
125
                                                StepDrive <= "0000";
126
 
127
                                        end if;
128 6 franksdeve
 
129
                                end if;
130
 
131
                        end if;
132
 
133
                end if;
134
 
135
        end process;
136
 
137 17 franksdeve
end StepperMotor;

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.