Simple Interface for Toshiba's TB6612FNG H-Bridge Motor Driver
Diff: TB6612FNG.cpp
- Revision:
- 1:3dd9137a5cec
- Parent:
- 0:30d6828516f5
- Child:
- 2:73d5d7514e4c
--- a/TB6612FNG.cpp Mon Mar 04 21:58:45 2013 +0000 +++ b/TB6612FNG.cpp Wed Mar 06 19:59:50 2013 +0000 @@ -6,20 +6,26 @@ #include "TB6612FNG.h" -TB6612FNG::TB6612FNG(PinName pwm, PinName ctrl1, PinName ctrl2) - :m_pwm(pwm), m_ctrl1(ctrl1), m_ctrl2(ctrl2), m_dc(0), m_on(false) +TB6612FNG::TB6612FNG(PinName pwm, PinName ctrl1, PinName ctrl2, int period, bool brakeOnZeroDC) + :m_pwm(pwm), + m_ctrl1(ctrl1), + m_ctrl2(ctrl2), + m_pw(0), + m_on(false), + m_brakeOnZeroDC(brakeOnZeroDC), + m_period(period) { m_ctrl1 = 0; m_ctrl2 = 0; - m_pwm.period_us(1000); + m_pwm.period_us(period); m_pwm.pulsewidth_us(0); } -void TB6612FNG::setDC(int dc) +void TB6612FNG::setPulseWidth(int pw) { - m_dc=dc; - if(m_dc>1000) m_dc=1000; - else if(m_dc<-1000) m_dc=-1000; + m_pw=pw; + if(m_pw>m_period) m_pw=m_period; + else if(m_pw<-m_period) m_pw=-m_period; if(m_on)on(); } @@ -27,19 +33,21 @@ void TB6612FNG::on() { m_on=true; - if(m_dc>0) { + if(m_pw>0) { m_ctrl1=1; m_ctrl2=0; - m_pwm.pulsewidth_us(m_dc); - } else if(m_dc<0) { + m_pwm.pulsewidth_us(m_pw); + } else if(m_pw<0) { m_ctrl1=0; m_ctrl2=1; - m_pwm.pulsewidth_us(-m_dc); + m_pwm.pulsewidth_us(-m_pw); + } else if (m_brakeOnZeroDC) { + m_pwm.pulsewidth_us(0); } else { m_ctrl1=0; m_ctrl2=0; } - + } void TB6612FNG::off()