![](/media/cache/profiles/3d72ce8096e7a0b5121204fc73cd7379.jpg.50x50_q85.jpg)
Controlador de motor-de-passo
JOG.cpp@1:fbb2f440794e, 2021-04-25 (annotated)
- Committer:
- diogonac
- Date:
- Sun Apr 25 12:08:52 2021 +0000
- Revision:
- 1:fbb2f440794e
- Parent:
- main.cpp@0:3674c9e06d98
- Child:
- 2:0da2d336090f
Nome alterado
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
diogonac | 0:3674c9e06d98 | 1 | #include "mbed.h" |
diogonac | 0:3674c9e06d98 | 2 | |
diogonac | 0:3674c9e06d98 | 3 | Serial pc (USBTX, USBRX); |
diogonac | 0:3674c9e06d98 | 4 | |
diogonac | 0:3674c9e06d98 | 5 | AnalogIn eixo_X(A0); |
diogonac | 0:3674c9e06d98 | 6 | AnalogIn eixo_Y(A1); |
diogonac | 0:3674c9e06d98 | 7 | InterruptIn botao_in(PA_8); |
diogonac | 0:3674c9e06d98 | 8 | |
diogonac | 0:3674c9e06d98 | 9 | BusOut motor_x(PA_9, PC_7, PB_6, PA_7); |
diogonac | 0:3674c9e06d98 | 10 | BusOut motor_y(PA_11, PB_12, PB_11, PB_2); |
diogonac | 0:3674c9e06d98 | 11 | |
diogonac | 0:3674c9e06d98 | 12 | double frequencia; //Hz |
diogonac | 0:3674c9e06d98 | 13 | double velocidade = 0.2; //RPM |
diogonac | 0:3674c9e06d98 | 14 | double tempo; //Segundos |
diogonac | 0:3674c9e06d98 | 15 | int i; |
diogonac | 0:3674c9e06d98 | 16 | int X; |
diogonac | 0:3674c9e06d98 | 17 | int Y; |
diogonac | 0:3674c9e06d98 | 18 | int botao_estado; |
diogonac | 0:3674c9e06d98 | 19 | bool rotacao; |
diogonac | 0:3674c9e06d98 | 20 | |
diogonac | 0:3674c9e06d98 | 21 | |
diogonac | 0:3674c9e06d98 | 22 | void SW() |
diogonac | 0:3674c9e06d98 | 23 | { |
diogonac | 0:3674c9e06d98 | 24 | if(botao_estado == 0) { |
diogonac | 0:3674c9e06d98 | 25 | |
diogonac | 0:3674c9e06d98 | 26 | rotacao = !rotacao; |
diogonac | 0:3674c9e06d98 | 27 | } |
diogonac | 0:3674c9e06d98 | 28 | } |
diogonac | 0:3674c9e06d98 | 29 | |
diogonac | 0:3674c9e06d98 | 30 | |
diogonac | 0:3674c9e06d98 | 31 | int main() |
diogonac | 0:3674c9e06d98 | 32 | { |
diogonac | 0:3674c9e06d98 | 33 | motor_x = 0x00; |
diogonac | 0:3674c9e06d98 | 34 | motor_y = 0x00; |
diogonac | 0:3674c9e06d98 | 35 | pc.baud(115200); |
diogonac | 0:3674c9e06d98 | 36 | rotacao = 0; |
diogonac | 0:3674c9e06d98 | 37 | botao_in.rise(&SW); |
diogonac | 0:3674c9e06d98 | 38 | frequencia = (2048*velocidade)/60; |
diogonac | 0:3674c9e06d98 | 39 | tempo = (1/frequencia); |
diogonac | 0:3674c9e06d98 | 40 | |
diogonac | 0:3674c9e06d98 | 41 | while(1) { |
diogonac | 0:3674c9e06d98 | 42 | |
diogonac | 0:3674c9e06d98 | 43 | X = eixo_X.read_u16(); |
diogonac | 0:3674c9e06d98 | 44 | Y = eixo_Y.read_u16(); |
diogonac | 0:3674c9e06d98 | 45 | botao_estado = botao_in.read(); |
diogonac | 0:3674c9e06d98 | 46 | |
diogonac | 0:3674c9e06d98 | 47 | pc.printf("X=%4d, Y=%4d, botao_estado=%d, rotacao_estado=%d \r\n", X, Y, botao_estado, rotacao); |
diogonac | 0:3674c9e06d98 | 48 | |
diogonac | 0:3674c9e06d98 | 49 | if(31000 <= X & X <= 35000) { |
diogonac | 0:3674c9e06d98 | 50 | |
diogonac | 0:3674c9e06d98 | 51 | motor_x = 0x00; |
diogonac | 0:3674c9e06d98 | 52 | } else { |
diogonac | 0:3674c9e06d98 | 53 | if (rotacao == 0) { |
diogonac | 0:3674c9e06d98 | 54 | for(i = 0; i < 4; i++) { |
diogonac | 0:3674c9e06d98 | 55 | |
diogonac | 0:3674c9e06d98 | 56 | motor_x = 1 <<= i; |
diogonac | 0:3674c9e06d98 | 57 | //motor_x = 1 << i+2; |
diogonac | 0:3674c9e06d98 | 58 | wait(tempo); |
diogonac | 0:3674c9e06d98 | 59 | } |
diogonac | 0:3674c9e06d98 | 60 | } |
diogonac | 0:3674c9e06d98 | 61 | |
diogonac | 0:3674c9e06d98 | 62 | if(rotacao == 1) { |
diogonac | 0:3674c9e06d98 | 63 | for(i = 3; i > -1; i--) { |
diogonac | 0:3674c9e06d98 | 64 | motor_x = 1 << i; |
diogonac | 0:3674c9e06d98 | 65 | wait(tempo); |
diogonac | 0:3674c9e06d98 | 66 | } |
diogonac | 0:3674c9e06d98 | 67 | } |
diogonac | 0:3674c9e06d98 | 68 | } |
diogonac | 0:3674c9e06d98 | 69 | |
diogonac | 0:3674c9e06d98 | 70 | // if(31000 <= Y & Y <= 35000) { |
diogonac | 0:3674c9e06d98 | 71 | // |
diogonac | 0:3674c9e06d98 | 72 | // motor_y = 0x00; |
diogonac | 0:3674c9e06d98 | 73 | // } |
diogonac | 0:3674c9e06d98 | 74 | // |
diogonac | 0:3674c9e06d98 | 75 | // else { |
diogonac | 0:3674c9e06d98 | 76 | // if (rotacao == 0) { |
diogonac | 0:3674c9e06d98 | 77 | // for(i = 0; i < 4; i++) { |
diogonac | 0:3674c9e06d98 | 78 | // motor_y = 1 << i; |
diogonac | 0:3674c9e06d98 | 79 | // wait(tempo); |
diogonac | 0:3674c9e06d98 | 80 | // } |
diogonac | 0:3674c9e06d98 | 81 | // } |
diogonac | 0:3674c9e06d98 | 82 | // |
diogonac | 0:3674c9e06d98 | 83 | // if(rotacao == 1) { |
diogonac | 0:3674c9e06d98 | 84 | // for(i = 3; i > -1; i--) { |
diogonac | 0:3674c9e06d98 | 85 | // motor_y = 1 << i; |
diogonac | 0:3674c9e06d98 | 86 | // wait(tempo); |
diogonac | 0:3674c9e06d98 | 87 | // } |
diogonac | 0:3674c9e06d98 | 88 | // } |
diogonac | 0:3674c9e06d98 | 89 | // } |
diogonac | 0:3674c9e06d98 | 90 | } |
diogonac | 0:3674c9e06d98 | 91 | } |