servo visual
Realizamos mediante comunicacion serial, ingresando el comando para los servomotores:
COMANDO MOVER MOTOR
POS 1 | POS 2 | POS 3 | POS 4 | POS 5 |
< | #M | , | #°G | > |
- M -> indica el motor que se va a mover (00,01,02)
- °G -> indica los grados a mover del servomotor <,> -> inicio, separdor y fin de comando el inicio de comando no se almacena en el buffer
si el comando no tiene correctamente ingresado los simbolos ',' y '>', nos dara un error.
comando para el paso a paso:
COMANDO MOVER PASO A PASO
POS 1 | POS 2 | POS 3 | POS 4 | POS 5 |
< | #M | DIR | #PASOS | > |
- M -> indica el motor que se va a mover ( 03)
- PASOS -> indica los pasos a mover del motor paso a paso DIR -> indica la direccion en la que girara el motor (00-derecha, 01- izquierda) < > ->inicio, separdor y fin de comando
el inicio de comando no se almacena en el buffer.
En la visualizacion de la matriz se observara el numero del motor que se esta moviendo y su direccion indicada con una flecha
<- hacia la izquierda cuando el angulo de los servos sea mayor que 90, y el sentido del paso a paso sea este. ^ hacia arriba cuando el angulo de los servos sea 90 -> hacia la derecha cuando el angulo de los servos sea menor que 90, y el sentido del paso a paso sea este.
1- servomotor uno (00) 2-servomotor dos (01) 3-servomotor tres (02) 4- paso a paso (03)
main.cpp@1:908a41a33553, 2017-04-27 (annotated)
- Committer:
- Paxtel
- Date:
- Thu Apr 27 21:39:55 2017 +0000
- Revision:
- 1:908a41a33553
- Parent:
- 0:65caec68ab34
funcionamiento de 3 servomoteores y un paso a paso , con vizualizacion sentido de giro en una matriz 8x8
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Paxtel | 0:65caec68ab34 | 1 | #include "mbed.h" |
Paxtel | 1:908a41a33553 | 2 | #include "max7219.h" // incluir librerias. |
Paxtel | 1:908a41a33553 | 3 | #include "vision.h" |
Paxtel | 1:908a41a33553 | 4 | |
Paxtel | 1:908a41a33553 | 5 | |
Paxtel | 1:908a41a33553 | 6 | int motorSpeed; // variable para la velocidad del motor |
Paxtel | 1:908a41a33553 | 7 | |
Paxtel | 1:908a41a33553 | 8 | |
Paxtel | 1:908a41a33553 | 9 | #define DEBUG 1 |
Paxtel | 1:908a41a33553 | 10 | |
Paxtel | 1:908a41a33553 | 11 | //***************************************************************************** |
Paxtel | 1:908a41a33553 | 12 | // COMANDO MOVER MOTOR |
Paxtel | 1:908a41a33553 | 13 | // |POS 1|POS 2|POS 3|POS 4| POS 5| |
Paxtel | 1:908a41a33553 | 14 | // | < | #M | , | #°G | > | |
Paxtel | 1:908a41a33553 | 15 | // |
Paxtel | 1:908a41a33553 | 16 | // #M -> indica el motor que se va a mover |
Paxtel | 1:908a41a33553 | 17 | // #°G -> indica los grados a mover del servomotor |
Paxtel | 1:908a41a33553 | 18 | // <,> -> inicio, separdor y fin de comando |
Paxtel | 1:908a41a33553 | 19 | // el inicio de comando no se almacena en el buffer |
Paxtel | 1:908a41a33553 | 20 | //***************************************************************************** |
Paxtel | 1:908a41a33553 | 21 | |
Paxtel | 1:908a41a33553 | 22 | // VARIABLES PARA DEFINIR EL COMMANDO |
Paxtel | 1:908a41a33553 | 23 | #define BUFF_SIZE 4 |
Paxtel | 1:908a41a33553 | 24 | #define COMM_NUM_MOTOR 0 |
Paxtel | 1:908a41a33553 | 25 | #define COMM_SEPARADOR 1 |
Paxtel | 1:908a41a33553 | 26 | #define COMM_GRADOS_MOTOR 2 |
Paxtel | 1:908a41a33553 | 27 | |
Paxtel | 1:908a41a33553 | 28 | |
Paxtel | 1:908a41a33553 | 29 | uint8_t buffer_command[BUFF_SIZE]={0,0,0,0}; |
Paxtel | 1:908a41a33553 | 30 | |
Paxtel | 1:908a41a33553 | 31 | |
Paxtel | 1:908a41a33553 | 32 | void print_num(uint8_t val) |
Paxtel | 1:908a41a33553 | 33 | |
Paxtel | 1:908a41a33553 | 34 | { |
Paxtel | 1:908a41a33553 | 35 | if (val <10) |
Paxtel | 1:908a41a33553 | 36 | command.putc(val+0x30); |
Paxtel | 1:908a41a33553 | 37 | else |
Paxtel | 1:908a41a33553 | 38 | command.putc(val-9+0x40); |
Paxtel | 1:908a41a33553 | 39 | |
Paxtel | 1:908a41a33553 | 40 | } |
Paxtel | 1:908a41a33553 | 41 | void print_bin2hex (uint8_t val) |
Paxtel | 1:908a41a33553 | 42 | { |
Paxtel | 1:908a41a33553 | 43 | command.printf(" 0x"); |
Paxtel | 1:908a41a33553 | 44 | print_num(val>>4); |
Paxtel | 1:908a41a33553 | 45 | print_num(val&0x0f); |
Paxtel | 1:908a41a33553 | 46 | |
Paxtel | 1:908a41a33553 | 47 | |
Paxtel | 1:908a41a33553 | 48 | } |
Paxtel | 1:908a41a33553 | 49 | |
Paxtel | 1:908a41a33553 | 50 | // TODO : TIMEOUT UART SERIAL |
Paxtel | 1:908a41a33553 | 51 | void Read_command() |
Paxtel | 1:908a41a33553 | 52 | { |
Paxtel | 1:908a41a33553 | 53 | for (uint8_t i=0; i<BUFF_SIZE;i++) |
Paxtel | 1:908a41a33553 | 54 | buffer_command[i]=command.getc(); |
Paxtel | 1:908a41a33553 | 55 | |
Paxtel | 1:908a41a33553 | 56 | } |
Paxtel | 1:908a41a33553 | 57 | |
Paxtel | 1:908a41a33553 | 58 | void echo_command() |
Paxtel | 1:908a41a33553 | 59 | { |
Paxtel | 1:908a41a33553 | 60 | for (uint8_t i=0; i<BUFF_SIZE;i++) |
Paxtel | 1:908a41a33553 | 61 | print_bin2hex(buffer_command[i]); |
Paxtel | 1:908a41a33553 | 62 | |
Paxtel | 1:908a41a33553 | 63 | } |
Paxtel | 1:908a41a33553 | 64 | |
Paxtel | 1:908a41a33553 | 65 | |
Paxtel | 1:908a41a33553 | 66 | uint8_t check_command() |
Paxtel | 1:908a41a33553 | 67 | { if(buffer_command[BUFF_SIZE-1]== '>' && buffer_command[COMM_NUM_MOTOR]==03){ // seleccion del motor paso a paso |
Paxtel | 1:908a41a33553 | 68 | #if DEBUG |
Paxtel | 1:908a41a33553 | 69 | command.printf("\nMover Motor:"); |
Paxtel | 1:908a41a33553 | 70 | print_bin2hex(buffer_command[COMM_NUM_MOTOR]); |
Paxtel | 1:908a41a33553 | 71 | command.printf(" -> "); |
Paxtel | 1:908a41a33553 | 72 | print_bin2hex(buffer_command[COMM_GRADOS_MOTOR]); |
Paxtel | 1:908a41a33553 | 73 | command.printf(" pasos "); |
Paxtel | 1:908a41a33553 | 74 | if(buffer_command[COMM_SEPARADOR]==00){ // seleccion direccion del motor paso a paso (derecha) |
Paxtel | 1:908a41a33553 | 75 | buffer_command[COMM_SEPARADOR]=0; |
Paxtel | 1:908a41a33553 | 76 | command.printf(" a la derecha \n"); |
Paxtel | 1:908a41a33553 | 77 | Print_pantalla_tabla(m4right); |
Paxtel | 1:908a41a33553 | 78 | stepper(buffer_command[COMM_GRADOS_MOTOR],buffer_command[COMM_SEPARADOR]); |
Paxtel | 1:908a41a33553 | 79 | |
Paxtel | 1:908a41a33553 | 80 | #endif |
Paxtel | 1:908a41a33553 | 81 | return 0; |
Paxtel | 1:908a41a33553 | 82 | } |
Paxtel | 1:908a41a33553 | 83 | else if(buffer_command[COMM_SEPARADOR]==01){// seleccion direccion del motor paso a paso (izquierda) |
Paxtel | 1:908a41a33553 | 84 | #if DEBUG |
Paxtel | 1:908a41a33553 | 85 | buffer_command[COMM_SEPARADOR]=1; |
Paxtel | 1:908a41a33553 | 86 | command.printf(" a la izquierda \n"); |
Paxtel | 1:908a41a33553 | 87 | Print_pantalla_tabla(m4left); |
Paxtel | 1:908a41a33553 | 88 | stepper(buffer_command[COMM_GRADOS_MOTOR],buffer_command[COMM_SEPARADOR]); |
Paxtel | 1:908a41a33553 | 89 | |
Paxtel | 1:908a41a33553 | 90 | |
Paxtel | 1:908a41a33553 | 91 | #endif |
Paxtel | 1:908a41a33553 | 92 | return 0; |
Paxtel | 1:908a41a33553 | 93 | } |
Paxtel | 1:908a41a33553 | 94 | #if DEBUG |
Paxtel | 1:908a41a33553 | 95 | command.printf("\n ERROR COMANDO -> "); // error si la direccion de entrada no es ni derecha ni izquierda |
Paxtel | 1:908a41a33553 | 96 | echo_command(); |
Paxtel | 1:908a41a33553 | 97 | #endif |
Paxtel | 1:908a41a33553 | 98 | return 0; |
Paxtel | 1:908a41a33553 | 99 | |
Paxtel | 1:908a41a33553 | 100 | } |
Paxtel | 1:908a41a33553 | 101 | |
Paxtel | 1:908a41a33553 | 102 | else if (buffer_command[BUFF_SIZE-1]== '>' && buffer_command[COMM_SEPARADOR]==','){ // seleccion solo servo motores, verifica que la entrada sea correcta |
Paxtel | 1:908a41a33553 | 103 | |
Paxtel | 1:908a41a33553 | 104 | #if DEBUG |
Paxtel | 1:908a41a33553 | 105 | command.printf("\nMover Motor:"); |
Paxtel | 1:908a41a33553 | 106 | print_bin2hex(buffer_command[COMM_NUM_MOTOR]); |
Paxtel | 1:908a41a33553 | 107 | command.printf(" -> "); |
Paxtel | 1:908a41a33553 | 108 | print_bin2hex(buffer_command[COMM_GRADOS_MOTOR]); |
Paxtel | 1:908a41a33553 | 109 | command.printf(" grados \n"); |
Paxtel | 1:908a41a33553 | 110 | #endif |
Paxtel | 1:908a41a33553 | 111 | return 1; |
Paxtel | 1:908a41a33553 | 112 | } |
Paxtel | 1:908a41a33553 | 113 | #if DEBUG |
Paxtel | 1:908a41a33553 | 114 | command.printf("\n ERROR COMANDO -> "); // error si los simbolos de entrada son incorrectos |
Paxtel | 1:908a41a33553 | 115 | echo_command(); |
Paxtel | 1:908a41a33553 | 116 | #endif |
Paxtel | 1:908a41a33553 | 117 | return 0; |
Paxtel | 1:908a41a33553 | 118 | |
Paxtel | 1:908a41a33553 | 119 | |
Paxtel | 1:908a41a33553 | 120 | } |
Paxtel | 0:65caec68ab34 | 121 | |
Paxtel | 0:65caec68ab34 | 122 | |
Paxtel | 0:65caec68ab34 | 123 | |
Paxtel | 1:908a41a33553 | 124 | |
Paxtel | 1:908a41a33553 | 125 | |
Paxtel | 1:908a41a33553 | 126 | |
Paxtel | 1:908a41a33553 | 127 | |
Paxtel | 1:908a41a33553 | 128 | void command_motor(uint8_t nm,uint8_t grados) |
Paxtel | 1:908a41a33553 | 129 | { |
Paxtel | 1:908a41a33553 | 130 | |
Paxtel | 1:908a41a33553 | 131 | led=1; |
Paxtel | 1:908a41a33553 | 132 | wait(1); |
Paxtel | 1:908a41a33553 | 133 | led=0; |
Paxtel | 1:908a41a33553 | 134 | unsigned int k; |
Paxtel | 1:908a41a33553 | 135 | |
Paxtel | 1:908a41a33553 | 136 | k=((grados*9)+550); // formula para los grados de los servomotores |
Paxtel | 1:908a41a33553 | 137 | |
Paxtel | 1:908a41a33553 | 138 | |
Paxtel | 1:908a41a33553 | 139 | |
Paxtel | 1:908a41a33553 | 140 | |
Paxtel | 1:908a41a33553 | 141 | switch(nm){ // seleccion del servo a mover |
Paxtel | 1:908a41a33553 | 142 | case 00: |
Paxtel | 1:908a41a33553 | 143 | if(grados==0x5a){ //90 grados |
Paxtel | 1:908a41a33553 | 144 | |
Paxtel | 1:908a41a33553 | 145 | Print_pantalla_tabla(m1up); |
Paxtel | 1:908a41a33553 | 146 | wait(1); |
Paxtel | 1:908a41a33553 | 147 | |
Paxtel | 1:908a41a33553 | 148 | mipwm.pulsewidth_us(k); |
Paxtel | 1:908a41a33553 | 149 | wait(1); |
Paxtel | 1:908a41a33553 | 150 | } |
Paxtel | 1:908a41a33553 | 151 | else if (grados<0x5a){ // visualizacion cuando es menor que 90 |
Paxtel | 1:908a41a33553 | 152 | Print_pantalla_tabla(m1right); |
Paxtel | 1:908a41a33553 | 153 | wait(1); |
Paxtel | 1:908a41a33553 | 154 | mipwm.pulsewidth_us(k); |
Paxtel | 1:908a41a33553 | 155 | wait(1); |
Paxtel | 1:908a41a33553 | 156 | } |
Paxtel | 1:908a41a33553 | 157 | else{ // visualizacion cuando es mayor que 90 |
Paxtel | 1:908a41a33553 | 158 | Print_pantalla_tabla(m1left); |
Paxtel | 1:908a41a33553 | 159 | wait(1); |
Paxtel | 1:908a41a33553 | 160 | mipwm.pulsewidth_us(k); |
Paxtel | 1:908a41a33553 | 161 | wait(1); |
Paxtel | 1:908a41a33553 | 162 | } |
Paxtel | 1:908a41a33553 | 163 | |
Paxtel | 1:908a41a33553 | 164 | mipwm.pulsewidth_us(k); |
Paxtel | 1:908a41a33553 | 165 | wait(1); |
Paxtel | 1:908a41a33553 | 166 | break; |
Paxtel | 1:908a41a33553 | 167 | case 01:if(grados==0x5a){ //90 grados |
Paxtel | 0:65caec68ab34 | 168 | |
Paxtel | 1:908a41a33553 | 169 | Print_pantalla_tabla(m2up); |
Paxtel | 1:908a41a33553 | 170 | wait(1); |
Paxtel | 1:908a41a33553 | 171 | } |
Paxtel | 1:908a41a33553 | 172 | else if (grados<0x5a){ // visualizacion cuando es menor que 90 |
Paxtel | 1:908a41a33553 | 173 | Print_pantalla_tabla(m2right); |
Paxtel | 1:908a41a33553 | 174 | wait(1); |
Paxtel | 1:908a41a33553 | 175 | } |
Paxtel | 1:908a41a33553 | 176 | else{// visualizacion cuando es mayor que 90 |
Paxtel | 1:908a41a33553 | 177 | Print_pantalla_tabla(m2left); |
Paxtel | 1:908a41a33553 | 178 | wait(1); |
Paxtel | 1:908a41a33553 | 179 | } |
Paxtel | 1:908a41a33553 | 180 | mipwm2.pulsewidth_us(k); |
Paxtel | 1:908a41a33553 | 181 | wait(1); |
Paxtel | 1:908a41a33553 | 182 | break; |
Paxtel | 1:908a41a33553 | 183 | case 02: |
Paxtel | 1:908a41a33553 | 184 | if(grados==0x5a){ //90 grados |
Paxtel | 1:908a41a33553 | 185 | |
Paxtel | 1:908a41a33553 | 186 | Print_pantalla_tabla(m3up); |
Paxtel | 1:908a41a33553 | 187 | wait(1); |
Paxtel | 1:908a41a33553 | 188 | } |
Paxtel | 1:908a41a33553 | 189 | else if (grados<0x5a){ // visualizacion cuando es menor que 90 |
Paxtel | 1:908a41a33553 | 190 | Print_pantalla_tabla(m3right); |
Paxtel | 1:908a41a33553 | 191 | wait(1); |
Paxtel | 1:908a41a33553 | 192 | } |
Paxtel | 1:908a41a33553 | 193 | else{ // visualizacion cuando es mayor que 90 |
Paxtel | 1:908a41a33553 | 194 | Print_pantalla_tabla(m3left); |
Paxtel | 1:908a41a33553 | 195 | wait(1); |
Paxtel | 1:908a41a33553 | 196 | } |
Paxtel | 1:908a41a33553 | 197 | mipwm3.pulsewidth_us(k); |
Paxtel | 1:908a41a33553 | 198 | wait(1); |
Paxtel | 1:908a41a33553 | 199 | break; |
Paxtel | 1:908a41a33553 | 200 | default: |
Paxtel | 1:908a41a33553 | 201 | break; |
Paxtel | 1:908a41a33553 | 202 | } |
Paxtel | 1:908a41a33553 | 203 | |
Paxtel | 1:908a41a33553 | 204 | |
Paxtel | 1:908a41a33553 | 205 | |
Paxtel | 1:908a41a33553 | 206 | } |
Paxtel | 1:908a41a33553 | 207 | |
Paxtel | 1:908a41a33553 | 208 | |
Paxtel | 1:908a41a33553 | 209 | |
Paxtel | 1:908a41a33553 | 210 | int main() { |
Paxtel | 1:908a41a33553 | 211 | Print_pantalla_tabla(clear); // limpiar matriz |
Paxtel | 1:908a41a33553 | 212 | #if DEBUG |
Paxtel | 1:908a41a33553 | 213 | command.printf("inicio con debug\n"); |
Paxtel | 1:908a41a33553 | 214 | #else |
Paxtel | 1:908a41a33553 | 215 | command.printf("inicio sin debug\n"); |
Paxtel | 1:908a41a33553 | 216 | #endif |
Paxtel | 1:908a41a33553 | 217 | uint8_t val; |
Paxtel | 1:908a41a33553 | 218 | while(1){ |
Paxtel | 1:908a41a33553 | 219 | val=command.getc(); |
Paxtel | 1:908a41a33553 | 220 | if (val== '<'){ |
Paxtel | 1:908a41a33553 | 221 | Read_command(); |
Paxtel | 1:908a41a33553 | 222 | if (check_command()){ |
Paxtel | 1:908a41a33553 | 223 | command_motor(buffer_command[COMM_NUM_MOTOR],buffer_command[COMM_GRADOS_MOTOR]); |
Paxtel | 1:908a41a33553 | 224 | } |
Paxtel | 1:908a41a33553 | 225 | } |
Paxtel | 1:908a41a33553 | 226 | |
Paxtel | 1:908a41a33553 | 227 | } |
Paxtel | 1:908a41a33553 | 228 | } |
Paxtel | 1:908a41a33553 | 229 | |
Paxtel | 1:908a41a33553 | 230 | |
Paxtel | 1:908a41a33553 | 231 | |
Paxtel | 1:908a41a33553 | 232 | |
Paxtel | 1:908a41a33553 | 233 | void Print_pantalla_tabla(unsigned char *pValor){ |
Paxtel | 1:908a41a33553 | 234 | int i; //funcion de puntero, impresión de pantalla |
Paxtel | 1:908a41a33553 | 235 | for (i =0;i<8;i++) |
Paxtel | 1:908a41a33553 | 236 | pantalla.write_digit(1,i+1,pValor[i]); |
Paxtel | 1:908a41a33553 | 237 | |
Paxtel | 1:908a41a33553 | 238 | } |
Paxtel | 0:65caec68ab34 | 239 | |
Paxtel | 0:65caec68ab34 | 240 | |
Paxtel | 0:65caec68ab34 | 241 | |
Paxtel | 0:65caec68ab34 | 242 | |
Paxtel | 0:65caec68ab34 | 243 | |
Paxtel | 1:908a41a33553 | 244 | |
Paxtel | 1:908a41a33553 | 245 | void anticlockwise() { // rotacion del motor paso a paso sentido antihorario |
Paxtel | 1:908a41a33553 | 246 | for (int i = 0; i < 8; i++) { |
Paxtel | 1:908a41a33553 | 247 | |
Paxtel | 1:908a41a33553 | 248 | switch (i) { // activar(1) o desactivar(0) las bobinas |
Paxtel | 1:908a41a33553 | 249 | case 0: { |
Paxtel | 1:908a41a33553 | 250 | _A0=0; |
Paxtel | 1:908a41a33553 | 251 | _A1=0; |
Paxtel | 1:908a41a33553 | 252 | _A2=0; |
Paxtel | 1:908a41a33553 | 253 | _A3=1; |
Paxtel | 1:908a41a33553 | 254 | } |
Paxtel | 1:908a41a33553 | 255 | break; |
Paxtel | 1:908a41a33553 | 256 | case 1: { |
Paxtel | 1:908a41a33553 | 257 | _A0=0; |
Paxtel | 1:908a41a33553 | 258 | _A1=0; |
Paxtel | 1:908a41a33553 | 259 | _A2=1; |
Paxtel | 1:908a41a33553 | 260 | _A3=1; |
Paxtel | 1:908a41a33553 | 261 | } |
Paxtel | 1:908a41a33553 | 262 | break; |
Paxtel | 1:908a41a33553 | 263 | case 2: { |
Paxtel | 1:908a41a33553 | 264 | _A0=0; |
Paxtel | 1:908a41a33553 | 265 | _A1=0; |
Paxtel | 1:908a41a33553 | 266 | _A2=1; |
Paxtel | 1:908a41a33553 | 267 | _A3=0; |
Paxtel | 1:908a41a33553 | 268 | } |
Paxtel | 1:908a41a33553 | 269 | break; |
Paxtel | 1:908a41a33553 | 270 | case 3: { |
Paxtel | 1:908a41a33553 | 271 | _A0=0; |
Paxtel | 1:908a41a33553 | 272 | _A1=1; |
Paxtel | 1:908a41a33553 | 273 | _A2=1; |
Paxtel | 1:908a41a33553 | 274 | _A3=0; |
Paxtel | 1:908a41a33553 | 275 | } |
Paxtel | 1:908a41a33553 | 276 | break; |
Paxtel | 1:908a41a33553 | 277 | case 4: { |
Paxtel | 1:908a41a33553 | 278 | _A0=0; |
Paxtel | 1:908a41a33553 | 279 | _A1=1; |
Paxtel | 1:908a41a33553 | 280 | _A2=0; |
Paxtel | 1:908a41a33553 | 281 | _A3=0; |
Paxtel | 1:908a41a33553 | 282 | } |
Paxtel | 1:908a41a33553 | 283 | break; |
Paxtel | 1:908a41a33553 | 284 | case 5: { |
Paxtel | 1:908a41a33553 | 285 | _A0=1; |
Paxtel | 1:908a41a33553 | 286 | _A1=1; |
Paxtel | 1:908a41a33553 | 287 | _A2=0; |
Paxtel | 1:908a41a33553 | 288 | _A3=0; |
Paxtel | 1:908a41a33553 | 289 | } |
Paxtel | 1:908a41a33553 | 290 | break; |
Paxtel | 1:908a41a33553 | 291 | case 6: { |
Paxtel | 1:908a41a33553 | 292 | _A0=1; |
Paxtel | 1:908a41a33553 | 293 | _A1=0; |
Paxtel | 1:908a41a33553 | 294 | _A2=0; |
Paxtel | 1:908a41a33553 | 295 | _A3=0; |
Paxtel | 1:908a41a33553 | 296 | } |
Paxtel | 1:908a41a33553 | 297 | break; |
Paxtel | 1:908a41a33553 | 298 | case 7: { |
Paxtel | 1:908a41a33553 | 299 | _A0=1; |
Paxtel | 1:908a41a33553 | 300 | _A1=0; |
Paxtel | 1:908a41a33553 | 301 | _A2=0; |
Paxtel | 1:908a41a33553 | 302 | _A3=1; |
Paxtel | 1:908a41a33553 | 303 | } |
Paxtel | 1:908a41a33553 | 304 | break; |
Paxtel | 1:908a41a33553 | 305 | } |
Paxtel | 1:908a41a33553 | 306 | |
Paxtel | 0:65caec68ab34 | 307 | |
Paxtel | 1:908a41a33553 | 308 | wait_us(motorSpeed); // velocidad |
Paxtel | 1:908a41a33553 | 309 | } |
Paxtel | 1:908a41a33553 | 310 | } |
Paxtel | 1:908a41a33553 | 311 | |
Paxtel | 1:908a41a33553 | 312 | void clockwise() { // rotacion del motor paso a paso sentido horario |
Paxtel | 1:908a41a33553 | 313 | for (int i = 7; i >= 0; i--) { |
Paxtel | 0:65caec68ab34 | 314 | |
Paxtel | 1:908a41a33553 | 315 | switch (i) { // activar(1) o desactivar(0) las bobinas |
Paxtel | 1:908a41a33553 | 316 | case 0: { |
Paxtel | 1:908a41a33553 | 317 | _A0=0; |
Paxtel | 1:908a41a33553 | 318 | _A1=0; |
Paxtel | 1:908a41a33553 | 319 | _A2=0; |
Paxtel | 1:908a41a33553 | 320 | _A3=1; |
Paxtel | 1:908a41a33553 | 321 | } |
Paxtel | 1:908a41a33553 | 322 | break; |
Paxtel | 1:908a41a33553 | 323 | case 1: { |
Paxtel | 1:908a41a33553 | 324 | _A0=0; |
Paxtel | 1:908a41a33553 | 325 | _A1=0; |
Paxtel | 1:908a41a33553 | 326 | _A2=1; |
Paxtel | 1:908a41a33553 | 327 | _A3=1; |
Paxtel | 1:908a41a33553 | 328 | } |
Paxtel | 1:908a41a33553 | 329 | break; |
Paxtel | 1:908a41a33553 | 330 | case 2: { |
Paxtel | 1:908a41a33553 | 331 | _A0=0; |
Paxtel | 1:908a41a33553 | 332 | _A1=0; |
Paxtel | 1:908a41a33553 | 333 | _A2=1; |
Paxtel | 1:908a41a33553 | 334 | _A3=0; |
Paxtel | 1:908a41a33553 | 335 | } |
Paxtel | 1:908a41a33553 | 336 | break; |
Paxtel | 1:908a41a33553 | 337 | case 3: { |
Paxtel | 1:908a41a33553 | 338 | _A0=0; |
Paxtel | 1:908a41a33553 | 339 | _A1=1; |
Paxtel | 1:908a41a33553 | 340 | _A2=1; |
Paxtel | 1:908a41a33553 | 341 | _A3=0; |
Paxtel | 1:908a41a33553 | 342 | } |
Paxtel | 1:908a41a33553 | 343 | break; |
Paxtel | 1:908a41a33553 | 344 | case 4: { |
Paxtel | 1:908a41a33553 | 345 | _A0=0; |
Paxtel | 1:908a41a33553 | 346 | _A1=1; |
Paxtel | 1:908a41a33553 | 347 | _A2=0; |
Paxtel | 1:908a41a33553 | 348 | _A3=0; |
Paxtel | 1:908a41a33553 | 349 | } |
Paxtel | 1:908a41a33553 | 350 | break; |
Paxtel | 1:908a41a33553 | 351 | case 5: { |
Paxtel | 1:908a41a33553 | 352 | _A0=1; |
Paxtel | 1:908a41a33553 | 353 | _A1=1; |
Paxtel | 1:908a41a33553 | 354 | _A2=0; |
Paxtel | 1:908a41a33553 | 355 | _A3=0; |
Paxtel | 1:908a41a33553 | 356 | } |
Paxtel | 1:908a41a33553 | 357 | break; |
Paxtel | 1:908a41a33553 | 358 | case 6: { |
Paxtel | 1:908a41a33553 | 359 | _A0=1; |
Paxtel | 1:908a41a33553 | 360 | _A1=0; |
Paxtel | 1:908a41a33553 | 361 | _A2=0; |
Paxtel | 1:908a41a33553 | 362 | _A3=0; |
Paxtel | 1:908a41a33553 | 363 | } |
Paxtel | 1:908a41a33553 | 364 | break; |
Paxtel | 1:908a41a33553 | 365 | case 7: { |
Paxtel | 1:908a41a33553 | 366 | _A0=1; |
Paxtel | 1:908a41a33553 | 367 | _A1=0; |
Paxtel | 1:908a41a33553 | 368 | _A2=0; |
Paxtel | 1:908a41a33553 | 369 | _A3=1; |
Paxtel | 1:908a41a33553 | 370 | } |
Paxtel | 1:908a41a33553 | 371 | break; |
Paxtel | 1:908a41a33553 | 372 | } |
Paxtel | 1:908a41a33553 | 373 | |
Paxtel | 1:908a41a33553 | 374 | |
Paxtel | 1:908a41a33553 | 375 | wait_us(motorSpeed); // velocidad de giro |
Paxtel | 1:908a41a33553 | 376 | } |
Paxtel | 0:65caec68ab34 | 377 | } |
Paxtel | 0:65caec68ab34 | 378 | |
Paxtel | 0:65caec68ab34 | 379 | |
Paxtel | 0:65caec68ab34 | 380 | |
Paxtel | 1:908a41a33553 | 381 | void stepper(uint8_t num_steps, uint8_t direction) {// funcion para el numero de pasos direccion(0-derecha, 1-izquierda) |
Paxtel | 1:908a41a33553 | 382 | int count=0; //inicializar conteo |
Paxtel | 1:908a41a33553 | 383 | motorSpeed=1200; //velocidad de giro |
Paxtel | 1:908a41a33553 | 384 | if (direction==0) // giro hacia la derecha |
Paxtel | 1:908a41a33553 | 385 | do { |
Paxtel | 1:908a41a33553 | 386 | clockwise(); |
Paxtel | 1:908a41a33553 | 387 | count++; |
Paxtel | 1:908a41a33553 | 388 | } while (count<num_steps); // definicion de pasos |
Paxtel | 1:908a41a33553 | 389 | else if (direction==1)// giro antihorario |
Paxtel | 1:908a41a33553 | 390 | do { |
Paxtel | 1:908a41a33553 | 391 | anticlockwise(); |
Paxtel | 1:908a41a33553 | 392 | count++; |
Paxtel | 1:908a41a33553 | 393 | } while (count<num_steps);// definicion de pasos |
Paxtel | 1:908a41a33553 | 394 | |
Paxtel | 1:908a41a33553 | 395 | } |