Simple Interface for Toshiba's TB6612FNG H-Bridge Motor Driver
Embed:
(wiki syntax)
Show/hide line numbers
TB6612FNG.cpp
00001 #include "TB6612FNG.h" 00002 00003 TB6612FNG::TB6612FNG(PinName pwm, PinName ctrl1, PinName ctrl2, int period, bool brakeOnZeroDC) 00004 :m_pwm(pwm), 00005 m_ctrl1(ctrl1), 00006 m_ctrl2(ctrl2), 00007 m_pw(0), 00008 m_on(false), 00009 m_brakeOnZeroDC(brakeOnZeroDC), 00010 m_period(period) 00011 { 00012 m_ctrl1 = 0; 00013 m_ctrl2 = 0; 00014 m_pwm.period_us(period); 00015 m_pwm.pulsewidth_us(0); 00016 } 00017 00018 void TB6612FNG::setPulseWidth(int pw) 00019 { 00020 m_pw=pw; 00021 if(m_pw>m_period) m_pw=m_period; 00022 else if(m_pw<-m_period) m_pw=-m_period; 00023 00024 if(m_on)on(); 00025 } 00026 00027 void TB6612FNG::on() 00028 { 00029 m_on=true; 00030 if(m_pw>0) { 00031 m_ctrl1=1; 00032 m_ctrl2=0; 00033 m_pwm.pulsewidth_us(m_pw); 00034 } else if(m_pw<0) { 00035 m_ctrl1=0; 00036 m_ctrl2=1; 00037 m_pwm.pulsewidth_us(-m_pw); 00038 } else if (m_brakeOnZeroDC) { 00039 m_pwm.pulsewidth_us(0); 00040 } else { 00041 m_ctrl1=0; 00042 m_ctrl2=0; 00043 } 00044 00045 } 00046 00047 void TB6612FNG::off() 00048 { 00049 m_on=false; 00050 m_ctrl1=0; 00051 m_ctrl2=0; 00052 } 00053 00054 void TB6612FNG::brake() 00055 { 00056 m_on=false; 00057 m_ctrl1=1; 00058 m_ctrl2=1; 00059 } 00060 00061 void TB6612FNG::setZeroDCReaction(bool brakeOnZeroDC) 00062 { 00063 m_brakeOnZeroDC=brakeOnZeroDC; 00064 on(); 00065 }
Generated on Wed Jul 13 2022 12:40:45 by 1.7.2