Program for controlling stepper motors with threads.
Fork of L298N_Stepper_4wire_example by
main.cpp@1:a3287196a9b3, 2016-04-07 (annotated)
- Committer:
- mjhaugsdal
- Date:
- Thu Apr 07 10:51:36 2016 +0000
- Revision:
- 1:a3287196a9b3
- Parent:
- 0:bd78e433db61
Program for controling stepper motor
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rlanghbv | 0:bd78e433db61 | 1 | #include "mbed.h" |
mjhaugsdal | 1:a3287196a9b3 | 2 | #include "rtos.h" |
rlanghbv | 0:bd78e433db61 | 3 | |
rlanghbv | 0:bd78e433db61 | 4 | |
mjhaugsdal | 1:a3287196a9b3 | 5 | //Analog Pins |
rlanghbv | 0:bd78e433db61 | 6 | DigitalOut IN1(D3); |
rlanghbv | 0:bd78e433db61 | 7 | DigitalOut IN2(D4); |
rlanghbv | 0:bd78e433db61 | 8 | DigitalOut IN3(D5); |
rlanghbv | 0:bd78e433db61 | 9 | DigitalOut IN4(D6); |
rlanghbv | 0:bd78e433db61 | 10 | |
mjhaugsdal | 1:a3287196a9b3 | 11 | Mutex stdio_mutex; |
mjhaugsdal | 1:a3287196a9b3 | 12 | static int fart = 1000; |
mjhaugsdal | 1:a3287196a9b3 | 13 | static char global_direction; |
mjhaugsdal | 1:a3287196a9b3 | 14 | static int global_counter; |
rlanghbv | 0:bd78e433db61 | 15 | |
rlanghbv | 0:bd78e433db61 | 16 | |
rlanghbv | 0:bd78e433db61 | 17 | void step4Right() |
rlanghbv | 0:bd78e433db61 | 18 | { |
rlanghbv | 0:bd78e433db61 | 19 | IN1=0; |
rlanghbv | 0:bd78e433db61 | 20 | IN2=1; |
rlanghbv | 0:bd78e433db61 | 21 | IN3=0; |
rlanghbv | 0:bd78e433db61 | 22 | IN4=1; |
mjhaugsdal | 1:a3287196a9b3 | 23 | wait_us(fart); //legg som global variabel "fart" |
rlanghbv | 0:bd78e433db61 | 24 | IN1=0; |
rlanghbv | 0:bd78e433db61 | 25 | IN2=1; |
rlanghbv | 0:bd78e433db61 | 26 | IN3=1; |
rlanghbv | 0:bd78e433db61 | 27 | IN4=0; |
mjhaugsdal | 1:a3287196a9b3 | 28 | wait_us(fart); |
rlanghbv | 0:bd78e433db61 | 29 | IN1=1; |
rlanghbv | 0:bd78e433db61 | 30 | IN2=0; |
rlanghbv | 0:bd78e433db61 | 31 | IN3=1; |
rlanghbv | 0:bd78e433db61 | 32 | IN4=0; |
mjhaugsdal | 1:a3287196a9b3 | 33 | wait_us(fart); |
rlanghbv | 0:bd78e433db61 | 34 | IN1=1; |
rlanghbv | 0:bd78e433db61 | 35 | IN2=0; |
rlanghbv | 0:bd78e433db61 | 36 | IN3=0; |
rlanghbv | 0:bd78e433db61 | 37 | IN4=1; |
mjhaugsdal | 1:a3287196a9b3 | 38 | wait_us(fart); |
rlanghbv | 0:bd78e433db61 | 39 | } |
rlanghbv | 0:bd78e433db61 | 40 | void step4Left() |
rlanghbv | 0:bd78e433db61 | 41 | { |
rlanghbv | 0:bd78e433db61 | 42 | IN1=1; |
rlanghbv | 0:bd78e433db61 | 43 | IN2=0; |
rlanghbv | 0:bd78e433db61 | 44 | IN3=0; |
rlanghbv | 0:bd78e433db61 | 45 | IN4=1; |
mjhaugsdal | 1:a3287196a9b3 | 46 | wait_us(fart); |
rlanghbv | 0:bd78e433db61 | 47 | |
rlanghbv | 0:bd78e433db61 | 48 | IN1=1; |
rlanghbv | 0:bd78e433db61 | 49 | IN2=0; |
rlanghbv | 0:bd78e433db61 | 50 | IN3=1; |
rlanghbv | 0:bd78e433db61 | 51 | IN4=0; |
mjhaugsdal | 1:a3287196a9b3 | 52 | wait_us(fart); |
rlanghbv | 0:bd78e433db61 | 53 | |
rlanghbv | 0:bd78e433db61 | 54 | IN1=0; |
rlanghbv | 0:bd78e433db61 | 55 | IN2=1; |
rlanghbv | 0:bd78e433db61 | 56 | IN3=1; |
rlanghbv | 0:bd78e433db61 | 57 | IN4=0; |
mjhaugsdal | 1:a3287196a9b3 | 58 | wait_us(fart); |
rlanghbv | 0:bd78e433db61 | 59 | |
rlanghbv | 0:bd78e433db61 | 60 | IN1=0; |
rlanghbv | 0:bd78e433db61 | 61 | IN2=1; |
rlanghbv | 0:bd78e433db61 | 62 | IN3=0; |
rlanghbv | 0:bd78e433db61 | 63 | IN4=1; |
mjhaugsdal | 1:a3287196a9b3 | 64 | wait_us(fart); |
rlanghbv | 0:bd78e433db61 | 65 | } |
rlanghbv | 0:bd78e433db61 | 66 | |
mjhaugsdal | 1:a3287196a9b3 | 67 | |
mjhaugsdal | 1:a3287196a9b3 | 68 | void input(void const *args) |
mjhaugsdal | 1:a3287196a9b3 | 69 | { |
mjhaugsdal | 1:a3287196a9b3 | 70 | while(true) |
mjhaugsdal | 1:a3287196a9b3 | 71 | { |
mjhaugsdal | 1:a3287196a9b3 | 72 | scanf(" %c", &global_direction); |
mjhaugsdal | 1:a3287196a9b3 | 73 | } |
mjhaugsdal | 1:a3287196a9b3 | 74 | } |
rlanghbv | 0:bd78e433db61 | 75 | |
rlanghbv | 0:bd78e433db61 | 76 | int main() |
rlanghbv | 0:bd78e433db61 | 77 | { |
mjhaugsdal | 1:a3287196a9b3 | 78 | //Thread 1 has constant feed from usb |
mjhaugsdal | 1:a3287196a9b3 | 79 | //Thread t1(input); |
mjhaugsdal | 1:a3287196a9b3 | 80 | while (true) |
mjhaugsdal | 1:a3287196a9b3 | 81 | { |
rlanghbv | 0:bd78e433db61 | 82 | step4Left(); |
rlanghbv | 0:bd78e433db61 | 83 | |
mjhaugsdal | 1:a3287196a9b3 | 84 | /* |
mjhaugsdal | 1:a3287196a9b3 | 85 | if (global_direction == '0') |
mjhaugsdal | 1:a3287196a9b3 | 86 | { |
mjhaugsdal | 1:a3287196a9b3 | 87 | step4Left(); |
mjhaugsdal | 1:a3287196a9b3 | 88 | printf("%c \n" + global_direction); |
mjhaugsdal | 1:a3287196a9b3 | 89 | } */ |
mjhaugsdal | 1:a3287196a9b3 | 90 | /*else if (global_direction == '1') |
mjhaugsdal | 1:a3287196a9b3 | 91 | { |
mjhaugsdal | 1:a3287196a9b3 | 92 | step4Right(); |
mjhaugsdal | 1:a3287196a9b3 | 93 | //printf("RIGHT \n"); |
mjhaugsdal | 1:a3287196a9b3 | 94 | } */ |
mjhaugsdal | 1:a3287196a9b3 | 95 | /* else |
mjhaugsdal | 1:a3287196a9b3 | 96 | { |
mjhaugsdal | 1:a3287196a9b3 | 97 | //Do nothing |
mjhaugsdal | 1:a3287196a9b3 | 98 | |
mjhaugsdal | 1:a3287196a9b3 | 99 | }*/ |
mjhaugsdal | 1:a3287196a9b3 | 100 | } |
mjhaugsdal | 1:a3287196a9b3 | 101 | |
mjhaugsdal | 1:a3287196a9b3 | 102 | } //END Main |