A simple motor driver for the MC33926 chip, commonly found on the Polulu md11a breakout https://www.pololu.com/product/1213.
MC33926.cpp@0:3163bb06484b, 2017-04-30 (annotated)
- Committer:
- sk398
- Date:
- Sun Apr 30 16:14:35 2017 +0000
- Revision:
- 0:3163bb06484b
Library for driving the MC33926 Motor Driver chip which is commonly found on the Polulu md11a, with two MC33926 drivers https://www.pololu.com/product/1213
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sk398 | 0:3163bb06484b | 1 | /******************************************************************************************** |
sk398 | 0:3163bb06484b | 2 | |
sk398 | 0:3163bb06484b | 3 | Filename: MC33926.cpp |
sk398 | 0:3163bb06484b | 4 | |
sk398 | 0:3163bb06484b | 5 | Original Author: Steven Kay |
sk398 | 0:3163bb06484b | 6 | |
sk398 | 0:3163bb06484b | 7 | Development Group: Steve!! |
sk398 | 0:3163bb06484b | 8 | |
sk398 | 0:3163bb06484b | 9 | Original Creation Date: April 2017 |
sk398 | 0:3163bb06484b | 10 | |
sk398 | 0:3163bb06484b | 11 | Description: <Desc> |
sk398 | 0:3163bb06484b | 12 | |
sk398 | 0:3163bb06484b | 13 | Revision History: Version 1.0 - Initial Release |
sk398 | 0:3163bb06484b | 14 | |
sk398 | 0:3163bb06484b | 15 | *********************************************************************************************/ |
sk398 | 0:3163bb06484b | 16 | #include "mbed.h" |
sk398 | 0:3163bb06484b | 17 | #include "MC33926.h" |
sk398 | 0:3163bb06484b | 18 | |
sk398 | 0:3163bb06484b | 19 | |
sk398 | 0:3163bb06484b | 20 | MC33926::MC33926(PinName pinIN1, PinName pinIN2, PinName pinFB, PinName pinSF) |
sk398 | 0:3163bb06484b | 21 | { |
sk398 | 0:3163bb06484b | 22 | // _pinSF.disable_irq(); |
sk398 | 0:3163bb06484b | 23 | _pinIN1 = new PwmOut(pinIN1); |
sk398 | 0:3163bb06484b | 24 | _pinIN2 = new PwmOut(pinIN2); |
sk398 | 0:3163bb06484b | 25 | _pinFB = new AnalogIn(pinFB); |
sk398 | 0:3163bb06484b | 26 | _pinSF = new InterruptIn(pinSF); |
sk398 | 0:3163bb06484b | 27 | |
sk398 | 0:3163bb06484b | 28 | // Set period to 2ms (frequency 500Hz) |
sk398 | 0:3163bb06484b | 29 | _pinIN1 -> period_ms(DRIVER_PWM_PERIOD); |
sk398 | 0:3163bb06484b | 30 | _pinIN2 -> period_ms(DRIVER_PWM_PERIOD); |
sk398 | 0:3163bb06484b | 31 | |
sk398 | 0:3163bb06484b | 32 | // Initialise to 0% Duty Cycle |
sk398 | 0:3163bb06484b | 33 | SetPWMPulsewidth(RESET,0); |
sk398 | 0:3163bb06484b | 34 | SetPWMPulsewidth(RESET,0); |
sk398 | 0:3163bb06484b | 35 | |
sk398 | 0:3163bb06484b | 36 | } |
sk398 | 0:3163bb06484b | 37 | |
sk398 | 0:3163bb06484b | 38 | |
sk398 | 0:3163bb06484b | 39 | |
sk398 | 0:3163bb06484b | 40 | void MC33926::SetPWMPulsewidth(int direction, float PWM_Pulsewidth) |
sk398 | 0:3163bb06484b | 41 | { |
sk398 | 0:3163bb06484b | 42 | // FORWARD = 1 |
sk398 | 0:3163bb06484b | 43 | if(direction == FORWARD) |
sk398 | 0:3163bb06484b | 44 | { |
sk398 | 0:3163bb06484b | 45 | _pinIN1 -> write(PWM_Pulsewidth); |
sk398 | 0:3163bb06484b | 46 | _pinIN2 -> write(0); |
sk398 | 0:3163bb06484b | 47 | } |
sk398 | 0:3163bb06484b | 48 | // REVERSE = 0 |
sk398 | 0:3163bb06484b | 49 | else if(direction == REVERSE) |
sk398 | 0:3163bb06484b | 50 | { |
sk398 | 0:3163bb06484b | 51 | _pinIN1 -> write(0); |
sk398 | 0:3163bb06484b | 52 | _pinIN2 -> write(PWM_Pulsewidth); |
sk398 | 0:3163bb06484b | 53 | } |
sk398 | 0:3163bb06484b | 54 | // DEFAUULT or RESET != 1 OR 0 |
sk398 | 0:3163bb06484b | 55 | else |
sk398 | 0:3163bb06484b | 56 | { |
sk398 | 0:3163bb06484b | 57 | _pinIN1 -> write(0); |
sk398 | 0:3163bb06484b | 58 | _pinIN2 -> write(0); |
sk398 | 0:3163bb06484b | 59 | } |
sk398 | 0:3163bb06484b | 60 | } |
sk398 | 0:3163bb06484b | 61 | |
sk398 | 0:3163bb06484b | 62 | |
sk398 | 0:3163bb06484b | 63 | |
sk398 | 0:3163bb06484b | 64 | float MC33926::ReadCurrentFeedback() |
sk398 | 0:3163bb06484b | 65 | { |
sk398 | 0:3163bb06484b | 66 | float temp = _pinFB -> read(); |
sk398 | 0:3163bb06484b | 67 | |
sk398 | 0:3163bb06484b | 68 | // Insert some conversion formats |
sk398 | 0:3163bb06484b | 69 | float currentValue = temp; |
sk398 | 0:3163bb06484b | 70 | |
sk398 | 0:3163bb06484b | 71 | return currentValue; |
sk398 | 0:3163bb06484b | 72 | } |
sk398 | 0:3163bb06484b | 73 | |
sk398 | 0:3163bb06484b | 74 | |
sk398 | 0:3163bb06484b | 75 | |
sk398 | 0:3163bb06484b | 76 | //void MC33926::StatusFlagWatch(bool latchPol) |
sk398 | 0:3163bb06484b | 77 | //{ |
sk398 | 0:3163bb06484b | 78 | // // if == 1, enable IRQ |
sk398 | 0:3163bb06484b | 79 | // if(latchPol) |
sk398 | 0:3163bb06484b | 80 | // { |
sk398 | 0:3163bb06484b | 81 | // _pinSF -> enable_irq(); |
sk398 | 0:3163bb06484b | 82 | // _pinSF -> fall(this,&MC33926::StatusFlagISR); |
sk398 | 0:3163bb06484b | 83 | // } |
sk398 | 0:3163bb06484b | 84 | // // if == 0, disable IRQ |
sk398 | 0:3163bb06484b | 85 | // else |
sk398 | 0:3163bb06484b | 86 | // { |
sk398 | 0:3163bb06484b | 87 | // _pinSF -> disable_irq(); |
sk398 | 0:3163bb06484b | 88 | // } |
sk398 | 0:3163bb06484b | 89 | //} |
sk398 | 0:3163bb06484b | 90 | // |
sk398 | 0:3163bb06484b | 91 | // |
sk398 | 0:3163bb06484b | 92 | //void MC33926::ResetStatus() |
sk398 | 0:3163bb06484b | 93 | //{ |
sk398 | 0:3163bb06484b | 94 | // statusFlag = 0; |
sk398 | 0:3163bb06484b | 95 | //} |
sk398 | 0:3163bb06484b | 96 | // |
sk398 | 0:3163bb06484b | 97 | // |
sk398 | 0:3163bb06484b | 98 | // |
sk398 | 0:3163bb06484b | 99 | //bool MC33926::GetStatus() |
sk398 | 0:3163bb06484b | 100 | //{ |
sk398 | 0:3163bb06484b | 101 | // return statusFlag; |
sk398 | 0:3163bb06484b | 102 | //} |
sk398 | 0:3163bb06484b | 103 | // |
sk398 | 0:3163bb06484b | 104 | // |
sk398 | 0:3163bb06484b | 105 | // |
sk398 | 0:3163bb06484b | 106 | //void MC33926::StatusFlagISR() |
sk398 | 0:3163bb06484b | 107 | //{ |
sk398 | 0:3163bb06484b | 108 | // // An Interrupt has been generated |
sk398 | 0:3163bb06484b | 109 | // statusFlag = 1; |
sk398 | 0:3163bb06484b | 110 | //} |