Thursday, December 05, 2013

IC Design Interview 7: Buck Regulator

Buck DC-DC Switch Mode Power Supply (SMPS) come up in interviews quite a bit these days. Here is my quick review sheet. You may want to be careful it; I bombed the interview I prepared it for.

The obvious things about a buck regulator need to be said. A buck regulator can only translate an input voltage to a lower voltage, never a higher voltage; that requires a boost regulator. The current through an inductor may not change instantly. The voltage across an inductor may change "instantly". 

I sometimes think of a buck regulator as a filter placed on a pulse width modulated (PWM) signal. The output is then simply the average value the voltage before the inductor/capacitor filter. This analogy is not acceptable to most engineers who actually design these circuits. 

Figure 1. Buck regulator overview.

The diode is almost always implemented as a transistor. The transistor that replaces the diode is turned on when the other transistor is turned off. They must not both be on at the same time. If they are both on, significant current shoots through from the input power straight to ground through them. 


Figure 2. Current through and voltage across the inductor.
V = L di/dt        the basic inductor equation
V/L dt = di        just pretend you can do that with derivatives
∫ V/L dt = ∆I     the integration is to ton
(Von-Vn)/L ton = ∆I

The area under the voltage curve (Vn-Vo)*ton is the same same as the area over the rest of the voltage curve -Vo*(toff-ton). That is the same as saying the energy into and out of the inductor is the same.

Wednesday, November 27, 2013

PLL: Math!

The last two posts briefly introduced what a PLL (Phase Locked Loop) does and what pieces go into making one. Now let's dive into the math. We'll start with the closed loop. 

Figure 1. Closed Loop PLL blocks.

Figure 2. Blocks with transfer functions. 


The basic transfer fuction.
φout / φin = A / (1+AB) where A is the feed-forward (Kd*F(s)*Ko/s) and B is the feedback (1/N).

Filling those variables in results in

 KdKoF(s)/s
1+KdKoF(s)/sN

There has got to be a better way way to write equations in html. If you have a suggestion, I'll try it.

Factor out the N and s to get

N KdKoF(s)
Ns+KdKoF(s)

Define a new variable K as KdKo/N

And consider the actual transfer function of the filter. 
Figure 3. The filter in the s-domain.
For F(s) the input is current and the output is voltage. We'll ignore the little C for now.
F(s) = (1+sCR)/sC

The transfer function is now
N sKR + K/C
s2+sKR+K/C

Control systems engineers like to write this transfer function in terms of the natural frequency and damping factor.

natural frequency = ωn = sqrt(K/C)
damping factor = ζ = R/2*sqrt(K/C)     and if it comes up Q= 1/2ζ but I'm not going into that.

Giving a final closed loop transfer function of

φout = 2ζωns+ωn2
φin      s2+ζωns+ωn2

With a current starved inverter VCO as shown in the last post, the dominant intrinsic phase noise is in the VCO. The VCO noise is high pass filtered. The filter noise is bandpass filtered around ωn. Everything else is low pass filtered. Jitter is the time domain noise; jitter in the time domain is phase noise in the frequency domain. Jitter transfer looks like figure 4. as ζ increases, the jitter peak decreases and spreads out.

Figure 4. Jitter transfer curve.

I should do the open loop stuff next time and pull this all together, maybe show some code to simplify exploring the design space. I'm more likely to drop back the trailer lights because I've been working on an improved version of them involving an actual battery charger and twice as many lights. Or my notes on buck DC-DC regulators for an interview I recently bombed.

Bruce McLaren


Wednesday, May 29, 2013

PLL: Pieces and Parts

So let's take that PLL we talked about last time , take it apart and look at the pieces. 

Figure 1 shows the blocks in a Phase Locked Loop (PLL); it is the block diagram from last time with the phase detector (PD), charge pump (CP), and filter broken out and a few details added. There are several ways to implement each block, so I will just mention a simple implementation for each. We don't want to get too bogged down in the circuit details yet.


Figure 1. PLL block diagram.

A simple and effective phase detector is shown in figure 2. The phase detector figures out wether the  feedback clock leads or lags the reference clock and provides up and down signals to the next block (charge pump) to speed up or slow down the system.  Consider A to be the reference clock and B the feedback clock. When the feedback clock lags the reference clock like the first half of the timing diagram, the system needs to speed up, the up signal predominates over the dn (down) signal.  On the other hand, when the feedback clock leads the reference clock like the last half of the timing diagram, the system needs to slow down, the dn (down) signal predominates over the up signal.
The charge pump in the next stage will need complementary up and down signals, so the inverters to produce them are usually placed in this cell. That keeps the current spike from the output switch on the digital supply rail.
This block is sometimes referred to as a PFD (phase/frequency detector). Since the block does not detect frequency, I prefer phase detector. 

Figure 2. Phase detector detail.

Figure 3 has an semi-ideal model of a charge pump. The purpose of the charge pump is to raise the control voltage (VC) with the up signal and lower VC with the dn signal. Since the filter in the next block has a capacitor, this is accomplished by charging and discharging that capacitor with current sources. The current source and sink are always on, the switches merely direct them.
There are four conditions to consider,
when up is high and dn is low, the current source will charge the capacitor
when up is low and dn is high, the current sink will discharge the capacitor
when up and dn are both low, the filter capacitor will retain its value
when up and dn are both high, the filter capacitor should retain its value (this assumes the current source and sink are of the same value, which they should be)

Figure 3. Idealized version of a PLL charge pump.


The simplest passive filter is shown in figure 4. I will discuss why this needs to a second order filter in the next installment, when we will do actual math.

Figure 4. PLL Filter.


The voltage controlled oscillator shown in figure 5 is a simple, noisy current starved inverter ring. This is usually suitable for creating clocks for digital circuits and has a wonderfully wide operating range with reasonably low power. Don't try to use this oscillator in radio circuits. The nMOS labeled "W" is a weak nMOS, it insures there is always some current in the mirrors so the VCO never gets quite to zero frequency.
This is really a current controlled oscillator with a a voltage-to-current converter on the front. The ground for that voltage-to-current converter should be the same as the filter ground. The ground and power for the rest of the oscillator should be separate from the filter. It is common to put the VCO on an internal voltage regulator for its power supply. This block can really throw a bunch of noise around, so be careful with it. 

Figure 5. VCO.


Next time I will discuss stability and noise sources. I'm just starting another contract, but I intend to keep up the once a month posting schedule. This is supposed to simply be a transcription of my already existing notes, though I keep finding my notes acceptable as reminders of what I need to know but inadequate as explanations. 

Bruce McLaren

Wednesday, May 01, 2013

PLL: Introduction

I've been thinking about PLLs (Phase Locked Loops) lately. I don't know why, it's just been where my brain has gone. So I'm going to try to write a description of a PLL that a non-expert can understand and then work my way up to the good stuff. This will likely be a three post topic.

A PLL takes in a single signal that has a frequency and puts out a signal that has a frequency that is a multiple of the input frequency. This is different than a frequency multiplier; a frequency multiplier makes harmonics of an input signal and then bandpasses just the desired harmonic as the output. The PLL system is a pretty cool example of what can be done with negative feedback. 

Basic PLL
Figure 1. Really basic PLL compared to a non-inverting op-amp.

Figure 1 shows the simplest view. The phase detector/charge pump/filter (PD/CP/Filter) compares the input and the output frequency and generates a signal (VC) related to the difference between them. That signal controls the voltage controlled oscillator (VCO) that generates the output. The frequency divider produces the feedback. For now, I'm intentionally confusing the terms phase and frequency. If you know the difference between those terms, this paragraph isn't for you anyway. If a non-inverting op-amp makes more sense to you, compare the PLL to the one at the bottom of figure 1. The resistor divider in the feedback path causes the output to be multiplied by the the divide ratio. In both circuits, the output adjusts until the input matches the feedback. That is the magic of negative feedback.

But wait, this PLL can only multiple an input frequency by an integer multiple. It can take a 10MHz reference and produce a 20MHz, 30MHz, 40MHz, etc. output. But what if I need a 25MHz output? Something I once had the opportunity to implement was a T1 clock (1.544MHz) converted to an E1 clock (2.048MHz). I've seen a couple of ways to generate odd frequency multiples. There must be a dozen other ways, some of which I've probably never heard of. What's your favorite way?

The simplest way to get a non-integer multiple is to just put a digital divider on the output as shown in figure 2. Now the feedback divider multiplies the output by N and the output divider divides it by M. To get 25MHz from 10MHz, use a divide-by-5 divider in the feedback path (N=5) and a divide-by-2 divider (M=2) in the output. Even better would be to use a divide-by-10 in the feedback and a divide-by-4 in the output; this makes it easy to keep the feedback clock with a nice 50% duty cycle. This is not strictly necessary, in general the phase detector is only paying attention to either the rising or falling edge of the feedback, but it is usually preferable to separate the edges as much as possible, i.e., 50% duty cycle. Keep in mind that using a higher divisor in the feedback means a higher frequency directly out of the VCO; there is certainly a top speed to the VCO that your circuit should not even get close to. By the way, that T1-to-E1 conversion was accomplished with a divide-by-256 in the feedback path (VCO frequency 395.264MHz) and a divide-by-193 on the output. Two posts from now, I'll discuss why such high divide ratios can give opportunities for noise (clock jitter). Of course, the divider on the output allows the output clock to be slower than the input clock. For example if N=193 and M=256, than the PLL could be an E1-to-T1 clock converter with 2.048MHz input and 1.544MHz output.

Figure 2. Simple addition to create N/M output.

The previous implementation of that T1-to-E1 frequency converter was in a process that did not allow the VCO to operate that fast so a fractional-N implementation was used like the one shown in figure 3. (I didn't do that implementation, and incredible engineer named Mark Thrower did.) The divider gets more complicated; sometimes it divides by N, sometimes it divides by N+1. For example, if the clock is divided by seven three times then divided by eight once, over and over, the output clock will be effectively divided by 7 3/4. Obviously, there will be "jumps" in the system when the divisor changes, so the filter will need to remove them.

Figure 3. Fractional-N PLL.

Another easy to explain method of generating troublesome ratios is cascade multiple PLLs with different feedback and output dividers. This method has the obvious drawback of using more circuitry. A more subtle drawback is reinforced jitter peaking which will have to wait until the math post to be discussed. It can still be a useful technique, especially when the intermediate frequency is needed for internal circuits. Figure 4 shows a contrived example starting at 10MHz and ending with 93.75Mhz and 75MHz with the VCO frequency staying below 200MHz. If the VCO frequency could go up to 750MHz, this could have been accomplished in a single step.

Figure 4. Multiple stages to keep VCO frequency down and produce multiple outputs. 

That's the end of the introduction. We are going to have to get into electronics to get any further, and I'll leave that until the next post. Next post: pieces and parts; what goes into those black boxes. After that we'll hit the math.


Bruce McLaren


Monday, April 08, 2013

Trailer Lights Physical Stuff

It's past time to get these lights actually built. The previous several posts have outlined the design and testing of an LED lighting system for the interior of a boy scout equipment trailer. Four steps to that.
1) PCB design and manufacture.
2) Circuit assembly.
3) Case design and manufacture.
4) Installation.
Let's take these each in turn.

PCB Design and Manufacture


The PCB layout for this circuit is straight-forward. A two layer circuit board suffices, with the bottom layer being a solid ground plane. I stuck with through-hole components for everything except the inductor. I kept the surface mount inductor that the manufacturer recommended; I am cautious about messing with the inductor. I used as wide traces as would fit.

I used Eagle PCB layout software. The software is straight-forward to use. The price is good. The instructions are great. I failed to install the Linux version; apparently the software is 32-bit and a large batch of libraries would have had to be installed on my 64-bit system. Instead, I used the Macintosh version; it installed easily. I had to make my own symbols and footprints for almost every component, which slowed things down a bit.

I used Silver Circuits to get the boards made. The price was good, the service was easy to use, the delivery was quick, the boards were of good quality. I would use their services again. 

Figure 1. Top and bottom of PCB.



Circuit Assembly


There isn't much too say about the assembly. Push the leads through the PCB then solder and trim the leads. The inductor took a bit more care. I melted some solder on to the inductor pads and the corresponding PCB pads then used a big chisel tip placed in between the two to melt the solder again. That worked well.

Figure 2. Top side of finished board.
Figure 3. Bottom side of finished board. Ground Plane.



Case Design and Manufacture


Let's leave the electronics and make this thing usable. I designed and printed a case in four parts. The criteria is a case that can hold a a UPS battery (I chose a CSB GP1272F2 battery because I had two different UPSes need a new battery, and both used this battery, so I figure this battery will be easily available). Holes are arranged so that if any water gets inside the case, it will have a clear path to continue out the bottom. The four bolts are longer than are easily available, but they can be ordered. The bolt holes are setup for 1/4" bolts. On the underside of the lid is the list of spare parts. I uploaded a model to Google's 3d warehouse

Battery: GP1272F2
Fuse: 3A
Bolts: 4-40x2"

The base takes about 28 hours to print on a MakerGear M2.

Figure 4. Prototype of 3d printed case.

I have enjoyed working with the M2. The learning curve was steeper than I expected. It turns out large, flat surfaces are hard to 3D print. I found a 1/4" lip drawn as part of the bottom layer and using the Hilberton curve for the bottom layer kept the corners down pretty well.



Installation


Installation must have been easy. I got a call that the troop's adult quartermaster wanted to install it in the trailer but I wasn't available to do anything more than drop it off and give a brief hookup explanation. He said it worked. I didn't even manage to get any pictures of the finished case before installation.

The troop will be at Camporee this weekend. That will be a good test of the charging system and well it holds up.




This finally finishes the trailer lights saga. When I get some pictures of the final box installed in the trailer, I will edit those pictures into this page. I'm not sure what comes next. I am considering doing a quick bias cell design.

Bruce



Wednesday, February 06, 2013

PIC12F609 for That Fancy On/Off Switch

My previous post showed the hardware to light a string of white LEDs to be placed to illuminate a boy scout equipment trailer. The post previous to that went into some detail about the boost DC-DC SMPS that is the LED driver. This time I'm going to show what I came up to put inside that 8-pin chip labeled "PIC12F609"

I chose the PIC12F609 because I have some experience with Microchip products, it is a small 8-pin package, it can be obtained in an easily soldered through whole package, it is low power, it requires no external components, and it is cheap. There are other micro-controllers that meet these criteria; I chose this one. 

I haven't messed with any Microchip PICs for about five years and they have moved forward in that time. My good old PICKit1 is no longer supported by the current IDE, so I had to purchase a new programmer. I got a PICKit3. The IDE is now available for Mac and Linux in addition to Windows. Since I do most of my development work on a Linux workstation, I was very happy to see that. But I had enough trouble getting the installation to actually work, that I gave up and just used the MPLAB8 under Windows. Microchip provides reasonably good getting started information on their website. 

I am a hacker by nature. I approach problems by breaking them into smaller problems until I can solve them easily. Then I string the solutions together while testing at each step. This is reflected in my programs. Looking back over this program, I realize it probably should have been written interrupt driven. Interrupt at the timer crossings and button changes. But it works as written. 



;
; Trailer Boost 2
; Bruce McLaren
; nanoDragon LLC
; Bruce.McLaren@nanoDragon.com
; 16 Dec 12 - 19 Dec 12
;
;
; Hardware notes:
; PIC12F609 runnimg at 4MHz using internal oscillator
;
; GP0 : Output, LIGHT - A high turns the lights on
; GP4 : Output, SHUTDOWN - A high turns power off
; GP5 : Input, RELEASE - High when button released
; GP1 : Output, TICK - 200msec period TEST NOT USED IN APP
; GP2 : Output, SECOND - 2sec period TEST NOT USED IN APP
;

#include

__CONFIG _FOSC_INTOSCIO & _WDTE_OFF & _PWRTE_ON & _MCLRE_OFF & _CP_OFF & _IOSCFS_4MHZ & _BOR_OFF

errorlevel -302 ; suppress "not in bank0" warning

; Variables
CBLOCK 0x40 ; 64 bytes RAM in BANK0
TENTHS ; tenths of seconds counter
SECONDS ; seconds counter
MINUTES ; minutes counter
ENDC

ORG 0
nop ;
nop ;
nop ;
nop ;
nop ;

Initialization:
banksel GPIO ;
clrf GPIO ; clear outputs
banksel ANSEL ;
clrf ANSEL ; all digital I/O
movlw b'00100000' ;
movwf TRISIO ; GP5 is input
banksel T1CON ;
movlw b'00110000' ;
movwf T1CON ; /8, internal clock, disabled
banksel CMCON1 ;
bcf CMCON1, T1ACS ; FOSC/4
btfss GPIO, GP5 ; check RELEASE pin
goto $-1 ; wait for it to be 1


LightOn:
banksel TENTHS ;
movlw .9 ;
movwf TENTHS ; TENTHS = .9 (Counts Down)
movlw .59 ;
movwf SECONDS ; SECONDS = .59 (Counts Down)
movlw .29 ;
movwf MINUTES ; MINUTES = .29 (Counts Down)
movlw b'11001111' ; Seed Timer1 for FFFF - .12,500
movwf TMR1H ;
movlw b'00101100' ;
movwf TMR1L ;

LightAgain:
banksel PIR1 ;
bcf PIR1, TMR1IF ; Clear Timer1 overflow
bsf GPIO, GP0 ; set LIGHT
bsf T1CON, TMR1ON ; enable Timer 1
btfss PIR1, TMR1IF ;
goto $-1 ;
movlw b'11001111' ; Seed Timer1 immediately after
movwf TMR1H ; roll over, otherwise time
movlw b'00101100' ; delay depends on logic
movwf TMR1L ;
bcf PIR1, TMR1IF ; Clear Timer1 overflow
decfsz TENTHS, F ;
goto OnDecDoneTenths ; TENTHS is 0
decfsz SECONDS, F ;
goto OnDecDoneSecs ; SECONDS is 0
decfsz MINUTES, F ;
goto OnDecDone ;
goto Blink ; Time is up! Start blinking.
OnDecDoneTenths:
btfss TENTHS, 7 ; -1
goto OnDecDone ;
movlw b'00000100' ; bit 2
xorwf GPIO, F ; toggle SECOND
banksel TENTHS ;
movlw .9 ;
movwf TENTHS ; TENTHS = .9 (Counts Down)
OnDecDoneSecs:
btfss SECONDS, 7 ; -1
goto OnDecDone ;
banksel SECONDS ;
movlw .59 ;
movwf SECONDS ; SECONDS = .59 (Counts Down)
OnDecDone:
btfss GPIO, GP5 ; check RELEASE pin
goto WaitToBlink1 ; If RELEASE is 0, might Blink
movlw b'00000010' ; bit 1
xorwf GPIO, F ; toggle TICK1
goto LightAgain ; Do it again, light stays on.


WaitToBlink1: ; debounce button
banksel TMR0 ;
btfss PIR1, TMR1IF ;
goto $-1 ;
btfss GPIO, GP5 ; Check RELEASE pin
goto WaitToBlink2 ; If RELEASE is still 0, head to Blink
movlw b'00000010' ; bit 1
xorwf GPIO, F ; Toggle TICK1
; decrement the counters here for more accuracy
bcf PIR1, TMR1IF ; Clear Timer1 overflow
movlw b'11001111' ; Seed Timer1 for FFFF - .12,500
movwf TMR1H ;
movlw b'00101100' ;
movwf TMR1L ;
goto LightAgain ; Just a glitch, stay lit


WaitToBlink2: ; wait for button release
banksel T1CON
bcf T1CON, TMR1ON ; disable Timer 1
btfsc GPIO, GP5 ; Check RELEASE pin
goto Blink ; Button Released, go Blink
goto WaitToBlink2 ; Button still held, check again


Blink:
movlw .9 ;
movwf TENTHS ; TENTHS = .9 (Counts Down)
movlw .33 ;
movwf SECONDS ; SECONDS = .33 (Counts Down)
movlw .0 ;
movwf MINUTES ; MINUTES = .00 (Counts Down)
BlinkAgain:
movlw b'11001111' ; Seed Timer1 for FFFF - .12,500
movwf TMR1H ;
movlw b'00101100' ;
movwf TMR1L ;
bcf PIR1, TMR1IF ; Clear Timer1 overflow
bsf T1CON, TMR1ON ; enable Timer 1
btfss PIR1, TMR1IF ;
goto $-1 ;

decfsz TENTHS, F ;
goto BlinkDecDoneTenths ; TENTHS is 0
decfsz SECONDS, F ;
goto BlinkDecDone ;
goto ShutDown ; SECONDS is 0
BlinkDecDoneTenths:
btfss TENTHS, 7 ; -1
goto BlinkDecDone ;
movlw b'00000100' ; bit 2
xorwf GPIO, F ; toggle SECOND
banksel TENTHS ;
movlw .9 ;
movwf TENTHS ; TENTHS = .9 (Counts Down)

movlw b'00000111'
andwf SECONDS, W
btfsc STATUS, Z
goto BlinkLight ; not divisible by 8, Light
bsf GPIO, GP0 ; divisble by 8, go DARK
goto BlinkDecDone
BlinkLight:
bcf GPIO, GP0 ; Light

BlinkDecDone:
btfss GPIO, GP5 ; check RELEASE pin
goto WaitToRemain1 ; If RELEASE is 0, might Remain lit
movlw b'00000010' ; bit 1
xorwf GPIO, F ; toggle TICK1
goto BlinkAgain


WaitToRemain1:
banksel TMR0 ;
btfss PIR1, TMR1IF ;
goto $-1 ;
btfss GPIO, GP5 ; Check RELEASE pin
goto WaitToRemain2 ; If RELEASE is still 0, head to LightOn
movlw b'00000010' ; bit 1
xorwf GPIO, F ; Toggle TICK1
bcf PIR1, TMR1IF ; Clear Timer1 overflow
goto BlinkAgain ; Just a glitch, keep blinking


WaitToRemain2: ; wait for button release
banksel T1CON
bcf T1CON, TMR1ON ; disable Timer 1
btfss GPIO, GP5 ; Check RELEASE pin
goto WaitToRemain2 ; Button still held, check again
goto LightOn ; Button Released, return to Light


ShutDown:
banksel T1CON
bcf T1CON, TMR1ON ; disable Timer 1
bcf GPIO, GP0 ; not LIGHT
bsf GPIO, GP4 ; SHUTDOWN
goto $ ; wait for power to die

end
;


One of the YouTube channels I subscribe to is EEVblog. I find it entertaining as well as informative. OK, the guy whines a bit, but I like his approach. One of the episodes was his replacement for a toggle switch. He was meeting different requirements then I am, but I found the solution cool.

I seem to be able to keep up a one post per month schedule. In spite of contract work coming (and going), I can always put enough minutes together in any given month get one of these little guys written. So I'll stick with that schedule. 

Next month will be the physical parts of the Trailer LED project: PC board, case, and putting it in the trailer. At long last the culmination of these efforts. 

Until next month, good luck on your own projects!

Bruce McLaren

Wednesday, January 02, 2013

Fancy On/Off Switch

The on/off switch for these lights became the most complex part. Remember the goals. The lights will be placed in a boy scout equipment trailer. They need to work for two nights every month. The battery may only get charged for two hours each month (an hour of travel time each direction). The users of the finished product vary between eleven year old boys and forty year old boys.
  • A single button to turn the lights on and off. You can't push the wrong button if there is only one. 
  • No power consumed when the lights are off. None. 
  • Lights turn off automatically after 30 minutes to conserve power. 
  • Before the lights turn off automatically, they blink to warn and give time to turn them back on. 
  • Pressing the button before the automatic off turns the lights off with the same warning blink. 
  • Pressing the button during the blinking turns the lights on for another 30 minutes.
Figure 1. Schematic for the whole she-bang.
An 600dpi version can be downloaded.
Figure 1 is the schematic for the whole operation. The boost regulator on the right was covered in the previous blog. The "Trailer Battery" is a "12V" lead-acid battery that is charged when the trailer is hooked up to the truck. The 7.5Ω 15W resistor (physically huge) will keep the charging current to about 1.3A even if the trailer battery falls to 10V and the truck electrical system is running at 20V. A 3A fuse is placed in the electrical path if that isn't sufficient. The diode keeps the trailer battery from feeding the truck if the trailer battery gets over charged somehow. 

A mechanical relay has the advantage of low resistance when on and effectively infinite resistance when off. Pushing the button will turn on the relay. The relay will then stay on as long as power is provided. It takes a 5V SHUTDOWN signal to the base of Q1 to pull down the coil voltage and depower the entire circuit. Note the two resistors (R1 of 430Ω and R2 of 150Ω) are 1W while the rest of the resistors are 1/4W; enough current could flow through those two resistors if the battery were to float high that I thought heftier components would be a good idea. 

I need to know when the button is pressed so I know when to turn off. The coil voltage is higher when the button is pressed than when the relay is operating self-latched. I can't directly use this voltage because all reasonably priced comparators that can operate from a 10-20V power supply have a common mode input range that only extends up to the power supply minus 2V. One side or the other of the comparator needs to be 2V below its power supply to get reliable operation. Thus the funky resistor network. Figure 2 shows just the resistor network with the resistor values as I decided them. 
Figure 2. Funky resistor network.
The basic idea of this funky network is that when the switch is open VPOS>VNEG and both are more than 2V below VPOW, and when the switch is closed, VNEG=VPOW, VPOS<VNEG, and VPOS<VPOW-2.
The relevant circuit equations are (hey, we had to have some equations in here somewhere!)

When the switch is open:
I2 = VPOW/(R2+(R2+RC)||(R3+R4))
VA = VPOW-I2R2
VNEG = VA*(RC/(RC+R1)
VPOS = VA*(R3/(R3+R4)
Check that even with VPOW=10V VPOS>VNEG and VPOS and VNEG < VPOW-2

When the switch is closed:
VNEG = VPOW
I3 = VPOW/(R2||R1+R4+R3)
VPOS = I3R3
Check that even with VPOW=10V VPOS<VNEG and VPOS < VPOW-2

I played with these in a spread sheet to find likely values then wrote a PERL script that varied all of the resistors by +/-1% or +/-5% to make sure it worked across corners.


The 7805 powers the microcontroller. It has a significant reservoir capacitor so the microcontroller will remain powered a bit longer than the rest of the circuit. This allows the microcontroller to turn off the main power supply. The 7805 can handle a back-power condition like this of up to 7V, so 5V is safe. 

I have not yet programmed the microcontroller; that will be next month's post. In the mean time, I am using a 74HCT74 dual D-Flip-Flop to control the SHUTDOWN signal as shown in figure 3. The button presses toggle the power on and off. This accomplishes nothing more than a push-on/push-off switch could accomplish. The microcontroller is needed for the 30 second time delay and blinking the lights in warning. 

Figure 3. Flip-flop used to verify hardware set-up until PIC software written.

The LIGHT signal is level-shifted up to the battery voltage and used to control a pnp high-side switch. The current flowing to the boost regulator is less than 250mA, so a small bipolar can be used. Another mechanical relay might have made sense here, I decided to go with the cheaper solution. 


Figure 4 is everything put together, though it using the flip-flop instead of the microcontroller. The boost regulator is soldered onto a protoboard to keep the parasitics down, but the rest of the circuit can be operated on a bread board. I am laying out a custom PCB, it will be the post after next. Since the circuit works when done somewhat sloppily on a bread board, I feel confident I can make it work on a two layer PCB. 


Figure 4. Prototype implementation, Boost supply on proto-board, everything else on breadboard.
Instead of listing the parts here, I am referencing my Mouser project. If you can't see that, let me know. That $33.93 includes all of the electrical components and sockets and plugs. It does not include the trailer battery, wire, case, or PCB. 

There you go.
 These things always seem simple once they are working. Next month a simple assembly language program to control this circuit. 

Bruce