Author Topic: Digital adjustable resistor  (Read 8099 times)

0 Members and 1 Guest are viewing this topic.

Offline Nominal Animal

  • Super Contributor
  • ***
  • Posts: 6173
  • Country: fi
    • My home page and email address
Re: Digital adjustable resistor
« Reply #25 on: September 28, 2022, 02:00:41 pm »
You cannot control the overall resistances in the parallel arrangement (where each MOSFET or relay is in series with each resistor) as well as in the series arrangement (where each MOSFET or relay is in parallel with each resistor).

Consider the case where you have three resistors, A, B, C, and therefore eight different states:
  • 0 = Open Open Open: Disconnected
  • 1 = Closed Open Open: Total resistance is A
  • 2 = Open Closed Open: Total resistance is B
  • 4 = Open Open Closed: Total resistance is C
  • 3 = Closed Closed Open: Total resistance is A//B = 1/(1/A + 1/B) = AB/(A+B)
  • 5 = Closed Open Closed: Total resistance is A//C = 1/(1/A + 1/C) = AC/(A+C)
  • 6 = Open Closed Closed: Total resistance is B//C = 1/(1/B + 1/C) = BC/(B+C)
  • 7 = Closed Closed Closed: Total resistance is A//B//C = 1/(1/A + 1/B + 1/C) = ABC/(AB + AC + BC)
In particular, if A = B = C = 1, the seven finite resistances are 1, 1, 1, 1/2, 1/2, and 1/3; yielding three unique ones: 1.000, 0.500, and 0.333.
If A=1, B=2, C=4, the seven finite resistances are 1, 2, 4, 2/3, 4/5, 4/3, 4/7; yielding 0.571, 0.667, 0.800, 1.000, 1.333, 2.000, 4.000.
If A=1, B=2, C=3, we get 0.545, 0.667, 0.750, 1.000, 1.200, 2.000, 3.000.

Simply put, your resolution at the low end will always be better than at the high end, because of how parallel resistances behave.

In particular, if your N resistances are R/2n = 2-nR, n=0..N-1, then you can obtain resistances R/k, k=1..2N-1.
Compare to the series configuration with N resistances 2nR, n=0..N-1, which yields resistances Rk, k=1..2N-1.

The serial configuration is superior, because with N resistors R, 2R, 4R, ..., 2N-1R, you can produce any resistance between 0 and 2NR - R, inclusive, at a resolution of R.  Furthermore, these can be built from resistors all having the same value, putting several in series-parallel configuration (although it is easier if you have two to four different values).

For example, let's say you want 256 steps, or eight shortable (via MOSFET or relay) resistance steps, from 0 to 255R, inclusive, using 4-8 resistors of 8R or 16R each:
  • R: eight 8R resistors in parallel
  • 2R: four 8R resistors in parallel
  • 4R: four 16R resistors in parallel
  • 8R: two pairs of two 8R series resistors in parallel
  • 16R: two pairs of two 16R series resistors in parallel
  • 32R: four 8R resistors in series
  • 64R: four 16R resistors in series
  • 128R: eight 16R resistors in series
for a total of 20 individual 8R resistors and 20 individual 16R resistors.  With 4 4R, 12 8R, 12 16R, and 4 32R resistors, each would consist of exactly four resistors.

With R=12.5Ω –– linear 12.5Ω steps up to 3187.5Ω ––, you'd need 32 resistors: 4×50Ω, 12×100Ω, 12×200Ω, 4×400Ω.  If you have resistors with the same resistance from different batches, and a precise resistance measurement, you can hand-tune the resistances to get very good resolution.

(Note that with 11 steps, 100%/211 = 100%/2048 ≃ 0.05%, so you'd need the highest resistor set to be exact to within 0.025%, to keep the midway step (the 50% glitch where you switch from 10 lowest to only the highest) within 0.25R and 0.75R.  With 8 steps, 100%/2⁸=100%/256≃0.39%, so the highest step overall resistance should be within 0.2% or so.  Because series resistors are easier to select, it can make a lot of sense to use twice the number of resistors for the largest resistance.)

When switching resistances, I would recommend always opening the short (enabling resistors) before adding new shorts (shorting resistors), doing the switch in two steps.  This means that switching from cocccccc to occccccc you actually do cocccccc → oocccccc → occccccc, with enough delay in between to stabilise the switching transistors or relays.  This way, you always 'glitch' toward higher resistance.  If you have a glitch that temporarily has very low resistance (for example, cocccccc → cccccccc → occccccc, you have a near zero-ohm short, cccccc, for a short while), the resistance could be so low it damages your voltage/current source.
« Last Edit: September 28, 2022, 02:19:40 pm by Nominal Animal »
 

Offline eslavkoTopic starter

  • Contributor
  • Posts: 43
  • Country: si
Re: Digital adjustable resistor
« Reply #26 on: September 28, 2022, 06:00:23 pm »
Ok I find that whatever I do with parallel connection will have more dense values at low end and coarse at high end. With series I can get linear response.
But FET switching at parallel connection is easy as all gates are referenced to ground. In series connection the gate drive will go complex as fets aren't ground biased. In parallel connection I can use any fet with RdsOn in miliohm range, in series I can imagine only optocoupler with fet and this one have RDSon in ohms.
Did I miss something?
 

Offline Zero999

  • Super Contributor
  • ***
  • Posts: 19345
  • Country: gb
  • 0999
Re: Digital adjustable resistor
« Reply #27 on: September 28, 2022, 07:24:02 pm »
Put the resistors in series. If the MOSFETs can be driven from 30V, use high value gate resistors and zener diodes to protect the gates.
 

Offline Nominal Animal

  • Super Contributor
  • ***
  • Posts: 6173
  • Country: fi
    • My home page and email address
Re: Digital adjustable resistor
« Reply #28 on: September 28, 2022, 07:46:40 pm »
Consider using P-channel MOSFETs with source tied to gate with say a 100k resistor, each across a resistor (or set of resistors), with the body diode in the blocking direction.  Use a Darlington transistor to pull each gate to ground, for example ULN2003 or ULQ2003 (seven in an 16-pin package).

Their gate-source voltage, Vgs, then limits the maximum voltage across the entire resistor pack.  The gate threshold voltage, Vgs(th), limits the minimum voltage across the entire resistor pack.  If the voltage across the resistor pack is too low, pulling the gates to ground will not turn them on.  This means that at low voltages, the resistor pack resistance will always be too high.

If you want the resistor pack to operate down to zero volts, or with negative voltages, I would use relays instead: one relay across each resistor (set), shorting it out, normally open (SPST-NO, aka 1 form A); say Omron G5Q-1A4-DC5 ones (with 5V coil voltage), rated at max. 5A 30VDC (30V maximum DC voltage) or 3A 250VAC (277VAC maximum AC voltage), available at Mouser for under 1.5€ apiece in lots of ten.  At 5V, each relay draws about 40mA continuous when energized.  Since the relay coils are isolated from the load voltages, you can run the relays off an USB wall wart or even a power bank.  It can be dropped to 16mA after 100ms, but that requires a somewhat smarter relay drive circuit (than just an IC and a diode per relay to handle the inductive spikes); a pair of NPN transistors per relay with some current-limiting resistors, for example.
 

Offline Zero999

  • Super Contributor
  • ***
  • Posts: 19345
  • Country: gb
  • 0999
Re: Digital adjustable resistor
« Reply #29 on: September 28, 2022, 09:05:13 pm »
P-MOSFETs will only work at higher voltages. N-channel MOSFETs should work over 0V to 30V, providing there's always 30V available for gate drive.

You're right, relays are the only way to do this with negative voltage. These can be made solid state with N-MOSFETs connected back-to-back with photovoltaic gate driver opto-couplers.
 

Offline Warpspeed

  • Contributor
  • Posts: 45
  • Country: au
Re: Digital adjustable resistor
« Reply #30 on: September 28, 2022, 09:14:17 pm »
You can do it with binary weighted resistors as suggested above ^^

Or you could build an R-2R resistor ladder network of as many bits as you want that only requires two different resistor values.
https://en.wikipedia.org/wiki/Resistor_ladder
These R-2R ladders can be bought ready made having precision resistors which really saves a lot of board space, and give excellent accuracy.
Check out the Bourns 4100R series.
https://octopart.com/datasheet/4116r-2-220-bourns-1428321?utm_source=bing&utm_medium=cpc&utm_campaign=b_cpc_intl_search_dsa_english_en_usd_datasheets&utm_term=datasheet&utm_content=Intl%20Datasheet%20DSA

Or you could build up something yourself, using high powered resistors for just the first few stages, for use as a stepped resistive test load.
It would be easy to do with 100R and 200R resistors, as these are standard easy to get values.

Either N or P channel mosfets would work, and all the mosfet sources are commoned which simplifies driving, especially for higher voltages, or where there are a  large numbers of bits.

For very high power loads, the first stage might have to carry many amps and require a high powered mosfet and resistor.
The tenth stage might only have to carry a few milliamps so the parts there can be quite small.
All the resistor values would be the same for each stage of course, maybe 100R and 200R, or whatever values you need.
« Last Edit: September 28, 2022, 09:31:20 pm by Warpspeed »
 

Offline MrAl

  • Super Contributor
  • ***
  • Posts: 1395
Re: Digital adjustable resistor
« Reply #31 on: September 28, 2022, 10:59:53 pm »
Hi,

Another idea is to go with the 8 4 2 1 series connection and just use a separate carefully designed bias circuit to drive the four transistors (that short each series resistor).  With proper bias it may work except in maybe the very very light current load applications because the gate current is very small once the transistor switches, although you may see transients as they switch in and out.

At this point i am not sure if you want a real resistor network or you just need to adjust a load current for testing some device or devices.  If you just want to provide a set load current the design task is much much simpler using a control circuit similar to what has been shown in this thread already.  It's not too hard to adjust a load current it's harder to adjust actual resistor values.  What is more is that an adjustable load current device can be adjusted for any 'equivalent' resistance not just in steps.  SO if you just need a variable load current then now is the time to point that out ... or not that.
 

Offline MrAl

  • Super Contributor
  • ***
  • Posts: 1395
Re: Digital adjustable resistor
« Reply #32 on: September 29, 2022, 01:43:53 am »
Hi,

Another idea is to go with the 8 4 2 1 series connection and just use a separate carefully designed bias circuit to drive the four transistors (that short each series resistor).  With proper bias it may work except in maybe the very very light current load applications because the gate current is very small once the transistor switches, although you may see transients as they switch in and out.

At this point i am not sure if you want a real resistor network or you just need to adjust a load current for testing some device or devices.  If you just want to provide a set load current the design task is much much simpler using a control circuit similar to what has been shown in this thread already.  It's not too hard to adjust a load current it's harder to adjust actual resistor values.  What is more is that an adjustable load current device can be adjusted for any 'equivalent' resistance not just in steps.  SO if you just need a variable load current then now is the time to point that out ... or not that.

I forgot to mention you can use optocoupler drivers to turn the mosfets on and off, along with some thought about the mosfet drive voltages.  This might not be too hard to set up.  For extreme use you may also need independent voltage supplies one for each mostfet.  That can be had with a multi winding transformer and rectifier/filter circuits.
 

Offline eslavkoTopic starter

  • Contributor
  • Posts: 43
  • Country: si
Re: Digital adjustable resistor
« Reply #33 on: September 29, 2022, 05:48:06 am »
The device is battery powered so releays is out of scope.  The power source is 4x AA cell, and dimensions are already defined. At least for prototype I must do that on existing PCB (parallel resistors) and there are two sets of resistors. One set is of 11 resistors and other is for 13 resistors. So I need 24 driver circuits in real.
 

Offline eslavkoTopic starter

  • Contributor
  • Posts: 43
  • Country: si
Re: Digital adjustable resistor
« Reply #34 on: September 29, 2022, 05:52:54 am »
I need to check if this circuit from post #13 improve performance.
Many times R2R ladder is suggested but AFAIK the R2R ladder need to drive leg to 0V or Reference. I don't know what result is if it's driven to 0V or left float. Can someone simulate this?
 

Offline MrAl

  • Super Contributor
  • ***
  • Posts: 1395
Re: Digital adjustable resistor
« Reply #35 on: September 29, 2022, 09:10:49 am »
The device is battery powered so releays is out of scope.  The power source is 4x AA cell, and dimensions are already defined. At least for prototype I must do that on existing PCB (parallel resistors) and there are two sets of resistors. One set is of 11 resistors and other is for 13 resistors. So I need 24 driver circuits in real.

If you can find dual action relays you only power them when they switch.
 

Offline MrAl

  • Super Contributor
  • ***
  • Posts: 1395
Re: Digital adjustable resistor
« Reply #36 on: September 29, 2022, 09:12:20 am »
I need to check if this circuit from post #13 improve performance.
Many times R2R ladder is suggested but AFAIK the R2R ladder need to drive leg to 0V or Reference. I don't know what result is if it's driven to 0V or left float. Can someone simulate this?


Yes the 'legs' are driven to either +vcc or ground never just open.
That's not a 1R2R ladder but even if it was you still have to drive the bottom of each leg to either +vcc or ground (P type and N type mosfets) but never just open.
It's usually used to act as a stepping voltage divider with equal steps.

« Last Edit: September 29, 2022, 09:33:33 am by MrAl »
 

Online ledtester

  • Super Contributor
  • ***
  • Posts: 3032
  • Country: us
Re: Digital adjustable resistor
« Reply #37 on: September 29, 2022, 04:56:51 pm »
I need to check if this circuit from post #13 improve performance.
Many times R2R ladder is suggested but AFAIK the R2R ladder need to drive leg to 0V or Reference. I don't know what result is if it's driven to 0V or left float. Can someone simulate this?

I've cobbled together this R-ladder calculator web app which allows you to play around with different resistance values:

https://pc88mxer.gitlab.io/public-pages/ladder.html

The "Control Word" represents which switches are closed.

Setting the top values to 0 will place all of the vertical resistors in parallel with each other.

You should enter the same number of values for both the top and vertical resistors, but if you don't the missing values will be set to 0.

Let me know if you have any questions or spot any issues.

The app probably displays best on a desktop.

 

Offline Warpspeed

  • Contributor
  • Posts: 45
  • Country: au
Re: Digital adjustable resistor
« Reply #38 on: September 29, 2022, 11:24:40 pm »
By far the easiest way to use an R-2R resistor network, is to operate in direct voltage switching mode.

To do this, the 2R resistors that extend out from the R-2R network are driven direct from CMOS output logic gates.
These switch from either ground to supply with fairly low impedance in either logic state.

The output voltage then switches in even steps between ground and the supply voltage to the CMOS driver.
The circuit below would ramp up the voltage in 16 even steps with each incoming clock pulse, then overflow back to zero.

For best accuracy the resistors need to be made reasonably higher than the output impedance of the CMOS driver, say typically 1k to 100k.
The output op amp can then increase the output voltage span, and provide a lower output impedance if required.
 

Offline Warpspeed

  • Contributor
  • Posts: 45
  • Country: au
Re: Digital adjustable resistor
« Reply #39 on: September 29, 2022, 11:35:18 pm »
The above circuit is basically correct, but looking at again it after I posted it, there are some devils in the detail.

The driver chip could certainly  be a ripple counter connected up as shown, but definitely not TTL.
It absolutely Must be CMOS chip to ensure full rail to rail switching.
A 74HC93 would work, but not a 74LS93.

The output op amp is also drawn as a summing amplifier, and would produce an inverted negative going output ramp, although the output terminals are labeled plus and minus, which suggests a positive going output ramp.  Which it ain't.
« Last Edit: September 29, 2022, 11:42:23 pm by Warpspeed »
 

Offline Warpspeed

  • Contributor
  • Posts: 45
  • Country: au
Re: Digital adjustable resistor
« Reply #40 on: September 29, 2022, 11:38:47 pm »
double post.
 

Offline eslavkoTopic starter

  • Contributor
  • Posts: 43
  • Country: si
Re: Digital adjustable resistor
« Reply #41 on: September 30, 2022, 06:37:59 am »
I need to check if this circuit from post #13 improve performance.
Many times R2R ladder is suggested but AFAIK the R2R ladder need to drive leg to 0V or Reference. I don't know what result is if it's driven to 0V or left float. Can someone simulate this?

I've cobbled together this R-ladder calculator web app which allows you to play around with different resistance values:

https://pc88mxer.gitlab.io/public-pages/ladder.html

The "Control Word" represents which switches are closed.

Setting the top values to 0 will place all of the vertical resistors in parallel with each other.

You should enter the same number of values for both the top and vertical resistors, but if you don't the missing values will be set to 0.

Let me know if you have any questions or spot any issues.

The app probably displays best on a desktop.

link doesn't work (404)
 

Offline eslavkoTopic starter

  • Contributor
  • Posts: 43
  • Country: si
Re: Digital adjustable resistor
« Reply #42 on: September 30, 2022, 06:55:19 am »
Thanks all for contribution.

This thread goes in little wrong direction. As I allready write I need variable resistor and not variable voltage. The device is battery powered handheld and there are 24 legs.

With design as is and resistors in 1/1, 1/2, 1/4 ... ratio I get desired values in low end without problem as there steps are superfine. But in higher values the steps are to coarse. I do the simulation and found that I need at least 15 legs (16 better) for each of two chain.

The real question was 'Is there any better resistor aragement than 1/1, 1/2, 1/4 ...
And seems that answer is NO.

So if anyone can arrange resistors to have finer steps at high range this is the answer for me. They don't need to be linear as I can use translation table in MCU.



 

Offline MrAl

  • Super Contributor
  • ***
  • Posts: 1395
Re: Digital adjustable resistor
« Reply #43 on: September 30, 2022, 10:36:23 am »
Thanks all for contribution.

This thread goes in little wrong direction. As I allready write I need variable resistor and not variable voltage. The device is battery powered handheld and there are 24 legs.

With design as is and resistors in 1/1, 1/2, 1/4 ... ratio I get desired values in low end without problem as there steps are superfine. But in higher values the steps are to coarse. I do the simulation and found that I need at least 15 legs (16 better) for each of two chain.

The real question was 'Is there any better resistor aragement than 1/1, 1/2, 1/4 ...
And seems that answer is NO.

So if anyone can arrange resistors to have finer steps at high range this is the answer for me. They don't need to be linear as I can use translation table in MCU.

Yes as i said the 1R2R ladder is usually used to generate voltage steps or if into a zero ohm grounded current sense then a current, so i cant see that being any use here.
 

Offline Nominal Animal

  • Super Contributor
  • ***
  • Posts: 6173
  • Country: fi
    • My home page and email address
Re: Digital adjustable resistor
« Reply #44 on: September 30, 2022, 01:20:57 pm »
The "optimum" resistor set is linear, down to about half the largest one.

As a practical example, with 100Ω, 75Ω, 50Ω you can get 23.1Ω, 30.0Ω, 33.3Ω, 42.9Ω, 50Ω, 75Ω, and 100Ω.

You can combine 1.0, 0.9, 0.8, 0.7, 0.6, 0.5 into 0.118, 0.134, 0.136, 0.139, 0.142, 0.147, 0.155, 0.158, 0.161, 0.164, 0.166, 0.169, 0.173, 0.173, 0.176, 0.181, 0.183, 0.187, 0.187, 0.192, 0.196, 0.199, 0.203, 0.209, 0.209, 0.214, 0.214, 0.220, 0.226, 0.229, 0.230, 0.235, 0.238, 0.243, 0.244, 0.248, 0.255, 0.264, 0.265, 0.272, 0.273, 0.283, 0.292, 0.298, 0.308, 0.321, 0.323, 0.333, 0.343, 0.360, 0.373, 0.375, 0.394, 0.412, 0.424, 0.444, 0.474, plus you then have the 0.500, 0.600, 0.700, 0.800, 0.900, 1.000 on their own.

This is because parallel resistor arrays simply mathematically cannot provide many combinations in the upper half:
For N resistors in parallel, the total resistance cannot exceed max(R)/N, where max(R) is the largest resistor in the parallel array of N resistors.  Thus, the distribution in the upper half is the set of individual resistances (within the upper half).

Given N such resistors, with largest 1 and smallest 0.5, the minimum resistance possible is 1/(2×(N-1)×sum(1/(N+k-1), k=0..N-1)). 

For N=2, 1/3≃0.333; for N=3, 3/13≃0.231; for N=4, 10/57≃0.175; for N=5, 105/743≃0.141; for N=6, 252/2131≃0.118; for N=7, 2310/22727≃0.102; for N=8, 25740/288851≃0.0891; for N=9, 9009/113567≃0.0793; for N=10, 136136/1904613≃0.0715; for N=11, 11639628/178964263≃0.0650 ≃ 1/15.  Since the minimum for N=24 is 204773655626139600/6837434095326667939≃0.0299489 ≃ 1/33, the tradeoff with this approach is the minimum stays relatively high (about 1/Nc, with 1≤c≤1.2555 for N≥4), even with many resistors.

In my opinion, if you want a linear distribution of total resistances, parallel configurations just won't work: the upper half of the total resistance range is always covered by individual resistors, no matter what you do!  It is a square peg in a round hole; the wrong tool for the job.
« Last Edit: September 30, 2022, 01:28:52 pm by Nominal Animal »
 

Offline Siwastaja

  • Super Contributor
  • ***
  • Posts: 8110
  • Country: fi
Re: Digital adjustable resistor
« Reply #45 on: September 30, 2022, 01:48:38 pm »
Also consider photo-MOSFETs. They can accept bidirectional (like two opposite back-to-back MOSFETs, removing the conduction of body diodes) voltages over them while off, so behave like ideal switches. Thanks to isolation, control voltage doesn't need to be in any relation to the switched voltage: if you can turn a LED on with arduino/whatever you are using, you can control photo-MOSFET.

They are just quite expensive and on-resistance might be too much and/or maximum blocking voltage too low for your application, but do check them out. It will be the easiest way to the 2^n series configuration.
 

Offline Zero999

  • Super Contributor
  • ***
  • Posts: 19345
  • Country: gb
  • 0999
Re: Digital adjustable resistor
« Reply #46 on: September 30, 2022, 05:46:36 pm »
The R2R network is no good for this. The person who mentioned it was mistaken.

Use the circuit I posted above with photo-voltaic output opto-couplers driving the MOSFETs.

Here's a link to a part which has two channels, so you only need two of them to give a 4-bit input.
https://rocelec.widen.net/view/pdf/tp3wmnljhw/IRSDS10626-1.pdf
https://www.digikey.co.uk/en/products/detail/rochester-electronics-llc/PVI5013RPBF/12608484
 

Offline eslavkoTopic starter

  • Contributor
  • Posts: 43
  • Country: si
Re: Digital adjustable resistor
« Reply #47 on: September 30, 2022, 06:59:32 pm »
The "optimum" resistor set is linear, down to about half the largest one.

I'm confused.
You say that I need linear gap in resistance? 25+25=50, 50+25=75,  75+25=100, 100+25=125...
..just take some base and does multiply value.

Strange as I get better result with values like 100, 200, 400, 800
I need to re check my simulation
 

Offline Zero999

  • Super Contributor
  • ***
  • Posts: 19345
  • Country: gb
  • 0999
Re: Digital adjustable resistor
« Reply #48 on: September 30, 2022, 07:31:54 pm »
The "optimum" resistor set is linear, down to about half the largest one.

I'm confused.
You say that I need linear gap in resistance? 25+25=50, 50+25=75,  75+25=100, 100+25=125...
..just take some base and does multiply value.

Strange as I get better result with values like 100, 200, 400, 800
I need to re check my simulation
Presumably he's talking about parallel?

You're right. You need resistors with doubling values, to get a linear variable resistance with series connected resistors.
 

Offline Nominal Animal

  • Super Contributor
  • ***
  • Posts: 6173
  • Country: fi
    • My home page and email address
Re: Digital adjustable resistor
« Reply #49 on: September 30, 2022, 09:53:53 pm »
The "optimum" resistor set is linear, down to about half the largest one.
I'm confused.
The "optimum" resistor set for the parallel configuration is linear, i.e. 1/1, 9/10, 4/5, 7/10, 3/5, 1/2.  It was an answer to your question in #42, showing that 'yes, there is an arrangement that gives larger linear range than powers of two', or fully linear upper half.  Rest of the post (tries to) explain why this is so, the drawbacks, and why the parallel configuration is simply unsuited for this.  (Simply put, even the "optimum" resistor set is horribly crappy, if one desires linear steps.)



For the series configuration, with power of two resistances (multiplied by a common constant), the combinations give an exactly linear set.  It's perfect.

Thus, the optimum resistor set for the series configuration has each switchable resistor value be twice the value of the next smaller one, giving a linear distribution, with step size the size of the smallest switchable resistor.

If the smallest resistance is R, and there are N switchable resistors, the total resistance of the combinations is k×R for k=0..2N-1.

If you have some target resistance r, calculate k=(int)(r/R+0.5).   (The add half before truncating to int is all about rounding, so that 4.4 rounds to 4, but 4.6 rounds to 5 and not 4.  With pure integers, k=(r+R/2)/R yields basically the same results.)
If you sort the resistors, so that the smallest resistance corresponds to the least significant bit (bit 0), and the largest resistance to the most significant bit (bit N-1), then the binary representation of k matches the resistor configuration, set bit indicating resistor in use, and clear bit resistor is not used (is shorted).

If you additionally have R (same as the smallest switchable resistance) always in series, then the total resistance of the combinations is k×R for k=1..2N.  Still, k=(int)(r/R+0.5), but now (k-1) in binary represents the resistor states.

In other words, if you do use power of two series, i.e. each resistor twice the value of the smaller one, you do not need any kind of a table at all in your microcontroller.  If you want, I can show you the very simple code needed for this, including the logic that ensures you enable resistors (remove shorts) before you disable them (enable shorts), so that the switching "glitches" always via a temporarily higher resistance, and never via temporarily lower resistance.
« Last Edit: September 30, 2022, 09:58:32 pm by Nominal Animal »
 
The following users thanked this post: eslavko


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf