# how do i drive the h bridge to control a motor?

I am using the ZXMHC3F381N8 MOSFET H-Bridge to drive a motor forwards and backwards. I am using a PIC16F1946. The H bridge is connected to PORTE of the PIC. This is the code I am using to set up the port:

#define MOTOR_STOP      0b11000000
//setup PORTE
PORTE = 0x00 | MOTOR_STOP;
TRISE = 0x00;
ANSELE = 0x00;


So, if I am not mistaken, here is what I need to do turn the motor:

• For forward motion: N1G = HIGH P2G = HIGH
• For backward motion: N2G = HIGH P1G = HIGH
• For stopping: P1G = P2G = HIGH
• What's your problem? – stevenvh Sep 26 '11 at 16:21
• @rashid - You haven't asked a question here. Have you implemented this and it doesn't work? Are you confused on how to implement this from this point? – Kevin Vermeer Sep 26 '11 at 16:47
• sorry if I have not asked the question appropriately! Yes it does not work properly. The motor does not turn fwd/bwd. Hope this helps. – rashid Sep 26 '11 at 17:36

In order to make a mosfet "turn on", it's neccesary to raise the voltage between the gate and source above a certain limit; Whether that's a positive value or negative depends on if you have a P-Channel or N-Channel mosfet.

This part has two of each; For the N-Channel mosfets, the gate has to be driven to about 3V above the source, which would be the N1S/N2S pins on the package; If that pin is common to the ground on your microcontroller, you'll set the N1G or N2G pins High to turn the corresponding leg "on".

For P-Channel Mosfets, you have to bring the gate to a voltage that is lower than the source; which on this package is the P1S/P2S pin. If the source is common with the +5 voltage on your microcontroller; when the P1G or P2G pins are HIGH, then the leg is "off", turning them "on" means driving the pin LOW.

If you are hoping to run the h-bridge on a voltage that is greater than the rail-to-rail voltage of your microcontroller, you will need some much more clever circuit to deal with one of those voltages.

When setting an H-Bridge, it's important to make sure you're setting all of the legs in the right state and in the right order; For instance, if you turned on P1G and N1G legs at the same time, there would only be about 100mΩ of resistence between the positive and negative rails; This high current "shoot through" state will almost instantly burn up the H-Bridge.

To make matters worse, N-Channel Mosfets respond Much faster than P-Channel mosfets (electrons move faster than holes), so even if you set the gates properly, you might accidentally have a brief shoot through while the P-Channel switch is still turning off, but the N-Channel switch has already turned on. For this particular part, both mosfets turn on in about 2ns, but the P-Channel has an "off" delay of around 50ns, which is about three times longer than the N-Channel of 17ns. So to be on the safe side; your mosfet needs to wait around 55ns after turning one side off before turning the other side on.

The actual states you'll be interested in:

"Off" *
N1G = N2G = LOW
P1G = P2G = HIGH

"forward"
N1G = P1G = LOW
N2G = P2G = HIGH

"reverse"
N1G = P1G = HIGH
N2G = P2G = LOW

"stop" **
N1G = N2G = HIGH
P1G = P2G = HIGH


*: For the reasons stated above, you should always go "through" the "off" state, for at least 55ns when transitioning from any state to any other state; In order to make sure that these are the "default" state when the microcontroller is powering up, it's common to add pullup and pulldown resistors in the 100KΩ to 1MΩ range from gates to drains; It doesn't look like this feature is integrated into this part, so you'll need to add it yourself

**: The "stop" state works by passing the back emf generated by the moving motor through the N-Channel legs; The current flows freely so its energy can be absorbed by the part; this could also be achieved using all LOW values; which would shunt the back emf through the P-Channel, but in many casese, N-Channel mosfets are a bit more durable than P-Channel equivalents (which is reflected in this datasheet by a slightly lower on-state resistance).

• for stop N1G=N2G=HIGH?? – rashid Sep 26 '11 at 18:20
• yes, you turn them "on", both of the motor's leads are then connected to ground. Compare spinning the shaft of a motor with nothing connected to its leads and then try doing the same thing after connecting the two leads together with a jumper wire; Unconnected, the motor shaft spins freely, but shorted, the shaft becomes hard to turn, as if a brake were applied to it. the "brake" is actually the resistance of the motor's windings; back emf drives current through the closed circuit and the circuit resistance uses all that energy up. – SingleNegationElimination Sep 26 '11 at 18:32