Intelligent Power Supply Design Tips ‘n Tricks M Tips ‘n Tricks Table of Contents Tips ‘N Tricks Introduction ....................................... 1 TIP #1: TIP #2: TIP #3: TIP #4: TIP #5: TIP #6: TIP #7: TIP #8: TIP #9: TIP #10: TIP #11: TIP #12: TIP #13: TIP #14: TIP #15: TIP #16: Soft-Start Using a PIC10F200..................... 2 A Start-Up Sequencer ................................. 5 A Tracking and Proportional Soft-Start of Two Power Supplies................................ 8 Creating a Dithered PWM Clock ............... 11 Using a PIC® Microcontroller as a Clock Source for a SMPS PWM Generator......... 13 Current Limiting Using the MCP1630........ 15 Using a PIC® Microcontroller for Power Factor Correction....................................... 18 Transformerless Power Supplies .............. 21 An IR Remote Control Actuated AC Switch for Linear Power Supply Designs ..................................................... 24 Driving High Side FETs ............................. 26 Generating a Reference Voltage with a PWM Output.......................................... 28 Using Auto-Shutdown CCP ....................... 30 Generating a Two-Phase Control Signal ........................................................ 33 Brushless DC Fan Speed Control ............. 35 High Current Delta-Sigma Based Current Measurement Using a Slotted Ferrite and Hall Effect Device.................... 40 Implementing a PID Feedback Control in a PIC12F683-Based SMPS Design....... 43 © 2006 Microchip Technology Inc. DS41283A-page i Tips ‘n Tricks Table of Contents (continued) TIP #17: TIP #18: TIP #19: TIP #20: TIP #21: An Error Detection and Restart Controller ...................................................46 Data-Indexed Software State Machine......48 Execution Indexed Software State Machine ...........................................51 Compensating Sensors Digitally................54 Using Output Voltage Monitoring to Create a Self-Calibration Function ............56 DS41283A-page ii © 2006 Microchip Technology Inc. Tips ‘n Tricks TIPS ‘N TRICKS INTRODUCTION Microchip continues to provide innovative products that are smaller, faster, easier-to-use and more reliable. PICmicro® microcontrollers (MCUs) are used in a wide range of everyday products from washing machines, garage door openers and television remotes to industrial, automotive and medical products. While some designs such as Switch Mode Power Supplies (SMPS) are traditionally implemented using a purely analog control scheme, these designs can benefit from the configurability and intelligence that can only be realized by adding a microcontroller. This document showcases several examples in which a PICmicro microcontroller may be used to increase the functionality of a design with a minimal increase in cost. Several of the tips provide working software examples or reference other documents for more information. The software and referenced documents can be found on the CD included with this booklet, or on the Microchip Technology web site at www.microchip.com. © 2006 Microchip Technology Inc. DS41283A-page 1 Tips ‘n Tricks TIP #1 Soft-Start Using a PIC10F200 Almost all power supply controllers are equipped with shutdown inputs that can be used to disable the MOSFET driver outputs. Using Pulse Width Modulation (PWM), the amount of time the power supply is allowed to operate can be slowly incremented to allow the output voltage to slowly rise from 0% to 100%. FIGURE 1-1: SOFT-START CIRCUIT SCHEMATIC VDD 0.1 μF 5 On/Off Control Input 1 GP0 GP1 3 Shutdown PWM Output 10 kΩ N.C. X 6 GP3 GP2 4 X N.C. 2 Note: Assumes SOT-23 packaging. This technique is called soft-start and is used to prevent the large inrush currents that are associated with the start-up of a switching power supply. DS41283A-page 2 © 2006 Microchip Technology Inc. Tips ‘n Tricks GP0 on the PIC® MCU is used to enable or disable the soft-start. Once enabled, the on-time of the PWM signal driving the shutdown output will increase each cycle until the power supply is fully on. During the PIC MCU Power-on Reset, the PWM output (GP1) is initially in a high-impedance state. A pull-down resistor on the PWM output ensures the power supply will not unexpectedly begin operating. FIGURE 1-2: TIMING DIAGRAM PWM Output Voltage 100% 0% It is important to note that this type of soft-start controller can only be used for switching regulators that respond very quickly to changes on their shutdown pins (such as those that do cycle-bycycle limiting). Some linear regulators have activelow shutdown inputs, however, these regulators do not respond fast enough to changes on their shutdown pins in order to perform soft-start. © 2006 Microchip Technology Inc. DS41283A-page 3 Tips ‘n Tricks Example software is provided for the PIC10F200 which was taken from TB081. Please refer to TB081, “Soft-Start Controller For Switching Power Supplies” (DS91081) for more information. The software and referenced documents can be found on the CD included with this booklet, or on the Microchip Technology web site at www.microchip.com. DS41283A-page 4 © 2006 Microchip Technology Inc. Tips ‘n Tricks TIP #2 A Start-Up Sequencer Some new devices have multiple voltage requirements (e.g., core voltages, I/O voltages, etc.). The sequence in which these voltages rise and fall may be important. By expanding on the previous tip, a start-up sequencer can be created to control two output voltages. Two PWM outputs are generated to control the shutdown pins of two SMPS controllers. Again, this type of control only works on controllers that respond quickly to changes on the shutdown pin (such as those that do cycle-by-cycle limiting). FIGURE 2-1: MULTIPLE PWM OUTPUT SOFT-START CONTROLLER VDD 0.1 μF UnderVoltage Lockout Shutdown 5 1 6 GP0/CIN+ GP1 GP3 GP2 3 4 2 PWM Output #1 10 kΩ PWM Output #2 10 kΩ Note: Assumes SOT-23 packaging. © 2006 Microchip Technology Inc. DS41283A-page 5 Tips ‘n Tricks This design uses the PIC MCU comparator to implement an under-voltage lockout. The input on the GP0/CIN+ pin must be above the internal 0.6V reference for soft-start to begin, as shown in Figure 2-2. Two conditions must be met in order for the soft-start sequence to begin: 1. The shutdown pin must be held at VDD (logic high). 2. The voltage on GP0 must be above 0.6V. Once both start-up conditions are met, the sequences will delay and PWM #1 will ramp from 0% to 100%. A second delay allows the first voltage to stabilize before the sequencer ramps PWM #2 from 0% to 100%. All delays and ramp times are under software control and can be customized for specific applications. If either soft-start condition becomes invalid, the circuit will shutdown the SMPS controllers. DS41283A-page 6 © 2006 Microchip Technology Inc. Tips ‘n Tricks FIGURE 2-2: TIMING DIAGRAM V1 V2 PWM1 PWM2 1 1. 2. 3. 4. 5. 2 3 4 5 Start-up conditions met Initial delay PWM Ramp #1 complete Between PWM delay PWM #2 complete Example software is provided for the PIC10F200 which was taken from TB093, “Multiple PWM Output Soft-Start Controller for Switching Power Supplies” (DS91093). The software and referenced documents can be found on the CD included with this booklet, or on the Microchip Technology web site at www.microchip.com. © 2006 Microchip Technology Inc. DS41283A-page 7 Tips ‘n Tricks TIP #3 A Tracking and Proportional Soft-Start of Two Power Supplies Expanding on the previous tip, we can also use a PIC MCU to ensure that two voltages in a system rise together or rise proportionally to one another, as shown in Figure 3-1. This type of start-up is often used in applications with devices that require multiple voltages (such as I/O and core voltages). Like the previous two, this tip is designed to control the shutdown pin of the SMPS controller and will only work with controllers that respond quickly to changes on the shutdown pin. FIGURE 3-1: TIMING DIAGRAM VA Voltage VB Time DS41283A-page 8 © 2006 Microchip Technology Inc. Tips ‘n Tricks FIGURE 3-2: EXAMPLE SCHEMATIC VDD Resistor Divider 2 0.1 μF VA Shutdown A + VB - Resistor Divider 1 Control Software R1 Shutdown B R2 PIC12F629 Shutdown The comparator of the PIC MCU is used to determine which voltage is higher and increases the on-time of the other output accordingly. The logic for the shutdown pins is as shown in Table 3-1. TABLE 3-1: SHUTDOWN PIN LOGIC Case Shutdown A Shutdown B High VA>VB Low VB>VA High Low VB> Internal Reference High High To determine if it has reached full voltage, VB is compared to the internal voltage reference. If VB is higher, both shutdown outputs are held high. © 2006 Microchip Technology Inc. DS41283A-page 9 Tips ‘n Tricks Resistor Divider 1 should be designed so that the potentiometer output is slightly higher than the comparator voltage reference when VB is at full voltage. The ratio of resistors in Resistor Divider 2 can be varied to change the slope at which VA rises. Pull-down resistors ensure the power supplies will not operate unexpectedly when the PIC MCU is being reset. DS41283A-page 10 © 2006 Microchip Technology Inc. Tips ‘n Tricks TIP #4 Creating a Dithered PWM Clock In order to meet emissions requirements as mandated by the FCC and other regulatory organizations, the switching frequency of a power supply can be varied. Switching at a fixed frequency produces energy at that frequency. By varying the switching frequency, the energy is spread out over a wider range and the resulting magnitude of the emitted energy at each individual frequency is lower. The PIC10F200 has an internal 4 MHz oscillator. A scaled version of oscillator can be output on a pin (FOSC/4). The scaled output is 1/4 of the oscillator frequency (1 MHz) and will always have a 50% duty cycle. Figure 4-1 shows a spectrum analyzer shot of the output of the FOSC/4 output. FIGURE 4-1: SPECTRUM OF CLOCK OUTPUT BEFORE DITHERING 10 dB/ REF 20 dBm Center 1.0 MHz © 2006 Microchip Technology Inc. Span 1.8 MHz DS41283A-page 11 Tips ‘n Tricks The PIC10F200 provides an Oscillator Calibration (OSCCAL) register that is used to calibrate the frequency of the oscillator. By varying the value of the OSCCAL setting, the frequency of the clock output can be varied. A pseudo-random sequence was used to vary the OSCCAL setting, allowing frequencies from approximately 600 kHz to 1.2 MHz. The resulting spectrum is shown in Figure 4-2. FIGURE 4-2: SPECTRUM OF CLOCK OUTPUT AFTER DITHERING 10 dB/ REF 20 dBm Center 1.0 MHz Span 1.8 MHz By spreading the energy over a wider range of frequencies, a drop of more than 20 dB is achieved. Example software is provided for the PIC10F200 that performs the pseudo-random sequence generation and loads the OSCCAL register. The software and referenced documents can be found on the CD included with this booklet, or on the Microchip Technology web site at www.microchip.com. DS41283A-page 12 © 2006 Microchip Technology Inc. Tips ‘n Tricks TIP #5 Using a PIC® Microcontroller as a Clock Source for a SMPS PWM Generator A PIC MCU can be used as the clock source for a PWM generator, such as the MCP1630. PIC® MCU AND MCP1630 EXAMPLE BOOST APPLICATION FIGURE 5-1: VIN L1 MCP1630 ® PIC MCU VIN VEXT OSC IN FB CS D1 M1 R1 C1 R2 RSENSE The MCP1630 begins its cycle when its clock/ oscillator source transitions from high-to-low, causing its PWM output to go high state. The PWM pulse can be terminated in any of three ways: 1. The sensed current in the magnetic device reaches 1/3 of the error amplifier output. 2. The voltage at the Feedback (FB) pin is higher than the reference voltage (VREF). 3. The clock/oscillator source transitions from low-to-high. © 2006 Microchip Technology Inc. DS41283A-page 13 Tips ‘n Tricks The switching frequency of the MCP1630 can be adjusted by changing the frequency of the clock source. The maximum on-timer of the MCP1630 PWM can be adjusted by changing the duty cycle of the clock source. The PIC MCU has several options for providing this clock source: • The FOSC/4 pin can be enabled. This will produce a 50% duty cycle square wave that is 1/4th of the oscillator frequency. TIP #4 provides both example software and information on clock dithering using the FOSC/4 output. • For PIC MCUs equipped with a Capture/Compare/PWM (CCP) or Enhanced CCP (ECCP) module, a variable frequency, variable duty cycle signal can be created with little software overhead. This PWM signal is entirely under software control and allows advanced features, such as soft-start, to be implemented using software. • For smaller parts that do not have a CCP or ECCP module, a software PWM can be created. Tips #1 and #2 use software PWM for soft-start and provide software examples. The software and referenced documents can be found on the CD included with this booklet, or on the Microchip Technology web site at www.microchip.com. DS41283A-page 14 © 2006 Microchip Technology Inc. Tips ‘n Tricks TIP #6 Current Limiting Using the MCP1630 FIGURE 6-1: MCP1630 HIGH-SPEED PWM VIN 0.1 μA UVLO VEXT OSC IN 0.1 μA CS Comp + – VIN 2R Q GND 100 kΩ Q R Comp Latch Truth Table R 2.7V Clamp Note 1: (1) S VIN FB – VREF EA + VIN Overtemperature S R Q 0 0 Qn 0 1 1 1 0 0 1 1 1 During overtemperature, VEXT driver is high-impedance. The block diagram for the MCP1630 high-speed PWM driver is shown in Figure 6-1. One of the features of the MCP1630 is the ability to perform current limiting. As shown in the bottom left corner of the diagram, the output of the Error Amplifier (EA) is limited by a 2.7V clamp. Therefore, regardless of the actual error, the input to the negative terminal of the comparator (labeled Comp) is limited to 2.7V÷3 or 0.9V. © 2006 Microchip Technology Inc. DS41283A-page 15 Tips ‘n Tricks It is possible to implement the current limiting by using a single sense resistor. In this case, the maximum current would be given by Equation 6-1. EQUATION 6-1: IMAX = (0.9V) /RSENSE For high current applications, this method may be acceptable. When lower current limits are required, the size of the sense resistor, RSENSE, must be increased. This will cause additional power dissipation. An alternative method for lower current limits is shown in Figure 6-2. FIGURE 6-2: LOW CURRENT LIMITS VDD L1 R1 R2 CS Input + RSENSE In this case, the Current Sense (CS) input of the MCP1630 is biased upward using the R1/R2 resistor divider. The equations for the new current limit are shown in Equation 6-2. DS41283A-page 16 © 2006 Microchip Technology Inc. Tips ‘n Tricks EQUATION 6-2: 0.9V = (VDD - IMAX • RSENSE) • R2 R1 + R2 Equation 6-2 can be solved to determine the values of R1 and R2 that provide the desired current limit. © 2006 Microchip Technology Inc. DS41283A-page 17 Tips ‘n Tricks TIP #7 Using a PIC® Microcontroller for Power Factor Correction In AC power systems, the term Power Factor (PF) is used to describe the fraction of power actually used by a load compared to the total apparent power supplied. Power Factor Correction (PFC) is used to increase the efficiency of power delivery by maximizing the PF. The basis for most Active PFC circuits is a boost circuit, shown in Figure 7-1. FIGURE 7-1: TYPICAL POWER FACTOR CORRECTION BOOST SUPPLY L1 + - DS41283A-page 18 D1 M1 + PWM C1 VBoost + C2 © 2006 Microchip Technology Inc. Tips ‘n Tricks The AC voltage is rectified and boosted to voltages as high as 400 VDC. The unique feature of the PFC circuit is that the inductor current is regulated to maintain a certain PF. A sine wave reference current is generated that is in phase with the line voltage. The magnitude of the sine wave is inversely proportional to the voltage at VBoost. Once the sine wave reference is established, the inductor current is regulated to follow it, as shown in Figure 7-2. FIGURE 7-2: DESIRED AND ACTUAL INDUCTOR CURRENTS Reference Inductor Current © 2006 Microchip Technology Inc. Actual Inductor Current DS41283A-page 19 Tips ‘n Tricks A PIC MCU has several features that allow it to perform power factor correction. • The PIC MCUs CCP module can be used to generate a PWM signal that, once filtered, can be used to generate the sine wave reference signal. • The PIC Analog-to-Digital (A/D) converter can be used to sense VBoost and the reference sine wave can be adjusted in software. • The interrupt-on-change feature of the PIC MCU input pins can be used to allow the PIC MCU to synchronize the sine wave reference to the line voltage by detecting the zero crossings. • The on-chip comparators can be used for driving the boost MOSFET(s) using the PWM sine wave reference as one input and the actual inductor current as another. DS41283A-page 20 © 2006 Microchip Technology Inc. Tips ‘n Tricks TIP #8 Transformerless Power Supplies When using a microcontroller in a line-powered application, such as the IR remote control actuated AC switch described in TIP #9, the cost of building a transformer-based AC/DC converter can be significant. However, there are transformerless alternatives which are described below. Capacitive Transformerless Power Supply FIGURE 8-1: CAPACITIVE POWER SUPPLY L VOUT IIN D1 5.1V N C2 470 uF IOUT C1 R1 .47u 250V 470Ω 1/2W © 2006 Microchip Technology Inc. D2 DS41283A-page 21 Tips ‘n Tricks Figure 8-1 shows the basics for a capacitive power supply. The Zener diode is reverse-biased to create the desired voltage. The current drawn by the Zener is limited by R1 and the impedance of C1. Advantages: • Significantly smaller than a transformer-based power supply • Lower cost than a transformer-based or switcher-based power supply • Power supply is more efficient than a resistive transformerless power supply Disadvantages: • Not isolated from the AC line voltage which introduces safety issues • Higher cost than a resistive power supply because X2 rated capacitors are required More information on either of these solutions, including equations used for calculating circuit parameters, can be found in AN954, “Transformerless Power Supplies: Resistive and Capacitive” (DS00954) or in TB008, “Transformerless Power Supply” (DS91008). The software and referenced documents can be found on the CD included with this booklet, or on the Microchip Technology web site at www.microchip.com. DS41283A-page 22 © 2006 Microchip Technology Inc. Tips ‘n Tricks Resistive Power Supply FIGURE 8-2: RESISTIVE POWER SUPPLY L VOUT IIN D1 5.1V C2 IOUT N R1 R2 1KΩ 5W 1KΩ 5W D2 The resistive power supply works in a similar manner to the capacitive power supply by using a reversed-biased Zener diode to produce the desired voltage. However, R1 is much larger and is the only current limiting element. Advantages: • Significantly smaller than a transformer-based power supply • Lower cost than a transformer-based power supply • Lower cost than a capacitive power supply Disadvantages: • Not isolated from the AC line voltage which introduces safety issues • Power supply is less energy efficient than a capacitive power supply • More energy is dissipated as heat in R1 © 2006 Microchip Technology Inc. DS41283A-page 23 Tips ‘n Tricks TIP #9 An IR Remote Control Actuated AC Switch for Linear Power Supply Designs Many line-powered applications (audio amplifiers, televisions, etc.) can be turned on and off using an infrared remote control. This requires that some components be energized to receive the remote signals even when the device is off. Low current PIC® microcontrollers are best in this application. Figure 9-1 shows an example circuit layout. FIGURE 9-1: PIC® MCU INFRARED RECEIVER SCHEMATIC . Q1 Load AC IN R1 C2 U1 + Do - DS41283A-page 24 Load +5V C1 VDD GP0/C+ GP2/COUT GP1/C- GP2/MCLR VSS R2 Zero Crossing Detection © 2006 Microchip Technology Inc. Tips ‘n Tricks The PIC10F200 has several features that make it ideally suited for this type of application: • Extremely low operating and standby current (350 μA operating, 0.1 μA when asleep) • Input/Output pins with configurable pull-ups and reset-on-change capability • High sink/source ability (+/- 25 mA) allows driving external devices, such as the IR receiver, directly from the I/O pin • Ability to use a low-cost resistive power supply • Small form factor (SOT-23 packaging) TB094, “Dimming AC Incandescent Lamps Using A PIC10F200” (DS91094) provides both software and hardware examples of an infrared controller. The software and referenced documents can be found on the CD included with this booklet, or on the Microchip Technology web site at www.microchip.com. © 2006 Microchip Technology Inc. DS41283A-page 25 Tips ‘n Tricks TIP #10 Driving High Side FETs In applications where high side N channel FETs are to be driven, there are several means for generating an elevated driving voltage. One very simple method is to use a voltage doubling charge pump as shown in Figure 10-1. Method 1 FIGURE 10-1: TYPICAL CHANGE PUMP VDD D1 VOUT max = 2 * VDD - 2 * VDIODE CLKOUT CPUMP D2 CFILTER The PIC MCU’s CLKOUT pin toggles at 1/4 of the oscillator frequency. When CLKOUT is low, D1 is forward biased and conducts current, thereby charging CPUMP. After CLKOUT is high, D2 is forward biased, moving the charge to CFILTER. The result is a voltage equal to twice the VDD minus two diode drops. This can be used with a PWM or any other I/O pin that toggles. In Figure 10-2, a standard FET driver is used to drive both the high and low side FETs by using the diode and capacitor arrangement. DS41283A-page 26 © 2006 Microchip Technology Inc. Tips ‘n Tricks Method 2 FIGURE 10-2: SCHEMATIC +5 D1 +12 FET Driver D2 M1 PWM1 C2 U1A L1 C1 . PWM2 U1B M2 The +5V is used for powering the microcontroller. Using this arrangement, the FET driver would have approximately 12 + (5 – VDIODE) – VDIODE volts as a supply and is able to drive both the high and low side FETs. The circuit above works by charging C1 through D1 to (5V – VDIODE) while M2 is on, effectively connecting C1 to ground. When M2 turns off and M1 turns on, one side of C1 is now at 12V and the other side is at 12V + (5V –VDIODE). The D2 turns on and the voltage supplied to the FET driver is 12V + (5V – VDIODE) – VDIODE. © 2006 Microchip Technology Inc. DS41283A-page 27 Tips ‘n Tricks TIP #11 Generating a Reference Voltage with a PWM Output FIGURE 11-1: LOW-PASS FILTER MCP6001 CCP1 Analog Out + R C - A PWM signal can be used to create a Digital-toAnalog Converter (DAC) with only a few external components. Conversion of PWM waveforms to analog signals involves the use of an analog lowpass filter. In order to eliminate unwanted harmonics caused by a PWM signal, the PWM frequency (FPWM) should be significantly higher than the bandwidth (FBW) of the desired analog signal. Equation 11-1 shows this relation. EQUATION 11-1: FPWM = K•FBW Where harmonics decrease as K increases R and C are chosen based on the following equation: EQUATION 11-2: RC = 1/(2•π•FBW) Where harmonics decrease as K increases DS41283A-page 28 © 2006 Microchip Technology Inc. Tips ‘n Tricks Choose the R value based on drive capability and then calculate the required C value. The attenuation of the PWM frequency for a given RC filter is shown in Equation 11-3. EQUATION 11-3: Att(dB) = -10 • log [1 + (2π • FPWM • RC) 2] If the attenuation calculated in Equation 11-3 is not sufficient, then K must be increased in Equation 11-1. In order to sufficiently attenuate the harmonics, it may be necessary to use small capacitor values or large resistor values. Any current draw will effect the voltage across the capacitor. Adding an op amp allows the analog voltage to be buffered and, because of this, any current drawn will be supplied by the op amp and not the filter capacitor. For more information on using a PWM signal to generate an analog output, refer to AN538, “Using PWM to Generate Analog Output” (DS00538). The software and referenced documents can be found on the CD included with this booklet, or on the Microchip Technology web site at www.microchip.com. © 2006 Microchip Technology Inc. DS41283A-page 29 Tips ‘n Tricks TIP #12 Using Auto-Shutdown CCP PWM Auto-Shutdown Several of Microchip’s PIC® MCUs, such as the PIC16F684, PIC16F685 and PIC16F690, have a PWM auto-shutdown feature. When autoshutdown is enabled, an event can terminate the current PWM pulse and prevent subsequent pulses unless the event is cleared. The ECCP can be setup to automatically start generating pulses again once the event clears. FIGURE 12-1: PWM AUTO-SHUTDOWN TIMING PWM Signal Shutdown Event Figure 12-1 shows an example timing for the PWM auto-shutdown. When the shutdown event occurs, the current pulse is immediately terminated. In this example, the next two pulses are also terminated because the shutdown event had not been cleared by the beginning of the pulse period. After the event has cleared, pulses are allowed to resume, but only at the beginning of a pulse period. DS41283A-page 30 © 2006 Microchip Technology Inc. Tips ‘n Tricks Using Auto-Shutdown to Create a Boost Supply By using the auto-shutdown feature, a very simple SMPS can be created. Figure 12-2 shows an example boost power supply. FIGURE 12-2: BOOST POWER SUPPLY VIN L1 Internal to PIC® MCU PWM Module out R1 Q1 VOUT C1 D1 R2 + R3 This power supply configuration has several unique features: 1. The switching frequency is determined by the PWM frequency and, therefore, can be changed at any time. 2. The maximum on-time is determined by the PWM duty cycle and, therefore, can be changed any time. This provides a very easy way to implement soft-start. 3. On PIC MCUs that have a programmable reference module, the output voltage can be configured and changed at any time. © 2006 Microchip Technology Inc. DS41283A-page 31 Tips ‘n Tricks The topology can also be re-arranged to create other types of power supplies. Example software is provided for the PIC16F685 (but can be adapted to any PIC MCU with the ECCP module). The software configures the PWM and comparator modules as shown in Figure 12-2. The software and referenced documents can be found on the CD included with this booklet, or on the Microchip Technology web site at www.microchip.com. DS41283A-page 32 © 2006 Microchip Technology Inc. Tips ‘n Tricks TIP #13 Generating a Two-Phase Control Signal Power supplies using a push-pull topology or with multiple switching components require a two-phase control signal as shown in Figure 13-1. FIGURE 13-1: TWO-PHASE CONTROL SIGNAL P1A P1B It is possible to produce this type of control signal with two out-of-phase square waves using a PIC MCU with an ECCP module. FIGURE 13-2: TWO-PHASE CONTROL SIGNAL SCHEMATIC P1A PIC® MCU P1B © 2006 Microchip Technology Inc. DS41283A-page 33 Tips ‘n Tricks In order to configure the ECCP to produce this type of output: 1. Configure the ECCP in half H-bridge configuration PWM pulse with both outputs active-high. 2. Set the duty cycle register (CCPR1L) with the maximum duty cycle of 50%. 3. Change the programmable dead-time generator to reduce the pulse width to the desired value. The programmable dead-time generator has a 7-bit resolution and, therefore, the resulting pulses will only have a 7-bit resolution. Each pulse will have a 50% duty cycle, less the dead time. Using an internal 4 MHz clock produces 31 kHz output pulses, and using a 20 MHz crystal would produce 156 kHz output. The frequency of the output could be increased with a loss in resolution. Example software is provided for the PIC16F684, but this tip is applicable to all PIC MCUs with ECCP modules. The software and referenced documents can be found on the CD included with this booklet, or on the Microchip Technology web site at www.microchip.com. DS41283A-page 34 © 2006 Microchip Technology Inc. Tips ‘n Tricks TIP #14 Brushless DC Fan Speed Control There are several methods to control the speed of a DC brushless fan. The type of fan, allowable power consumption and the type of control desired are all factors in choosing the appropriate type. FIGURE 14-1: LOW-SIDE PWM DRIVE 12V PWM Drive FIGURE 14-2: HIGH-SIDE PWM DRIVE 12V 12V PWM Drive © 2006 Microchip Technology Inc. DS41283A-page 35 Tips ‘n Tricks Method 1 – Pulse Width Modulation As shown in Figure 14-1 and Figure 14-2, a simple PWM drive may be used to switch a two-wire fan on and off. While it is possible to use the circuit in Figure 14-1 without a high-side MOSFET driver, some manufacturers state that switching on the low side of the fan will void the warranty. Because of this, it is necessary to switch the high side of the fan in order to control the speed. The simplest type of speed control is ‘on’ or ‘off’. However, if a higher degree of control is desired, PWM can be used to vary the speed of the fan. For 3-wire fans, the tachometer output will not be accurate if PWM is used. The sensor providing the tachometer output on 3-wire fans is powered from the same supply as the fan coils, thus using a PWM to control fan speed will render the fan’s tachometer inaccurate. One solution to this is to use a 4-wire fan which includes both the tachometer output and a drive input. Figure 14-3 shows a diagram of a 4-wire fan. DS41283A-page 36 © 2006 Microchip Technology Inc. Tips ‘n Tricks FIGURE 14-3: TYPICAL 4-WIRE FAN V+ Drive TACH TACH GND A 4-wire fan allows speed to be controlled using PWM via the Drive line. Since power to the tachometer sensor is not interrupted, it will continue to output the correct speed. © 2006 Microchip Technology Inc. DS41283A-page 37 Tips ‘n Tricks Method 2 – Linear Control When using PWM, the voltage will vary between a maximum and a minimum, however, is it also possible to use a linear method to control fan speed, as shown in Figure 14-4. FIGURE 14-4: LINEAR CONTROL DRIVE 12V VREF + Out - R1 R2 The voltage applied at the non-inverting terminal of the op amp is used to vary the voltage across the op amp. The non-inverting terminal voltage can be produced by a Digital-to-Analog Converter (DAC) or by the method shown in TIP #11. DS41283A-page 38 © 2006 Microchip Technology Inc. Tips ‘n Tricks When using this method, care must be taken to ensure that the fan voltage is not too low or the fan will stop spinning. One advantage this method has over PWM is that the tachometer output will function properly on 3-wire fans. The disadvantage, however, is that it often offers less speed control. For example, a 12V fan will not spin below 8V, so a range of only 4V is available for speed control. A 5V fan will not spin below 4V and so the control range is only 1V, which is often unacceptable. Another disadvantage is the power consumption of the circuit. The transistor will dissipate more power than the PWM method. © 2006 Microchip Technology Inc. DS41283A-page 39 Tips ‘n Tricks TIP #15 High Current Delta-Sigma Based Current Measurement Using a Slotted Ferrite and Hall Effect Device Many current sensors rely on ferrite cores. Nonlinearity in the ferrite can lead to inaccurate results, especially at high currents. One way to avoid the nonlinearities is to keep the net flux in the ferrite near zero. Consider the circuit in Figure 15-1. FIGURE 15-1: HALL EFFECT CURRENT MEASUREMENT SCHEMATIC PIC® MCU + MOSFET Driver VDD VREF Hall Effect Sensor The Hall Effect sensor output is proportional to the current being measured. When IIN = 0 amps, the output of the sensor will be VDD/2. A current passing through the sensor in one direction will increase the output of the sensor, and a current in the other direction will decrease the output of the sensor. DS41283A-page 40 © 2006 Microchip Technology Inc. Tips ‘n Tricks The output of the comparator is used to drive a coil of wire wound around the ferrite core. This coil of wire will be used to create flux in the opposite direction as the flux imposed in the core. FIGURE 15-2: FLUX DIRECTIONS Flux due to input current IIN Flux due to driven coil The net flux in the core should be approximately zero. Because the flux will always be very near zero, the core will be very linear over the small operating range. When IIN = 0, the output of the comparator will have an approximate 50% duty cycle. As the current moves one direction, the duty cycle will increase. As the current moves the other direction, the duty cycle will decrease. By measuring the duty cycle of the resulting comparator output, we can determine the value of IIN. © 2006 Microchip Technology Inc. DS41283A-page 41 Tips ‘n Tricks Finally, a Delta-Sigma ADC can be used to perform the actual measurement. Features such as comparator sync and Timer1 gate allow the Delta-Sigma conversion to be taken care of entirely in hardware. By taking 65,536 (2^16) samples and counting the number of samples that the comparator output is low or high, we can obtain a 16-bit A/D result. Example schematic and software are provided for the PIC12F683 in both C and Assembly. For more information on using a PIC® MCU to implement a Delta-Sigma converter, please refer to AN700, “Make a Delta-Sigma Converter Using a Microcontroller’s Analog Comparator Module” (DS00700), which includes example software. The software and referenced documents can be found on the CD included with this booklet, or on the Microchip Technology web site at www.microchip.com. DS41283A-page 42 © 2006 Microchip Technology Inc. Tips ‘n Tricks TIP #16 Implementing a PID Feedback Control in a PIC12F683-Based SMPS Design Simple switching power supplies can be controlled digitally using a Proportional Integral Derivative (PID) algorithm in place of an analog error amplifier and sensing the voltage using the Analog-to-Digital Converter (ADC). FIGURE 16-1: SIMPLE PID POWER SUPPLY +5V VSENSE VDD CCP1 GP5 VSS PIC12F683 AN0 GP1 GP3 GP4 +5V R1 Q1 C2 L1 D2 VOUT VSENSE + R2 C1 R3 The design in Figure 16-1 utilizes an 8-pin PIC12F683 PIC® MCU in a buck topology. The PIC12F683 has the basic building blocks needed to implement this type of power supply: an A/D converter and a CCP module. © 2006 Microchip Technology Inc. DS41283A-page 43 Tips ‘n Tricks FIGURE 16-2: PID BLOCK DIAGRAM PIC® Microcontroller A/D Voltage Sense + Desired Value - Software PID Routine CCP Duty Cycle The A/D converter is used to sense the output voltage for this particular application, VDD is used as the reference to the A/D converter. If desired, a more accurate reference could be used. The output voltage is subtracted from the desired value, creating an error value. This error becomes the input to the PID routine. The PID routine uses the error voltage to determine the appropriate duty cycle for the output drive. The PID constants are weighted so that the main portion of the control is proportional and integral. The differential component is not essential to this system and is not used. Furthermore, the PID constants could be optimized if a particular type of transient response was desired, or if a predictable transient load was to be connected. Finally, the CCP module is used to create a PWM signal at the chosen frequency with the proper duty cycle. DS41283A-page 44 © 2006 Microchip Technology Inc. Tips ‘n Tricks Example software is provided for the PIC12F683 using the schematic in Figure 16-1. The following application notes are related to PID control algorithms and all include example software: • AN258, “Low Cost USB Microcontroller Programmer The Building of the PICkit® 1 Flash Starter Kit” (DS00258) • AN937, “Implementing a PID Controller Using a PIC18 MCU” (DS00937) • AN964, “Software PID Control of an Inverted Pendulum Using the PIC16F684” (DS00937) The software and referenced documents can be found on the CD included with this booklet, or on the Microchip Technology web site at www.microchip.com. © 2006 Microchip Technology Inc. DS41283A-page 45 Tips ‘n Tricks TIP #17 An Error Detection and Restart Controller An error detection and restart controller can be created by combining TIP #18 and TIP #19. The controller uses the PIC® microcontroller (MCU) Analog-to-Digital Converter (ADC) for making voltage and current measurements. Input voltage, input current, output voltage, output current, temperature and more can all be measured using the A/D converter. The on-board comparators are used for monitoring faster signals, such as output current, ensuring that they do not exceed maximum allowable levels. Many PIC MCUs have internal programmable comparator references, simplifying the circuit. FIGURE 17-1: BLOCK DIAGRAM Input Voltage Input Current Status Indicators DS41283A-page 46 Output Voltage PIC® MCU Output Voltage Temperature © 2006 Microchip Technology Inc. Tips ‘n Tricks Using a PIC MCU as a controller allows for a greater level of intelligence in system monitoring. Rather than a single event causing a shutdown, a combination of events can cause a shutdown. A certain number of events in a certain time frame or possibly a certain sequence of events could be responsible for a shutdown. The PIC MCU has the ability to restart the supply based on the shutdown event. Some events (such as overcurrent) may call for immediate restart, while other events (such as overtemperature) may require a delay before restarting, perhaps monitoring other parameters and using those to determine when to restart. It is also possible to build this type of error detection and restart controller into many of the tips listed within this guide. © 2006 Microchip Technology Inc. DS41283A-page 47 Tips ‘n Tricks TIP #18 Data-Indexed Software State Machine A state machine can be used to simplify a task by breaking the task up into smaller segments. Based on a state variable, the task performed or the data used by the state machine can be changed. There are three basic types of state machines: dataindexed, execution indexed and a hybrid of the two. This tip will focus on a data-indexed state machine. The data-indexed state machine is ideal for monitoring multiple analog inputs with the Analogto-Digital Converter (ADC). The state variable in these state machines determines which data is acted upon. In this case, the tasks of changing the ADC channel, storing the current result and starting a new conversion are always the same. A very simple flow diagram for a data-indexed state machine is shown in Figure 18-1. DS41283A-page 48 © 2006 Microchip Technology Inc. Tips ‘n Tricks FIGURE 18-1: DATA-INDEXED STATE MACHINE FLOWCHART ADC Interrupt Load ADC control register with CONSTANT[i] to change ADC channel Store current measurement data to ADCDATA[i] registers Start next ADC conversion Increment state variable, checking for rollover End ADC Interrupt Service Routine As shown in Figure 18-1, a constant array (CONSTANT[i]) can be created to store the values to be loaded into the ADC control register to change the ADC channel. Furthermore, a data array (ADCDATA[i]) can be used to store the results of the ADC conversion. Finally, the next conversion is started and the logic required to increment and bind the state variable is executed. © 2006 Microchip Technology Inc. DS41283A-page 49 Tips ‘n Tricks This particular example used the ADC interrupt to signal when a conversion has completed, and will attempt to take measurements as quickly as possible. A subroutine could also be built to perform the same task, allowing the user to call the subroutine when needed. Example software is provided using the PIC16F676 and RS-232 to monitor several ADC channels. The software and referenced documents can be found on the CD included with this booklet, or on the Microchip Technology web site at www.microchip.com. DS41283A-page 50 © 2006 Microchip Technology Inc. Tips ‘n Tricks TIP #19 Execution-Indexed Software State Machine Another common type of state machine is the execution-indexed state machine. This type of state machine uses a state variable in order to determine what is executed. In C, this can be thought of as the switch statement structure as shown in Example 19-1. EXAMPLE 19-1: EXAMPLE USING SWITCH STATEMENT SWITCH (State) { CASE 0:IF (in_key()==5)THEN state = 1; Break; CASE 1:IF (in_key()==8)THEN State = 2; Else State = 0; Break; CASE 2:IF (in_key()==3)THEN State = 3; Else State = 0; Break; CASE 3:IF (in_key()==2)THEN UNLOCK(); Else State = 0; Break; } © 2006 Microchip Technology Inc. DS41283A-page 51 Tips ‘n Tricks Each time the software runs through the loop, the action taken by the state machine changes with the value in the state variable. By allowing the state machine to control its own state variable, it adds memory, or history, because the current state will be based on previous states. The microcontroller is able to make current decisions based on previous inputs and data. In assembly, an execution-indexed state machine can be implemented using a jump table. EXAMPLE 19-2: EXAMPLE USING A JUMP TABLE MOVFW state ADDWF PCL,f GOTO GOTO GOTO GOTO GOTO GOTO state0 state1 state2 state3 state4 state5 DS41283A-page 52 ;load state into w ;jump to state ;number ;state 0 ;state 1 ;state 2 ;state 3 ;state 4 ;state 5 © 2006 Microchip Technology Inc. Tips ‘n Tricks In Example 19-2, the program will jump to a GOTO statement based on the state variable. The GOTO statement will send the program to the proper branch. Caution must be taken to ensure that the variable will never be larger than intended. For example, six states (000 to 101) require a threebit state variable. Should the state variable be set to an undefined state (110 to 111), program behavior would become unpredictable. Means for safeguarding this problem include: • Mask off any unused bits of the variable. In the above example, ANDLW b'00000111' will ensure that only the lower 3 bits of the number contain a value. • Add extra cases to ensure that there will always be a known jump. For example in this case, two extra states must be added and used as error or Reset states. © 2006 Microchip Technology Inc. DS41283A-page 53 Tips ‘n Tricks TIP #20 Compensating Sensors Digitally Many sensors and references tend to drift with temperature. For example, the MCP9700 specification states that its typical is +/-0.5°C and its max error is +/- 4°C. FIGURE 20-1: MCP9700 ACCURACY 6.0 Accuracy (°C) 4.0 Spec. Limit 2.0 0.0 -2.0 + V Average - V -4.0 -55 -35 -15 5 25 45 65 Temperature (°C) 85 105 125 Figure 20-1 shows the accuracy of a 100 sample lot of MCP9700 temperature sensors. Despite the fact that the sensor’s error is nonlinear, a PIC® microcontroller (MCU) can be used to compensate the sensor’s reading. Polynomials can be fitted to the average error of the sensor. Each time a temperature reading is received, the PIC MCU can use the measured result and the error compensation polynomials to determine what the true temperature is. DS41283A-page 54 © 2006 Microchip Technology Inc. Tips ‘n Tricks FIGURE 20-2: MCP9700 AVERAGE ACCURACY AFTER COMPENSATION 0.3 Accuracy (°C) 0.2 0.1 0.0 -0.1 -0.2 Average -0.3 -55 -35 -15 5 25 45 65 Temperature (°C) 85 105 125 Figure 20-2 shows the average accuracy for the 100 sample lot of MCP9700 temperature sensors after compensation. The average error has been decreased over the full temperature range. It is also possible to compensate for error from voltage references using this method. For more information on compensating a temperature sensor digitally, refer to AN1001, “IC Temperature Sensor Accuracy Compensation with a PICmicro® Microcontroller” (DS01001). The software and referenced documents can be found on the CD included with this booklet, or on the Microchip Technology web site at www.microchip.com. © 2006 Microchip Technology Inc. DS41283A-page 55 Tips ‘n Tricks TIP #21 Using Output Voltage Monitoring to Create a Self-Calibration Function A PIC® microcontroller can be used to create a switching power supply controlled by a PID loop (as described in Tip #16). This type of power supply senses its output voltage digitally, compares that voltage to the desired reference voltage and makes duty cycle changes accordingly. Without calibration, it is sensitive to component tolerances. FIGURE 21-1: TYPICAL POWER SUPPLY OUTPUT STAGE VOUT Switching Circuitry C1 R1 Voltage Sense R2 The output stage of many power supplies is similar to Figure 21-1. R1 and R2 are used to set the ratio of the voltage that is sensed and compared to the reference. DS41283A-page 56 © 2006 Microchip Technology Inc. Tips ‘n Tricks A simple means of calibrating this type of power supply is as follows: 1. Supply a known reference voltage to the output of the supply. 2. Place the supply in Calibration mode and allow it to sense that reference voltage. By providing the supply with the output voltage that it is to produce, it can then sense the voltage across the resistor divider and store the sensed value. Regardless of resistor tolerances, the sensed value will always correspond to the proper output value for that particular supply. Futhermore, this setup could be combined with TIP #20 to calibrate at several temperatures. This setup could also be used to create a programmable power supply by changing the supplied reference and the resistor divider for voltage feedback. © 2006 Microchip Technology Inc. DS41283A-page 57 Tips ‘n Tricks NOTES: DS41283A-page 58 © 2006 Microchip Technology Inc. Tips ‘n Tricks NOTES: © 2006 Microchip Technology Inc. DS41283A-page 59 Tips ‘n Tricks NOTES: DS41283A-page 60 © 2006 Microchip Technology Inc. Information contained in this publication regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. 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 Microchip intellectual property rights. Trademarks The Microchip name and logo, the Microchip logo, Accuron, dsPIC, KEELOQ, microID, MPLAB, PIC, PICmicro, PICSTART, PRO MATE, PowerSmart, rfPIC, and SmartShunt are registered trademarks of Microchip Technology Incorporated in the U.S.A. and other countries. AmpLab, FilterLab, Migratable Memory, MXDEV, MXLAB, SEEVAL, SmartSensor and The Embedded Control Solutions Company are registered trademarks of Microchip Technology Incorporated in the U.S.A. Analog-for-the-Digital Age, Application Maestro, dsPICDEM, dsPICDEM.net, dsPICworks, ECAN, ECONOMONITOR, FanSense, FlexROM, fuzzyLAB, In-Circuit Serial Programming, ICSP, ICEPIC, Linear Active Thermistor, MPASM, MPLIB, MPLINK, MPSIM, PICkit, PICDEM, PICDEM.net, PICLAB, PICtail, PowerCal, PowerInfo, PowerMate, PowerTool, REAL ICE, rfLAB, rfPICDEM, Select Mode, Smart Serial, SmartTel, Total Endurance, UNI/O, WiperLock and ZENA are trademarks of Microchip Technology Incorporated in the U.S.A. and other countries. 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. © 2006, Microchip Technology Incorporated, Printed in the U.S.A., All Rights Reserved. Printed on recycled paper. © 2006 Microchip Technology Inc. DS41283A-page 61 M Worldwide Sales and Service AMERICAS Corporate Office Tel: 480-792-7200 Technical Support: http://support.microchip.com Atlanta Tel: 770-640-0034 Boston Tel: 774-760-0087 Chicago Tel: 630-285-0071 Dallas Tel: 972-818-7423 Detroit Tel: 248-538-2250 Kokomo Tel: 765-864-8360 Los Angeles Tel: 949-462-9523 San Jose Tel: 650-215-1444 Toronto Tel: 905-673-0699 ASIA/PACIFIC Australia Tel: 61-2-9868-6733 China-Beijing Tel: 86-10-8528-2100 China-Chengdu Tel: 86-28-8676-6200 China-Fuzhou Tel: 86-591-87503506 China-Hong Kong SAR Tel: 852-2401-1200 DS41283A-page 62 China-Qingdao Tel: 86-532-85027355 China-Shanghai Tel: 86-21-5407-5533 China-Shenyang Tel: 86-24-2334-2829 China-Shenzhen Tel: 86-755-82032660 China-Shunde Tel: 86-757-28395507 China-Wuhan Tel: 86-27-5980-5300 China-Xian Tel: 86-29-8833-7250 India-Bangalore Tel: 91-80-4182-8400 India-New Delhi Tel: 91-11-5160-8631 India-Pune Tel: 91-20-2566-1512 Japan Tel: 81-45-471- 6166 Korea-Gumi Tel: 82-54-473-4301 Korea-Seoul Tel: 82-2-554-7200 Malaysia Tel: 60-4-646-8870 Philippines Tel: 63-2-634-9065 Singapore Tel: 65-6334-8870 Taiwan-Hsin Chu Tel: 886-3-572-9526 Taiwan-Kaohsiung Tel: 886-7-536-4818 Taiwan-Taipei Tel: 886-2-2500-6610 Thailand Tel: 66-2-694-1351 EUROPE Austria Tel: 43-7242-2244-399 Denmark Tel: 45-4450-2828 France Tel: 33-1-69-53-63-20 Germany Tel: 49-89-627-144-0 Italy Tel: 39-0331-742611 Netherlands Tel: 31-416-690399 Spain Tel: 34-91-708-08-90 England Tel: 44-118-921-5869 02/16/06 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. © 2006 Microchip Technology Inc. Microchip Technology Inc. 2355 W. Chandler Blvd. • Chandler, AZ 85224 U.S.A. Phone: 480-792-7200 • Fax: 480-792-9210 www.microchip.com © 2006, Microchip Technology Inc., 4/06 DS41283A *DS41283A*