Library for 4 Phase Step Motor like 28BYJ-48
Dependents: Stepper_4 LV9_GRUPA2_TIM008_ZADATAK2 LV9_PAI_Grupa2_Tim003-Zadatak1 LV9_PAI_Grupa2-Tim003 ... more
sMotor.cpp@0:4b3b9e047ce3, 2012-06-14 (annotated)
- Committer:
- XtaticO
- Date:
- Thu Jun 14 12:16:40 2012 +0000
- Revision:
- 0:4b3b9e047ce3
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
XtaticO | 0:4b3b9e047ce3 | 1 | /* |
XtaticO | 0:4b3b9e047ce3 | 2 | ############################################ |
XtaticO | 0:4b3b9e047ce3 | 3 | ## sMotor v0.1 Library ## |
XtaticO | 0:4b3b9e047ce3 | 4 | ## created by Samuel Matildes ## |
XtaticO | 0:4b3b9e047ce3 | 5 | ############################################ |
XtaticO | 0:4b3b9e047ce3 | 6 | ---- sam.naeec@gmail.com ----- |
XtaticO | 0:4b3b9e047ce3 | 7 | This library was made for 4-Phase Stepper Motors |
XtaticO | 0:4b3b9e047ce3 | 8 | I don't take any resposability for the damage caused to your equipment. |
XtaticO | 0:4b3b9e047ce3 | 9 | |
XtaticO | 0:4b3b9e047ce3 | 10 | */ |
XtaticO | 0:4b3b9e047ce3 | 11 | |
XtaticO | 0:4b3b9e047ce3 | 12 | #include "sMotor.h" |
XtaticO | 0:4b3b9e047ce3 | 13 | |
XtaticO | 0:4b3b9e047ce3 | 14 | #include "mbed.h" |
XtaticO | 0:4b3b9e047ce3 | 15 | |
XtaticO | 0:4b3b9e047ce3 | 16 | int motorSpeed; // Steper speed |
XtaticO | 0:4b3b9e047ce3 | 17 | |
XtaticO | 0:4b3b9e047ce3 | 18 | sMotor::sMotor(PinName A0, PinName A1, PinName A2, PinName A3) : _A0(A0), _A1(A1), _A2(A2), _A3(A3) { // Defenition of motor pins |
XtaticO | 0:4b3b9e047ce3 | 19 | _A0=0; |
XtaticO | 0:4b3b9e047ce3 | 20 | _A1=0; |
XtaticO | 0:4b3b9e047ce3 | 21 | _A2=0; |
XtaticO | 0:4b3b9e047ce3 | 22 | _A3=0; |
XtaticO | 0:4b3b9e047ce3 | 23 | } |
XtaticO | 0:4b3b9e047ce3 | 24 | |
XtaticO | 0:4b3b9e047ce3 | 25 | |
XtaticO | 0:4b3b9e047ce3 | 26 | void sMotor::anticlockwise() { // rotate the motor 1 step anticlockwise |
XtaticO | 0:4b3b9e047ce3 | 27 | for (int i = 0; i < 8; i++) { |
XtaticO | 0:4b3b9e047ce3 | 28 | |
XtaticO | 0:4b3b9e047ce3 | 29 | switch (i) { // activate the ports A0, A2, A3, A3 in a binary sequence for steps |
XtaticO | 0:4b3b9e047ce3 | 30 | case 0: { |
XtaticO | 0:4b3b9e047ce3 | 31 | _A0=0; |
XtaticO | 0:4b3b9e047ce3 | 32 | _A1=0; |
XtaticO | 0:4b3b9e047ce3 | 33 | _A2=0; |
XtaticO | 0:4b3b9e047ce3 | 34 | _A3=1; |
XtaticO | 0:4b3b9e047ce3 | 35 | } |
XtaticO | 0:4b3b9e047ce3 | 36 | break; |
XtaticO | 0:4b3b9e047ce3 | 37 | case 1: { |
XtaticO | 0:4b3b9e047ce3 | 38 | _A0=0; |
XtaticO | 0:4b3b9e047ce3 | 39 | _A1=0; |
XtaticO | 0:4b3b9e047ce3 | 40 | _A2=1; |
XtaticO | 0:4b3b9e047ce3 | 41 | _A3=1; |
XtaticO | 0:4b3b9e047ce3 | 42 | } |
XtaticO | 0:4b3b9e047ce3 | 43 | break; |
XtaticO | 0:4b3b9e047ce3 | 44 | case 2: { |
XtaticO | 0:4b3b9e047ce3 | 45 | _A0=0; |
XtaticO | 0:4b3b9e047ce3 | 46 | _A1=0; |
XtaticO | 0:4b3b9e047ce3 | 47 | _A2=1; |
XtaticO | 0:4b3b9e047ce3 | 48 | _A3=0; |
XtaticO | 0:4b3b9e047ce3 | 49 | } |
XtaticO | 0:4b3b9e047ce3 | 50 | break; |
XtaticO | 0:4b3b9e047ce3 | 51 | case 3: { |
XtaticO | 0:4b3b9e047ce3 | 52 | _A0=0; |
XtaticO | 0:4b3b9e047ce3 | 53 | _A1=1; |
XtaticO | 0:4b3b9e047ce3 | 54 | _A2=1; |
XtaticO | 0:4b3b9e047ce3 | 55 | _A3=0; |
XtaticO | 0:4b3b9e047ce3 | 56 | } |
XtaticO | 0:4b3b9e047ce3 | 57 | break; |
XtaticO | 0:4b3b9e047ce3 | 58 | case 4: { |
XtaticO | 0:4b3b9e047ce3 | 59 | _A0=0; |
XtaticO | 0:4b3b9e047ce3 | 60 | _A1=1; |
XtaticO | 0:4b3b9e047ce3 | 61 | _A2=0; |
XtaticO | 0:4b3b9e047ce3 | 62 | _A3=0; |
XtaticO | 0:4b3b9e047ce3 | 63 | } |
XtaticO | 0:4b3b9e047ce3 | 64 | break; |
XtaticO | 0:4b3b9e047ce3 | 65 | case 5: { |
XtaticO | 0:4b3b9e047ce3 | 66 | _A0=1; |
XtaticO | 0:4b3b9e047ce3 | 67 | _A1=1; |
XtaticO | 0:4b3b9e047ce3 | 68 | _A2=0; |
XtaticO | 0:4b3b9e047ce3 | 69 | _A3=0; |
XtaticO | 0:4b3b9e047ce3 | 70 | } |
XtaticO | 0:4b3b9e047ce3 | 71 | break; |
XtaticO | 0:4b3b9e047ce3 | 72 | case 6: { |
XtaticO | 0:4b3b9e047ce3 | 73 | _A0=1; |
XtaticO | 0:4b3b9e047ce3 | 74 | _A1=0; |
XtaticO | 0:4b3b9e047ce3 | 75 | _A2=0; |
XtaticO | 0:4b3b9e047ce3 | 76 | _A3=0; |
XtaticO | 0:4b3b9e047ce3 | 77 | } |
XtaticO | 0:4b3b9e047ce3 | 78 | break; |
XtaticO | 0:4b3b9e047ce3 | 79 | case 7: { |
XtaticO | 0:4b3b9e047ce3 | 80 | _A0=1; |
XtaticO | 0:4b3b9e047ce3 | 81 | _A1=0; |
XtaticO | 0:4b3b9e047ce3 | 82 | _A2=0; |
XtaticO | 0:4b3b9e047ce3 | 83 | _A3=1; |
XtaticO | 0:4b3b9e047ce3 | 84 | } |
XtaticO | 0:4b3b9e047ce3 | 85 | break; |
XtaticO | 0:4b3b9e047ce3 | 86 | } |
XtaticO | 0:4b3b9e047ce3 | 87 | |
XtaticO | 0:4b3b9e047ce3 | 88 | |
XtaticO | 0:4b3b9e047ce3 | 89 | wait_us(motorSpeed); // wait time defines the speed |
XtaticO | 0:4b3b9e047ce3 | 90 | } |
XtaticO | 0:4b3b9e047ce3 | 91 | } |
XtaticO | 0:4b3b9e047ce3 | 92 | |
XtaticO | 0:4b3b9e047ce3 | 93 | void sMotor::clockwise() { // rotate the motor 1 step clockwise |
XtaticO | 0:4b3b9e047ce3 | 94 | for (int i = 7; i >= 0; i--) { |
XtaticO | 0:4b3b9e047ce3 | 95 | |
XtaticO | 0:4b3b9e047ce3 | 96 | switch (i) { |
XtaticO | 0:4b3b9e047ce3 | 97 | case 0: { |
XtaticO | 0:4b3b9e047ce3 | 98 | _A0=0; |
XtaticO | 0:4b3b9e047ce3 | 99 | _A1=0; |
XtaticO | 0:4b3b9e047ce3 | 100 | _A2=0; |
XtaticO | 0:4b3b9e047ce3 | 101 | _A3=1; |
XtaticO | 0:4b3b9e047ce3 | 102 | } |
XtaticO | 0:4b3b9e047ce3 | 103 | break; |
XtaticO | 0:4b3b9e047ce3 | 104 | case 1: { |
XtaticO | 0:4b3b9e047ce3 | 105 | _A0=0; |
XtaticO | 0:4b3b9e047ce3 | 106 | _A1=0; |
XtaticO | 0:4b3b9e047ce3 | 107 | _A2=1; |
XtaticO | 0:4b3b9e047ce3 | 108 | _A3=1; |
XtaticO | 0:4b3b9e047ce3 | 109 | } |
XtaticO | 0:4b3b9e047ce3 | 110 | break; |
XtaticO | 0:4b3b9e047ce3 | 111 | case 2: { |
XtaticO | 0:4b3b9e047ce3 | 112 | _A0=0; |
XtaticO | 0:4b3b9e047ce3 | 113 | _A1=0; |
XtaticO | 0:4b3b9e047ce3 | 114 | _A2=1; |
XtaticO | 0:4b3b9e047ce3 | 115 | _A3=0; |
XtaticO | 0:4b3b9e047ce3 | 116 | } |
XtaticO | 0:4b3b9e047ce3 | 117 | break; |
XtaticO | 0:4b3b9e047ce3 | 118 | case 3: { |
XtaticO | 0:4b3b9e047ce3 | 119 | _A0=0; |
XtaticO | 0:4b3b9e047ce3 | 120 | _A1=1; |
XtaticO | 0:4b3b9e047ce3 | 121 | _A2=1; |
XtaticO | 0:4b3b9e047ce3 | 122 | _A3=0; |
XtaticO | 0:4b3b9e047ce3 | 123 | } |
XtaticO | 0:4b3b9e047ce3 | 124 | break; |
XtaticO | 0:4b3b9e047ce3 | 125 | case 4: { |
XtaticO | 0:4b3b9e047ce3 | 126 | _A0=0; |
XtaticO | 0:4b3b9e047ce3 | 127 | _A1=1; |
XtaticO | 0:4b3b9e047ce3 | 128 | _A2=0; |
XtaticO | 0:4b3b9e047ce3 | 129 | _A3=0; |
XtaticO | 0:4b3b9e047ce3 | 130 | } |
XtaticO | 0:4b3b9e047ce3 | 131 | break; |
XtaticO | 0:4b3b9e047ce3 | 132 | case 5: { |
XtaticO | 0:4b3b9e047ce3 | 133 | _A0=1; |
XtaticO | 0:4b3b9e047ce3 | 134 | _A1=1; |
XtaticO | 0:4b3b9e047ce3 | 135 | _A2=0; |
XtaticO | 0:4b3b9e047ce3 | 136 | _A3=0; |
XtaticO | 0:4b3b9e047ce3 | 137 | } |
XtaticO | 0:4b3b9e047ce3 | 138 | break; |
XtaticO | 0:4b3b9e047ce3 | 139 | case 6: { |
XtaticO | 0:4b3b9e047ce3 | 140 | _A0=1; |
XtaticO | 0:4b3b9e047ce3 | 141 | _A1=0; |
XtaticO | 0:4b3b9e047ce3 | 142 | _A2=0; |
XtaticO | 0:4b3b9e047ce3 | 143 | _A3=0; |
XtaticO | 0:4b3b9e047ce3 | 144 | } |
XtaticO | 0:4b3b9e047ce3 | 145 | break; |
XtaticO | 0:4b3b9e047ce3 | 146 | case 7: { |
XtaticO | 0:4b3b9e047ce3 | 147 | _A0=1; |
XtaticO | 0:4b3b9e047ce3 | 148 | _A1=0; |
XtaticO | 0:4b3b9e047ce3 | 149 | _A2=0; |
XtaticO | 0:4b3b9e047ce3 | 150 | _A3=1; |
XtaticO | 0:4b3b9e047ce3 | 151 | } |
XtaticO | 0:4b3b9e047ce3 | 152 | break; |
XtaticO | 0:4b3b9e047ce3 | 153 | } |
XtaticO | 0:4b3b9e047ce3 | 154 | |
XtaticO | 0:4b3b9e047ce3 | 155 | |
XtaticO | 0:4b3b9e047ce3 | 156 | wait_us(motorSpeed); // wait time defines the speed |
XtaticO | 0:4b3b9e047ce3 | 157 | } |
XtaticO | 0:4b3b9e047ce3 | 158 | } |
XtaticO | 0:4b3b9e047ce3 | 159 | void sMotor::step(int num_steps, int direction, int speed) {// steper function: number of steps, direction (0- right, 1- left), speed (default 1200) |
XtaticO | 0:4b3b9e047ce3 | 160 | int count=0; // initalize step count |
XtaticO | 0:4b3b9e047ce3 | 161 | motorSpeed=speed; //set motor speed |
XtaticO | 0:4b3b9e047ce3 | 162 | if (direction==0) // turn clockwise |
XtaticO | 0:4b3b9e047ce3 | 163 | do { |
XtaticO | 0:4b3b9e047ce3 | 164 | clockwise(); |
XtaticO | 0:4b3b9e047ce3 | 165 | count++; |
XtaticO | 0:4b3b9e047ce3 | 166 | } while (count<num_steps); // turn number of steps applied |
XtaticO | 0:4b3b9e047ce3 | 167 | else if (direction==1)// turn anticlockwise |
XtaticO | 0:4b3b9e047ce3 | 168 | do { |
XtaticO | 0:4b3b9e047ce3 | 169 | anticlockwise(); |
XtaticO | 0:4b3b9e047ce3 | 170 | count++; |
XtaticO | 0:4b3b9e047ce3 | 171 | } while (count<num_steps);// turn number of steps applied |
XtaticO | 0:4b3b9e047ce3 | 172 | |
XtaticO | 0:4b3b9e047ce3 | 173 | } |