H-Bridge driver

Fork of TB6612FNG by Robert Abad

Committer:
gorian
Date:
Wed Dec 09 16:50:09 2015 +0000
Revision:
3:7d60ce61990e
Parent:
2:4231e0b6e68d
Working H-bridge with my own PWM (not from mbed)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rabad1 0:1a07771ff613 1 /* File: TB6612FNG.h
rabad1 0:1a07771ff613 2 * Author: Robert Abad Copyright (c) 2013
rabad1 0:1a07771ff613 3 *
rabad1 0:1a07771ff613 4 * Desc: driver for TB6612FNG Motor Driver. For further details see
rabad1 0:1a07771ff613 5 * header file, TB6612FNG.h
rabad1 0:1a07771ff613 6 */
rabad1 0:1a07771ff613 7
rabad1 0:1a07771ff613 8 #include "mbed.h"
rabad1 0:1a07771ff613 9 #include "TB6612FNG.h"
rabad1 0:1a07771ff613 10
rabad1 0:1a07771ff613 11 #define SIGNAL_HIGH (1)
rabad1 0:1a07771ff613 12 #define SIGNAL_LOW (0)
rabad1 0:1a07771ff613 13
gorian 2:4231e0b6e68d 14 TB6612FNG::TB6612FNG( PinName pinPwmA, PinName pinAin1, PinName pinAin2, PinName pinPwmB, PinName pinBin1, PinName pinBin2, PinName pinNStby ) :
rabad1 0:1a07771ff613 15 pwmA(pinPwmA),
rabad1 0:1a07771ff613 16 Ain1(pinAin1),
rabad1 0:1a07771ff613 17 Ain2(pinAin2),
rabad1 0:1a07771ff613 18 pwmB(pinPwmB),
rabad1 0:1a07771ff613 19 Bin1(pinBin1),
rabad1 0:1a07771ff613 20 Bin2(pinBin2),
rabad1 0:1a07771ff613 21 nStby(pinNStby)
rabad1 0:1a07771ff613 22 {
rabad1 0:1a07771ff613 23 Ain1 = SIGNAL_LOW;
rabad1 0:1a07771ff613 24 Ain2 = SIGNAL_LOW;
rabad1 0:1a07771ff613 25 Bin1 = SIGNAL_LOW;
rabad1 0:1a07771ff613 26 Bin2 = SIGNAL_LOW;
gorian 3:7d60ce61990e 27 pwmA = SIGNAL_LOW;
gorian 3:7d60ce61990e 28 pwmB = SIGNAL_LOW;
gorian 2:4231e0b6e68d 29 //pwmA.period(TB6612FNG_PWM_PERIOD_DEFAULT);
gorian 2:4231e0b6e68d 30 //pwmA = TB6612FNG_PWM_PULSEWIDTH_DEFAULT;
gorian 2:4231e0b6e68d 31 //pwmB.period(TB6612FNG_PWM_PERIOD_DEFAULT);
gorian 2:4231e0b6e68d 32 //pwmB = TB6612FNG_PWM_PULSEWIDTH_DEFAULT;
rabad1 0:1a07771ff613 33 nStby = SIGNAL_LOW;
gorian 3:7d60ce61990e 34
gorian 3:7d60ce61990e 35 on_delayA = 0;
gorian 3:7d60ce61990e 36 off_delayA = 0;
gorian 3:7d60ce61990e 37 on_delayB = 0;
gorian 3:7d60ce61990e 38 off_delayB = 0;
rabad1 0:1a07771ff613 39 }
rabad1 0:1a07771ff613 40
gorian 3:7d60ce61990e 41 void TB6612FNG::setPwmA(int p_us, float dc)
gorian 3:7d60ce61990e 42 {
gorian 3:7d60ce61990e 43 timerA.detach();
gorian 3:7d60ce61990e 44 if ((p_us == 0) || (dc == 0)) {
gorian 3:7d60ce61990e 45 pwmA = 0;
gorian 3:7d60ce61990e 46 return;
gorian 3:7d60ce61990e 47 }
gorian 3:7d60ce61990e 48 if (dc >= 1) {
gorian 3:7d60ce61990e 49 pwmA = 1;
gorian 3:7d60ce61990e 50 return;
gorian 3:7d60ce61990e 51 }
gorian 3:7d60ce61990e 52 on_delayA = (int)(p_us * dc);
gorian 3:7d60ce61990e 53 off_delayA = p_us - on_delayA;
gorian 3:7d60ce61990e 54 toggleA_On();
rabad1 0:1a07771ff613 55 }
rabad1 0:1a07771ff613 56
gorian 3:7d60ce61990e 57 void TB6612FNG::setPwmB(int p_us, float dc)
rabad1 1:8f562bdd5e93 58 {
gorian 3:7d60ce61990e 59 timerB.detach();
gorian 3:7d60ce61990e 60 if ((p_us == 0) || (dc == 0)) {
gorian 3:7d60ce61990e 61 pwmB = 0;
gorian 3:7d60ce61990e 62 return;
gorian 3:7d60ce61990e 63 }
gorian 3:7d60ce61990e 64 if (dc >= 1) {
gorian 3:7d60ce61990e 65 pwmB = 1;
gorian 3:7d60ce61990e 66 return;
gorian 3:7d60ce61990e 67 }
gorian 3:7d60ce61990e 68 on_delayB = (int)(p_us * dc);
gorian 3:7d60ce61990e 69 off_delayB = p_us - on_delayB;
gorian 3:7d60ce61990e 70 toggleB_On();
rabad1 0:1a07771ff613 71 }
rabad1 0:1a07771ff613 72
rabad1 0:1a07771ff613 73 void TB6612FNG::standby(void)
rabad1 0:1a07771ff613 74 {
rabad1 0:1a07771ff613 75 nStby = SIGNAL_LOW;
rabad1 0:1a07771ff613 76 }
rabad1 0:1a07771ff613 77
rabad1 0:1a07771ff613 78 void TB6612FNG::motorA_stop(void)
rabad1 0:1a07771ff613 79 {
rabad1 0:1a07771ff613 80 Ain1 = SIGNAL_LOW;
rabad1 0:1a07771ff613 81 Ain2 = SIGNAL_LOW;
rabad1 0:1a07771ff613 82 }
rabad1 0:1a07771ff613 83
rabad1 0:1a07771ff613 84 void TB6612FNG::motorA_ccw(void)
rabad1 0:1a07771ff613 85 {
rabad1 0:1a07771ff613 86 Ain1 = SIGNAL_LOW;
rabad1 0:1a07771ff613 87 Ain2 = SIGNAL_HIGH;
rabad1 0:1a07771ff613 88 nStby = SIGNAL_HIGH;
rabad1 0:1a07771ff613 89 }
rabad1 0:1a07771ff613 90
rabad1 0:1a07771ff613 91 void TB6612FNG::motorA_cw(void)
rabad1 0:1a07771ff613 92 {
rabad1 0:1a07771ff613 93 Ain1 = SIGNAL_HIGH;
rabad1 0:1a07771ff613 94 Ain2 = SIGNAL_LOW;
rabad1 0:1a07771ff613 95 nStby = SIGNAL_HIGH;
rabad1 0:1a07771ff613 96 }
rabad1 0:1a07771ff613 97
rabad1 0:1a07771ff613 98 void TB6612FNG::motorB_stop(void)
rabad1 0:1a07771ff613 99 {
rabad1 0:1a07771ff613 100 Bin1 = SIGNAL_LOW;
rabad1 0:1a07771ff613 101 Bin2 = SIGNAL_LOW;
rabad1 0:1a07771ff613 102 }
rabad1 0:1a07771ff613 103
rabad1 0:1a07771ff613 104 void TB6612FNG::motorB_ccw(void)
rabad1 0:1a07771ff613 105 {
rabad1 0:1a07771ff613 106 Bin1 = SIGNAL_LOW;
rabad1 0:1a07771ff613 107 Bin2 = SIGNAL_HIGH;
rabad1 0:1a07771ff613 108 nStby = SIGNAL_HIGH;
rabad1 0:1a07771ff613 109 }
rabad1 0:1a07771ff613 110
rabad1 0:1a07771ff613 111 void TB6612FNG::motorB_cw(void)
rabad1 0:1a07771ff613 112 {
rabad1 0:1a07771ff613 113 Bin1 = SIGNAL_HIGH;
rabad1 0:1a07771ff613 114 Bin2 = SIGNAL_LOW;
rabad1 0:1a07771ff613 115 nStby = SIGNAL_HIGH;
rabad1 0:1a07771ff613 116 }
rabad1 0:1a07771ff613 117
gorian 3:7d60ce61990e 118 void TB6612FNG::toggleA_On(void) {
gorian 3:7d60ce61990e 119 pwmA = 1;
gorian 3:7d60ce61990e 120 timerA.attach_us(this, &TB6612FNG::toggleA_Off, on_delayA);
gorian 3:7d60ce61990e 121 }
rabad1 1:8f562bdd5e93 122
gorian 3:7d60ce61990e 123 void TB6612FNG::toggleA_Off(void) {
gorian 3:7d60ce61990e 124 pwmA = 0;
gorian 3:7d60ce61990e 125 timerA.attach_us(this, &TB6612FNG::toggleA_On, off_delayA);
gorian 3:7d60ce61990e 126 }
gorian 3:7d60ce61990e 127
gorian 3:7d60ce61990e 128 void TB6612FNG::toggleB_On(void) {
gorian 3:7d60ce61990e 129 pwmB = 1;
gorian 3:7d60ce61990e 130 timerB.attach_us(this, &TB6612FNG::toggleB_Off, on_delayB);
gorian 3:7d60ce61990e 131 }
gorian 3:7d60ce61990e 132
gorian 3:7d60ce61990e 133 void TB6612FNG::toggleB_Off(void) {
gorian 3:7d60ce61990e 134 pwmB = 0;
gorian 3:7d60ce61990e 135 timerB.attach_us(this, &TB6612FNG::toggleB_On, off_delayB);
gorian 3:7d60ce61990e 136 }
gorian 3:7d60ce61990e 137