You are on page 1of 6

designideas

Edited By Martin Rowe and Fran Granville

readerS SOLVE DESIGN PROBLEMS

Read multiple switches and a potentiometer setting with one microcontroller input pin
Kevin Fodor, Palatine, IL
The circuit in this Design Idea provides a way to convey mixed analog and digital inputs into a micro- controller using one input pin. The out- put of the circuit connects to a micro- controllers ADC-input pin. The circuit comprises a single variable resistor and a number of SPST (single-pole/singlethrow) switches (Figure 1). The push- buttons allow the user to select modes, states, or options, and the analog input provides a method of conveying an ad- justable parameter. The implementa- tion requires you to analyze a parallel resistor circuit and a voltage divider. If you carefully select the resistor val- ues, the circuit for provides a discernible Equations edn100401 di 4591 analog input as well as a number of dis- crete pushbutton-input states. Selecting Equationthe 1 resistor values is a

D Is Inside
72 Three-transistor modulator-

amplifier circuit works with sweptcontrol frequencies

76 Tables ease microcontroller programming multistep process, and a spreadsheet, which you can download at www.edn. 76 Monitor alarm and indicator com/100422dia, aids in performing the display multiple deviation calculations. Say, for example, that you boundaries want 5-kV potentiometer RADJ to pro- ETo see all of EDN s Design duce a 0 to 100% value into the micro- Ideas, visit www.edn.com/ controller. Typically, you would map designideas. the sampled value of 0 to 255 into a 0 to 100 value to represent a percent- age. However, by selecting the values of bias resistor RBIAS, you arrive at a di- The computed value of RBIAS is rect analog input centered on the 0 to 3875V. Using a standard value of 3.3 Equations for edn100401di 4591 rang- 255 range of the ADCfor example, k V, the potentiometers input Equations for edn100401di 4591 78 to 178. es from 73 to 182. This range yields a To compute the appropriate high- larger dynamic range than you need but allows for 1 a guard range between the and low-side bias-resistor values, the Equation Equations for edn 100401 di 4591 Equation 1 following equations solve this circuit potentiometers values and the push- as a simple voltage divider: buttons values. Because the position of RADJ affects R the overall resistance R BIA Equation 1 BIAS VLOW = VMAX R BIAS + when R the circuit sees either R BIAS ; VHIGH = ADJ you press Equations for edn 100401 di 4591 R ADJ + 2 R BIAS R ADJ VLOW = VMAX; VHIGH = the Vmust MAX . switch, inter- R ADJ + 2 R BIAS R ADJmicrocontroller + 2 R BIAS pret a range of values for each switch. V R BIAS + R ADJ R BIAS To determine the switch resistance, V = V V = VMAX . LOW MAX; HIGH Equations for edn100401di 4591 Equation R 1BIAS +R R ADJ+ 2 R R BIAS 2 use R BIAS Equation 2 SR ADJ BIAS ADJ , for either or S2,+ you a paral- VLOW = VMAX; VHIGH = VMAX . R SW 1 Equation 2 R ADJ + 2 RR BIAS R R ADJ + 2 R BIAS lel-resistor network at both extremes of BIAS SW the potentiometers position. Equation 1 R +S R R BIAS BIAS ADJ Substituting and solving for R When you press and at the S2 V R R ADJ (VHIGHVMAX Equation 2 L OW ADJR BIAS 1 ADJ VLOW = VMAX VR = Vis ;V HIGH MAX . = = V R R ( V ) BIAS R + 2 R R + 2 R L O W ADJ ADJ HIGH MAX and given that the maximum voltage maximum position, the effective resis- Equation 2 ADJ BIAS ADJ BIAS V 2 V V 2 VHIGH MICROCONTROLLER R BIAS = = = 3875VLOW . MAX MAX RADJ reports a value of2255, the maximum tance of the bottom leg of the divider VMAX VLOW VMAX 2 VHIGH ADC S1 R78, R BIAS low voltage reports aRvalue of is RSW in parallel with the series combi- BIAS + ADJthe R V VLOW = VMAX; VHIGH = . (VHIGHVMAX ) LOW R ADJ VMAX ADJ R = = 3875 . maximum high voltage value reported nation of RADJ and RBIAS. = At theV mini- R + 2 R R + 2 R BIAS Equation 2 ) V VLOW ADJ R BIAS ADJ BIAS RR R ADJ ADJ (V HIGHVMAX V 2 VHIGH Equ uation 3 SW MAX LOW MAX R BIAS = = BIAS = 3875 V.2 is 178, and RADJ has a value ofV 5 kV mum position, the effective resistance Equ u ation 3 VMAX2 VLOW VMAX 2 V HIGH yield the following equation: is RSW in parallel with RBIAS:

R ADJ R ADJ (VHIGHVMAX ) (R L OW 3 R SW Equ uV ation R SW ADJ R BIAS = = ) = 3875 V. + R BIAS ) ; R R = R ( R + R R R EFFMAX EFFMIN = SW ADJ BIAS V 2 V V 2 V SW BIAS Equ uation 3 LOW MAX HIGH R SW + R R SW R EFFMAX = MAX ;R .ADJ + R BIAS Figure 1 This circuit allows one EFFMIN = R SW + R ADJ + R BIAS R SW + R BIAS microcontroller pin to read multiple VLOW R ADJ R ADJ (VHIGHVMAX ) (R R SW R SW R BIAS ADJ R BIAS = = = 3875 V. + R BIAS ) ; R switches and a potentiometer R = . edn100401di45911 DIANE EFFMAX EFFMIN = V 2 V V 2 V Equ u ation 3 R ( R + R ) R R MAX LOW MAX HIGH SW BIAS R SW R SW + R BIAS value. BIAS IN THE ADJ 4-8 FOLDER) Equation 4 R EFFMAX(PLACED = ; R EFFMIN = SW . + R ADJ + R BIAS R SW + R ADJ + R BIAS Equation 4 R SW + R BIAS Equation 2 Equ uation 3 Equation 4 R R SW (R ADJ + R BIAS ) R R BIAS Equation 4 R EFFMAX = ; RV = SW R EFFMAX . EFFMIN VMAX . R S1MAX = R R + R + R + R BIAS EFFMAX SW ADJ V BIAS . SW R +R April 22, 2010 VS1MAX = EFFMAX BIAS | EDN 71 MAX R EFFMAX + R BIAS R SW (R ADJ + R BIAS ) R R BIAS = ;R = SW R EFFMAX .

designideas

R BIAS =

VMAX2 VLOW

VMAX2 VHIGH

= 3875V.

R SW (R ADJ + R BIAS ) Equ uation 3 R R BIAS R EFFMAX = ; R EFFMIN = SW . R SW R SW + R BIAS Equ uation 3 + R ADJ + R BIAS

R SW (R ADJ + R BIAS ) R SW R BIAS You determine the value when you; R R . EFFMAX = EFFMIN = R SW +R + R R +R R SW ( R + R ) press S1 by the voltage di- Equation 4 evaluating R SW R BIAS ADJ SW BIAS ADJ BIAS BIAS ; R EFFMIN = . EFFMAX viderR that RBIAS=and RRFFMAX form: R SW + R ADJ + R BIAS R SW + R BIAS Equation 4 R EFFMAX VS1MAX = VMAX . R EFFMAX + R BIAS Equation 4

Observe that when RADJ is at its max- R EFFMAX VS1MAX = and you VMAX . imum value press S1, itmust R +R Equation 5 R EFFMAX BIAS produce a value less than the smallest EFFMAX VS1MAX = V . R EFFMAX + to R BIAS value R produces by itself unique- MAX ADJ ly determine that you have pressed the Equation 5 maximum switch. So the R BIAS2 effective re- R < . EFFMAXR sistance, produce a value Equation 5 , must EFFMAX R BIAS + R ADJ less than the maximum low voltage, as 2 the following equation shows: R
BIAS R EFFMAX < . 2 R BIAS R + R ADJ Equation 6 BIAS R EFFMAX < . R BIAS + R ADJ

Figure 2 The circuit can have 10 pushbuttons and one potentiometer.

3 solving2this equation Equation 6and Substituting R BIAS + R BIAS R ADJ R < switch resistance yields: . SW for the 2 6 Equation R ADJ + 2 R ADJ R BIAS

values is not necessary, it minimizes the number of calculations you need to perform and simplifies the design. 3 2 Furthermore, selecting smaller series R BIAS + R BIAS R ADJ R SW < . switch resistors opens the guard range 3 2 2 R ADJ between them and the potentiometer, R R BIAS + 2 + RR R BIAS ADJ BIAS R SW <ADJ . R ADJ2 + 2 R ADJ R BIAS which may be desirable if the result- Using the spreadsheet to compute ing values are too close together. The the switch resistance yields 1558V, microcontroller uses a small subrou- and you can choose a nominal 1.5- tine, Listing 1, which you can down- kV resistor. This selection causes S1 to load at www.edn.com/100422dia, to produce a range of 28 to 71 when you determine both switch positions and press it, depending on the potentiom- the potentiometers setting. eters position. Likewise, choosing the The limitation of this technique is same value for S2 produces a range of that you cannot press more than one 184 to 227. These ranges are bands of pushbutton at any time. In addition, values that you can use to determine the microcontroller can read the po- which switch you pressed regardless tentiometers position only when you of the potentiometers position. Al- are not pressing any other pushbut- though selecting symmetrical resistor tons. This example shows how to use

two pushbuttons, but the number of pushbuttons can vary. Input ranges are available for as many as 10 push- buttons and one potentiometer, all of which share the same input pin (Figure 2). Although the computed rang- es do not overlap and are unique, it is doubtful that your ADC hardware can reliably distinguish these bands under all circumstances. Choosing smaller resistor values keeps these bands farther apart, creating a larger guard range. Using this technique with four pushbuttons and one potentiometer is well within reason. Experimenting with the spreadsheet helps make quick work of determining just the right se- ries-resistor values for each switch and its output range.EDN

Three-transistor modulatoramplifier circuit works with swept-control frequencies


Horia-Nicolai L Teodorescu and Victor Cojocaru, Gheorghe Asachi Technical University, Iasi, Romania
Many applications require a circuit to perform pulse mod- ulation and voltage amplification to drive a load with a train of impulses. A typical application is driving a piezo-

electric generator in a robot. Other ap- plications include driving small motors or LEDs. Echolocation and ultrasound visualization use a sweeping-frequen- cy, or chirp, signal. Nonlinear distor-

tion is not important in these applica- tions. When you drive a piezoelectric load, its natural resonance removes any frequency components other than the fundamental. This circuit combines a modulator and an amplifier into a sin- gle stage. The compactness of the cir- cuit makes it appropriate for portablesystem applications. The load is in series with two switch- es (Figure 1). The input signal controls S2, S3 controls S1, and the modulating signal controls S3. This circuits mod-

72 EDN | April 22, 2010

designideas
RLOAD S3 S1

S2

Figure 1 This simple modulator uses three switches.

ulation operation is similar to that of an AND gate. The switches must have internal resistance to dissipate the har- monics that the resonant load reflects. This circuit uses transistors Q1 and Q2 as switches, although they operate in the active region (Figure 2). Their op- eration resembles that of controlled re- sistors, and they perform voltage and current amplification. You drive Q2 with a 42-kHz signal that matches the loads resonance. You modulate the Q3 transistor with a periodic low-frequen- cy impulse signal. These impulses open Q3, which drives Q1 and Q2 toward sat- uration. When Q3 opens, it drops the voltage across the base of Q1, blocking

If the load impedance varies, the circuit does not degrade the impulse shape.
the state of Q2. Q1 and Q2 operate con- jointly; Q1 conducts only when Q2 is conducting. You can view this scheme as a differential amplifier in which the signal in one branch controls the load of another branch. Q2 and Q3 operate over large signals yet remain in the active region most of the time. The resistor values in the base and collector of Q1 are critical. When the frequency of the signal is higher than the loads resonant frequency, D1 protects Q1 from the effects of L1 and of harmonics on the LC circuit. The collector voltage has a spectrum rich in harmonics due to the nonlinear behav- ior of transistors. This characteristic is not a serious disadvantage because the resonant load removes the harmonics. The value of R1 is critical to the cur- rent and voltage amplification of the Q1/Q2 stage. The swing of voltage in the collector of Q1 is sensitive to the value of R1 (Figure 3). Q1 operates in the active mode because its collector voltage increases slowly toward the maximal value. The significant glitch at small collector voltages shows that the blocking process partly occurs in the active regions of Q2 and Q3. If the load impedance varies, the circuit does not degrade the impulse shape. This situation is true even at twice the loads resonant frequency. The circuit func- tions with input voltages of 4.5 and 11V. This voltage range allows you to drive the circuit with a 5V microcon- troller (Reference 1).EDN R e fe r e nce
Teodorescu, Horia-Nicolai L, Algorithm for Adaptive Distance Estimators for Echolocation in Air, International Solid-State Circuits Conference, 2009, www.adbiosonar.ugal.ro/ ad/content/funding.
1

MURATA MA40B8R

edn100401di46181 DIANE 1 (PLACED IN 1 THE 4-8L FOLDER) 2


R1 100 R2 5k 2 D1 1 1N6392 Q2 BC517 Q1 BC516 R5 100k R4 90k + 25V dc V1

C1 62F

R5 100

R5 100k

Q3 BC517

Figure 2 A three-transistor modulator with a resonant load works over a large input range.

Figure 3 Changing the value of R1 yields different response waveforms.

74 EDN | April 22, 2010

designideas
Tables ease microcontroller programming
Abel Raynus, Armatron International, Malden, MA
When creating microcontroller firmware, you often need to work with data arrays. Tables make easy work of data arrays, such as those for digital-code transformation, correc- tion for sensor linearity, sophisticated calculations, and multiple output orga- nization. Table 1 shows how you can organize data in a table. Outputs A, B, and C have values based on the input value, V. When using a lookup table, choose the proper microcontroller input and outputs. Assign values for input and outputs data in Table 2. These data can consist of constants in binary, hexadec- imal, or decimal format or names. For names, you should assign a constant value to each one. For example: data1 equ $0a data2 equ $0b data3 equ $0c data3 equ $0d Next, put the data from Table 2 in ei- ther the beginning or the end of ROM, which makes the data easy to find. For definition of 1-byte data storage, use pseudo operators FCB or DB. For stor- age of data comprising 2 bytes, use FDB or DW, as in the following example: When working with tables, you should always use indexed addressing mode. It provides access to data using variable addresses. Most microcon- trollers have two index registers, X and H. Register X contains the low byte of the conditional address of the operand; H contains the high byte. The algo- rithm of working with tables is straight- forward. After you detect the input value, you should then compare it with the tables input data. The X index de- termines this value, starting with X50 and ending with X5N. In this exam- ple, N54. When you find table data equal to the input value, you use the corresponding X as an index to load the output registers with their values. In the case of 2-byte numbers, you should load the output registers separately, first with a high byte and then with a low one. Figure 1 illustrates this process. The listing of assembler code is available from the online version of this Design Idea at www.edn.com/ article/100422dib. In the listing, you can double-check the table content in memory at addresses $F800 through $F813. The listing uses Freescale (www. freescale.com) assembler because most of the appropriate applications employ inexpensive, 8-bit microcontrollers from Freescales HC08 Nitron family. You can, however, use this approach with any type of microcontroller and assembly language.EDN

START

CLEAR REGISTER X CLEAR REGISTER H

GET THE INPUT VALUE, V

V=VX? NO X>N? NO INCREMENT X

YES

YES

AXREGISTER A BXREGISTER B CXREGISTER C

END

Figure 1 You can use a look-up table in microcontroller code.

ORG ROM Vx FCB 0T,2T,4T,6T Ax FCB data1,data2,data3,data4 Bx FCB $aa,$bb,$cc,$dd edn100401di46341 DIANE Cx FDB $1122,$3344,$5566,$7788 (PLACED IN THE 4-8 FOLDER) Note that commas separate the data. Dont place a comma after the last data, or it will be considered as $00.

Table 1 Output values versus input values


Input V V1 V2 .... VN Output A A1 A2 .... AN Output B B1 B2 .... BN Output C C1 C2 .... CN

Table 2 Input and output values


Input V V1=0T V2=2T V3=4T V4=6T Output A data1 data2 data3 data4 Output B $aa $bb $cc $dd Output C $1122 $3344 $5566 $7788

Monitor alarm and indicator display multiple deviation boundaries


William Grill, Riverhead Systems, Lenexa, KS
A low-cost monitor can visu- ally indicate a process problem, such as a failed cabinet fan or other

high- or low-temperature characteris- tic. The microcontroller-based circuit in Figure 1 provided a simple visual

indication of both the direction and the magnitude of the temperatures de- viation from a user-set mean in a sol- der pot. Using a Microchip (www.mi- crochip.com) 12F675 controller, the coded sequences allow the user to both set the mean and scale the range of the monitored variation. The application uses the controllers internal clock and

76 EDN | April 22, 2010

designideas
two of the controllers four ADCs. Asserting switch S1 on Pin 4 copies the input voltage under test from Pin 7, which becomes the mean value. The code then evaluates the input-voltage deviation from the mean and applies scaled boundaries to a corresponding display format. The processor moni- tors both the input under test and a second analog level, on Pin 6, to scale the internal deviation/boundary tables. It then schedules as many as four se-
10 F 1N4148 7 TO 12V 4.7 F 78L05 4.7k 1 7 6 4.7k S1 SET MEAN 4 12F675 8 2 3 5 GREEN LED ALARM/ERROR RED LED RED LED 470 470 470

PARAMETER/VOLTAGE UNDER TEST SCALER-SET VOLTAGE

Figure 1 This microcontroller-based circuit provides a simple visual indication of both the direction and the magnitude of the temperatures deviation from a userset mean in a solder pot.

quences of one or both LEDs. The monitor also asserts an output on Pin 5 when the measured variation exceeds the third tabled boundary. The circuit provides independent positive- and negative-deviation ta- bles and multiplies the ranges by inter- preting the voltage on Pin 6, resulting in the application of a multiple from one to eight on the boundary limits. You configure the converter reference to use the controllers VDD voltage. Using only 8 bits of the controllers 10-bit ADC, the deviation can be as small as one step or 1/2563VDD, the drain-to-drain voltage. For a 5V ref- erence, this voltage is approximately 9 mV. Figure 2 shows the boundaries and their possible spans, which Pin 6 and corresponding display-format num- bers set (Table 1). Using the provid- ed minimum value of the deviation/ boundary table, neglecting the error that results from the use of the 78L05 as a reference, and assuming the scal-

edn100401di46321 DIANE (PLACED IN THE 4-8 FOLDER)

78 EDN | April 22, 2010

MEAN SETPOINT 1 (DERIVED FROM PIN 6) DISPLAY-FORMAT NUMBER DEVIATION/ BOUNDARY COUNT 1 PIN 5 ALARM OUTPUT

Table 1 Display-format numbers and table-based sequence


Display-format number >5 5 Sequence Green, red/green, red/green, red/green Green, green, green/red, green/red Green, red, green/red Green, green, green, red Green, green, red Green Red Red, red, green Red, red, red, green Red, green, red/green Red, red, red/green, red/green Red, red/green, red/greeen, red/green

5 20 14

3 2 1 1 2 3 9 5 2 2 5 9

4 14

5 20

ADC-BIT DEVIATION FROM MEASURED VALUE TO MEAN

Figure 2 Pin 6 and the corresponding display-format numbers set the boundaries and their possible spans.

4 3 2 1 21 22 23 24 25 <5

ing derived from Pin 6 result in 31, the first display-format step, in this ap- plication, which occurs when the mea- sured input deviates more than the de- viation/boundary-table value times the scale derived from Pin 6 times 1/256 times the drain-to-drain voltage equals 235/25631, or 39 mV. You can change the display-sequence formats for the five positive boundaries, beginning in a green-LED flash, and five negative boundaries, beginning in a red-LED flash, to suit simpler go or

no/go applications or other needs. The circuit may also find a use in airflow or other physical-parameter monitors. Using the controllers ADC, you can monitor any parameter that you can represent with a voltage. You can modify the code-based tables to ac- commodate a variety of other display sequences, parameter nonlinearities, or error distributions. You can download Listing 1, code for the error monitor, from www.edn. com/100422dic.EDN

,,<<

April 22, 2010 | EDN 79

You might also like