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