AN906 Stepper Motor Control Using the PIC16F684 Author: MICROSTEPPING Reston Condit Microchip Technology Inc. Single stepping, or turning a stepping motor at its rated step size, results in less than smooth movement. Microstepping is a technique used to smooth the motor’s movement between full steps and to improve the step resolution of the motor. Microstepping also improves the efficiency of the system, because the current in the windings of the motor is manipulated in a controlled manner rather than being turned on and off abruptly. INTRODUCTION This application note describes how to drive a bipolar stepping motor with the PIC16F684. The Enhanced Capture Compare PWM (ECCP) module is used to implement a microstepping technique known as hightorque microstepping. The microcontroller’s 8 MHz internal oscillator allows the signals generated by the ECCP module to achieve frequencies above the audible range. Note: A microstepping technique known as high torque microstepping alternately varies the current in the two windings of a stepping motor. Figure 1 shows a graph of the current in the windings vs. angular position using this technique. Please refer to AN907: Stepping Motor Fundamentals for information on the types of stepper motors, microstepping and current limiting techniques. HIGH TORQUE MICROSTEPPING Current FIGURE 1: 1st Winding +3S +6S +9S 2nd Winding Angular Position (S = rated step size) A brief description of what is happening is that one winding is powered while the current in the other winding is gradually dropped to zero, reversed, and then ramped up again. This sequence is then repeated for the other winding. Note that the transition between a winding being energized in one direction and then energized in the other direction has a sinusoidal shape (refer to Figure 1). This shape gives the smoothest transition between the motor’s rated step increments (i.e., 7.5 degrees). The way this shape is achieved using a microcontroller is through the use of pulsewidth modulation. Modulating the input to the drive circuitry for a particular winding will result in a current that is proportional to the duty cycle of the modulated waveform. 2004 Microchip Technology Inc. For instance, if a 5V stepping motor is rated at 1 amp, then modulating a 5V supply across the winding at 50% will result in a current of 1/2 amp (assuming a low inductance motor). Equation 1 shows this relationship: EQUATION 1: I = D× IMAX where IMAX is the rated current of the motor and D is the duty cycle. DS00906B-page 1 AN906 In order to achieve the sinusoidal transition from a positive to negative charge in a winding, numerous microsteps are needed. The number of microsteps typically ranges from 4 to 32 microsteps per rated step size. Rather than calculating the duty cycle for a particular microstep on the fly, a duty cycle look-up table is implemented in firmware. The number of table values is equal to the number of steps desired for a particular microstepping sequence. Equation 2 is used to obtain the duty cycle values for the top half of the table. The second half of the table is simply the top half in reverse order. EQUATION 2: PWM Generation Using the ECCP Module The ECCP module on the PIC16F684 is well suited for generating the PWM signal required for microstepping. The module is capable of generating a 10-bit resolution PWM waveform at frequencies ranging up to 7.81 kHz using the microcontroller’s 8 MHz internal oscillator. Higher frequencies are more practical in motor control applications because a motor will typically produce undesirable audible noise at frequencies less than 16 kHz. Only 8-bit resolution is needed for this application, which means frequencies up to 31.2 kHz can be achieved with the ECCP module. The ECCP module has four modes of operation: D(step number) = cos((step number×π)/((number of steps)+1)) × ((2^bits resolution)-1) 1) Single Output 2) Half-bridge Output 3) Full-bridge Forward Output Using Equation 2, the following duty cycle values were calculated for a 16 microsteps per full step sequence using an 8-bit resolution PWM waveform: TABLE 1: DUTY CYCLE VALUES FOR MICROSTEPPING Step Number D 1 251 9 24 2 238 10 70 3 217 11 114 4 188 12 154 5 154 13 188 6 114 14 217 7 70 15 238 8 24 16 251 DS00906B-page 2 Step Number D 4) Full-bridge Reverse Output In Half-bridge mode, the module modulates two pins simultaneously, pins P1A and P1B. For this application, these two outputs are used to drive the two windings of a stepping motor. Only one pin is set active at a time. Enabling and disabling one pin or the other is done by modifying the TRISC register. The following circuit diagram shows how these pins are connected to a bipolar drive circuit. 2004 Microchip Technology Inc. AN906 FIGURE 2: BIPOLAR DRIVE CIRCUIT 10K R1 VSUPPLY P1A VSUPPLY Winding 1 CTRLA2 CTRLA1 VSUPPLY VSUPPLY P1B CTRLB1 2004 Microchip Technology Inc. Winding 2 CTRLB2 DS00906B-page 3 AN906 Note the pull-up resistor on pins P1A and P1B. These resistors clamp the respective line high when the pin is tristated. It is important that the non-modulated line be clamped high so that the NAND gates on either end of the winding can turn on the adjacent MOSFET when the respective control line is enabled. TABLE 2: The ECCP module is set up so that the waveforms on pins P1A and P1B are identical. This is done by configuring the CCP1CON register so that P1A is active high and P1B is active low. With no dead band delay, these pins will behave identically. Configuring the module in this way enables each winding control block to use the same duty cycle look-up table values for it’s transition sequence. The following table shows all eight winding states. WINDING STATES STATE 0 1 2 3 4 5 6 7 Winding 1 Polarity + to 0 0 to - - - - to 0 0 to + + + Winding 2 Polarity + + + to 0 0 to - - - - to 0 0 to + P1A Duty Cycle 100% to 0 0 to 100% 100%* 100%* 100% to 0 0 to 100% 100%* 100%* P1B Duty Cycle 100%* 100%* 100% to 0 0 to 100% 100%* 100%* 100% to 0 0 to 100% TRISC, P1A 0 0 1 1 0 0 1 1 TRISC, P1B 1 1 0 0 1 1 0 0 CTRLA1 1 0 0 0 0 1 1 1 CTRLA2 0 1 1 1 1 0 0 0 CTRLB1 1 1 1 0 0 0 0 1 CTRLB2 0 0 0 1 1 1 1 0 * Pin is tristated and the pull-up resistor is clamping the line high. In states 0, 2, 4 and 6, the first half of the duty cycle sine look-up table (decreasing values) is referenced. In states 1, 3, 5 and 7, the second half of the duty cycle sine look-up table (increasing values) is referenced. DS00906B-page 4 2004 Microchip Technology Inc. AN906 EXAMPLE APPLICATION CONCLUSION This example application demonstrates how to drive a 3.6 degree-per-step stepping motor. The motor used is a bipolar stepping motor rated to draw 1/2 amp at 12V. The PIC16F684 has an ideal set of features for lowcost stepper motor control. High torque microstepping can be implemented using its ECCP module and very few external logic components. The PIC16F684's 8 MHz internal oscillator will allow the ECCP module to drive the transitioning phase of the bipolar stepping motor at a frequency of 31.2 kHz and still provide 8-bits of duty cycle resolution. This frequency effectively eliminates unwanted audible noise generated by the motor. Hardware Appendix A shows a schematic for the example application included with this application note. The drive circuit is composed of four Fairchild® Semiconductor half-bridge MOSFET ICs (part number FDC6420C). Two Microchip logic-input CMOS quad drivers are used to drive the MOSFET ICs and to provide the logic necessary for the implementation described in this application note. The TC4467 has four on-chip NAND gates and the TC4468 has four on-chip AND gates. The inputs to each of the AND gates on the TC4468 are tied together because this IC is used as a non-inverting quad MOSFET driver for this implementation. REFERENCES AN907: “Stepper Motor Fundamentals” Firmware A flowchart illustrating the microstepping firmware implementation of this example is in Appendix B. The source code for this application note is included with this application note on Microchip’s web site, www.microchip.com. Operation There are five modes of operation in the example that are sequenced through with single button presses. The modes of operation are: 1. 2. 3. 4. 5. Motor Off Single-step mode Half-step mode Microstep mode Position Control mode In modes 2, 3 and 4, the speed of the stepping motor is controlled by turning the potentiometer. Mode 5 uses the potentiometer as a position dial. Turning the potentiometer will cause the motor to microstep in one direction or the other for a distance proportional to the distance the potentiometer was turned. 2004 Microchip Technology Inc. DS00906B-page 5 R20 10K 100R 2 R9 CCW 10K RC3 R18 1K POT-3352E RC3 P1B P1A RA4 RA5 CW +5V SW-B3F1000 S1 R10 0.1 uF 3 1 RA0 13 RA1 12 2 RA5 3 RA4 10K RC2 R8 100R R19 10K RA5 RA4 10K R7 AN2 RC2 8 7 RC3 PIC16F684 RC1 RC1 9 6 RC4 R6 P1B RC2 RC0 RC0 10 RA1 RA0 5 RC5 4 RA3/MCLR RA2 11 GND 14 1 VDD U1 P1A +5V AN2 R2 +5V 10K C16 +5V R3 DS00906B-page 6 10K +5V GND 12 GND 7 3Y 11 4Y 10 2Y VDD 14 1Y 13 TC4468 6 3B 8 4A 9 4B 3 2A 4 2B 5 3A 2 1B 1 1A U3 7 3Y 11 4Y 10 1Y 13 2Y 12 VSUPPLY VSUPPLY VDD 14 TC4467 9 4B 8 4A 6 3B 5 3A 4 2B 3 2A 2 1B 1 1A U2 1 G1 S1 5 Q9:A 6 FDC6420C D1 FDC6420C D2 Q10:B 4 2 S2 3 G2 6 Q4:A FDC6420C D1 1 G1 S1 5 FDC6420C D2 Q6:B 4 2 S2 3 G2 D5 3 BAT54S 3 1 1 Winding 1 BAT54S 2 VSUPPLY 2 D4 1 BAT54S D6 3 1 3 2 Winding 1 D3 BAT54S 2 VSUPPLY 6 Q7:A D1 FDC6420C G1 1 S1 5 2 S2 G2 3 D2 FDC6420C 4 Q8:B 6 Q3:A D1 FDC6420C G1 1 S1 5 2 S2 G2 3 D2 FDC6420C 4 Q5:B AN906 APPENDIX A: 2004 Microchip Technology Inc. AN906 APPENDIX A: (CONTINUED) J7 DIN5P_RECEPTICAL VSUPPLY 1 1 RA5 1 RA5 2 2 RA4 2 RA4 3 3 VPP 3 RA3 4 4 RC5 4 5 5 RC4 5 6 6 RC3 6 7 7 ICSPDAT 7 8 8 ICSPCLK 8 9 9 10 RA2 9 10 RC0 10 11 11 RC1 11 12 12 RC2 12 13 13 13 14 14 14 HDR1X14 C13 0.1 uF C12 LM78L05ACM OUT IN GND GND GND GND 2 3 6 7 1 +5V P3 P4 P5 1 uF 7 C11 6 100 uF 4 2 5 8 0.1 uF 1 3 U10 C14 VSUPPLY HDR1X14 2004 Microchip Technology Inc. P1A P1B RC3 RA0 RA1 AN2 RC0 RC1 RC2 +5V HDR1X14 DS00906B-page 7 AN906 APPENDIX B: Initialize Internal Oscillator Frequency = 8 MHz Assign I/O Pins Setup ADC Pin: AN2 Setup ECCP Module: Half-bridge mode, 31.25 kHz Waveform Set TMR0 Parameters Turn on TMR2 goto MotorState DS00906B-page 8 2004 Microchip Technology Inc. AN906 APPENDIX B: (CONTINUED) MotorState Clear TMR0 Interrupt Flag Yes TMR0 Interrupt Flag Set? Delay = Delay - 1 No No Is Delay equal to zero? Yes Read Potentiometer R3 Move high 4-bits of ADC Value into Delay and Increment Duty Cycle Look-up Table Initiate next ADC read Load CCP1CON an CCPR1L with Next Duty Cycle Value No State? 0 0 1 goto State0 2 3 goto State2 goto State1 2004 Microchip Technology Inc. Yes End/Middle of Look-up Table? Increment State 4 5 goto State4 goto State3 6 7 goto State6 goto State5 goto State7 DS00906B-page 9 AN906 APPENDIX B: (CONTINUED) State0 State1 State2 State3 Enable P1A Disable P1B CTRLA1 = 0 CTRLA2 = 0 CTRLB1 = 1 CTRLB2 = 1 Enable P1A Disable P1B CTRLA1 = 0 CTRLA2 = 1 CTRLB1 = 1 CTRLB2 = 0 Disable P1A Enable P1B CTRLA1 = 0 CTRLA2 = 0 CTRLB1 = 1 CTRLB2 = 1 Disable P1A Enable P1B CTRLA1 = 0 CTRLA2 = 1 CTRLB1 = 0 CTRLB2 = 1 goto MotorState goto MotorState goto MotorState goto MotorState State4 State5 State6 State7 Enable P1A Disable P1B CTRLA1 = 0 CTRLA2 = 1 CTRLB1 = 0 CTRLB2 = 1 Enable P1A Disable P1B CTRLA1 = 1 CTRLA2 = 0 CTRLB1 = 0 CTRLB2 = 1 Disable P1A Enable P1B CTRLA1 = 1 CTRLA2 = 0 CTRLB1 = 0 CTRLB2 = 1 Disable P1A Enable P1B CTRLA1 = 0 CTRLA2 = 0 CTRLB1 = 1 CTRLB2 = 1 goto MotorState goto MotorState goto MotorState goto MotorState DS00906B-page 10 2004 Microchip Technology Inc. Note the following details of the code protection feature on Microchip devices: • Microchip products meet the specification contained in their particular Microchip Data Sheet. • Microchip believes that its family of products is one of the most secure families of its kind on the market today, when used in the intended manner and under normal conditions. • There are dishonest and possibly illegal methods used to breach the code protection feature. All of these methods, to our knowledge, require using the Microchip products in a manner outside the operating specifications contained in Microchip's Data Sheets. Most likely, the person doing so is engaged in theft of intellectual property. • Microchip is willing to work with the customer who is concerned about the integrity of their code. • Neither Microchip nor any other semiconductor manufacturer can guarantee the security of their code. Code protection does not mean that we are guaranteeing the product as “unbreakable.” Code protection is constantly evolving. We at Microchip are committed to continuously improving the code protection features of our products. Attempts to break Microchip’s code protection feature may be a violation of the Digital Millennium Copyright Act. If such acts allow unauthorized access to your software or other copyrighted work, you may have a right to sue for relief under that Act. Information contained in this publication regarding device applications and the like is intended through suggestion only and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. No representation or warranty is given and no liability is assumed by Microchip Technology Incorporated with respect to the accuracy or use of such information, or infringement of patents or other intellectual property rights arising from such use or otherwise. Use of Microchip’s products as critical components in life support systems is not authorized except with express written approval by Microchip. No licenses are conveyed, implicitly or otherwise, under any intellectual property rights. Trademarks The Microchip name and logo, the Microchip logo, Accuron, dsPIC, KEELOQ, MPLAB, PIC, PICmicro, PICSTART, PRO MATE, PowerSmart and rfPIC are registered trademarks of Microchip Technology Incorporated in the U.S.A. and other countries. AmpLab, FilterLab, microID, MXDEV, MXLAB, PICMASTER, SEEVAL, SmartShunt and The Embedded Control Solutions Company are registered trademarks of Microchip Technology Incorporated in the U.S.A. Application Maestro, dsPICDEM, dsPICDEM.net, dsPICworks, ECAN, ECONOMONITOR, FanSense, FlexROM, fuzzyLAB, In-Circuit Serial Programming, ICSP, ICEPIC, Migratable Memory, MPASM, MPLIB, MPLINK, MPSIM, PICkit, PICDEM, PICDEM.net, PICtail, PowerCal, PowerInfo, PowerMate, PowerTool, rfLAB, Select Mode, SmartSensor, SmartTel and Total Endurance are trademarks of Microchip Technology Incorporated in the U.S.A. and other countries. Serialized Quick Turn Programming (SQTP) is a service mark of Microchip Technology Incorporated in the U.S.A. All other trademarks mentioned herein are property of their respective companies. © 2004, Microchip Technology Incorporated, Printed in the U.S.A., All Rights Reserved. Printed on recycled paper. Microchip received ISO/TS-16949:2002 quality system certification for its worldwide headquarters, design and wafer fabrication facilities in Chandler and Tempe, Arizona and Mountain View, California in October 2003. The Company’s quality system processes and procedures are for its PICmicro® 8-bit MCUs, KEELOQ® code hopping devices, Serial EEPROMs, microperipherals, nonvolatile memory and analog products. In addition, Microchip’s quality system for the design and manufacture of development systems is ISO 9001:2000 certified. 2004 Microchip Technology Inc. DS00906B-page 11 WORLDWIDE SALES AND SERVICE AMERICAS China - Beijing Korea Corporate Office Unit 706B Wan Tai Bei Hai Bldg. No. 6 Chaoyangmen Bei Str. Beijing, 100027, China Tel: 86-10-85282100 Fax: 86-10-85282104 168-1, Youngbo Bldg. 3 Floor Samsung-Dong, Kangnam-Ku Seoul, Korea 135-882 Tel: 82-2-554-7200 Fax: 82-2-558-5932 or 82-2-558-5934 China - Chengdu 200 Middle Road #07-02 Prime Centre Singapore, 188980 Tel: 65-6334-8870 Fax: 65-6334-8850 2355 West Chandler Blvd. Chandler, AZ 85224-6199 Tel: 480-792-7200 Fax: 480-792-7277 Technical Support: 480-792-7627 Web Address: http://www.microchip.com 3780 Mansell Road, Suite 130 Alpharetta, GA 30022 Tel: 770-640-0034 Fax: 770-640-0307 Rm. 2401-2402, 24th Floor, Ming Xing Financial Tower No. 88 TIDU Street Chengdu 610016, China Tel: 86-28-86766200 Fax: 86-28-86766599 Boston China - Fuzhou 2 Lan Drive, Suite 120 Westford, MA 01886 Tel: 978-692-3848 Fax: 978-692-3821 Unit 28F, World Trade Plaza No. 71 Wusi Road Fuzhou 350001, China Tel: 86-591-7503506 Fax: 86-591-7503521 Atlanta Chicago 333 Pierce Road, Suite 180 Itasca, IL 60143 Tel: 630-285-0071 Fax: 630-285-0075 Dallas 4570 Westgrove Drive, Suite 160 Addison, TX 75001 Tel: 972-818-7423 Fax: 972-818-2924 Detroit Tri-Atria Office Building 32255 Northwestern Highway, Suite 190 Farmington Hills, MI 48334 Tel: 248-538-2250 Fax: 248-538-2260 Kokomo 2767 S. Albright Road Kokomo, IN 46902 Tel: 765-864-8360 Fax: 765-864-8387 Los Angeles 18201 Von Karman, Suite 1090 Irvine, CA 92612 Tel: 949-263-1888 Fax: 949-263-1338 San Jose 1300 Terra Bella Avenue Mountain View, CA 94043 Tel: 650-215-1444 Fax: 650-961-0286 Toronto 6285 Northam Drive, Suite 108 Mississauga, Ontario L4V 1X5, Canada Tel: 905-673-0699 Fax: 905-673-6509 ASIA/PACIFIC Australia Suite 22, 41 Rawson Street Epping 2121, NSW Australia Tel: 61-2-9868-6733 Fax: 61-2-9868-6755 China - Hong Kong SAR Unit 901-6, Tower 2, Metroplaza 223 Hing Fong Road Kwai Fong, N.T., Hong Kong Tel: 852-2401-1200 Fax: 852-2401-3431 Singapore Taiwan Kaohsiung Branch 30F - 1 No. 8 Min Chuan 2nd Road Kaohsiung 806, Taiwan Tel: 886-7-536-4818 Fax: 886-7-536-4803 Taiwan Taiwan Branch 11F-3, No. 207 Tung Hua North Road Taipei, 105, Taiwan Tel: 886-2-2717-7175 Fax: 886-2-2545-0139 EUROPE China - Shanghai Austria Room 701, Bldg. B Far East International Plaza No. 317 Xian Xia Road Shanghai, 200051 Tel: 86-21-6275-5700 Fax: 86-21-6275-5060 Durisolstrasse 2 A-4600 Wels Austria Tel: 43-7242-2244-399 Fax: 43-7242-2244-393 Denmark China - Shenzhen Regus Business Centre Lautrup hoj 1-3 Ballerup DK-2750 Denmark Tel: 45-4420-9895 Fax: 45-4420-9910 Rm. 1812, 18/F, Building A, United Plaza No. 5022 Binhe Road, Futian District Shenzhen 518033, China Tel: 86-755-82901380 Fax: 86-755-8295-1393 China - Shunde Room 401, Hongjian Building, No. 2 Fengxiangnan Road, Ronggui Town, Shunde District, Foshan City, Guangdong 528303, China Tel: 86-757-28395507 Fax: 86-757-28395571 China - Qingdao Rm. B505A, Fullhope Plaza, No. 12 Hong Kong Central Rd. Qingdao 266071, China Tel: 86-532-5027355 Fax: 86-532-5027205 India Divyasree Chambers 1 Floor, Wing A (A3/A4) No. 11, O’Shaugnessey Road Bangalore, 560 025, India Tel: 91-80-22290061 Fax: 91-80-22290062 Japan Benex S-1 6F 3-18-20, Shinyokohama Kohoku-Ku, Yokohama-shi Kanagawa, 222-0033, Japan Tel: 81-45-471- 6166 Fax: 81-45-471-6122 France Parc d’Activite du Moulin de Massy 43 Rue du Saule Trapu Batiment A - ler Etage 91300 Massy, France Tel: 33-1-69-53-63-20 Fax: 33-1-69-30-90-79 Germany Steinheilstrasse 10 D-85737 Ismaning, Germany Tel: 49-89-627-144-0 Fax: 49-89-627-144-44 Italy Via Quasimodo, 12 20025 Legnano (MI) Milan, Italy Tel: 39-0331-742611 Fax: 39-0331-466781 Netherlands P. A. De Biesbosch 14 NL-5152 SC Drunen, Netherlands Tel: 31-416-690399 Fax: 31-416-690340 United Kingdom 505 Eskdale Road Winnersh Triangle Wokingham Berkshire, England RG41 5TU Tel: 44-118-921-5869 Fax: 44-118-921-5820 02/17/04 DS00906B-page 12 2004 Microchip Technology Inc.