Software techniques for building more efficient motor control designs -

Software techniques for building more efficient motor control designs


Small motors operating at less than 300 W are used in a wide variety of applications including automotive systems, printers, copiers, paper handlers, toys, factory automation, test equipment, robotics, aerospace and military, and many others. The most popular small motors types are DC, brushless DC, and stepper motors. The quantity of motors produced is roughly inversely proportional to the power level. Small motors are produced in much higher quantities than larger motors.

Motor control-specific DSPs are designed primarily to address the requirements of large off-line motors. Off-line motors are typically AC induction or brushless DC motors operating from 110 to 480 VAC and ranging from 1/4 to 100 HP. Motor control-specific DSPs are often too costly for small motors control systems.

This article will provide software examples of motor control designs that use C8051F3xx MCUs to illustrate their use in controlling various types of motors. While the examples are relatively simple, they demonstrate effective solutions for the various motor types. While a typical motor control system often requires additional features and higher functionality, these software examples may be used as a starting point for the development of more complex motor drive systems.

DC motor control
DC motors are the most common and least expensive of all small motors. In this article the term “DC motor” refers specifically to a brush-commutated, permanent-magnet motor. DC motors are used in a wide range of applications in the automotive, consumer and industrial market segments. Brushless DC (BLDC) motors promise improved reliability, reduced noise, and potentially lower cost. However, BLDC motors have only supplanted conventional DC motors in a few specialized high volume applications—disk drives and computer fans.

The characteristics of a DC motor make it the easiest motor to use in a variable-speed system. The torque speed characteristics are shown in Figure 1. The no-load speed of a DC motor is proportional to the voltage applied across the motor. The voltage-speed characteristics of a DC motor driving a constant-torque load, linear-load, or exponential-load are also continuous, positive-slope, and predictable. Thus, in most cases it is feasible to use open-loop control.

By simply varying the voltage across the motor, one can control the speed of the motor. Pulse width modulation (PWM) can be used to vary the voltage applied to the motor. The average voltage applied to the motor is proportional to the PWM duty cycle (ignoring the second order effects of the motor inductance and discontinuous operation).

Figure 1: DC motor characteristics

A basic example provides simple speed control of a DC motor using F3xx MCUs. This example reads the position of a potentiometer using the ADC and outputs a corresponding PWM signal using the PCA 8-bit PWM mode. The hardware configuration is illustrated in Figure 2 .

A single N-channel Power MOSFET Q1 is used to drive the DC motor. The Power MOSFET should be chosen for the particular motor voltage and current requirements. A free-wheeling diode D1 is connected across the DC motor. When the MOSFET is turned off, the current through the motor inductance will continue to flow. The MOSFET drain voltage will rise to one diode-drop above the motor supply voltage. The current will then flow through the free-wheeling diode.

Most low-voltage motor drive circuits employ Schottky power rectifiers for the free-wheel diode. Schottky rectifiers have a low forward voltage and a very fast reverse recovery time. Both are important factors in a motor drive application.

Figure 2: DC motor drive circuit

The power MOSFET is driven by an inverting gate driver. The port pins of ‘F300 are configured by default as inputs with a weak 100 kW pullup enabled. The port pins will remain high until the port is configured and the crossbar and peripherals are enabled. The port pins will also be configured as inputs with the weak pullup enabled while the reset pin is held low. By using an inverting driver, the power transistor will be off in the default state. If a non-inverting driver is used, a 10 kW pulldown resistor should be connected between the port pin and ground.

The gate driver should have a 3 V compatible input level threshold for use with a 3 V microcontroller. If the motor voltage is between 5 V and 15 V, the gate drive can be powered directly off the motor supply voltage. If the motor voltage is higher than 15 V, a separate gate drive supply voltage is needed, typically 5 V or 12 V. A logic-level power MOSFET should be used when working with a gate drive supply voltage below 10 V.

The software is very simple. The main() function initializes the clock, ports, and peripherals and enters the while(1) loop. The while(1) loop reads the value of the potentiometer voltage using the avgADC() function and outputs the value to the 8-bit PWM.

The PORT_Init() function configures the port I/O, peripherals, and enables the digital crossbar. Here, the output is enabled for the 8-bit PWM and a push-pull output for the gate drive.

The system clock, SYSCLK, is configured to operate at the maximum speed of 24.5 MHz which allows the 8-bit PWM a clock period of 160 ns and a frequency of 24 kHz.

The ADC0_Init() function configures the ADC for polled mode. The ADC gain is set to 1 and a conservative frequency of 1 MHz is chosen for the ADC clock. It is important to remember to also initialize the voltage reference and configure the ADC to use VDD for full-scale.

The function readADC() reads the voltage one time using polled mode and returns the ADC value. The function avgADC() calls the readADC() function and will return the average value of 64 samples. Averaging the ADC reading minimizes the effects of noise and reduces jitter in the PWM output.

When using the PCA 8-bit PWM mode, a value of 0x00 corresponds to a duty cycle of 100% and a value of 0xFF corresponds to a duty cycle of 0.39% at the CEX0 output. A duty cycle of 0% may be achieved by clearing the ECOM0 bit in PCA0CPM0 SFR.

When using an inverting driver, the relationship is reversed. A value of 0x00 corresponds to a 0% duty cycle and a value of 0xFF corresponds to a duty cycle of 99.6% on the MOSFET gate. All software examples in this white paper using 8-bit PWM are limited to 99.6% PWM for simplicity.

There are some cases where a 100% duty cycle is desirable. A 100% duty-cycle will effectively eliminate switching losses. Since the MOSFET never turns off, there are no switching losses in the MOSFET and no losses in the diode. The only power losses are conduction losses in the power MOSFET. If the motor is expected to run at full-speed most of the time, a maximum duty cycle of 100% is desirable. A duty cycle of 100% may be achieved by clearing the ECOM0 bit in PCA0CPM0 SFR.

Figure 3: DC motor full-bridge circuit

DC motor with reversing

Permanent-magnetDC motors are often used in applications that require the ability toreverse the direction of the motor. To reverse the direction ofrotation, it is necessary to reverse the polarity of the voltage on themotor. This requires the use of an H-Bridge. An H-Bridge has fourtransistors as shown in Figure 3 . When driving the motor in theforward direction, Q4 is turned on and a PWM signal is applied totransistor Q1. To drive the motor in the reverse direction, Q3 is turnedon and a PWM signal is applied to Q2. In this example, the lowertransistors are used for PWM speed control and the upper transistors areused for steering. Using this topology, it is possible to providevariable speed control in both directions.

In Figure 3, N-channelpower MOSFETs are used for the low-side transistors and P-channel powerMOSFETs are used for the high-side transistors. Using complementarypower MOSFETs is very cost effective solution for DC motor drives below20 V. As shown in Figure 3, the low-side gate drivers are inverting andthe high-side gate drivers are non-inverting. The gate driver polaritiesare chosen to ensure that the power transistors are off while the portpins are in the reset configuration with the weak pull-ups enabled.

Thesoftware for this example builds on the code of the initial example.The main loop now includes an if statement that checks the state of thereverse switch SW1. When the reverse button is pressed, the PWM isdisabled and all of the P0 outputs are disabled. When the button isreleased the motor will reverse directions.

The initialization functions are similar to Example 1, except that additional pins are configured as push-pull outputs.

The reverse() function is called to reverse the direction of the motor. A flag bit Fwd is used to save the state of the motor. The Fwd bit is toggled and then used to determine which outputs to activate.

Thereis a potential problem reversing the motor. While the reverse switchSW1 is held down the motor may continue to spin for some time due to theinertia of the motor. While the motor is turning, it will generate aback-emf proportional to the speed of the motor. If the reverse buttonis released before the motor stops spinning, the motor back-emf will beshorted out by the upper transistors as described below.

Referring to Figure 4 ,suppose Q4 is initially on and the motor is turning in the forwarddirection. Assume the motor is turning and the back-EMF is about 6 V.Now the switch is pressed and all four transistors are turned off. Theright side of the motor will be 6 V higher than the left side of themotor. Then the switch is released and Q3 is turned on. The left side ofthe motor is pulled up to the supply voltage and the back-emf of themotor is shorted by the internal diode of Q4.

The end result isthat the motor stops and all energy stored in the mechanical inertia ofthe motor is dumped into Q4. This could easily damage the uppertransistors during reversal. In some applications with a largefrictional load, a fixed delay may be adequate to ensure the motor hastime to stop. In other applications, the motor may take several secondsto come to a complete stop. A universal solution to this problem isillustrated by Figure 4 .

Figure 4: DC motor reversing hazard

Figure 5: DC motor drive with voltage sensing

DC motor with soft reversing
Thissoftware example for a DC motor builds on the second example andprovides soft reversing. To safely reverse a DC motor, it is necessaryto determine if the motor is still in motion.

A simple andeffective method to determine if the motor is still spinning is tomeasure the differential voltage across the motor terminals. The ADC canbe configured to measure the differential voltage between any twoinputs of the analog multiplexer. The programmable window detector mayalso be used to determine if the differential voltage has fallen withinpreset limits. In this example, the motor will reverse after thedifferential motor voltage remains below 3% of full scale for 100 ms.

Thehardware for a DC motor drive with voltage sensing is similar with theaddition of two resistor dividers connected to the motor terminals, asshown in Figure 5 .

The main loop has been modified to detect motor stop. The detectStop() function first configures the ADC to measure the differential voltage.The ADC and window detector are both used in polled mode. If the ADCvalue is within the preset window a counter is incremented. A 10 msdelay using timer T0 sets the sample time. Any sample outside the windowwill reset the counter. It will take 10 consecutive samples within thewindow before exiting the while loop. The detectStop() function will re-configure the ADC to measure the speed potentiometer before returning to the main loop.

Brushless DC motor control
BrushlessDC (BLDC) motors offer some advantages over conventionalbrush-commutated DC motors. The electronics and sensors effectivelyreplace the role of the brushes, offering long life, reducedmaintenance, and no brush noise. The torque-speed characteristics of aproperly commutated BLDC motor are identical to the DC motor as shown inFigure 1.

Thus, brushless DC motors exhibit the same desirablequalities that make DC motors so well suited for variable speed control.This example provides simple open-loop control of a BLDC motor usingHall-effect sensors to control the motor commutation. The speed of theBLDC motor is controlled using a simple potentiometer. Thecharacteristics of the BLDC motor controlled in this manner are similarto the original DC motor control example.

The hardware required for this example is illustrated in Figure 6 .Since additional outputs are required for a BLDC motor, the C8051F330was chosen. The motor is driven by six power transistors in athree-phase bridge configuration. The lower transistors Q1–3 areN-channel power MOSFETs. The upper three transistors are P-channel powerMOSFETs. This simplifies the gate drive arrangement. Again,complementary gate drivers are used so that the power transistors areoff in the default state.

Figure 6: Brushless DC motor drive

Halleffect sensors have open-collector outputs and require pull-upresistors. Check the motor specifications to ensure the Hall effectsensors are configured properly. The open-collector outputs are usually3 V compatible. However, the Hall effect sensors also require a biassupply that typically requires more than 3.0 V. In most systems, theHall effect sensors can be powered off the motor supply voltage or thegate drive supply voltage.

Debugging software with breakpointscan place the motor and MOSFET’s in an undesirable state. When the MCUhits a breakpoint, the pins are effectively frozen in time and may leavethe PWM outputs in the active state. The recommended procedure is toalways disconnect the motor leads before single stepping code or usingbreakpoints. A BLDC motor will stall with full voltage across onewinding. The BLDC motor stall current is only limited by the internalresistance of the winding. This will most likely damage the powerMOSFETs.

The software for the BLDC motor example contains many new elements as discussed below.

The PORT_Init() functionconfigures the crossbar and output pin assignments. The additionalcontrol pins are configured as push-pull outputs for the 3-phase controland inputs to read the hall sensors

The programmable counterarray time-base is configured to use the 160 ns time base, and thecounter is started. However, the Module 0 mode SFR is not initializedfor 8-bit PWM. No motor drive will be enabled until the Hall-effectposition is determined.

The main() functionfirst initializes everything and sets the start flag bit. The main loopfirst checks the position of the Hall-effect sensors using the hallPosition() function. If the start flag bit is set or the Hall position has changed, the motor is commutated by calling the commutate() function. Next the speed input is read and the speed setting is written to the PWM output.

The hallPosition() function returns a zero on an error condition. This occurs if theHall-effect inputs are all high or all low. If an error occurs, the mainloop disables all outputs by calling the coast() function. The start bit is also set on an error condition to force a commutation on the next valid Hall position reading.

The readHalls() function reads and debounces the Hall effect code on the Hall effectinput port pins. This function waits for three consecutive identicalreadings. This reduces the likelihood of an erroneous reading while theHall effect code is changing.

The hallPosition() first reads the Hall-effect code by calling the readHalls() function described above. The Hall code pattern is stored in the constant array hallPattern[] . A single line for loop with post decrement is used to find the corresponding index for the matching Hall effect code. The hallPosition() function returns a value 1 through 6 if it finds a matching pattern. If no match is found the hallPosition() function returns a zero value.

The commutate() function is used to initialize the outputs on start-up, to change thestate of the outputs when the Hall position changes, and to restart themotor after a Hall error has been corrected. The commutate() function first disables the PWM and the upper transistors. It then uses the index obtained from the hall Position() function.

Thereis no universal standard for the Hall-effect pattern or the commutationpattern. Consult the motor manufacturer’s data sheet for the particularmotor you are using. Carefully check both patterns against themanufacturer’s data sheet. Also, check the correspondence between theHall effect pattern and the commutation pattern. It may be necessary tochange the offset between the two patterns.

Ken Berringer is a member of the Staff Systems Engineering group at Silicon Labs.

1. Silicon Labs Application Note: AN191 Motor Control Software Examples .

2 thoughts on “Software techniques for building more efficient motor control designs

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.