Diogo Cintra
/
APS3
Controlador de motor-de-passo
Diff: JOG.cpp
- Revision:
- 3:1f7f109c9042
- Parent:
- 2:0da2d336090f
diff -r 0da2d336090f -r 1f7f109c9042 JOG.cpp --- a/JOG.cpp Sun Apr 25 12:13:32 2021 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,91 +0,0 @@ -#include "mbed.h" - -Serial pc (USBTX, USBRX); - -AnalogIn eixo_X(A0); -AnalogIn eixo_Y(A1); -InterruptIn botao_in(PA_8); - -BusOut motor_x(PA_9, PC_7, PB_6, PA_7); -BusOut motor_y(PA_11, PB_12, PB_11, PB_2); - -double frequencia; //Hz -double velocidade = 0.2; //RPM -double tempo; //Segundos -int i; -int X; -int Y; -int botao_estado; -bool rotacao; - - -void SW() -{ - if(botao_estado == 0) { - - rotacao = !rotacao; - } -} - - -int main() -{ - motor_x = 0x00; - motor_y = 0x00; - pc.baud(115200); - rotacao = 0; - botao_in.rise(&SW); - frequencia = (2048*velocidade)/60; - tempo = (1/frequencia); - - while(1) { - - X = eixo_X.read_u16(); - Y = eixo_Y.read_u16(); - botao_estado = botao_in.read(); - - pc.printf("X=%4d, Y=%4d, botao_estado=%d, rotacao_estado=%d \r\n", X, Y, botao_estado, rotacao); - - if(31000 <= X & X <= 35000) { - - motor_x = 0x00; - } else { - if (rotacao == 0) { - for(i = 0; i < 4; i++) { - - motor_x = 1 <<= i; - //motor_x = 1 << i+2; - wait(tempo); - } - } - - if(rotacao == 1) { - for(i = 3; i > -1; i--) { - motor_x = 1 << i; - wait(tempo); - } - } - } - - if(31000 <= Y & Y <= 35000) { - - motor_y = 0x00; - } - - else { - if (rotacao == 0) { - for(i = 0; i < 4; i++) { - motor_y = 1 << i; - wait(tempo); - } - } - - if(rotacao == 1) { - for(i = 3; i > -1; i--) { - motor_y = 1 << i; - wait(tempo); - } - } - } - } -} \ No newline at end of file