mbed device driver for Toshiba TB6612FNG motor driver

Dependents:   ROBot TB6612FNG_Sample TB6612FNG_Sample

Committer:
rabad1
Date:
Sun Nov 24 21:47:54 2013 +0000
Revision:
0:1a07771ff613
Child:
1:8f562bdd5e93
initial release

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 Toshiiba TB6612FNG Motor Driver. Though this motor driver
rabad1 0:1a07771ff613 5 * can be used to drive two motors (A and B), it can be used to drive
rabad1 0:1a07771ff613 6 * just one. Member functions can be used to set period and pulsewidth
rabad1 0:1a07771ff613 7 * but are not necessary as the constructor does set default values).
rabad1 0:1a07771ff613 8 * The datasheet for this device can be found here:
rabad1 0:1a07771ff613 9 * http://www.toshiba.com/taec/components2/Datasheet_Sync/261/27197.pdf
rabad1 0:1a07771ff613 10 *
rabad1 0:1a07771ff613 11 * Below is some sample code:
rabad1 0:1a07771ff613 12 *
rabad1 0:1a07771ff613 13 * #include "mbed.h"
rabad1 0:1a07771ff613 14 * #include "TB6612FNG.h"
rabad1 0:1a07771ff613 15 *
rabad1 0:1a07771ff613 16 * #define TB6612FNG_PIN_PWMA (p22)
rabad1 0:1a07771ff613 17 * #define TB6612FNG_PIN_AIN1 (p17)
rabad1 0:1a07771ff613 18 * #define TB6612FNG_PIN_AIN2 (p16)
rabad1 0:1a07771ff613 19 * #define TB6612FNG_PIN_PWMB (p21)
rabad1 0:1a07771ff613 20 * #define TB6612FNG_PIN_BIN1 (p19)
rabad1 0:1a07771ff613 21 * #define TB6612FNG_PIN_BIN2 (p20)
rabad1 0:1a07771ff613 22 * #define TB6612FNG_PIN_NSTBY (p18)
rabad1 0:1a07771ff613 23 * TB6612FNG motorDriver( TB6612FNG_PIN_PWMA, TB6612FNG_PIN_AIN1, TB6612FNG_PIN_AIN2,
rabad1 0:1a07771ff613 24 * TB6612FNG_PIN_PWMB, TB6612FNG_PIN_BIN1, TB6612FNG_PIN_BIN2,
rabad1 0:1a07771ff613 25 * TB6612FNG_PIN_NSTBY );
rabad1 0:1a07771ff613 26 * float fPwmPeriod;
rabad1 0:1a07771ff613 27 * float fPwmPulsewidth;
rabad1 0:1a07771ff613 28 *
rabad1 0:1a07771ff613 29 * int main()
rabad1 0:1a07771ff613 30 * {
rabad1 0:1a07771ff613 31 * fPwmPeriod = 0.00002f; // 50KHz
rabad1 0:1a07771ff613 32 * fPwmPulsewidth = 0.50; // 50% duty cycle
rabad1 0:1a07771ff613 33 * motorDriver.setPwmAperiod(fPwmPeriod);
rabad1 0:1a07771ff613 34 * motorDriver.setPwmBperiod(fPwmPeriod);
rabad1 0:1a07771ff613 35 * motorDriver.setPwmApulsewidth(fPwmPulsewidth);
rabad1 0:1a07771ff613 36 * motorDriver.setPwmBpulsewidth(fPwmPulsewidth);
rabad1 0:1a07771ff613 37 *
rabad1 0:1a07771ff613 38 * while(1)
rabad1 0:1a07771ff613 39 * {
rabad1 0:1a07771ff613 40 * motorDriver.motorA_ccw();
rabad1 0:1a07771ff613 41 * wait(2);
rabad1 0:1a07771ff613 42 * motorDriver.motorA_cw();
rabad1 0:1a07771ff613 43 * wait(2);
rabad1 0:1a07771ff613 44 * motorDriver.motorA_stop();
rabad1 0:1a07771ff613 45 * wait(2);
rabad1 0:1a07771ff613 46 * motorDriver.motorB_ccw();
rabad1 0:1a07771ff613 47 * wait(2);
rabad1 0:1a07771ff613 48 * motorDriver.motorB_cw();
rabad1 0:1a07771ff613 49 * wait(2);
rabad1 0:1a07771ff613 50 * motorDriver.motorB_stop();
rabad1 0:1a07771ff613 51 * wait(2);
rabad1 0:1a07771ff613 52 * }
rabad1 0:1a07771ff613 53 * }
rabad1 0:1a07771ff613 54 */
rabad1 0:1a07771ff613 55
rabad1 0:1a07771ff613 56 #ifndef __TB6612FNG_H__
rabad1 0:1a07771ff613 57 #define __TB6612FNG_H__
rabad1 0:1a07771ff613 58
rabad1 0:1a07771ff613 59 #include "mbed.h"
rabad1 0:1a07771ff613 60
rabad1 0:1a07771ff613 61 #define TB6612FNG_PWM_PERIOD_DEFAULT (0.00002) // 50KHz
rabad1 0:1a07771ff613 62 #define TB6612FNG_PWM_PULSEWIDTH_DEFAULT (0.50) // 50% duty cycle
rabad1 0:1a07771ff613 63
rabad1 0:1a07771ff613 64 class TB6612FNG
rabad1 0:1a07771ff613 65 {
rabad1 0:1a07771ff613 66 public:
rabad1 0:1a07771ff613 67 TB6612FNG( PinName pinPwmA, PinName pinAin1, PinName pinAin2,
rabad1 0:1a07771ff613 68 PinName pinPwmB, PinName pinBin1, PinName pinBin2,
rabad1 0:1a07771ff613 69 PinName pinNStby );
rabad1 0:1a07771ff613 70 void setPwmAperiod(float fPeriod);
rabad1 0:1a07771ff613 71 void setPwmApulsewidth(float fPulsewidth);
rabad1 0:1a07771ff613 72 void setPwmBperiod(float fPeriod);
rabad1 0:1a07771ff613 73 void setPwmBpulsewidth(float fPulsewidth);
rabad1 0:1a07771ff613 74 void standby(void);
rabad1 0:1a07771ff613 75 void motorA_stop(void);
rabad1 0:1a07771ff613 76 void motorA_ccw(void);
rabad1 0:1a07771ff613 77 void motorA_cw(void);
rabad1 0:1a07771ff613 78 void motorB_stop(void);
rabad1 0:1a07771ff613 79 void motorB_ccw(void);
rabad1 0:1a07771ff613 80 void motorB_cw(void);
rabad1 0:1a07771ff613 81
rabad1 0:1a07771ff613 82 private:
rabad1 0:1a07771ff613 83 PwmOut pwmA;
rabad1 0:1a07771ff613 84 DigitalOut Ain1;
rabad1 0:1a07771ff613 85 DigitalOut Ain2;
rabad1 0:1a07771ff613 86 PwmOut pwmB;
rabad1 0:1a07771ff613 87 DigitalOut Bin1;
rabad1 0:1a07771ff613 88 DigitalOut Bin2;
rabad1 0:1a07771ff613 89 DigitalOut nStby;
rabad1 0:1a07771ff613 90 };
rabad1 0:1a07771ff613 91
rabad1 0:1a07771ff613 92 #endif /* __TB6612FNG_H__ */