MC33926 is a H-Bridge from freescale. This driver allow to use it to drive a DC-Motor using 3 different driving methods: - Lock Antiphase - Sign-Magnitude braking - Sign-Magnitude coasting I used this board https://www.sparkfun.com/products/11080 for my tests and find a lot of informations here: http://modularcircuits.tantosonline.com/blog/articles/h-bridge-secrets/h-bridge-control/
mc33926.cpp@0:217bc3688cff, 2013-10-08 (annotated)
- Committer:
- garfunkheul
- Date:
- Tue Oct 08 21:09:37 2013 +0000
- Revision:
- 0:217bc3688cff
This is the first commit for MC33926 driver. It brings 3 driving methods: lock-antiphase, sign-magnitude braking and sign-magnitude coasting
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
garfunkheul | 0:217bc3688cff | 1 | #include "mc33926.h" |
garfunkheul | 0:217bc3688cff | 2 | |
garfunkheul | 0:217bc3688cff | 3 | mc33926::mc33926(PinName inv, PinName in1, PinName in2, PinName d1, PinName d2, PinName en, PinName sf) |
garfunkheul | 0:217bc3688cff | 4 | { |
garfunkheul | 0:217bc3688cff | 5 | pin_inv = inv; |
garfunkheul | 0:217bc3688cff | 6 | pin_in1 = in1; |
garfunkheul | 0:217bc3688cff | 7 | pin_in2 = in2; |
garfunkheul | 0:217bc3688cff | 8 | pin_d1 = d1; |
garfunkheul | 0:217bc3688cff | 9 | pin_d2 = d2; |
garfunkheul | 0:217bc3688cff | 10 | pin_en = en; |
garfunkheul | 0:217bc3688cff | 11 | pin_sf = sf; |
garfunkheul | 0:217bc3688cff | 12 | |
garfunkheul | 0:217bc3688cff | 13 | period = 50; |
garfunkheul | 0:217bc3688cff | 14 | speed = 0; |
garfunkheul | 0:217bc3688cff | 15 | pwm = NULL; |
garfunkheul | 0:217bc3688cff | 16 | |
garfunkheul | 0:217bc3688cff | 17 | disable(); |
garfunkheul | 0:217bc3688cff | 18 | set_mode(SIGN_MAGNITUDE_COASTING); |
garfunkheul | 0:217bc3688cff | 19 | } |
garfunkheul | 0:217bc3688cff | 20 | |
garfunkheul | 0:217bc3688cff | 21 | void mc33926::setup_lock_antiphase() |
garfunkheul | 0:217bc3688cff | 22 | { |
garfunkheul | 0:217bc3688cff | 23 | DigitalOut in1(pin_in1); |
garfunkheul | 0:217bc3688cff | 24 | DigitalOut in2(pin_in2); |
garfunkheul | 0:217bc3688cff | 25 | DigitalOut d1(pin_d1); |
garfunkheul | 0:217bc3688cff | 26 | DigitalOut d2(pin_d2); |
garfunkheul | 0:217bc3688cff | 27 | |
garfunkheul | 0:217bc3688cff | 28 | in1 = d2 = 1; |
garfunkheul | 0:217bc3688cff | 29 | in2 = d1 = 0; |
garfunkheul | 0:217bc3688cff | 30 | |
garfunkheul | 0:217bc3688cff | 31 | /* Force Pin reconfiguration */ |
garfunkheul | 0:217bc3688cff | 32 | if (pwm) |
garfunkheul | 0:217bc3688cff | 33 | delete pwm; |
garfunkheul | 0:217bc3688cff | 34 | pwm = new PwmOut(pin_inv); |
garfunkheul | 0:217bc3688cff | 35 | |
garfunkheul | 0:217bc3688cff | 36 | period_lock_antiphase(); |
garfunkheul | 0:217bc3688cff | 37 | } |
garfunkheul | 0:217bc3688cff | 38 | |
garfunkheul | 0:217bc3688cff | 39 | float mc33926::speed_lock_antiphase() |
garfunkheul | 0:217bc3688cff | 40 | { |
garfunkheul | 0:217bc3688cff | 41 | float duty; |
garfunkheul | 0:217bc3688cff | 42 | /* Use same sens as sign_magnitude */ |
garfunkheul | 0:217bc3688cff | 43 | int _speed = (-1) * speed; |
garfunkheul | 0:217bc3688cff | 44 | |
garfunkheul | 0:217bc3688cff | 45 | if (_speed > 0) |
garfunkheul | 0:217bc3688cff | 46 | duty = (((_speed * 50 / MAX_SPEED) + 50) / 100.0f); |
garfunkheul | 0:217bc3688cff | 47 | else if (_speed < 0) |
garfunkheul | 0:217bc3688cff | 48 | duty = ((50 - ((_speed * -50 / MAX_SPEED))) / 100.0f); |
garfunkheul | 0:217bc3688cff | 49 | else |
garfunkheul | 0:217bc3688cff | 50 | duty = 0.5; |
garfunkheul | 0:217bc3688cff | 51 | |
garfunkheul | 0:217bc3688cff | 52 | pwm->write(duty); |
garfunkheul | 0:217bc3688cff | 53 | |
garfunkheul | 0:217bc3688cff | 54 | return duty; |
garfunkheul | 0:217bc3688cff | 55 | } |
garfunkheul | 0:217bc3688cff | 56 | |
garfunkheul | 0:217bc3688cff | 57 | void mc33926::period_lock_antiphase() |
garfunkheul | 0:217bc3688cff | 58 | { |
garfunkheul | 0:217bc3688cff | 59 | pwm->period_us(period); |
garfunkheul | 0:217bc3688cff | 60 | speed_lock_antiphase(); |
garfunkheul | 0:217bc3688cff | 61 | } |
garfunkheul | 0:217bc3688cff | 62 | |
garfunkheul | 0:217bc3688cff | 63 | void mc33926::setup_sign_magnitude_braking() |
garfunkheul | 0:217bc3688cff | 64 | { |
garfunkheul | 0:217bc3688cff | 65 | DigitalOut in2(pin_in2); |
garfunkheul | 0:217bc3688cff | 66 | DigitalOut d1(pin_d1); |
garfunkheul | 0:217bc3688cff | 67 | DigitalOut d2(pin_d2); |
garfunkheul | 0:217bc3688cff | 68 | |
garfunkheul | 0:217bc3688cff | 69 | in2 = d1 = 0; |
garfunkheul | 0:217bc3688cff | 70 | d2 = 1; |
garfunkheul | 0:217bc3688cff | 71 | |
garfunkheul | 0:217bc3688cff | 72 | /* Force Pin reconfiguration */ |
garfunkheul | 0:217bc3688cff | 73 | if (pwm) |
garfunkheul | 0:217bc3688cff | 74 | delete pwm; |
garfunkheul | 0:217bc3688cff | 75 | pwm = new PwmOut(pin_in1); |
garfunkheul | 0:217bc3688cff | 76 | |
garfunkheul | 0:217bc3688cff | 77 | period_sign_magnitude(); |
garfunkheul | 0:217bc3688cff | 78 | } |
garfunkheul | 0:217bc3688cff | 79 | |
garfunkheul | 0:217bc3688cff | 80 | float mc33926::speed_sign_magnitude() |
garfunkheul | 0:217bc3688cff | 81 | { |
garfunkheul | 0:217bc3688cff | 82 | float duty; |
garfunkheul | 0:217bc3688cff | 83 | DigitalOut inv(pin_inv); |
garfunkheul | 0:217bc3688cff | 84 | |
garfunkheul | 0:217bc3688cff | 85 | if (speed > 0) { |
garfunkheul | 0:217bc3688cff | 86 | inv = 0; |
garfunkheul | 0:217bc3688cff | 87 | duty = (speed * 100 / MAX_SPEED) / 100.0f; |
garfunkheul | 0:217bc3688cff | 88 | } else if (speed < 0) { |
garfunkheul | 0:217bc3688cff | 89 | inv = 1; |
garfunkheul | 0:217bc3688cff | 90 | duty = (speed * -100 / MAX_SPEED) / 100.0f; |
garfunkheul | 0:217bc3688cff | 91 | } else { |
garfunkheul | 0:217bc3688cff | 92 | duty = 0; |
garfunkheul | 0:217bc3688cff | 93 | } |
garfunkheul | 0:217bc3688cff | 94 | |
garfunkheul | 0:217bc3688cff | 95 | pwm->write(duty); |
garfunkheul | 0:217bc3688cff | 96 | return duty; |
garfunkheul | 0:217bc3688cff | 97 | } |
garfunkheul | 0:217bc3688cff | 98 | |
garfunkheul | 0:217bc3688cff | 99 | void mc33926::period_sign_magnitude() |
garfunkheul | 0:217bc3688cff | 100 | { |
garfunkheul | 0:217bc3688cff | 101 | pwm->period_us(period); |
garfunkheul | 0:217bc3688cff | 102 | speed_sign_magnitude(); |
garfunkheul | 0:217bc3688cff | 103 | } |
garfunkheul | 0:217bc3688cff | 104 | |
garfunkheul | 0:217bc3688cff | 105 | void mc33926::setup_sign_magnitude_coasting() |
garfunkheul | 0:217bc3688cff | 106 | { |
garfunkheul | 0:217bc3688cff | 107 | DigitalOut in1(pin_in1); |
garfunkheul | 0:217bc3688cff | 108 | DigitalOut in2(pin_in2); |
garfunkheul | 0:217bc3688cff | 109 | DigitalOut d1(pin_d1); |
garfunkheul | 0:217bc3688cff | 110 | |
garfunkheul | 0:217bc3688cff | 111 | in2 = d1 = 0; |
garfunkheul | 0:217bc3688cff | 112 | in1 = 1; |
garfunkheul | 0:217bc3688cff | 113 | |
garfunkheul | 0:217bc3688cff | 114 | /* Force Pin reconfiguration */ |
garfunkheul | 0:217bc3688cff | 115 | if (pwm) |
garfunkheul | 0:217bc3688cff | 116 | delete pwm; |
garfunkheul | 0:217bc3688cff | 117 | pwm = new PwmOut(pin_d2); |
garfunkheul | 0:217bc3688cff | 118 | |
garfunkheul | 0:217bc3688cff | 119 | period_sign_magnitude(); |
garfunkheul | 0:217bc3688cff | 120 | } |
garfunkheul | 0:217bc3688cff | 121 | |
garfunkheul | 0:217bc3688cff | 122 | void mc33926::set_period(int _period) |
garfunkheul | 0:217bc3688cff | 123 | { |
garfunkheul | 0:217bc3688cff | 124 | period = _period; |
garfunkheul | 0:217bc3688cff | 125 | |
garfunkheul | 0:217bc3688cff | 126 | switch(mode) { |
garfunkheul | 0:217bc3688cff | 127 | case LOCK_ANTIPHASE: |
garfunkheul | 0:217bc3688cff | 128 | period_lock_antiphase(); |
garfunkheul | 0:217bc3688cff | 129 | break; |
garfunkheul | 0:217bc3688cff | 130 | case SIGN_MAGNITUDE_BRAKING: |
garfunkheul | 0:217bc3688cff | 131 | case SIGN_MAGNITUDE_COASTING: |
garfunkheul | 0:217bc3688cff | 132 | period_sign_magnitude(); |
garfunkheul | 0:217bc3688cff | 133 | break; |
garfunkheul | 0:217bc3688cff | 134 | } |
garfunkheul | 0:217bc3688cff | 135 | } |
garfunkheul | 0:217bc3688cff | 136 | |
garfunkheul | 0:217bc3688cff | 137 | float mc33926::set_speed(int _speed) |
garfunkheul | 0:217bc3688cff | 138 | { |
garfunkheul | 0:217bc3688cff | 139 | if (_speed > MAX_SPEED) |
garfunkheul | 0:217bc3688cff | 140 | speed = MAX_SPEED; |
garfunkheul | 0:217bc3688cff | 141 | else if (_speed < -MAX_SPEED) |
garfunkheul | 0:217bc3688cff | 142 | speed = -MAX_SPEED; |
garfunkheul | 0:217bc3688cff | 143 | else |
garfunkheul | 0:217bc3688cff | 144 | speed = _speed; |
garfunkheul | 0:217bc3688cff | 145 | |
garfunkheul | 0:217bc3688cff | 146 | switch(mode) { |
garfunkheul | 0:217bc3688cff | 147 | case LOCK_ANTIPHASE: |
garfunkheul | 0:217bc3688cff | 148 | return speed_lock_antiphase(); |
garfunkheul | 0:217bc3688cff | 149 | case SIGN_MAGNITUDE_BRAKING: |
garfunkheul | 0:217bc3688cff | 150 | case SIGN_MAGNITUDE_COASTING: |
garfunkheul | 0:217bc3688cff | 151 | return speed_sign_magnitude(); |
garfunkheul | 0:217bc3688cff | 152 | } |
garfunkheul | 0:217bc3688cff | 153 | } |
garfunkheul | 0:217bc3688cff | 154 | |
garfunkheul | 0:217bc3688cff | 155 | void mc33926::set_mode(int _mode) |
garfunkheul | 0:217bc3688cff | 156 | { |
garfunkheul | 0:217bc3688cff | 157 | mode = _mode; |
garfunkheul | 0:217bc3688cff | 158 | |
garfunkheul | 0:217bc3688cff | 159 | switch(mode) { |
garfunkheul | 0:217bc3688cff | 160 | case LOCK_ANTIPHASE: |
garfunkheul | 0:217bc3688cff | 161 | setup_lock_antiphase(); |
garfunkheul | 0:217bc3688cff | 162 | break; |
garfunkheul | 0:217bc3688cff | 163 | case SIGN_MAGNITUDE_BRAKING: |
garfunkheul | 0:217bc3688cff | 164 | setup_sign_magnitude_braking(); |
garfunkheul | 0:217bc3688cff | 165 | case SIGN_MAGNITUDE_COASTING: |
garfunkheul | 0:217bc3688cff | 166 | setup_sign_magnitude_coasting(); |
garfunkheul | 0:217bc3688cff | 167 | break; |
garfunkheul | 0:217bc3688cff | 168 | } |
garfunkheul | 0:217bc3688cff | 169 | } |
garfunkheul | 0:217bc3688cff | 170 | |
garfunkheul | 0:217bc3688cff | 171 | void mc33926::enable() |
garfunkheul | 0:217bc3688cff | 172 | { |
garfunkheul | 0:217bc3688cff | 173 | DigitalOut en(pin_en); |
garfunkheul | 0:217bc3688cff | 174 | |
garfunkheul | 0:217bc3688cff | 175 | if (pin_en != NC) |
garfunkheul | 0:217bc3688cff | 176 | en = 1; |
garfunkheul | 0:217bc3688cff | 177 | } |
garfunkheul | 0:217bc3688cff | 178 | |
garfunkheul | 0:217bc3688cff | 179 | void mc33926::disable() |
garfunkheul | 0:217bc3688cff | 180 | { |
garfunkheul | 0:217bc3688cff | 181 | DigitalOut en(pin_en); |
garfunkheul | 0:217bc3688cff | 182 | |
garfunkheul | 0:217bc3688cff | 183 | if (pin_en != NC) |
garfunkheul | 0:217bc3688cff | 184 | en = 0; |
garfunkheul | 0:217bc3688cff | 185 | } |
garfunkheul | 0:217bc3688cff | 186 | |
garfunkheul | 0:217bc3688cff | 187 | int mc33926::get_speed() |
garfunkheul | 0:217bc3688cff | 188 | { |
garfunkheul | 0:217bc3688cff | 189 | return speed; |
garfunkheul | 0:217bc3688cff | 190 | } |
garfunkheul | 0:217bc3688cff | 191 | |
garfunkheul | 0:217bc3688cff | 192 | int mc33926::get_mode() |
garfunkheul | 0:217bc3688cff | 193 | { |
garfunkheul | 0:217bc3688cff | 194 | return mode; |
garfunkheul | 0:217bc3688cff | 195 | } |
garfunkheul | 0:217bc3688cff | 196 | |
garfunkheul | 0:217bc3688cff | 197 | |
garfunkheul | 0:217bc3688cff | 198 | int mc33926::get_period() |
garfunkheul | 0:217bc3688cff | 199 | { |
garfunkheul | 0:217bc3688cff | 200 | return period; |
garfunkheul | 0:217bc3688cff | 201 | } |