![](/media/cache/profiles/nikki.png.50x50_q85.jpg)
19E042PIM Stepper motor 28BYJ-48
main.cpp@0:ebdf9583ef95, 2022-01-04 (annotated)
- Committer:
- tzwell
- Date:
- Tue Jan 04 14:35:48 2022 +0000
- Revision:
- 0:ebdf9583ef95
FIrst publish, first commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tzwell | 0:ebdf9583ef95 | 1 | /* |
tzwell | 0:ebdf9583ef95 | 2 | * An example of motor control, using step 28BYJ-48 motor and NUCLEO-L476RG. |
tzwell | 0:ebdf9583ef95 | 3 | * |
tzwell | 0:ebdf9583ef95 | 4 | * Katedra za Elektroniku i digitalne sisteme |
tzwell | 0:ebdf9583ef95 | 5 | * Elektrotehnicki fakultet |
tzwell | 0:ebdf9583ef95 | 6 | * Beograd |
tzwell | 0:ebdf9583ef95 | 7 | * |
tzwell | 0:ebdf9583ef95 | 8 | * Decembar 2021. |
tzwell | 0:ebdf9583ef95 | 9 | * |
tzwell | 0:ebdf9583ef95 | 10 | */ |
tzwell | 0:ebdf9583ef95 | 11 | |
tzwell | 0:ebdf9583ef95 | 12 | /* |
tzwell | 0:ebdf9583ef95 | 13 | * Biblioteke za uvoz: |
tzwell | 0:ebdf9583ef95 | 14 | */ |
tzwell | 0:ebdf9583ef95 | 15 | #include "mbed.h" |
tzwell | 0:ebdf9583ef95 | 16 | #include "mb_pins.h" |
tzwell | 0:ebdf9583ef95 | 17 | |
tzwell | 0:ebdf9583ef95 | 18 | /* |
tzwell | 0:ebdf9583ef95 | 19 | * Definisanje makroa: |
tzwell | 0:ebdf9583ef95 | 20 | */ |
tzwell | 0:ebdf9583ef95 | 21 | #define DEBOUNCE_DELAY_MS 50 |
tzwell | 0:ebdf9583ef95 | 22 | #define MOTOR_PERIOD_MS 5 |
tzwell | 0:ebdf9583ef95 | 23 | |
tzwell | 0:ebdf9583ef95 | 24 | /* |
tzwell | 0:ebdf9583ef95 | 25 | * Globalne promenljive: |
tzwell | 0:ebdf9583ef95 | 26 | */ |
tzwell | 0:ebdf9583ef95 | 27 | InterruptIn clkwise (MB_SW1); |
tzwell | 0:ebdf9583ef95 | 28 | // D11 - D10 - D9 - D8 |
tzwell | 0:ebdf9583ef95 | 29 | // blue - pink - yellow - orange |
tzwell | 0:ebdf9583ef95 | 30 | BusOut motor_out(PA_7, PB_6, PC_7, PA_9); |
tzwell | 0:ebdf9583ef95 | 31 | int step = 0; |
tzwell | 0:ebdf9583ef95 | 32 | int dir = 0; // direction |
tzwell | 0:ebdf9583ef95 | 33 | |
tzwell | 0:ebdf9583ef95 | 34 | /* |
tzwell | 0:ebdf9583ef95 | 35 | * Deklaracija funkcija: |
tzwell | 0:ebdf9583ef95 | 36 | */ |
tzwell | 0:ebdf9583ef95 | 37 | void ISR_clkwise(void); |
tzwell | 0:ebdf9583ef95 | 38 | |
tzwell | 0:ebdf9583ef95 | 39 | |
tzwell | 0:ebdf9583ef95 | 40 | /* |
tzwell | 0:ebdf9583ef95 | 41 | * Glavna funkcija: |
tzwell | 0:ebdf9583ef95 | 42 | */ |
tzwell | 0:ebdf9583ef95 | 43 | int main() |
tzwell | 0:ebdf9583ef95 | 44 | { |
tzwell | 0:ebdf9583ef95 | 45 | clkwise.fall(&ISR_clkwise); |
tzwell | 0:ebdf9583ef95 | 46 | while(1) |
tzwell | 0:ebdf9583ef95 | 47 | { |
tzwell | 0:ebdf9583ef95 | 48 | switch(step) |
tzwell | 0:ebdf9583ef95 | 49 | { |
tzwell | 0:ebdf9583ef95 | 50 | // Half-step control: |
tzwell | 0:ebdf9583ef95 | 51 | case 0: motor_out = 0x1; break; // 0001 |
tzwell | 0:ebdf9583ef95 | 52 | case 1: motor_out = 0x3; break; // 0011 |
tzwell | 0:ebdf9583ef95 | 53 | case 2: motor_out = 0x2; break; // 0010 |
tzwell | 0:ebdf9583ef95 | 54 | case 3: motor_out = 0x6; break; // 0110 |
tzwell | 0:ebdf9583ef95 | 55 | case 4: motor_out = 0x4; break; // 0100 |
tzwell | 0:ebdf9583ef95 | 56 | case 5: motor_out = 0xC; break; // 1100 |
tzwell | 0:ebdf9583ef95 | 57 | case 6: motor_out = 0x8; break; // 1000 |
tzwell | 0:ebdf9583ef95 | 58 | case 7: motor_out = 0x9; break; // 1001 |
tzwell | 0:ebdf9583ef95 | 59 | |
tzwell | 0:ebdf9583ef95 | 60 | default: motor_out = 0x0; break; // 0000 |
tzwell | 0:ebdf9583ef95 | 61 | } |
tzwell | 0:ebdf9583ef95 | 62 | |
tzwell | 0:ebdf9583ef95 | 63 | if(dir) step++; else step--; |
tzwell | 0:ebdf9583ef95 | 64 | if(step>7)step=0; |
tzwell | 0:ebdf9583ef95 | 65 | if(step<0)step=7; |
tzwell | 0:ebdf9583ef95 | 66 | wait_ms(MOTOR_PERIOD_MS); // speed |
tzwell | 0:ebdf9583ef95 | 67 | } |
tzwell | 0:ebdf9583ef95 | 68 | } |
tzwell | 0:ebdf9583ef95 | 69 | /* |
tzwell | 0:ebdf9583ef95 | 70 | * Definicija funkcija: |
tzwell | 0:ebdf9583ef95 | 71 | */ |
tzwell | 0:ebdf9583ef95 | 72 | void ISR_clkwise(void){ |
tzwell | 0:ebdf9583ef95 | 73 | if(!clkwise.read()) |
tzwell | 0:ebdf9583ef95 | 74 | { |
tzwell | 0:ebdf9583ef95 | 75 | wait_ms(DEBOUNCE_DELAY_MS); |
tzwell | 0:ebdf9583ef95 | 76 | if (!clkwise.read()) dir = !dir; |
tzwell | 0:ebdf9583ef95 | 77 | } |
tzwell | 0:ebdf9583ef95 | 78 | } |