mbed device driver for Toshiba TB6612FNG motor driver
Dependents: ROBot TB6612FNG_Sample TB6612FNG_Sample
Revision 0:1a07771ff613, committed 2013-11-24
- Comitter:
- rabad1
- Date:
- Sun Nov 24 21:47:54 2013 +0000
- Child:
- 1:8f562bdd5e93
- Commit message:
- initial release
Changed in this revision
| TB6612FNG.cpp | Show annotated file Show diff for this revision Revisions of this file |
| TB6612FNG.h | Show annotated file Show diff for this revision Revisions of this file |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/TB6612FNG.cpp Sun Nov 24 21:47:54 2013 +0000
@@ -0,0 +1,100 @@
+/* File: TB6612FNG.h
+ * Author: Robert Abad Copyright (c) 2013
+ *
+ * Desc: driver for TB6612FNG Motor Driver. For further details see
+ * header file, TB6612FNG.h
+ */
+
+#include "mbed.h"
+#include "TB6612FNG.h"
+
+#define SIGNAL_HIGH (1)
+#define SIGNAL_LOW (0)
+
+TB6612FNG::TB6612FNG( PinName pinPwmA, PinName pinAin1, PinName pinAin2,
+ PinName pinPwmB, PinName pinBin1, PinName pinBin2,
+ PinName pinNStby ) :
+pwmA(pinPwmA),
+Ain1(pinAin1),
+Ain2(pinAin2),
+pwmB(pinPwmB),
+Bin1(pinBin1),
+Bin2(pinBin2),
+nStby(pinNStby)
+{
+ Ain1 = SIGNAL_LOW;
+ Ain2 = SIGNAL_LOW;
+ Bin1 = SIGNAL_LOW;
+ Bin2 = SIGNAL_LOW;
+ pwmA.period(TB6612FNG_PWM_PERIOD_DEFAULT);
+ pwmA = TB6612FNG_PWM_PULSEWIDTH_DEFAULT;
+ pwmB.period(TB6612FNG_PWM_PERIOD_DEFAULT);
+ pwmB = TB6612FNG_PWM_PULSEWIDTH_DEFAULT;
+ nStby = SIGNAL_LOW;
+}
+
+void TB6612FNG::setPwmAperiod(float fPeriod)
+{
+ pwmA.period(fPeriod);
+}
+
+void TB6612FNG::setPwmApulsewidth(float fPulsewidth)
+{
+ pwmA = fPulsewidth;
+}
+
+void TB6612FNG::setPwmBperiod(float fPeriod)
+{
+ pwmB.period(fPeriod);
+}
+
+void TB6612FNG::setPwmBpulsewidth(float fPulsewidth)
+{
+ pwmB = fPulsewidth;
+}
+
+void TB6612FNG::standby(void)
+{
+ nStby = SIGNAL_LOW;
+}
+
+void TB6612FNG::motorA_stop(void)
+{
+ Ain1 = SIGNAL_LOW;
+ Ain2 = SIGNAL_LOW;
+}
+
+void TB6612FNG::motorA_ccw(void)
+{
+ Ain1 = SIGNAL_LOW;
+ Ain2 = SIGNAL_HIGH;
+ nStby = SIGNAL_HIGH;
+}
+
+void TB6612FNG::motorA_cw(void)
+{
+ Ain1 = SIGNAL_HIGH;
+ Ain2 = SIGNAL_LOW;
+ nStby = SIGNAL_HIGH;
+}
+
+void TB6612FNG::motorB_stop(void)
+{
+ Bin1 = SIGNAL_LOW;
+ Bin2 = SIGNAL_LOW;
+}
+
+void TB6612FNG::motorB_ccw(void)
+{
+ Bin1 = SIGNAL_LOW;
+ Bin2 = SIGNAL_HIGH;
+ nStby = SIGNAL_HIGH;
+}
+
+void TB6612FNG::motorB_cw(void)
+{
+ Bin1 = SIGNAL_HIGH;
+ Bin2 = SIGNAL_LOW;
+ nStby = SIGNAL_HIGH;
+}
+
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/TB6612FNG.h Sun Nov 24 21:47:54 2013 +0000
@@ -0,0 +1,92 @@
+/* File: TB6612FNG.h
+ * Author: Robert Abad Copyright (c) 2013
+ *
+ * Desc: driver for Toshiiba TB6612FNG Motor Driver. Though this motor driver
+ * can be used to drive two motors (A and B), it can be used to drive
+ * just one. Member functions can be used to set period and pulsewidth
+ * but are not necessary as the constructor does set default values).
+ * The datasheet for this device can be found here:
+ * http://www.toshiba.com/taec/components2/Datasheet_Sync/261/27197.pdf
+ *
+ * Below is some sample code:
+ *
+ * #include "mbed.h"
+ * #include "TB6612FNG.h"
+ *
+ * #define TB6612FNG_PIN_PWMA (p22)
+ * #define TB6612FNG_PIN_AIN1 (p17)
+ * #define TB6612FNG_PIN_AIN2 (p16)
+ * #define TB6612FNG_PIN_PWMB (p21)
+ * #define TB6612FNG_PIN_BIN1 (p19)
+ * #define TB6612FNG_PIN_BIN2 (p20)
+ * #define TB6612FNG_PIN_NSTBY (p18)
+ * TB6612FNG motorDriver( TB6612FNG_PIN_PWMA, TB6612FNG_PIN_AIN1, TB6612FNG_PIN_AIN2,
+ * TB6612FNG_PIN_PWMB, TB6612FNG_PIN_BIN1, TB6612FNG_PIN_BIN2,
+ * TB6612FNG_PIN_NSTBY );
+ * float fPwmPeriod;
+ * float fPwmPulsewidth;
+ *
+ * int main()
+ * {
+ * fPwmPeriod = 0.00002f; // 50KHz
+ * fPwmPulsewidth = 0.50; // 50% duty cycle
+ * motorDriver.setPwmAperiod(fPwmPeriod);
+ * motorDriver.setPwmBperiod(fPwmPeriod);
+ * motorDriver.setPwmApulsewidth(fPwmPulsewidth);
+ * motorDriver.setPwmBpulsewidth(fPwmPulsewidth);
+ *
+ * while(1)
+ * {
+ * motorDriver.motorA_ccw();
+ * wait(2);
+ * motorDriver.motorA_cw();
+ * wait(2);
+ * motorDriver.motorA_stop();
+ * wait(2);
+ * motorDriver.motorB_ccw();
+ * wait(2);
+ * motorDriver.motorB_cw();
+ * wait(2);
+ * motorDriver.motorB_stop();
+ * wait(2);
+ * }
+ * }
+ */
+
+#ifndef __TB6612FNG_H__
+#define __TB6612FNG_H__
+
+#include "mbed.h"
+
+#define TB6612FNG_PWM_PERIOD_DEFAULT (0.00002) // 50KHz
+#define TB6612FNG_PWM_PULSEWIDTH_DEFAULT (0.50) // 50% duty cycle
+
+class TB6612FNG
+{
+public:
+ TB6612FNG( PinName pinPwmA, PinName pinAin1, PinName pinAin2,
+ PinName pinPwmB, PinName pinBin1, PinName pinBin2,
+ PinName pinNStby );
+ void setPwmAperiod(float fPeriod);
+ void setPwmApulsewidth(float fPulsewidth);
+ void setPwmBperiod(float fPeriod);
+ void setPwmBpulsewidth(float fPulsewidth);
+ void standby(void);
+ void motorA_stop(void);
+ void motorA_ccw(void);
+ void motorA_cw(void);
+ void motorB_stop(void);
+ void motorB_ccw(void);
+ void motorB_cw(void);
+
+private:
+ PwmOut pwmA;
+ DigitalOut Ain1;
+ DigitalOut Ain2;
+ PwmOut pwmB;
+ DigitalOut Bin1;
+ DigitalOut Bin2;
+ DigitalOut nStby;
+};
+
+#endif /* __TB6612FNG_H__ */
\ No newline at end of file