To control motors of Arduino motor shield L293D v1

Files at this revision

API Documentation at this revision

Comitter:
vtqNhi
Date:
Thu Sep 21 15:57:56 2017 +0000
Commit message:
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.

Changed in this revision

DCMotorControl.cpp Show annotated file Show diff for this revision Revisions of this file
DCMotorControl.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/DCMotorControl.cpp	Thu Sep 21 15:57:56 2017 +0000
@@ -0,0 +1,59 @@
+#include "mbed.h"
+#include "DCMotorControl.h"
+
+//To begin using this library, define these pin in main.cpp
+//pwm1 = pwm pin of motor 1 and so on
+//data, clock, latch, enable pin
+Motor::Motor(PinName pwm1, PinName pwm2, PinName pwm3, PinName pwm4, PinName data_pin, PinName clock_pin, PinName latch_pin, PinName enable_pin) : _pwm1(pwm1), _pwm2(pwm2), _pwm3(pwm3), _pwm4(pwm4), _SERIALDATA(data_pin), _CLOCK(clock_pin), _LATCH(latch_pin), _ENABLE(enable_pin)
+{
+    _ENABLE.write(0);//set low to enable output - active low
+    _LATCH.write(1);//set high to send if any data is storage
+    _LATCH.write(0);//set back to low
+}
+
+//define turning direction of 4 motors - if set M1a value to be 1 and M1b value to be 0, will make terminant M1A on the shield have higher volatage than terminant M1B
+void Motor::Direction(int M1a, int M1b, int M2a, int M2b, int M3a, int M3b, int M4a, int M4b)
+{
+    //define direction of each motor
+    //Note: due to unknow reason, the order of these M motor pins may differrent from schematic info but it works for my case
+    int M_ab[8];
+    M_ab[0] = M3a;
+    M_ab[1] = M4a;
+    M_ab[2] = M3b;
+    M_ab[3] = M2a;
+    M_ab[4] = M1a;
+    M_ab[5] = M1b;
+    M_ab[6] = M2b;
+    M_ab[7] = M4b;
+    
+    //operation: shift register to make output as desire direction
+    for (int i = 0; i < 8; i++)
+    {
+        if (M_ab[i] == 1)
+        {
+            _SERIALDATA.write(1);
+        }
+        else 
+        {
+            _SERIALDATA.write(0);
+        }
+        _CLOCK.write(1);
+        _CLOCK.write(0);
+    }
+    //send and run result
+    _LATCH.write(1);
+    _LATCH.write(0);    
+}
+
+//define speed of 4 motors in percentage in main.cpp
+void Motor::setSpeed(float percentage_M1, float percentage_M2, float percentage_M3, float percentage_M4)
+{
+    float speedM1 = percentage_M1/100;
+    float speedM2 = percentage_M2/100;
+    float speedM3 = percentage_M3/100;
+    float speedM4 = percentage_M4/100;
+    _pwm3 = speedM3;
+    _pwm4 = speedM4;
+    _pwm1 = speedM1;
+    _pwm2 = speedM2;
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/DCMotorControl.h	Thu Sep 21 15:57:56 2017 +0000
@@ -0,0 +1,58 @@
+/*
+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)
+The code specificly control the 2 pwm pins of 2 L293D ICs and serial, clock, latch, enable pins of 74HC595 (shift register)
+
+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.
+==This code is written by Vo Trieu Quang Nhi on 21-9-17== 
+
+
+//~~example main.cpp~~
+#include "mbed.h"
+#include "DCMotorControl.h"
+
+//The board is FRDM-KL25Z
+//Using the user guide of this board and identify the pin control pwm1 pwm2 pwm3 pwm4 data clock latch enable (of motor shiled)
+//Here it is PTD2, PTA12, PTC8, PTA5, PTA13, PTA4, PTD3, PTC9
+Motor myMotor(PTD2, PTA12, PTC8, PTA5, PTA13, PTA4, PTD3, PTC9);
+
+//The code below is a test to turn motor from terminant B to terminant A
+//begin speed is 10% then 50% and 100% with 3 seconds delay with the previous speed 
+int main(void)
+{
+    myMotor.Direction(0,1,0,1,0,1,0,1);
+    int i = 10;
+    myMotor.setSpeed(i,i,i,i);
+    wait(3);
+    i=50;
+    myMotor.setSpeed(i,i,i,i);
+    wait(3);
+    i=100;
+    myMotor.setSpeed(i,i,i,i);
+}
+//~~end example~~
+*/
+
+#ifndef MBED_MOTOR_H
+#define MBED_MOTOR_H
+
+#include "mbed.h"
+
+class Motor
+{
+public:
+    Motor(PinName pwm1, PinName pwm2, PinName pwm3, PinName pwm4, PinName data_pin, PinName clock_pin, PinName latch_pin, PinName enable_pin);
+    void Direction(int M1a, int M1b, int M2a, int M2b, int M3a, int M3b, int M4a, int M4b);
+    void setSpeed(float percentage_M1, float percentage_M2, float percentage_M3, float percentage_M4);
+private:
+    PwmOut _pwm1;//pwm1
+    PwmOut _pwm2;//pwm2
+    PwmOut _pwm3;//pwm3
+    PwmOut _pwm4;//pwm4
+    
+    DigitalOut _SERIALDATA;//serial data
+    DigitalOut _CLOCK;//clock
+    DigitalOut _LATCH;//latch
+    DigitalOut _ENABLE;//enable, set low to be in duty
+};
+
+#endif
\ No newline at end of file