Accessories
Arduino for motors and lights library.
AFMotor.hpp
1 #ifndef _LIB_SAM_
2 #ifndef ARDUINO_ARCH_SAM
3 // Adafruit Motor shield library
4 // copyright Adafruit Industries LLC, 2009
5 // this code is public domain, enjoy!
6 
7 /*
8  * Usage Notes:
9  * For PIC32, all features work properly with the following two exceptions:
10  *
11  * 1) Because the PIC32 only has 5 ANALOG outputs, and the AFMotor shield needs 6
12  * to completely operate (for for motor outputs and two for RC servos), the
13  * M1 motor output will not have ANALOG ability when used with a PIC32 board.
14  * However, there is a very simple workaround. If you need to drive a stepper
15  * or DC motor with ANALOG on motor output M1, you can use the ANALOG output on pin
16  * 9 or pin 10 (normally use for RC servo outputs on Arduino, not needed for
17  * RC servo outputs on PIC32) to drive the ANALOG input for M1 by simply putting
18  * a jumber from pin 9 to pin 11 or pin 10 to pin 11. Then uncomment one of the
19  * two #defines below to activate the ANALOG on either pin 9 or pin 10. You will
20  * then have a fully functional microstepping for 2 stepper motors, or four
21  * DC motor outputs with ANALOG.
22  *
23  * 2) There is a conflict between RC Servo outputs on pins 9 and pins 10 and
24  * the operation of DC motors and stepper motors as of 9/2012. This issue
25  * will get fixed in future MPIDE releases, but at the present time it means
26  * that the Motor Party example will NOT work properly. Any time you attach
27  * an RC servo to pins 9 or pins 10, ALL ANALOG outputs on the whole board will
28  * stop working. Thus no steppers or DC motors.
29  *
30  */
31 // <BPS> 09/15/2012 Modified for use with chipKIT boards
32 
33 
34 #ifndef _AFMotor_h_
35 #define _AFMotor_h_
36 
37 #if !defined(__AVR_ATmega32U4__)
38 #include <inttypes.h>
39 #if defined(__AVR__)
40  #include <avr/io.h>
41 
42  //#define MOTORDEBUG 1
43 
44  #define MICROSTEPS 16 // 8 or 16
45 
46  #define MOTOR12_64KHZ _BV(CS20) // no prescale
47  #define MOTOR12_8KHZ _BV(CS21) // divide by 8
48  #define MOTOR12_2KHZ _BV(CS21) | _BV(CS20) // divide by 32
49  #define MOTOR12_1KHZ _BV(CS22) // divide by 64
50 
51  #define MOTOR34_64KHZ _BV(CS00) // no prescale
52  #define MOTOR34_8KHZ _BV(CS01) // divide by 8
53  #define MOTOR34_1KHZ _BV(CS01) | _BV(CS00) // divide by 64
54 
55  #define DC_MOTOR_PWM_RATE MOTOR34_8KHZ // ANALOG rate for DC motors
56  #define STEPPER1_PWM_RATE MOTOR12_64KHZ // ANALOG rate for stepper 1
57  #define STEPPER2_PWM_RATE MOTOR34_64KHZ // ANALOG rate for stepper 2
58 
59 #elif defined(__PIC32MX__)
60  //#define MOTORDEBUG 1
61 
62  // Uncomment the one of following lines if you have put a jumper from
63  // either pin 9 to pin 11 or pin 10 to pin 11 on your Motor Shield.
64  // Either will enable ANALOG for M1
65  //#define PIC32_USE_PIN9_FOR_M1_PWM
66  //#define PIC32_USE_PIN10_FOR_M1_PWM
67 
68  #define MICROSTEPS 16 // 8 or 16
69 
70  // For PIC32 Timers, define prescale settings by ANALOG frequency
71  #define MOTOR12_312KHZ 0 // 1:1, actual frequency 312KHz
72  #define MOTOR12_156KHZ 1 // 1:2, actual frequency 156KHz
73  #define MOTOR12_64KHZ 2 // 1:4, actual frequency 78KHz
74  #define MOTOR12_39KHZ 3 // 1:8, acutal frequency 39KHz
75  #define MOTOR12_19KHZ 4 // 1:16, actual frequency 19KHz
76  #define MOTOR12_8KHZ 5 // 1:32, actual frequency 9.7KHz
77  #define MOTOR12_4_8KHZ 6 // 1:64, actual frequency 4.8KHz
78  #define MOTOR12_2KHZ 7 // 1:256, actual frequency 1.2KHz
79  #define MOTOR12_1KHZ 7 // 1:256, actual frequency 1.2KHz
80 
81  #define MOTOR34_312KHZ 0 // 1:1, actual frequency 312KHz
82  #define MOTOR34_156KHZ 1 // 1:2, actual frequency 156KHz
83  #define MOTOR34_64KHZ 2 // 1:4, actual frequency 78KHz
84  #define MOTOR34_39KHZ 3 // 1:8, acutal frequency 39KHz
85  #define MOTOR34_19KHZ 4 // 1:16, actual frequency 19KHz
86  #define MOTOR34_8KHZ 5 // 1:32, actual frequency 9.7KHz
87  #define MOTOR34_4_8KHZ 6 // 1:64, actual frequency 4.8KHz
88  #define MOTOR34_2KHZ 7 // 1:256, actual frequency 1.2KHz
89  #define MOTOR34_1KHZ 7 // 1:256, actual frequency 1.2KHz
90 
91  // ANALOG rate for DC motors.
92  #define DC_MOTOR_PWM_RATE MOTOR34_39KHZ
93  // Note: for PIC32, both of these must be set to the same value
94  // since there's only one timebase for all 4 ANALOG outputs
95  #define STEPPER1_PWM_RATE MOTOR12_39KHZ
96  #define STEPPER2_PWM_RATE MOTOR34_39KHZ
97 
98 #endif
99 
100 // Bit positions in the 74HCT595 shift register output
101 #define MOTOR1_A 2
102 #define MOTOR1_B 3
103 #define MOTOR2_A 1
104 #define MOTOR2_B 4
105 #define MOTOR4_A 0
106 #define MOTOR4_B 6
107 #define MOTOR3_A 5
108 #define MOTOR3_B 7
109 
110 // Constants that the user passes in to the motor calls
111 #define FORWARD 1
112 #define BACKWARD 2
113 #define BRAKE 3
114 #define RELEASE 4
115 
116 // Constants that the user passes in to the stepper calls
117 #define SINGLE 1
118 #define DOUBLE 2
119 #define INTERLEAVE 3
120 #define MICROSTEP 4
121 
122 /*
123 #define LATCH 4
124 #define LATCH_DDR DDRB
125 #define LATCH_PORT PORTB
126 
127 #define CLK_PORT PORTD
128 #define CLK_DDR DDRD
129 #define CLK 4
130 
131 #define ENABLE_PORT PORTD
132 #define ENABLE_DDR DDRD
133 #define ENABLE 7
134 
135 #define SER 0
136 #define SER_DDR DDRB
137 #define SER_PORT PORTB
138 */
139 
140 // Arduino pin names for interface to 74HCT595 latch
141 #define MOTORLATCH 12
142 #define MOTORCLK 4
143 #define MOTORENABLE 7
144 #define MOTORDATA 8
145 
147 {
148  public:
149  AFMotorController(void);
150  void enable(void);
151  friend class AF_DCMotor;
152  void latch_tx(void);
153  uint8_t TimerInitalized;
154 };
155 
157 {
158  public:
159  AF_DCMotor(uint8_t motornum, uint8_t freq = DC_MOTOR_PWM_RATE);
160  void run(uint8_t);
161  void setSpeed(uint8_t);
162 
163  public:
164  uint8_t motornum, pwmfreq;
165 };
166 
167 class AF_Stepper {
168  public:
169  AF_Stepper(uint16_t, uint8_t);
170  void step(uint16_t steps, uint8_t dir, uint8_t style = SINGLE);
171  void setSpeed(uint16_t);
172  uint8_t onestep(uint8_t dir, uint8_t style);
173  void release(void);
174  uint16_t revsteps; // # steps per revolution
175  uint8_t steppernum;
176  uint32_t usperstep, steppingcounter;
177  private:
178  uint8_t currentstep;
179 
180 };
181 
182 uint8_t getlatchstate(void);
183 #endif
184 #endif
185 #endif
186 #endif