Daniel Vizcaya / Mbed OS 04_RTOS_Embebidos
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers stepmotor.cpp Source File

stepmotor.cpp

00001 #include "stepmotor.h"
00002 #include "mbed.h"
00003  
00004 
00005 
00006 
00007 
00008 
00009 stepmotor::stepmotor(PinName in1, PinName in2, PinName in3, PinName in4) : motor_out(in1,in2,in3,in4) { 
00010 
00011 
00012     motor_out=0x0;
00013     nstep=0;
00014     motorSpeed=1100;
00015     
00016 }
00017  
00018  
00019 void stepmotor::move() { 
00020    
00021    switch(nstep)
00022         { 
00023             case 0: motor_out = 0x1; break;  // 0001
00024             case 1: motor_out = 0x3; break;  // 0011
00025             case 2: motor_out = 0x2; break;  // 0010   
00026             case 3: motor_out = 0x6; break;  // 0110
00027             case 4: motor_out = 0x4; break;  // 0100
00028             case 5: motor_out = 0xC; break;  // 1100
00029             case 6: motor_out = 0x8; break;  // 1000
00030             case 7: motor_out = 0x9; break;  // 1001
00031             
00032             default: motor_out = 0x0; break; // 0000
00033         }
00034         wait_us(motorSpeed);
00035         
00036 }
00037 
00038 void stepmotor::set_speed(int speed){
00039     motorSpeed=speed; //set motor speed us 
00040 }
00041 uint32_t stepmotor::get_speed(){
00042     return motorSpeed; // 
00043 }
00044 
00045 void stepmotor::step(uint32_t num_steps, bool cw) {
00046 // funcion para mover el motor N pasos CW o CCW   
00047 // num_steps  número de paso que da el motor 
00048 // cw =True  para dirección en sentido del reloj 
00049 // cw =False para dirección contraria de las manecillas del reloj         
00050    
00051     uint32_t count=num_steps ;
00052     while(count){
00053         if (cw)   nstep++;     
00054         else      nstep--;
00055         if (nstep>7) nstep=0;
00056         if (nstep<0) nstep=7;
00057         move();
00058         count--;
00059 
00060     }
00061   
00062 }