st72141 bldc motor control software and flowchart example · st72141 bldc motor control software...

31
AN1083/1101 1/31 AN1083 APPLICATION NOTE ST72141 BLDC MOTOR CONTROL SOFTWARE AND FLOWCHART EXAMPLE by Microcontroller Division Applications INTRODUCTION The purpose of this application note is to give the flowcharts of the software examples showing how to drive a motor in both current and voltage modes and give examples of Open Loop or Closed Loop speed regulation. The software examples described in this application note are those generated by the ST7MTC2 Kanda kit for Sensorless mode. 80% of the code is generic, the remaining 20% is specific to the implementation of the ST72141 microcontroller (MCU) in the ST7MTC2 kit (user interface and communication data). Note: Please refer to Application Note 1321 for details concerning Sensor mode. The software examples given in the file attached with this application note illustrate Current mode and Closed Loop driving mode for a 4-pole, brushless DC (BLDC) motor. 1 DESCRIPTION OF SOURCE FILES The source files described in this section refer to the software provided in the file attached with this application note. – Aut_cc.asm is the main file and also contains the interrupt routines, – Sub_cc.asm contains the software subroutines, – Sub_cc.inc contains the subroutine declarations, – Cst_cc.asm and Cst_cc.inc contain the constant declarations, – ST72e141.asm and st72e141.inc are the map files. Paramcc.txt is a complete set of motor parameters. These parameters are declared in the ROM part of the ST72141 MCU (with an include file) and then transferred to the RAM part of the ST72141 MCU at the beginning of the main file. This is because the value of some param- eters can be changed while the motor is running. Most of these parameters are coded as indi- cated in the datasheet. Deframp.txt is the ramp table for the start-up sequence of the motor. It contains 38 consecu- tive decreasing step times required to accelerate the motor and to detect the first Back-EMF zero-crossing event. Def_prep.txt is the current ramp for the pre-positioning phase of the motor. It contains 25 steps of increasing current values required to set the rotor in a given position. 1 www.BDTIC.com/ST

Upload: dodung

Post on 24-Apr-2018

217 views

Category:

Documents


2 download

TRANSCRIPT

AN1083/1101 1/31

AN1083APPLICATION NOTE

ST72141 BLDC MOTOR CONTROL SOFTWARE ANDFLOWCHART EXAMPLE

by Microcontroller Division Applications

INTRODUCTION

The purpose of this application note is to give the flowcharts of the software examples showinghow to drive a motor in both current and voltage modes and give examples of Open Loop orClosed Loop speed regulation.

The software examples described in this application note are those generated by theST7MTC2 Kanda kit for Sensorless mode. 80% of the code is generic, the remaining 20% isspecific to the implementation of the ST72141 microcontroller (MCU) in the ST7MTC2 kit (userinterface and communication data).

Note: Please refer to Application Note 1321 for details concerning Sensor mode.

The software examples given in the file attached with this application note illustrate Currentmode and Closed Loop driving mode for a 4-pole, brushless DC (BLDC) motor.

1 DESCRIPTION OF SOURCE FILES

The source files described in this section refer to the software provided in the file attached withthis application note.

– Aut_cc.asm is the main file and also contains the interrupt routines,

– Sub_cc.asm contains the software subroutines,

– Sub_cc.inc contains the subroutine declarations,

– Cst_cc.asm and Cst_cc.inc contain the constant declarations,

– ST72e141.asm and st72e141.inc are the map files.

Paramcc.txt is a complete set of motor parameters. These parameters are declared in theROM part of the ST72141 MCU (with an include file) and then transferred to the RAM part ofthe ST72141 MCU at the beginning of the main file. This is because the value of some param-eters can be changed while the motor is running. Most of these parameters are coded as indi-cated in the datasheet.

Deframp.txt is the ramp table for the start-up sequence of the motor. It contains 38 consecu-tive decreasing step times required to accelerate the motor and to detect the first Back-EMFzero-crossing event.

Def_prep.txt is the current ramp for the pre-positioning phase of the motor. It contains 25 stepsof increasing current values required to set the rotor in a given position.

1

www.BDTIC.com/ST

2/31

Table of Contents

31

INTRODUCTION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1

1 DESCRIPTION OF SOURCE FILES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1

2 PERIPHERAL INITIALIZATION AND START-UP SEQUENCE . . . . . . . . . . . . . . . . . 6

3 MAIN PROGRAM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

4 THE THREE PRINCIPAL INTERRUPT ROUTINES (C, D AND Z) . . . . . . . . . . . . . . 12

4.1 FIRST PART OF C AND D INTERRUPT ROUTINES . . . . . . . . . . . . . . . . . . . . 12

4.2 BODY OF C INTERRUPT ROUTINE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13

4.3 SYNCHRONOUS MODE PART OF C INTERRUPT ROUTINE . . . . . . . . . . . . . 14

4.4 FIRST PART OF Z AND R INTERRUPT ROUTINES . . . . . . . . . . . . . . . . . . . . 16

4.5 BODY OF Z INTERRUPT ROUTINE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17

4.6 TRANSITION FROM SYNCHRONOUS TO AUTO-SWITCHED MODE . . . . . . 18

4.7 AUTO-SWITCHED MODE PART OF C INTERRUPT ROUTINE . . . . . . . . . . . 19

4.8 LAST PART OF C INTERRUPT ROUTINE . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20

5 SPECIAL FEATURES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22

5.1 R INTERRUPT (MTIM TIMER RATIO CHANGE) . . . . . . . . . . . . . . . . . . . . . . . 22

5.2 SOFTWARE DEMAGNETIZATION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24

5.3 O AND E INTERRUPT ROUTINES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29

6 SUMMARY . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30

2

www.BDTIC.com/ST

3/31

ST72141 BLDC MOTOR CONTROL SOFTWARE AND FLOWCHART EXAMPLE

The Start/Stop order of the motor is given by pin PB0 on the microcontroller board in theKanda Kit. Do not forget to unplug the communication cable from the microcontroller board toensure the correct behaviour of pin PB0.

This software (except for the declaration of motor parameters) is for the Current mode andClosed Loop Driving mode in the third stage of the ST7MTC2 Motor Control Evaluation Kit.

Figure 1. BLDC Control Strategy

Back-EMF Zero crossing?

Start Motor in Synchronous mode

No Yes

End of Ramp up?

NoYes

Motor in Auto-switched Mode

End of Stabilization

Phase?

Valid Speed Loop

Target Speed

crossing?

No Yes

Allow Speed Loop

Stop?

Stop

No

Yes

NoYes

End of Forced Synchronous

Mode?

Yes

No

www.BDTIC.com/ST

4/31

ST72141 BLDC MOTOR CONTROL SOFTWARE AND FLOWCHART EXAMPLE

Figure 1 gives a general overview of the control software after receiving an order to start themotor.

After the start order, the motor begins to run in Forced Synchronous mode. Forced Synchro-nous mode is the first part of Synchronous (switched) mode. It is the motor start-up se-quence. Please refer to Application Note 1276 “BLDC Motor Start Routine for ST72141 Micro-controllers” for details concerning the start-up phase of the motor. During Synchronous(switched) mode, the rotor is set to a given position (pre-positioning phase) and then de-creasing step times are imposed in order to accelerate the motor and to detect the first Back-EMF zero crossing event (Z event). The current is also imposed. This succession of de-creasing step times is referred to as the motor ramp-up.

Forced Synchronous mode corresponds to the first few steps of the ramp-up before the Zevent interrupt is enabled. The number of steps in Forced Synchronous mode (with Z inter-rupts disabled) is chosen by the software programmer.

After these few steps in Forced Synchronous mode, the Z event interrupt is enabled and themotor is still in the ramp-up, or start-up, phase and switches to Synchronous (switched) mode.The step time and current are still imposed. A Z event has not yet been detected.

If Z event is not detected before the end of the ramp-up, the motor is stopped.

When the first Z event is detected in Synchronous (switched) mode, the user can choose tohave 2 or more consecutive Z events detected before setting the motor in Auto-switched run-ning mode.

Once the motor is in Auto-switched mode, it must wait for the end of the stabilization phase be-fore entering the speed regulation loop. For example, in Closed Loop driving mode, the stabi-lization phase consists of a few steps between the first step in Auto-switched mode and en-tering the speed regulation loop. The number of steps in the stabilization phase is chosen bythe software programmer.

In summary, after receiving the start order, the motor starts in Forced Synchronous mode,then enters Synchronous (switched) mode when the Z event interrupt is enabled by the soft-ware. The motor will then enter Auto-switched mode when 2 or more consecutive Z events aredetected.

www.BDTIC.com/ST

5/31

ST72141 BLDC MOTOR CONTROL SOFTWARE AND FLOWCHART EXAMPLE

Figure 2. Start-up Sequence Summary Example

In this application note, the flowchart illustrates the start-up sequence of the motor and thetransition between Synchronous and Auto-switched modes. First, the three main interrupt rou-tines are described (C, D, Z events), and then other features are described in Section 5 of thisdocument.

Other special cases are presented at the end of this application note.

Note: All the names of the software routines presented in this document are strictly the same as in the code generated in the ST7MTC2 kit.

StartOrder

Forced Synchronous mode

Synchronous (switched) mode

Synchronous (switched) mode

Z event interrupt enabledDetection of 2 or moreconsecutive Z events

Auto-switched mode

Pre-positioning Phase StabilizationRamp-up Phase Speed Regulation LoopPhase

www.BDTIC.com/ST

6/31

ST72141 BLDC MOTOR CONTROL SOFTWARE AND FLOWCHART EXAMPLE

2 PERIPHERAL INITIALIZATION AND START-UP SEQUENCE

Figure 3. ST72141 Start-up Sequence Flowchart

Before starting the motor, a basic initialization of the peripheral is performed by the software(Figure 4). Once the peripheral is initialized, the motor waits for the start order.

When the start order is received, the Motor Control (MTC) peripheral is initialized (Figure 5)according to the selected driving mode and motor parameters. The motor then starts in Syn-chronous (switched) mode and the software switches to the main program (Figure 7).

In Forced Synchronous mode, only the C (commutation) event interrupt is enabled. Afterswitching into Synchronous (switched) mode, the Z event interrupt is also enabled. The D

Reset

START Order?

Yes

Main Program

Initialization ofPeripheral

Basic Initialization ofMotor Control Peripheral

Motor Control PeripheralInitialization

Start Motor in

Enable IT on CSynchronous mode and

for Step update

Start

No

www.BDTIC.com/ST

7/31

ST72141 BLDC MOTOR CONTROL SOFTWARE AND FLOWCHART EXAMPLE

(demagnetization) event interrupt can be disabled in both Forced Synchronous and Syn-chronous (switched) modes. Otherwise, it is software demagnetization only.

Figure 4. Basic Initialization Phase of the Motor Control Peripheral

Figure 4 shows the basic initialization phase of the peripheral performed by the software be-fore the detection loop for the motor start-up order can be enabled.

The following actions takes place during the basic initialization phase:

– The Motor Control Peripheral is reset (by setting a control bit in the MCRA register).

– All outputs are set to high impedance and to OFF states.

– The channel polarity is selected (by the MPOL register).

– The internal clock frequency (current mode) is selected as well as the MTIM internal 8-bittimer ratio with the MPRSR register.

– The clock is disabled.

Basic Initialization

Reset MTC Peripheral & MCOx in Hiz

MPOL RegisterSelect "OFF" time

and Channel Polarity

MCES Pin(Emergency Stop)?

MPHST Register resetSet PWM Frequency & MTIM Clock with

MPRSR Register

MCRA Register Clock disable

(MOE =1 & DAC =1)MCOx in "OFF" state

No

Yes

www.BDTIC.com/ST

8/31

ST72141 BLDC MOTOR CONTROL SOFTWARE AND FLOWCHART EXAMPLE

Once the basic initialization is completed and the motor start order is detected, the motor con-trol peripheral is initialized according to the selected driving mode and motor parameters.

Figure 5. Motor Control Peripheral Initialization Phase

When the start order is received:

– All the flags present in the RAM variables are reset, as well as the status_step variable(RAM variable).

– The pre-positioning phase is executed in order to place the rotor in a known position. Referto Application Note 1276 for more information.

– The motor starts in Synchronous (switched) mode according to the step time in theMCOMP register due to the acceleration ramp table.

The step time is imposed by the ramp table in the software. (The ramp table is the successionof decreasing step times used to accelerate the motor during the start-up sequence). The firststep time is loaded in the MCOMP register (in Synchronous mode, the MCOMP register con-tains the step time).

MTC Peripheral Initialization

Reset all Flags and Status_step variable

MPAR Register SelectZ & D edge polarityChannel to read Z

Channel Parity

Load MCOMP with first step timeLoad MDREG with Software Demagnetization time

Initialize MCRA / MCRB and output first step word

Set IREF and start MTIM

End of Pre-positioning

Phase?

Ramp-up

No Yes

www.BDTIC.com/ST

9/31

ST72141 BLDC MOTOR CONTROL SOFTWARE AND FLOWCHART EXAMPLE

The MDREG register is loaded with the Software Demagnetization time. In Synchronous(switched) mode, all the Demagnetization (D) events are simulated by software. There are nohardware End of Demagnetization events in Synchronous (switched) mode.

The Z event and D event edge polarities are selected using the ZVD and CPB bits.

The MCRA and MCRB registers are initialized when the driving mode (current or voltagemode with or without sensors) is selected and all the motor parameters are downloaded in thesoftware.

The reference current or voltage for the ramp table is set and the internal 8-bit timer is startedby enabling the clock.

In summary, once the Pre-positioning phase is completed, the software goes through theramp table (ramp-up phase shown in Figure 6) using the decreasing imposed step times to ac-celerate the motor.

Figure 6. Synchronous Starting Ramp-up

Figure 6 shows what the software does in each step during the ramp up. As the MPHST andMCRB registers have preload registers (see Application Note 1082), in each step the data forthe next step is prepared and entered in the preload register. This data will be taken in accountand loaded in the active registers when the C event is detected.

In Forced Synchronous mode, only the C event interrupt is enabled. After a few steps in theramp table, the Z event interrupt will also be enabled.

Ramp-up

Prepare next Step Configuration

Load preload Register for next C eventMPHST with phase word

MCRB with Phase comparator edge, Demagnetization Mode and PWM orientation

Enable IT on C event

and update the MISR Registerfor clock radio action on C event.

www.BDTIC.com/ST

10/31

ST72141 BLDC MOTOR CONTROL SOFTWARE AND FLOWCHART EXAMPLE

3 MAIN PROGRAM

Figure 7. Main Program Flowchart

Figure 7 shows what is done in the main program. The main program continuously checks forthe stop order. This is the only operation that is carried out until the motor enters the Auto-switched mode. If the motor is in Synchronous (switched) mode, the main program will be in-terrupted by a C event and a Z event. A D event interrupt is not enabled in Synchronous(switched) mode.

If the motor is in Auto-switched mode, the software waits until one mechanical rotation is com-pleted (this is the stabilization phase, where the number of steps of the stabilization phase isdecided by the software programmer). In Auto-switched mode, the main program can be inter-

Main Program

STOP order?

Stop the MOTOR

Start

Update IREF

No

Yes

Yes

Yes

No

No

No

Compute?

Compute the New Reference Current to

reach the Targeted Speed

Auto-SwitchedMode?

One MechanicalRotation Completed?

NoYes

UpdateIREF?

www.BDTIC.com/ST

11/31

ST72141 BLDC MOTOR CONTROL SOFTWARE AND FLOWCHART EXAMPLE

rupted by all the events enabled by the software programmer. This will be described in Section5.2 of this document.

The dotted line indicates what is done in the main program in Closed Loop speed regulationmode. After the stabilization phase, the main program checks if there is a need to computeand update a new reference current or voltage in order to keep the motor running at the targetspeed fixed by the user.

If the motor is still in Synchronous (switched) mode, the software waits for a C event interrupt(Figure 8) before computing the reference current or voltage.

www.BDTIC.com/ST

12/31

ST72141 BLDC MOTOR CONTROL SOFTWARE AND FLOWCHART EXAMPLE

4 THE THREE PRINCIPAL INTERRUPT ROUTINES (C, D AND Z)

4.1 FIRST PART OF C AND D INTERRUPT ROUTINES

When a C event or a D event occurs, the software reads the MISR register and stores the in-terrupt flags in a RAM variable (step_isr) before resetting these flags in the MISR register.

Figure 8. C Event and D Event Interrupt Routines

The RAM variable is then read in order to know what flags have been set. If the commutationflag is set (CI flag), the software enters the C interrupt routine (Is_C_IT shown in Figure 9). Ifthis flag is not set, the software checks if the End of Demagnetization flag is set (DI flag). If theDI flag is set, the software enters the End of Demagnetization event interrupt routine. In this in-terrupt routine, only the flag reset operation is performed. Afterwards, the software returns tothe main program and waits for an interrupt. Figure 9 shows what is done when the commuta-tion event interrupt flag (CI) is set.

C or D IT

Read MISR

Memorize CI & DI bit in step _isrReset these flags in MISR

Reset DI in step_isr

IRET

Reset DI flag in MISR register

Is_C_IT

No

Yes

No

Yes

End_IT_CD

CI set?

DI set?

www.BDTIC.com/ST

13/31

ST72141 BLDC MOTOR CONTROL SOFTWARE AND FLOWCHART EXAMPLE

4.2 BODY OF C INTERRUPT ROUTINE

Figure 9. Overview of the C Event Interrupt Routine

Regardless of the motor driving mode or sequence during a C event interrupt routine, the De-magnetization time in the MDREG register is stored in a RAM variable. The value of theMCOMP register is also stored. Then, to avoid a parasitic Demagnetization when the interruptroutine is executed, a value less than the value in the MCOMP register is loaded in theMDREG register (meaning that the internal timer has already at that time a value greater thanthe value loaded in the MDREG register. This will prevent a Demagnetization interrupt fromoccurring while this part of the interrupt routine is executed).

The interrupt flags are reset and the MCRB and MPHST preload registers are updated to pre-pare the data for the next C event.

temdreg = MDREGtemcomp = MCOMPMDREG = MCOMP-2

Reset CI and ZI in step_isrReset R-plus flag

Update MPHST & MCRBpreload registers

Auto-switched Mode?

First Auto-switched Step?

Last Switched Step?

C-action-autosw

Set-ki-autosw

Val-autosw

Synchronous Mode

NoYes

No

Yes

No

Yes

Is_C_IT

www.BDTIC.com/ST

14/31

ST72141 BLDC MOTOR CONTROL SOFTWARE AND FLOWCHART EXAMPLE

Once the targeted number of consecutive Z events (set by the software programmer) is de-tected in Synchronous (switched) mode, the fact that the motor is in its last step in Synchro-nous (switched) mode is memorized (Figure 12).

When the next C event is detected, the software will memorize that the motor is in the first stepin Auto-switched mode.

Then the C event interrupt routine is sub-divided to handle the several cases when the inter-rupt occurs:

– The motor is still in synchronous (switched) mode

– The motor is on the last step in synchronous (switched) mode

– The motor is in the first step in Auto-switched mode

– The motor is already in Auto-switched mode

Let’s take each case independently.

In the first case, the motor is in Synchronous (switched) mode when a C event is detected.When this occurs, the software enters the C event interrupt routine and goes to the Synchro-nous (switched) mode routine (Figure 10).

In the second case, the motor is in its last step in Synchronous (switched) mode (positionmemorized during the previous Z interrupt routine) when the C event interrupt is detected.When this occurs, the software goes to the val_autosw routine (Figure 13). In this routine, thesoftware memorizes that the motor is in its first step in Auto-switched mode.

In the third case, the motor is in its first step in Auto-switched mode when the C event interruptis detected. When this occurs, the software goes to the Set_ki_autosw routine (Figure 14).

In the last case, the motor is in Auto-switched mode when the C event interrupt is detected.When this occurs, the software goes to the C_action_autosw routine (Figure 14).

4.3 SYNCHRONOUS MODE PART OF C INTERRUPT ROUTINE

In the event that the motor is still in Synchronous (switched) mode when the C event interruptoccurs, the software goes to the Synchronous mode routine (Figure 10).

When a commutation (C) event occurs, the motor is in Synchronous (switched) mode, so thesoftware must enter the synchronous routine in the C event interrupt routine.

The MDREG, MCOMP and MISR registers are updated for this step. The data for the next Cevent is prepared and memorized in RAM variables. The data will be loaded in the preloadregister one step before it is used.

The software checks if the motor is at the end of the ramp table. If this is the case, the motormust be stopped because it is not yet in Auto-switched mode. Normally, the motor entersAuto-switched mode after only a few steps.

www.BDTIC.com/ST

15/31

ST72141 BLDC MOTOR CONTROL SOFTWARE AND FLOWCHART EXAMPLE

Figure 10. Commutation Interrupt in Synchronous (switched) Mode

If the motor is not at the end of the ramp table, the software checks if it is at the end of ForcedSynchronous mode. If this is the case, the Z event interrupt is enabled.

All the register values are memorized by the software which also checks if a Z event has oc-curred in the previous step. When a Z event is detected, a counter is incremented until thenumber of consecutive Z events reaches the target number set by the software programmer.When this number is reached, the motor switches to Auto-switched mode and the software re-turns to the main program (Figure 7) and waits for the next interrupt.

Synchronous Mode

MDREG = mem-mdreg MCOMP = mem-mcomp

MISR = mem-misr

Prepare Data for next C eventmem-phst, mem-mcrb

End of Ramp-up?

STOP

Z IT enabled

Load memory of registermem-comp with step time

mem-ratio with clock mem-mdreg with soft demagn time

Consecutive Z?

Increment Z counter

IRET

NoYes

No

Yes

No

No

Yes

Yes

End of Forced Synchronous

mode?

Z event?

Clear Z counter

www.BDTIC.com/ST

16/31

ST72141 BLDC MOTOR CONTROL SOFTWARE AND FLOWCHART EXAMPLE

In the event that the motor is in Synchronous (switched) mode and the Z event interrupt hasbeen enabled, the next interrupt in the main program could be a Z event and the software willgo through the Z event interrupt routine (Figure 11).

Note that we are assuming for the moment that we are in Synchronous (switched) mode,therefore only the C and Z event interrupts have been enabled by software.

4.4 FIRST PART OF Z AND R INTERRUPT ROUTINES

Figure 11 shows the beginning of the R (Ratio) and Z (Back- EMF zero crossing) interrupt rou-tines. These routines read the interrupt flags, store them in a RAM variable and then reset theflags. If the R event (ratio) flag has been set, the software enters the R event interrupt routinedepending on whether it is an R- or an R+ event. This is explained in Section 5 at the end ofthis application note (Figure 17 and Figure 18). If the Z event flag (ZI) has been set in theMISR register, the software goes through the Z event interrupt routine (BEMF_Z) (Figure 12).Otherwise, the software returns to the main program and waits for the next interrupt.

Figure 11. R (Ratio) and Z (Back-EMF) Interrupt Routine

R or Z IT

Read MISR

Memorize RPI, RMI & ZI bits in step _isrReset these flags in MISR

No

Yes

NoYes

IRET

Timer_ovf

Z_55

BEMF_Z

No

Yes

No

Yes

RIM set?

RMI set?

RPI set?

ZI set?

Valid RPI & RMI in step_isr

IT_mng

www.BDTIC.com/ST

17/31

ST72141 BLDC MOTOR CONTROL SOFTWARE AND FLOWCHART EXAMPLE

4.5 BODY OF Z INTERRUPT ROUTINE

In Auto-switched mode, no actions are performed in the Z event interrupt routine. An interruptis generated and the software just returns to the main program (Figure 7). If the motor is stillin Synchronous (switched) mode, the zero crossing times (current and previous) are memo-rized in two RAM variables and the counter of consecutive Z events is incremented. If thenumber of consecutive zero-crossing events is equal to the target number fixed by the soft-ware programmer, the software memorizes that the motor is in its last step in Synchronous(switched) mode.

Figure 12. Z Event Interrupt Routine

BEMF_Z

Restore Ratio update

Auto-switched mode?Yes

No

BEMF_mem = BEMF_n

BEMF_n = MZREG

Increment nbemf

nbemf = target?

Yes

No

Z event is centered on last step in Synchronous mode

MCOMP = MZREG *2

Memorize last step in Synchronous mode

Closed loop mode?

Yes

No

Initialize all closed loop parameter

IRETVR02141K

www.BDTIC.com/ST

18/31

ST72141 BLDC MOTOR CONTROL SOFTWARE AND FLOWCHART EXAMPLE

The Z event time is centered on the current step, meaning that the value entered in theMCOMP register (step time in Synchronous mode) is twice the value in the MZREG register.

If the motor is in Closed Loop mode (speed regulation), the Closed Loop parameters are ini-tialized at this point in the program.

The software then returns to the main program (Figure 7) and waits for the next interrupt whichwill be a C event. As shown in Figure 9, if the software has memorized that the motor is in itslast step in Synchronous (switched) mode, the program will enter the Val_autosw routinewhen the next C event is detected (Figure 13).

4.6 TRANSITION FROM SYNCHRONOUS TO AUTO-SWITCHED MODE

Figure 13. Val_autosw Routine

In this case, the motor is at the last step of the Synchronous (switched) mode and the softwarehas entered the corresponding C event interrupt routine (see Figure 9). Auto-switched modeis enabled and the maximum value is loaded in the MCOMP register to prevent parasitic Cevents from occurring during the enabling routine.

The SWA bit in the MCRA register is set to enable Auto-switched mode. When this step iscompleted, the internal 8-bit timer MTIM will be reset on a Z event.

The software memorizes that the motor is in its first step in Auto-switched mode.

The demagnetization selection for the first auto-switched step is restored and all the interruptsare enabled as the motor enters Auto-switched mode.

Val-autosw

Set SWA bit in MCRA

MDREG = mem-mdreg MCOMP = $FF

Memorize first auto-switched step

Restore demagnetization selection for first auto-switched step

Enable IT on C, D, Z, E and R

IRET

Prepare Data for next C event

www.BDTIC.com/ST

19/31

ST72141 BLDC MOTOR CONTROL SOFTWARE AND FLOWCHART EXAMPLE

The E event and R event interrupts are described in Section 5 of this document (see Figure17, Figure 18 and Figure 22).

The data for the next commutation event is prepared and memorized so that it may be loadedin the preload registers.

Once this data is stored in the preload registers, the software returns to the main program andwaits for the next interrupt which will be a D event. But as shown in Figure 8, when a D eventis detected, the interrupt routine only stores the DI bit and clears the MISCR flags before re-turning to the main program and waiting for the next interrupt.

The next interrupt will be a Z event, but as the motor is now in Auto-switched mode (first stepin this mode), this interrupt routine does not perform any actions either (as shown in Figure12). The software will again return to the main program and wait for the next interrupt whichwill be a C event. Now, the software has memorized that the motor is in its first step in Auto-switched mode, so as shown in Figure 9, the program will enter the Set_ki_autosw routine(Figure 14).

4.7 AUTO-SWITCHED MODE PART OF C INTERRUPT ROUTINE

Figure 14. Commutation Event Routine (first step in Auto-switched mode)

Set-ki-autosw

Enable Auto-switched modeReset memory of change of ratio

MWGHT= selected Delay

C-action-autosw

Read current ratio of TIMERbemf_n+1 = MZREG

Save MCOMP for next Software Demagnetization

Hardware Demagnetization?

End-C_action SDM-autosw

No

Yes

www.BDTIC.com/ST

20/31

ST72141 BLDC MOTOR CONTROL SOFTWARE AND FLOWCHART EXAMPLE

In this case, Auto-switched mode is enabled and the MWGHT register is loaded with the delaycoefficient. This means that the MCOMP register contains the value of the real delay (time ineach step between the zero crossing event and the next commutation event).

Note: For more information concerning the real delay value, refer to Application Note 1082.

The Z event time is memorized and then depending on the step configuration (falling or risingedge of the end of Demagnetization event), there will be a software or a hardware Demagnet-ization. Software Demagnetization is a special case that is described in more detail in Section5.2 of this document (Figure 19 and Figure 20). If there is a hardware Demagnetization, thesoftware enters the End_C_action routine where the data for the next commutation event isprepared. The software then goes to the C_action_end routine (Figure 15).

Note: For more information concerning the step configuration, refer to Application Note 1130.

4.8 LAST PART OF C INTERRUPT ROUTINE

Figure 15. C_action_end Routine

In this case, the execution of the routine depends on whether the motor is running in OpenLoop or Closed Loop mode. If the motor is running in Closed Loop (speed regulation), the soft-

C_action_end

NoYes

No

Yes

Step_cpt = Step_cpt - 1

Step_cpt = 0?

Step_cpt = $80 End_IT_CD

Crossing

No

Yes

Closed Loop?

End of stabilization after entering Auto-switched mode?

(Step_cpt = $80)

www.BDTIC.com/ST

21/31

ST72141 BLDC MOTOR CONTROL SOFTWARE AND FLOWCHART EXAMPLE

ware checks if the stabilization phase is completed (fixed number of steps in Auto-switchedmode before entering the speed regulation loop).

If the stabilization phase is not finished, the value of the counter for the number of steps inAuto-switched mode is incremented and the software goes to the End_IT_CD routine (Figure8) before returning to the main program.

If the stabilization phase is completed, the software goes to the crossing routine (Figure 16) tocheck if the target speed of the motor has been crossed.

If the motor is running in Open Loop mode, the software just exits the C interrupt routine.

Note that the number of steps for the stabilization phase is fixed by the software programmerin a RAM variable. Step_cpt acts like a flag and $80 is an arbitrary value set by the softwareprogrammer.

Figure 16. Crossing Routine

In this routine, the software checks if the motor has already crossed the target speed. If themotor has not crossed the target speed, the software exits the C interrupt routine. (This routineis only available in Closed Loop mode.)

If the motor has crossed the target speed, the software checks if the speed has to be updatedor if the step time average has to be computed by the speed regulation subroutine. After-wards, the software returns to the main program.

End_IT_CD

No

Yes

Reset Average memory

No

Yes

No

YesSpeed Update?

End of Average of Step time?

Target speed crossing?(Crossing = $80)

Crossing

www.BDTIC.com/ST

22/31

ST72141 BLDC MOTOR CONTROL SOFTWARE AND FLOWCHART EXAMPLE

As the motor is now in Auto-switched mode, the same path is always followed by the software:

– Main program: software waits for an interrupt

– C event interrupt is detected: the software enters the C_action_autosw (Figure 14) routinethen returns to the main program

– D event interrupt is detected: nothing is done in the software

– Z event interrupt is detected: nothing is done in the software on a Z event in Auto-switchedmode

However, C, D and Z events are not the only events that can provoke an interrupt. In Auto-switched mode, R (ratio) events, E (emergency stop) events and O (delay multiplication over-flow) events are all able to generate interrupts. These cases and others such as software De-magnetization and setting the carry bit after a computation are all managed by the software.They are described in the last part of this document.

5 SPECIAL FEATURES

5.1 R INTERRUPT (MTIM TIMER RATIO CHANGE)

There are two types of R event interrupts; R+ event and R- event. The type of interrupt de-pends on the value of the MTIM timer counter when the event is detected.

Note: For more information concerning R event interrupts, refer to Application Note 1082.

If a Z event is detected while the MTIM timer counter is between 55h and FFh, the event isprocessed normally.

If the timer reaches FFh before the Z or D event is detected, an R+ event (timer overflow) oc-curs. In this case, the clock needs to be slowed down, so the ratio is increased.

If a Z event is detected before the timer has reached the value 55h, an R- event (timer under-flow) occurs. In this case, the clock needs to be accelerated, so the ratio is decreased.

Figure 17 and Figure 18 show what is done by the software when R+ or an R- event occurs.

As shown in Figure 11, when an R event is detected, the software checks the interrupt flags todetermine if it is an R+ or an R- event.

If RMI flag is set, it is an R- event and the software goes to the Z_55 routine (Figure 17).

If RPI flag is set, it is an R+ event and the software goes to the Timer_ovf routine (Figure 18).

www.BDTIC.com/ST

23/31

ST72141 BLDC MOTOR CONTROL SOFTWARE AND FLOWCHART EXAMPLE

Figure 17. Z_55: Internal 8-bit Timer Underflow

Figure 17 shows what takes place in the event of a timer underflow (an R- event). For all prac-tical purposes, the timer ratio is automatically decreased by the microcontroller. Therefore, allthe values are multiplied by two for the current step in order to avoid computation errors.

This means that the previous Zn and Z times are multiplied by 2, as well as the Demagnetiza-tion time value in the MDREG register.

If the carry is set during the computation, the maximum value is stored in the MCOMP register.

Then the software returns to the IT_mng routine (Figure 11), checks again for interrupt flagsand returns to the main program and waits for another interrupt.

Figure 18 shows what is done when a timer overflow occurs (an R+ event). For all practicalpurposes, the timer ratio is automatically increased by the microcontroller. Therefore, all thevalues are divided by two for the current step in order to avoid computation errors.

This means that the previous Zn and Z times are divided by 2, as well as the Demagnetizationtime value in the MDREG register.

The timer is reset to its middle value, i.e. 7Fh.

After division, if the value in the MCOMP register is equal to zero, the software stores a min-imum value of 1 in the register.

Then the software checks again for the R+ event flag and returns to the IT_mng routine(Figure 11).

SLL of mem_mcompfor computation of Software Demagnetization

No

Yes Carry set?

mem_mcomp = $FF

IT_mng

Z_55

www.BDTIC.com/ST

24/31

ST72141 BLDC MOTOR CONTROL SOFTWARE AND FLOWCHART EXAMPLE

Figure 18. Timer_ovf: Internal 8-bit Timer Overflow

5.2 SOFTWARE DEMAGNETIZATION

In the software included with this application note, if Auto-switched mode is enabled, everysecond step will use software demagnetization. Note that this is a software example and thatthe fact that every second step uses software demagnetization can be changed.

Therefore, each step is checked by software for hardware or software demagnetization (setusing the HDM or SDM bits in the MCRB register). This is shown in Figure 14.

Figure 19 and Figure 20 show what is done by the software in the event of software demag-netization.

Figure 19 shows the first part of the software demagnetization. In the event that a software de-magnetization takes place in the current step (step n), a hardware demagnetization must havetaken place in the previous step (step n-1). Therefore, Dn-1 was a hardware demagnetization.

Timer_ovf

SRL of mem_mcompfor computation of Software Demagnetization

No

Yesmem_mcomp = 0?

mem_mcomp = 1

Flag R_plus = 0?

R_plus = R_plus - 1

Flag R_plus = 0?

MDREG = mem_mdreg

IT_mng

Yes

No

Yes

No

VR02140Q

www.BDTIC.com/ST

25/31

ST72141 BLDC MOTOR CONTROL SOFTWARE AND FLOWCHART EXAMPLE

The values in the MDREG (n-1) register for the hardware demagnetization and theMCOMP(n-1) register for the computed delay have been memorized.

The value in the MDREG (n-1) register represents the time between the previous Z event (Z n-2) and the End of Demagnetization event at step n-1 (Dn-1).

The real hardware demagnetization time is the time between the commutation event (n-1) andthe End of Demagnetization event (n-1). So, the first thing done by the software is to computethe value of the real hardware demagnetization (HDM) event from the previous step:

HDM (n-1) = MDREG(n-1)-MCOMP(n-1)

Then the software checks if this value is greater than 0. If not, a minimum value of 1 is set.Then, to compute the software demagnetization time for the current step (step n), the hard-ware Demagnetization time is multiplied by a correction factor (chosen arbitrarily by the soft-ware programmer) to ensure that the software Demagnetization time for the current step willbe greater than the hardware demagnetization time from the previous step (for safety pur-poses to prevent the End of Demagnetization event from occurring too soon):

SDM (software Demagnetization value) = HDM*1.25

Then the software checks if this value is greater than 255. If yes, this value is forced to 255.

The software then checks if the value of the software Demagnetization time is greater than thetime needed to process the interrupt routine (IT_time). If not, this interrupt processing time isset as a minimum value.

This is done because if the value in the internal 8-bit timer is greater than the value of the soft-ware Demagnetization when the routine is completed, the End of Demagnetization event willnever occur since the value of the software Demagnetization would have been alreadyreached by the internal timer before the end of the routine.

Then, the value of the delay between the Zn-1 event and the next commutation event Cn isadded in order to obtain the time between the Zn-1 event and the software End of Demagnet-ization event.

If the carry has been set by these computations, the software goes to the carry-set routine(Figure 21). Otherwise, the software Demagnetization routine continues (Figure 20).

www.BDTIC.com/ST

26/31

ST72141 BLDC MOTOR CONTROL SOFTWARE AND FLOWCHART EXAMPLE

Figure 19. Software Demagnetization (First Part)

The software adds another correction factor to the software Demagnetization time corre-sponding to the time needed to load the register. If the carry has been set, the program goesto the carry_set routine. Otherwise, the actual value for the software End of Demagnetizationevent is memorized and the software checks if this value is greater than the current value ofthe MTIM timer.

If the value set for the software End of Demagnetization event is greater than the current valueof the MTIM timer, a value greater than that of the MTIM timer is set for the software Demag-netization time.

SDM-autosw

HDM = MDREG(n-1) - MCOMP (n-1)

HDM > 0? No

YesHDM =1

SDM =HDM * 1.25

SDM > 255?

SDM =255

SDM > IT_time Update?

SDM =SDM + MCOMP (n)

Carry?

Carry_set

B

No

Yes

No

Yes

No

Yes

Zn-2 Zn-1 Zn

MCOMP(n-1)

HDM

MDREG(n-1)

MDREG(n-1)-MCOMP(n-1)=HDM

Cn-1

Dn-1

Cn

Dn

IT_time_low

SDM =IT_time

www.BDTIC.com/ST

27/31

ST72141 BLDC MOTOR CONTROL SOFTWARE AND FLOWCHART EXAMPLE

Figure 20. Software Demagnetization (Second Part)

If the value set for the software End of Demagnetization event is less than the current value ofthe MTIM timer, the software checks if the flag for an R+ event is set.

If the flag for an R+ event is set, the flag is reset and the software returns to the beginning ofthe C interrupt routine. Otherwise, the value is written in MDREG register, then the softwaregoes to the End_C_action routine to prepare the data for the next C event and then returns tothe main program.

If the carry has been set during the computation for the software End of Demagnetizationevent, the software enters the carry_set routine as shown in Figure 21.

SDM =SDM + (timer_table)

Carry?

Carry_set No

Yes

MTIM > SDM?NoYes

mem_mdreg = SDM

SDM = MTIM + (timer_table)

Carry?

Carry_set

No

mem_mdreg = SDM

Read MISR

Is R+?

Reset RPI in MISR

SRL MCOMP

Write MDREG

End-C_action

No

Yes

Yes

Is_C_IT

B

www.BDTIC.com/ST

28/31

ST72141 BLDC MOTOR CONTROL SOFTWARE AND FLOWCHART EXAMPLE

Figure 21. Carry Set Routine Flowchart

If the carry has been set, the software End of Demagnetization event will occur after the timethe MTIM timer would have reached the value FFh. This will cause an overflow of the MTIMtimer (an R+ event). Therefore, the software checks if the ratio of the MTIM timer is set to themaximum value.

If the ratio of the MTIM timer is set to the maximum value, an arbitrary value is memorized forthe software End of Demagnetization event and is written in the MDREG register.

If the ratio of the MTIM timer is not set to the maximum value, the R_plus flag is incremented,meaning that the overflow will occur during the software Demagnetization. To prevent thisfrom occurring, the clock must be slowed down, i.e. the ratio must be incremented. The valueof the ratio is updated to the new future ratio by dividing by 2 the value for the software De-magnetization. Then, when the ratio changes, the MTIM timer is reset to its middle value(7Fh), which is added to the software Demagnetization value. This new value is then memo-rized in the mem_mdreg variable and the MTIM timer value is read. To ensure that an End ofDemagnetization event will not take place before the ratio change (when the R+ event will

Carry_set

Ratio = $0F?

mem_mdreg = $FA

Write MDREG

End-C_action

No

Yes

Increment flag R_plusSRL A

A = A + $7F

No

Yes

mem_mdreg = A

Read MTIM

MDREG = $70

No

Yes

Carry?

MTIM > $7F?

MDREG = MTIM - 2

www.BDTIC.com/ST

29/31

ST72141 BLDC MOTOR CONTROL SOFTWARE AND FLOWCHART EXAMPLE

occur), a value lower than the MTIM value is written in the MDREG register. The next interruptwill be an R+ event. At that time, the MDREG register is written.

5.3 O AND E INTERRUPT ROUTINES

Figure 22. Multiplier Overflow and Emergency Stop Interrupt Routine

If a multiplication overflow is detected when computing the delay between the zero crossingevent and the next commutation, nothing is really done by the software. The interrupt flag isreset and the software returns to the main program.

For the emergency stop interrupt, the reference current is reset and the software enters a per-manent loop that waits for the end of the emergency stop signal.

O or EIT

Read MISR

Memorize OI & EI bit in step _isrReset these flags in MISR

OI set?

EI set?

Reset Reference Current

Permanent Waiting Loop with Blinking LED

Reset OI in step_isr

IRET

No

Yes

No

Yes

www.BDTIC.com/ST

30/31

ST72141 BLDC MOTOR CONTROL SOFTWARE AND FLOWCHART EXAMPLE

6 SUMMARY

Figure 23. Software Overview

Figure 23 shows how the software behaves in the start-up sequence, when the motor ispassing from Synchronous mode (switched mode) to Auto-switched mode. This figure illus-trates the case where the software programmer wants 2 consecutive Z events before enablingAuto-switched mode and 3 steps in Forced Synchronous mode.

At C1, the motor is in Forced Synchronous mode. In this phase, the Z event interrupt is disa-bled. At the end of Forced Synchronous mode (set by software), the Z event interrupt is ena-bled (C3). The software then returns to the main program and counts the number of consecu-tive Z events detected.

When the second consecutive Z event is detected (Z2), the target number of consecutive Zevents (set by software) is reached. Z2 is centered on the current step and the software mem-orizes that this is the last step in Synchronous mode.

At the following step, the motor enters Auto-switched mode and the software memorizes thatit is the first step in this mode and the MTIM timer is now reset on the Z event and the D eventinterrupt is enabled.

Note that during all steps in Synchronous mode, the D event interrupt is not enabled and allEnd of Demagnetization events are software D events.

C1 C2 C3 C4 C5 Z3 Z4 Z5 Z6

Case: 2 consecutive Z events are required to enable Auto-switched mode.

Synchronous mode Auto-switched mode

Forced Synchronous modeZ Interrupt is not allowed

Z Event Interrupt

Z1

Go back to the mainprogram, waiting for the next interrupt.

1st Z interrupt is detected.nbemf is not equal to target number.Return to the main program.

Z counter =1At next interrupt C,go back to main program.

Z2MZREG

2nd Z interrupt is detected.Z event counter = target number.Last step in Switched mode.We change MCOMP to MCOMP=MZREG*2Go back main program and wait for next C interrupt (C5).

MCOMP

Ds

First step in Auto-switched mode. Wait for C6.

C6

MCOMPwith the default Ki

MCOMPwith the right delay

C7

D

Restore rightWEIGHT

If it is at first a soft demagnetization,we compute the value with previousMCOMP (first time with the defaultdelay coefficient)

is enabled

www.BDTIC.com/ST

31/31

ST72141 BLDC MOTOR CONTROL SOFTWARE AND FLOWCHART EXAMPLE

"THE PRESENT NOTE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS WITH INFORMATIONREGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. AS A RESULT, STMICROELECTRONICSSHALL NOT BE HELD LIABLE FOR ANY DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TOANY CLAIMS ARISING FROM THE CONTENT OF SUCH A NOTE AND/OR THE USE MADE BY CUSTOMERS OFTHE INFORMATION CONTAINED HEREIN IN CONNEXION WITH THEIR PRODUCTS."

Information furnished is believed to be accurate and reliable. However, STMicroelectronics assumes no responsibility for the consequencesof use of such information nor for any infringement of patents or other rights of third parties which may result from its use. No license is grantedby implication or otherwise under any patent or patent rights of STMicroelectronics. Specifications mentioned in this publication are subjectto change without notice. This publication supersedes and replaces all information previously supplied. STMicroelectronics products are notauthorized for use as critical components in life support devices or systems without the express written approval of STMicroelectronics.

The ST logo is a registered trademark of STMicroelectronics

2001 STMicroelectronics - All Rights Reserved.

Purchase of I2C Components by STMicroelectronics conveys a license under the Philips I2C Patent. Rights to use these components in an I2C system is granted provided that the system conforms to the I2C Standard Specification as defined by Philips.

STMicroelectronics Group of CompaniesAustralia - Brazil - Canada - China - Finland - France - Germany - Hong Kong - India - Israel - Italy - Japan

Malaysia - Malta - Morocco - Singapore - Spain - Sweden - Switzerland - United Kingdom - U.S.A.

http://www.st.com

www.BDTIC.com/ST