Simple Interface for Toshiba's TB6612FNG H-Bridge Motor Driver
TB6612FNG.cpp@0:30d6828516f5, 2013-03-04 (annotated)
- Committer:
- humlet
- Date:
- Mon Mar 04 21:58:45 2013 +0000
- Revision:
- 0:30d6828516f5
- Child:
- 1:3dd9137a5cec
myfirst
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
humlet | 0:30d6828516f5 | 1 | /** |
humlet | 0:30d6828516f5 | 2 | * H-Bridge / Motor Driver TB6612FNG Control Library |
humlet | 0:30d6828516f5 | 3 | */ |
humlet | 0:30d6828516f5 | 4 | |
humlet | 0:30d6828516f5 | 5 | |
humlet | 0:30d6828516f5 | 6 | #include "TB6612FNG.h" |
humlet | 0:30d6828516f5 | 7 | |
humlet | 0:30d6828516f5 | 8 | |
humlet | 0:30d6828516f5 | 9 | TB6612FNG::TB6612FNG(PinName pwm, PinName ctrl1, PinName ctrl2) |
humlet | 0:30d6828516f5 | 10 | :m_pwm(pwm), m_ctrl1(ctrl1), m_ctrl2(ctrl2), m_dc(0), m_on(false) |
humlet | 0:30d6828516f5 | 11 | { |
humlet | 0:30d6828516f5 | 12 | m_ctrl1 = 0; |
humlet | 0:30d6828516f5 | 13 | m_ctrl2 = 0; |
humlet | 0:30d6828516f5 | 14 | m_pwm.period_us(1000); |
humlet | 0:30d6828516f5 | 15 | m_pwm.pulsewidth_us(0); |
humlet | 0:30d6828516f5 | 16 | } |
humlet | 0:30d6828516f5 | 17 | |
humlet | 0:30d6828516f5 | 18 | void TB6612FNG::setDC(int dc) |
humlet | 0:30d6828516f5 | 19 | { |
humlet | 0:30d6828516f5 | 20 | m_dc=dc; |
humlet | 0:30d6828516f5 | 21 | if(m_dc>1000) m_dc=1000; |
humlet | 0:30d6828516f5 | 22 | else if(m_dc<-1000) m_dc=-1000; |
humlet | 0:30d6828516f5 | 23 | |
humlet | 0:30d6828516f5 | 24 | if(m_on)on(); |
humlet | 0:30d6828516f5 | 25 | } |
humlet | 0:30d6828516f5 | 26 | |
humlet | 0:30d6828516f5 | 27 | void TB6612FNG::on() |
humlet | 0:30d6828516f5 | 28 | { |
humlet | 0:30d6828516f5 | 29 | m_on=true; |
humlet | 0:30d6828516f5 | 30 | if(m_dc>0) { |
humlet | 0:30d6828516f5 | 31 | m_ctrl1=1; |
humlet | 0:30d6828516f5 | 32 | m_ctrl2=0; |
humlet | 0:30d6828516f5 | 33 | m_pwm.pulsewidth_us(m_dc); |
humlet | 0:30d6828516f5 | 34 | } else if(m_dc<0) { |
humlet | 0:30d6828516f5 | 35 | m_ctrl1=0; |
humlet | 0:30d6828516f5 | 36 | m_ctrl2=1; |
humlet | 0:30d6828516f5 | 37 | m_pwm.pulsewidth_us(-m_dc); |
humlet | 0:30d6828516f5 | 38 | } else { |
humlet | 0:30d6828516f5 | 39 | m_ctrl1=0; |
humlet | 0:30d6828516f5 | 40 | m_ctrl2=0; |
humlet | 0:30d6828516f5 | 41 | } |
humlet | 0:30d6828516f5 | 42 | |
humlet | 0:30d6828516f5 | 43 | } |
humlet | 0:30d6828516f5 | 44 | |
humlet | 0:30d6828516f5 | 45 | void TB6612FNG::off() |
humlet | 0:30d6828516f5 | 46 | { |
humlet | 0:30d6828516f5 | 47 | m_on=false; |
humlet | 0:30d6828516f5 | 48 | m_ctrl1=0; |
humlet | 0:30d6828516f5 | 49 | m_ctrl2=0; |
humlet | 0:30d6828516f5 | 50 | } |
humlet | 0:30d6828516f5 | 51 | |
humlet | 0:30d6828516f5 | 52 | void TB6612FNG::brake() |
humlet | 0:30d6828516f5 | 53 | { |
humlet | 0:30d6828516f5 | 54 | m_on=false; |
humlet | 0:30d6828516f5 | 55 | m_ctrl1=1; |
humlet | 0:30d6828516f5 | 56 | m_ctrl2=1; |
humlet | 0:30d6828516f5 | 57 | } |