nicolas rojas
/
segundo_copy
pausa
Fork of primercorte by
stepmotor.cpp@0:0119b611fc51, 2018-04-11 (annotated)
- Committer:
- ANTONIO_VARGAS
- Date:
- Wed Apr 11 02:19:13 2018 +0000
- Revision:
- 0:0119b611fc51
- Child:
- 1:6ed951d975cc
nnhjk
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ANTONIO_VARGAS | 0:0119b611fc51 | 1 | |
ANTONIO_VARGAS | 0:0119b611fc51 | 2 | /****************************************************************************** |
ANTONIO_VARGAS | 0:0119b611fc51 | 3 | Desarrollado por ferney beltran fbeltran@ecci.edu.co |
ANTONIO_VARGAS | 0:0119b611fc51 | 4 | |
ANTONIO_VARGAS | 0:0119b611fc51 | 5 | libreria ejemplo para el motor paso a paso unipolar de 4 fases |
ANTONIO_VARGAS | 0:0119b611fc51 | 6 | |
ANTONIO_VARGAS | 0:0119b611fc51 | 7 | ******************************************************************************/ |
ANTONIO_VARGAS | 0:0119b611fc51 | 8 | |
ANTONIO_VARGAS | 0:0119b611fc51 | 9 | //***************************************************************************** |
ANTONIO_VARGAS | 0:0119b611fc51 | 10 | |
ANTONIO_VARGAS | 0:0119b611fc51 | 11 | #include "stepmotor.h" |
ANTONIO_VARGAS | 0:0119b611fc51 | 12 | #include "mbed.h" |
ANTONIO_VARGAS | 0:0119b611fc51 | 13 | |
ANTONIO_VARGAS | 0:0119b611fc51 | 14 | |
ANTONIO_VARGAS | 0:0119b611fc51 | 15 | |
ANTONIO_VARGAS | 0:0119b611fc51 | 16 | |
ANTONIO_VARGAS | 0:0119b611fc51 | 17 | |
ANTONIO_VARGAS | 0:0119b611fc51 | 18 | |
ANTONIO_VARGAS | 0:0119b611fc51 | 19 | stepmotor::stepmotor(PinName in1, PinName in2, PinName in3, PinName in4) : motor_out(in1,in2,in3,in4) { |
ANTONIO_VARGAS | 0:0119b611fc51 | 20 | |
ANTONIO_VARGAS | 0:0119b611fc51 | 21 | |
ANTONIO_VARGAS | 0:0119b611fc51 | 22 | motor_out=0x0; |
ANTONIO_VARGAS | 0:0119b611fc51 | 23 | nstep=0; |
ANTONIO_VARGAS | 0:0119b611fc51 | 24 | motorSpeed=1100; |
ANTONIO_VARGAS | 0:0119b611fc51 | 25 | |
ANTONIO_VARGAS | 0:0119b611fc51 | 26 | } |
ANTONIO_VARGAS | 0:0119b611fc51 | 27 | |
ANTONIO_VARGAS | 0:0119b611fc51 | 28 | |
ANTONIO_VARGAS | 0:0119b611fc51 | 29 | void stepmotor::move() { |
ANTONIO_VARGAS | 0:0119b611fc51 | 30 | |
ANTONIO_VARGAS | 0:0119b611fc51 | 31 | switch(nstep) |
ANTONIO_VARGAS | 0:0119b611fc51 | 32 | { |
ANTONIO_VARGAS | 0:0119b611fc51 | 33 | case 0: motor_out = 0x1; break; // 0001 |
ANTONIO_VARGAS | 0:0119b611fc51 | 34 | case 1: motor_out = 0x3; break; // 0011 |
ANTONIO_VARGAS | 0:0119b611fc51 | 35 | case 2: motor_out = 0x2; break; // 0010 |
ANTONIO_VARGAS | 0:0119b611fc51 | 36 | case 3: motor_out = 0x6; break; // 0110 |
ANTONIO_VARGAS | 0:0119b611fc51 | 37 | case 4: motor_out = 0x4; break; // 0100 |
ANTONIO_VARGAS | 0:0119b611fc51 | 38 | case 5: motor_out = 0xC; break; // 1100 |
ANTONIO_VARGAS | 0:0119b611fc51 | 39 | case 6: motor_out = 0x8; break; // 1000 |
ANTONIO_VARGAS | 0:0119b611fc51 | 40 | case 7: motor_out = 0x9; break; // 1001 |
ANTONIO_VARGAS | 0:0119b611fc51 | 41 | |
ANTONIO_VARGAS | 0:0119b611fc51 | 42 | default: motor_out = 0x0; break; // 0000 |
ANTONIO_VARGAS | 0:0119b611fc51 | 43 | } |
ANTONIO_VARGAS | 0:0119b611fc51 | 44 | wait_us(motorSpeed); |
ANTONIO_VARGAS | 0:0119b611fc51 | 45 | |
ANTONIO_VARGAS | 0:0119b611fc51 | 46 | } |
ANTONIO_VARGAS | 0:0119b611fc51 | 47 | |
ANTONIO_VARGAS | 0:0119b611fc51 | 48 | void stepmotor::set_speed(int speed){ |
ANTONIO_VARGAS | 0:0119b611fc51 | 49 | motorSpeed=speed; //set motor speed us |
ANTONIO_VARGAS | 0:0119b611fc51 | 50 | } |
ANTONIO_VARGAS | 0:0119b611fc51 | 51 | uint32_t stepmotor::get_speed(){ |
ANTONIO_VARGAS | 0:0119b611fc51 | 52 | return motorSpeed; // |
ANTONIO_VARGAS | 0:0119b611fc51 | 53 | } |
ANTONIO_VARGAS | 0:0119b611fc51 | 54 | |
ANTONIO_VARGAS | 0:0119b611fc51 | 55 | void stepmotor::step(uint32_t num_steps, bool cw) { |
ANTONIO_VARGAS | 0:0119b611fc51 | 56 | // funcion para mover el motor N pasos CW o CCW |
ANTONIO_VARGAS | 0:0119b611fc51 | 57 | // num_steps número de paso que da el motor |
ANTONIO_VARGAS | 0:0119b611fc51 | 58 | // cw =True para dirección en sentido del reloj |
ANTONIO_VARGAS | 0:0119b611fc51 | 59 | // cw =False para dirección contraria de las manecillas del reloj |
ANTONIO_VARGAS | 0:0119b611fc51 | 60 | |
ANTONIO_VARGAS | 0:0119b611fc51 | 61 | uint32_t count=num_steps ; |
ANTONIO_VARGAS | 0:0119b611fc51 | 62 | while(count){ |
ANTONIO_VARGAS | 0:0119b611fc51 | 63 | if (cw) nstep++; |
ANTONIO_VARGAS | 0:0119b611fc51 | 64 | else nstep--; |
ANTONIO_VARGAS | 0:0119b611fc51 | 65 | if (nstep>7) nstep=0; |
ANTONIO_VARGAS | 0:0119b611fc51 | 66 | if (nstep<0) nstep=7; |
ANTONIO_VARGAS | 0:0119b611fc51 | 67 | move(); |
ANTONIO_VARGAS | 0:0119b611fc51 | 68 | count--; |
ANTONIO_VARGAS | 0:0119b611fc51 | 69 | |
ANTONIO_VARGAS | 0:0119b611fc51 | 70 | } |
ANTONIO_VARGAS | 0:0119b611fc51 | 71 | |
ANTONIO_VARGAS | 0:0119b611fc51 | 72 | } |