Entrega 3er corte - sistemas embebidos

Committer:
Bethory
Date:
Wed May 30 04:46:28 2018 +0000
Revision:
1:fcdb45ee95b9
Parent:
0:6ad07c9019fd
Entrega Final

Who changed what in which revision?

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