picolo
Fork of Step_Motor by
Step_Motor.cpp@0:ea300190c1f5, 2018-03-27 (annotated)
- Committer:
- Stevenor1997
- Date:
- Tue Mar 27 23:41:42 2018 +0000
- Revision:
- 0:ea300190c1f5
- Child:
- 1:4c622abe2483
Librer?a para el control de un motor paso a paso.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Stevenor1997 | 0:ea300190c1f5 | 1 | #include "Step_Motor.h" |
Stevenor1997 | 0:ea300190c1f5 | 2 | #include "mbed.h" |
Stevenor1997 | 0:ea300190c1f5 | 3 | |
Stevenor1997 | 0:ea300190c1f5 | 4 | stepmotor::stepmotor(PinName in1, PinName in2, PinName in3, PinName in4) : motor_out(in1,in2,in3,in4) { |
Stevenor1997 | 0:ea300190c1f5 | 5 | |
Stevenor1997 | 0:ea300190c1f5 | 6 | |
Stevenor1997 | 0:ea300190c1f5 | 7 | motor_out=0x0; |
Stevenor1997 | 0:ea300190c1f5 | 8 | nstep=0; |
Stevenor1997 | 0:ea300190c1f5 | 9 | motorSpeed=1100; |
Stevenor1997 | 0:ea300190c1f5 | 10 | |
Stevenor1997 | 0:ea300190c1f5 | 11 | } |
Stevenor1997 | 0:ea300190c1f5 | 12 | |
Stevenor1997 | 0:ea300190c1f5 | 13 | |
Stevenor1997 | 0:ea300190c1f5 | 14 | void stepmotor::move() { |
Stevenor1997 | 0:ea300190c1f5 | 15 | |
Stevenor1997 | 0:ea300190c1f5 | 16 | switch(nstep) |
Stevenor1997 | 0:ea300190c1f5 | 17 | { |
Stevenor1997 | 0:ea300190c1f5 | 18 | case 0: motor_out = 0x1; break; // 0001 |
Stevenor1997 | 0:ea300190c1f5 | 19 | case 1: motor_out = 0x3; break; // 0011 |
Stevenor1997 | 0:ea300190c1f5 | 20 | case 2: motor_out = 0x2; break; // 0010 |
Stevenor1997 | 0:ea300190c1f5 | 21 | case 3: motor_out = 0x6; break; // 0110 |
Stevenor1997 | 0:ea300190c1f5 | 22 | case 4: motor_out = 0x4; break; // 0100 |
Stevenor1997 | 0:ea300190c1f5 | 23 | case 5: motor_out = 0xC; break; // 1100 |
Stevenor1997 | 0:ea300190c1f5 | 24 | case 6: motor_out = 0x8; break; // 1000 |
Stevenor1997 | 0:ea300190c1f5 | 25 | case 7: motor_out = 0x9; break; // 1001 |
Stevenor1997 | 0:ea300190c1f5 | 26 | |
Stevenor1997 | 0:ea300190c1f5 | 27 | default: motor_out = 0x0; break; // 0000 |
Stevenor1997 | 0:ea300190c1f5 | 28 | } |
Stevenor1997 | 0:ea300190c1f5 | 29 | wait_us(motorSpeed); |
Stevenor1997 | 0:ea300190c1f5 | 30 | |
Stevenor1997 | 0:ea300190c1f5 | 31 | } |
Stevenor1997 | 0:ea300190c1f5 | 32 | |
Stevenor1997 | 0:ea300190c1f5 | 33 | void stepmotor::set_speed(int speed){ |
Stevenor1997 | 0:ea300190c1f5 | 34 | motorSpeed=speed; //set motor speed us |
Stevenor1997 | 0:ea300190c1f5 | 35 | } |
Stevenor1997 | 0:ea300190c1f5 | 36 | uint32_t stepmotor::get_speed(){ |
Stevenor1997 | 0:ea300190c1f5 | 37 | return motorSpeed; // |
Stevenor1997 | 0:ea300190c1f5 | 38 | } |
Stevenor1997 | 0:ea300190c1f5 | 39 | |
Stevenor1997 | 0:ea300190c1f5 | 40 | void stepmotor::step(uint32_t num_steps, uint8_t cw) { |
Stevenor1997 | 0:ea300190c1f5 | 41 | // funcion para mover el motor N pasos CW o CCW |
Stevenor1997 | 0:ea300190c1f5 | 42 | // num_steps número de paso que da el motor |
Stevenor1997 | 0:ea300190c1f5 | 43 | // cw =True para dirección en sentido del reloj |
Stevenor1997 | 0:ea300190c1f5 | 44 | // cw =False para dirección contraria de las manecillas del reloj |
Stevenor1997 | 0:ea300190c1f5 | 45 | |
Stevenor1997 | 0:ea300190c1f5 | 46 | uint32_t count=num_steps ; |
Stevenor1997 | 0:ea300190c1f5 | 47 | while(count){ |
Stevenor1997 | 0:ea300190c1f5 | 48 | if (cw==1) nstep++; |
Stevenor1997 | 0:ea300190c1f5 | 49 | if (cw==0) nstep--; |
Stevenor1997 | 0:ea300190c1f5 | 50 | if (nstep>7) nstep=0; |
Stevenor1997 | 0:ea300190c1f5 | 51 | if (nstep<0) nstep=7; |
Stevenor1997 | 0:ea300190c1f5 | 52 | move(); |
Stevenor1997 | 0:ea300190c1f5 | 53 | count--; |
Stevenor1997 | 0:ea300190c1f5 | 54 | |
Stevenor1997 | 0:ea300190c1f5 | 55 | } |
Stevenor1997 | 0:ea300190c1f5 | 56 | |
Stevenor1997 | 0:ea300190c1f5 | 57 | } |