H-Bridge driver

Fork of TB6612FNG by Robert Abad

Files at this revision

API Documentation at this revision

Comitter:
gorian
Date:
Wed Dec 09 16:50:09 2015 +0000
Parent:
2:4231e0b6e68d
Commit message:
Working H-bridge with my own PWM (not from mbed)

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
--- a/TB6612FNG.cpp	Tue Dec 08 07:32:17 2015 +0000
+++ b/TB6612FNG.cpp	Wed Dec 09 16:50:09 2015 +0000
@@ -24,45 +24,50 @@
     Ain2 = SIGNAL_LOW;
     Bin1 = SIGNAL_LOW;
     Bin2 = SIGNAL_LOW;
-    pwmA = SIGNAL_HIGH;
-    pwmB = SIGNAL_HIGH;
+    pwmA = SIGNAL_LOW;
+    pwmB = 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::setPwmA(float fPeriod, float fPulsewidth)
-{
-//    pwmA.period(fPeriod);
-    pwmA = fPulsewidth;
-}
-
-void TB6612FNG::setPwmAperiod(float fPeriod)
-{
-  //  pwmA.period(fPeriod);
+    
+    on_delayA = 0;
+    off_delayA = 0;
+    on_delayB = 0;
+    off_delayB = 0;
 }
 
-void TB6612FNG::setPwmApulsewidth(float fPulsewidth)
-{
-    pwmA = fPulsewidth;
+void TB6612FNG::setPwmA(int p_us, float dc)
+{    
+    timerA.detach();
+    if ((p_us == 0) || (dc == 0)) {
+        pwmA = 0;
+        return;
+    }
+    if (dc >= 1) {
+        pwmA = 1;
+        return;
+    }
+    on_delayA = (int)(p_us * dc);
+    off_delayA = p_us - on_delayA;
+    toggleA_On();
 }
 
-void TB6612FNG::setPwmB(float fPeriod, float fPulsewidth)
+void TB6612FNG::setPwmB(int p_us, float dc)
 {
-  //  pwmB.period(fPeriod);
-    pwmB = fPulsewidth;
-}
-
-void TB6612FNG::setPwmBperiod(float fPeriod)
-{
-  //  pwmB.period(fPeriod);
-}
-
-void TB6612FNG::setPwmBpulsewidth(float fPulsewidth)
-{
-    pwmB = fPulsewidth;
+    timerB.detach();
+    if ((p_us == 0) || (dc == 0)) {
+        pwmB = 0;
+        return;
+    }
+    if (dc >= 1) {
+        pwmB = 1;
+        return;
+    }
+    on_delayB = (int)(p_us * dc);
+    off_delayB = p_us - on_delayB;
+    toggleB_On();
 }
 
 void TB6612FNG::standby(void)
@@ -110,4 +115,23 @@
     nStby = SIGNAL_HIGH;
 }
 
+void TB6612FNG::toggleA_On(void) {
+    pwmA = 1;
+    timerA.attach_us(this, &TB6612FNG::toggleA_Off, on_delayA);
+}
 
+void TB6612FNG::toggleA_Off(void) {
+    pwmA = 0;
+    timerA.attach_us(this, &TB6612FNG::toggleA_On, off_delayA);
+}
+
+void TB6612FNG::toggleB_On(void) {
+    pwmB = 1;
+    timerB.attach_us(this, &TB6612FNG::toggleB_Off, on_delayB);
+}
+
+void TB6612FNG::toggleB_Off(void) {
+    pwmB = 0;
+    timerB.attach_us(this, &TB6612FNG::toggleB_On, off_delayB);
+}
+
--- a/TB6612FNG.h	Tue Dec 08 07:32:17 2015 +0000
+++ b/TB6612FNG.h	Wed Dec 09 16:50:09 2015 +0000
@@ -58,7 +58,7 @@
 
 #include "mbed.h"
 
-#define TB6612FNG_PWM_PERIOD_DEFAULT      (0.00002)   // 50KHz
+#define TB6612FNG_PWM_PERIOD_DEFAULT      (20000)   // 50KHz
 #define TB6612FNG_PWM_PULSEWIDTH_DEFAULT  (0.50)      // 50% duty cycle
  
 class TB6612FNG
@@ -67,12 +67,9 @@
     TB6612FNG( PinName pinPwmA, PinName pinAin1, PinName pinAin2,
                PinName pinPwmB, PinName pinBin1, PinName pinBin2,
                PinName pinNStby );
-    void setPwmA(float fPeriod, float fPulsewidth);
-    void setPwmAperiod(float fPeriod);
-    void setPwmApulsewidth(float fPulsewidth);
-    void setPwmB(float fPeriod, float fPulsewidth);
-    void setPwmBperiod(float fPeriod);
-    void setPwmBpulsewidth(float fPulsewidth);
+    
+    void setPwmA(int p_us, float dc);
+    void setPwmB(int p_us, float dc);
     void standby(void);
     void motorA_stop(void);
     void motorA_ccw(void);
@@ -80,6 +77,10 @@
     void motorB_stop(void);
     void motorB_ccw(void);
     void motorB_cw(void);
+    void toggleA_Off(void);
+    void toggleA_On(void);
+    void toggleB_Off(void);
+    void toggleB_On(void);
     
 private:
     DigitalOut pwmA;
@@ -89,6 +90,16 @@
     DigitalOut Bin1;
     DigitalOut Bin2;
     DigitalOut nStby;
+    
+    int on_delayA;
+    int off_delayA;
+    int on_delayB;
+    int off_delayB;
+    
+    Timeout timerA;
+    Timeout timerB;
+    
+    
 };
 
 #endif /* __TB6612FNG_H__ */
\ No newline at end of file