To control motors of Arduino motor shield L293D v1
DCMotorControl.h@0:90137e94bed0, 2017-09-21 (annotated)
- Committer:
- vtqNhi
- Date:
- Thu Sep 21 15:57:56 2017 +0000
- Revision:
- 0:90137e94bed0
The library is written to control the motors of arduino motor shield v1, which include of 2 L293D motor ICs and 1 74HC595 shift register ICs.; The library original purpose is for a project which uses FRDM-KL25X to control the attached motor shield.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
vtqNhi | 0:90137e94bed0 | 1 | /* |
vtqNhi | 0:90137e94bed0 | 2 | This library is written to control 4 motors of the arduino motor shield L293D V1 (https://i0.wp.com/sribasu.com/wp-content/uploads/2015/11/adafruit-motor-shield-v1-connect-dc-and-servo-motors.jpg) |
vtqNhi | 0:90137e94bed0 | 3 | The code specificly control the 2 pwm pins of 2 L293D ICs and serial, clock, latch, enable pins of 74HC595 (shift register) |
vtqNhi | 0:90137e94bed0 | 4 | |
vtqNhi | 0:90137e94bed0 | 5 | The library is purposely examined on board FRDM-KL25Z. However, an appropriate defining the pins in main.cpp will make it suitable for other boards too. |
vtqNhi | 0:90137e94bed0 | 6 | ==This code is written by Vo Trieu Quang Nhi on 21-9-17== |
vtqNhi | 0:90137e94bed0 | 7 | |
vtqNhi | 0:90137e94bed0 | 8 | |
vtqNhi | 0:90137e94bed0 | 9 | //~~example main.cpp~~ |
vtqNhi | 0:90137e94bed0 | 10 | #include "mbed.h" |
vtqNhi | 0:90137e94bed0 | 11 | #include "DCMotorControl.h" |
vtqNhi | 0:90137e94bed0 | 12 | |
vtqNhi | 0:90137e94bed0 | 13 | //The board is FRDM-KL25Z |
vtqNhi | 0:90137e94bed0 | 14 | //Using the user guide of this board and identify the pin control pwm1 pwm2 pwm3 pwm4 data clock latch enable (of motor shiled) |
vtqNhi | 0:90137e94bed0 | 15 | //Here it is PTD2, PTA12, PTC8, PTA5, PTA13, PTA4, PTD3, PTC9 |
vtqNhi | 0:90137e94bed0 | 16 | Motor myMotor(PTD2, PTA12, PTC8, PTA5, PTA13, PTA4, PTD3, PTC9); |
vtqNhi | 0:90137e94bed0 | 17 | |
vtqNhi | 0:90137e94bed0 | 18 | //The code below is a test to turn motor from terminant B to terminant A |
vtqNhi | 0:90137e94bed0 | 19 | //begin speed is 10% then 50% and 100% with 3 seconds delay with the previous speed |
vtqNhi | 0:90137e94bed0 | 20 | int main(void) |
vtqNhi | 0:90137e94bed0 | 21 | { |
vtqNhi | 0:90137e94bed0 | 22 | myMotor.Direction(0,1,0,1,0,1,0,1); |
vtqNhi | 0:90137e94bed0 | 23 | int i = 10; |
vtqNhi | 0:90137e94bed0 | 24 | myMotor.setSpeed(i,i,i,i); |
vtqNhi | 0:90137e94bed0 | 25 | wait(3); |
vtqNhi | 0:90137e94bed0 | 26 | i=50; |
vtqNhi | 0:90137e94bed0 | 27 | myMotor.setSpeed(i,i,i,i); |
vtqNhi | 0:90137e94bed0 | 28 | wait(3); |
vtqNhi | 0:90137e94bed0 | 29 | i=100; |
vtqNhi | 0:90137e94bed0 | 30 | myMotor.setSpeed(i,i,i,i); |
vtqNhi | 0:90137e94bed0 | 31 | } |
vtqNhi | 0:90137e94bed0 | 32 | //~~end example~~ |
vtqNhi | 0:90137e94bed0 | 33 | */ |
vtqNhi | 0:90137e94bed0 | 34 | |
vtqNhi | 0:90137e94bed0 | 35 | #ifndef MBED_MOTOR_H |
vtqNhi | 0:90137e94bed0 | 36 | #define MBED_MOTOR_H |
vtqNhi | 0:90137e94bed0 | 37 | |
vtqNhi | 0:90137e94bed0 | 38 | #include "mbed.h" |
vtqNhi | 0:90137e94bed0 | 39 | |
vtqNhi | 0:90137e94bed0 | 40 | class Motor |
vtqNhi | 0:90137e94bed0 | 41 | { |
vtqNhi | 0:90137e94bed0 | 42 | public: |
vtqNhi | 0:90137e94bed0 | 43 | Motor(PinName pwm1, PinName pwm2, PinName pwm3, PinName pwm4, PinName data_pin, PinName clock_pin, PinName latch_pin, PinName enable_pin); |
vtqNhi | 0:90137e94bed0 | 44 | void Direction(int M1a, int M1b, int M2a, int M2b, int M3a, int M3b, int M4a, int M4b); |
vtqNhi | 0:90137e94bed0 | 45 | void setSpeed(float percentage_M1, float percentage_M2, float percentage_M3, float percentage_M4); |
vtqNhi | 0:90137e94bed0 | 46 | private: |
vtqNhi | 0:90137e94bed0 | 47 | PwmOut _pwm1;//pwm1 |
vtqNhi | 0:90137e94bed0 | 48 | PwmOut _pwm2;//pwm2 |
vtqNhi | 0:90137e94bed0 | 49 | PwmOut _pwm3;//pwm3 |
vtqNhi | 0:90137e94bed0 | 50 | PwmOut _pwm4;//pwm4 |
vtqNhi | 0:90137e94bed0 | 51 | |
vtqNhi | 0:90137e94bed0 | 52 | DigitalOut _SERIALDATA;//serial data |
vtqNhi | 0:90137e94bed0 | 53 | DigitalOut _CLOCK;//clock |
vtqNhi | 0:90137e94bed0 | 54 | DigitalOut _LATCH;//latch |
vtqNhi | 0:90137e94bed0 | 55 | DigitalOut _ENABLE;//enable, set low to be in duty |
vtqNhi | 0:90137e94bed0 | 56 | }; |
vtqNhi | 0:90137e94bed0 | 57 | |
vtqNhi | 0:90137e94bed0 | 58 | #endif |