pausa

Dependencies:   mbed

Fork of primercorte by edson antonio vargas villarreal

Committer:
nicolasrojas
Date:
Wed Jun 06 18:31:04 2018 +0000
Revision:
5:9187685429b3
Parent:
1:6ed951d975cc
pausa

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ANTONIO_VARGAS 0:0119b611fc51 1
ANTONIO_VARGAS 0:0119b611fc51 2 #include "stepmotor.h"
ANTONIO_VARGAS 0:0119b611fc51 3 #include "mbed.h"
ANTONIO_VARGAS 0:0119b611fc51 4
ANTONIO_VARGAS 0:0119b611fc51 5 stepmotor::stepmotor(PinName in1, PinName in2, PinName in3, PinName in4) : motor_out(in1,in2,in3,in4) {
ANTONIO_VARGAS 0:0119b611fc51 6
ANTONIO_VARGAS 0:0119b611fc51 7
ANTONIO_VARGAS 0:0119b611fc51 8 motor_out=0x0;
ANTONIO_VARGAS 0:0119b611fc51 9 nstep=0;
ANTONIO_VARGAS 0:0119b611fc51 10 motorSpeed=1100;
ANTONIO_VARGAS 0:0119b611fc51 11
ANTONIO_VARGAS 0:0119b611fc51 12 }
ANTONIO_VARGAS 0:0119b611fc51 13
ANTONIO_VARGAS 0:0119b611fc51 14
ANTONIO_VARGAS 0:0119b611fc51 15 void stepmotor::move() {
ANTONIO_VARGAS 0:0119b611fc51 16
ANTONIO_VARGAS 0:0119b611fc51 17 switch(nstep)
ANTONIO_VARGAS 0:0119b611fc51 18 {
ANTONIO_VARGAS 0:0119b611fc51 19 case 0: motor_out = 0x1; break; // 0001
ANTONIO_VARGAS 0:0119b611fc51 20 case 1: motor_out = 0x3; break; // 0011
ANTONIO_VARGAS 0:0119b611fc51 21 case 2: motor_out = 0x2; break; // 0010
ANTONIO_VARGAS 0:0119b611fc51 22 case 3: motor_out = 0x6; break; // 0110
ANTONIO_VARGAS 0:0119b611fc51 23 case 4: motor_out = 0x4; break; // 0100
ANTONIO_VARGAS 0:0119b611fc51 24 case 5: motor_out = 0xC; break; // 1100
ANTONIO_VARGAS 0:0119b611fc51 25 case 6: motor_out = 0x8; break; // 1000
ANTONIO_VARGAS 0:0119b611fc51 26 case 7: motor_out = 0x9; break; // 1001
ANTONIO_VARGAS 0:0119b611fc51 27
ANTONIO_VARGAS 0:0119b611fc51 28 default: motor_out = 0x0; break; // 0000
ANTONIO_VARGAS 0:0119b611fc51 29 }
ANTONIO_VARGAS 0:0119b611fc51 30 wait_us(motorSpeed);
ANTONIO_VARGAS 0:0119b611fc51 31
ANTONIO_VARGAS 0:0119b611fc51 32 }
ANTONIO_VARGAS 0:0119b611fc51 33
ANTONIO_VARGAS 0:0119b611fc51 34 void stepmotor::set_speed(int speed){
ANTONIO_VARGAS 0:0119b611fc51 35 motorSpeed=speed; //set motor speed us
ANTONIO_VARGAS 0:0119b611fc51 36 }
ANTONIO_VARGAS 0:0119b611fc51 37 uint32_t stepmotor::get_speed(){
ANTONIO_VARGAS 0:0119b611fc51 38 return motorSpeed; //
ANTONIO_VARGAS 0:0119b611fc51 39 }
ANTONIO_VARGAS 0:0119b611fc51 40
ANTONIO_VARGAS 0:0119b611fc51 41 void stepmotor::step(uint32_t num_steps, bool cw) {
ANTONIO_VARGAS 0:0119b611fc51 42 // funcion para mover el motor N pasos CW o CCW
ANTONIO_VARGAS 0:0119b611fc51 43 // num_steps número de paso que da el motor
ANTONIO_VARGAS 0:0119b611fc51 44 // cw =True para dirección en sentido del reloj
ANTONIO_VARGAS 0:0119b611fc51 45 // cw =False para dirección contraria de las manecillas del reloj
ANTONIO_VARGAS 0:0119b611fc51 46
ANTONIO_VARGAS 0:0119b611fc51 47 uint32_t count=num_steps ;
ANTONIO_VARGAS 0:0119b611fc51 48 while(count){
ANTONIO_VARGAS 0:0119b611fc51 49 if (cw) nstep++;
ANTONIO_VARGAS 0:0119b611fc51 50 else nstep--;
ANTONIO_VARGAS 0:0119b611fc51 51 if (nstep>7) nstep=0;
ANTONIO_VARGAS 0:0119b611fc51 52 if (nstep<0) nstep=7;
ANTONIO_VARGAS 0:0119b611fc51 53 move();
ANTONIO_VARGAS 0:0119b611fc51 54 count--;
ANTONIO_VARGAS 0:0119b611fc51 55
ANTONIO_VARGAS 0:0119b611fc51 56 }
ANTONIO_VARGAS 0:0119b611fc51 57
ANTONIO_VARGAS 0:0119b611fc51 58 }