Controlador de motor-de-passo

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
diogonac
Date:
Mon May 24 23:31:04 2021 +0000
Parent:
2:0da2d336090f
Commit message:
Codigo APS3;

Changed in this revision

JOG.cpp Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- 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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Mon May 24 23:31:04 2021 +0000
@@ -0,0 +1,113 @@
+#include "mbed.h"
+
+//====================== Definição os pinos ==================
+
+InterruptIn botao_D(D8);
+InterruptIn botao_EN(D9);
+DigitalIn botao_P(D10);
+DigitalOut simula_passos(D11);
+
+//============ Declaração dos timers para debouce ============
+Timer debouce_D;
+Timer debouce_EN;
+//////////////////////////////////////////////////////////////
+
+BusOut motor(PB_1, PB_15, PB_14, PB_13); //Função BusOut para o acionamento das fases do motor
+
+int i, P; //Declaro o incremento "i" e a variável para leitura do botao_P
+
+float tempo = 0.003; //Tempo padrão entre o acionamento das fases
+
+//======== Declaração das booleanas ==========================
+
+bool inverte_direcao = false;
+bool habilita_passos = false;
+
+//======== Declaração das funções ============================
+
+void rotina_horario(void);
+void rotina_anti_horario(void);
+void rotina_botao_D(void);
+void rotina_botao_EN(void);
+void rotina_botao_P(void);
+
+
+int main()
+{
+    motor = 0x00; //Estado inicial do motor
+
+    debouce_D.start(); //Inicialização do timer
+    debouce_EN.start(); //Inicialização do timer
+
+    botao_D.rise(&rotina_botao_D); //Define o método de detecção
+    botao_EN.rise(&rotina_botao_EN); //Define o método de detecção
+
+    while(1) {
+
+        P = botao_P.read(); //Leitura do botão P
+
+//=============== Lógica descrita no fluxograma ===========
+
+        if(inverte_direcao == false) {
+            rotina_horario();
+        }
+
+        else {
+            rotina_anti_horario();
+        }
+        if(P == 1) {
+            simula_passos = 1;
+            wait(tempo);
+            simula_passos = 0;
+            wait(tempo);
+        }
+//=========================================================      
+    }
+}
+
+void rotina_horario()
+{
+
+    if(habilita_passos == false) {
+        for(i = 0; i < 4; i++) {
+            motor = 1 << i;
+            wait(tempo);
+        }
+    } else {
+        motor = 0x00;
+    }
+
+}
+
+void rotina_anti_horario()
+{
+    if(habilita_passos == false) {
+        for(i = 3; i > -1; i--) {
+            motor = 1 << i;
+            wait(tempo);
+        }
+    } else {
+        motor = 0x00;
+    }
+
+}
+
+void rotina_botao_D()
+{
+    if(debouce_D.read_ms() >= 5) {
+
+        inverte_direcao =! inverte_direcao;
+        debouce_D.reset();
+    }
+}
+
+void rotina_botao_EN()
+{
+    if(debouce_EN.read_ms() >= 5) {
+
+        habilita_passos =! habilita_passos;
+        debouce_EN.reset();
+    }
+}
+
+