intentomotorpasoapaso
Fork of 00_LAB_STEPMOTOR by
Embed:
(wiki syntax)
Show/hide line numbers
stepmotor.cpp
00001 00002 /****************************************************************************** 00003 Desarrollado por ferney beltran fbeltran@ecci.edu.co 00004 00005 libreria ejemplo para el motor paso a paso unipolar de 4 fases 00006 00007 ******************************************************************************/ 00008 00009 //***************************************************************************** 00010 00011 #include "stepmotor.h" 00012 #include "mbed.h" 00013 00014 00015 00016 00017 00018 00019 stepmotor::stepmotor(PinName in1, PinName in2, PinName in3, PinName in4) : motor_out(in1,in2,in3,in4) { 00020 00021 00022 motor_out=0x0; 00023 nstep=0; 00024 motorSpeed=1100; 00025 00026 } 00027 00028 00029 void stepmotor::move() { 00030 00031 switch(nstep) 00032 { 00033 case 0: motor_out = 0x1; break; // 0001 00034 case 1: motor_out = 0x3; break; // 0011 00035 case 2: motor_out = 0x2; break; // 0010 00036 case 3: motor_out = 0x6; break; // 0110 00037 case 4: motor_out = 0x4; break; // 0100 00038 case 5: motor_out = 0xC; break; // 1100 00039 case 6: motor_out = 0x8; break; // 1000 00040 case 7: motor_out = 0x9; break; // 1001 00041 00042 default: motor_out = 0x0; break; // 0000 00043 } 00044 wait_us(motorSpeed); 00045 00046 } 00047 00048 void stepmotor::set_speed(int speed){ 00049 motorSpeed=speed; //set motor speed us 00050 } 00051 uint32_t stepmotor::get_speed(){ 00052 return motorSpeed; // 00053 } 00054 00055 void stepmotor::step(uint32_t num_steps, bool cw) { 00056 // funcion para mover el motor N pasos CW o CCW 00057 // num_steps número de paso que da el motor 00058 // cw =True para dirección en sentido del reloj 00059 // cw =False para dirección contraria de las manecillas del reloj 00060 00061 uint32_t count=num_steps ; 00062 while(count){ 00063 if (cw) nstep++; 00064 else nstep--; 00065 if (nstep>7) nstep=0; 00066 if (nstep<0) nstep=7; 00067 move(); 00068 count--; 00069 00070 } 00071 00072 }
Generated on Tue Aug 2 2022 05:20:44 by
1.7.2
