# Op-Amp Gain and Offset Design with the HP-41C Programmable Calculator

May 26, 2009

This is a program I originally wrote for the HP-67 calculator, and then ported to the HP-41C series. This program is for designing offset-and-gain stages using a single operational amplifier. Such stages are often necessary to convert an input signal covering one range of voltages (e.g., 0.1V to 0.2V from a sensor) to an output signal covering a different range (e.g., 1.0 to 4.0V into an A/D converter).

Mathematically, such a stage performs a linear transformation on the input voltage,

`V _{OUT}` =

`m`

`V`+

_{IN}`b`

where `m` is the *slope* and `b` is the *intercept* or *offset*.

Given an input voltage range, `V _{IL}` to

`V`, and an output voltage range,

_{IH}`V`to

_{OL}`V`, the slope and offset are given by:

_{OH}There are four main cases to consider, with different circuits for each, that are addressed by this program:

- Positive slope and offset (
`m`> 0 and`b`> 0) - Positive slope and negative offset (
`m`> 0 and`b`< 0) - Negative slope and positive offset (
`m`< 0 and`b`> 0) - Negative slope and offset (
`m`< 0 and`b`< 0)

### Positive Slope and Offset

A positive slope and offset stage is implemented by the following circuit:

The designer must select values for `R _{1}` and

`R`, and then appropriate values for

_{F}`R`and

_{2}`R`can be calculated using the following formulae:

_{G}After determining theoretically ideal values for `R _{2}` and

`R`, real-world values can be chosen and the following formula applied to

_{G}`V`and

_{IL}`V`to see the resulting values of

_{IH}`V`and

_{OL}`V`respectively:

_{OH}This case is also used to handle the special case where `b` = 0 (postive gain with no offset). In such a case, the formula for `R _{2}` would result in dividing by zero, which means

`R`is infinite. In other words,

_{2}`R`and

_{2}`V`are not needed. The value of

_{REF}`R`won’t matter then either, and can be replaced by a direct connection. The circuit reduces to:

_{1}### Positive Slope and Negative Offset

The following circuit implements a positive slope and negative offset stage:

After choosing values for `R _{1}` and

`R`, values for

_{F}`R`and

_{2}`R`can be be calculated using these formulae:

_{G}The following formula can then be used to determine the effect of real-world values of `R _{2}` and

`R`on the transformation:

_{G}### Negative Slope and Positive Offset

A negative slope and postive offset stage is implemented by the following circuit:

Given values for `R _{1}` and

`R`, values for

_{F}`R`and

_{2}`R`can be be calculated as follows:

_{G}The following formula can then be used to determine the effect of substituting real-world values of `R _{2}` and

`R`:

_{G}This case is also used to handle the special case where `b` = 0 (negative gain with no offset). As in case 1, the formula for `R _{2}` would involve division by zero, so

`R`and

_{2}`V`are not needed. The non-inverting input of the op-amp can be connected directly to ground, giving the following circuit:

_{REF}### Negative Slope and Offset

The following circuit implements a negative slope and offset stage:

This circuit has no `R _{1}`, so it is only necessary to choose a value for

`R`, after which

_{F}`R`and

_{2}`R`are given by:

_{G}The effect of using real-world values of `R _{2}` and

`R`can then be tested using this formula:

_{G}### Limitations

*Theoretically*, the formulae presented here work perfectly well for gains between -1 and 1 (i.e. |`m`| < 1). However, many real-world op-amps are unstable in such cases. Instead, it will usually be necessary to design for a higher gain (|`m`| ≥ 1), with an attenuator on the input side. The design procedures for this are described on Texas Instruments’ *Op Amp Gain and Offset Page*.

## Using the Program

First type in the program and save it, or read it from a previously recorded magnetic card. The card should be labelled as follows:

OP-AMP GAIN AND OFFSET STAGE DESIGN | ||||
---|---|---|---|---|

V_{REF} |
V,_{IL}V_{IH} |
V,_{OL}V_{OH} |
||

→m,b |
→CASE | R,_{1}R→_{F}R,_{2}R_{G} |
R,_{2}R→_{G}V,_{OL}V_{OH} |

### Forward Solution: Finding `R`_{2} and `R`_{G}

_{2}

_{G}

Consider the following example. A sensor has an output ranging from 0.5V to 0.7V, and we want to interface it to an A/D converter that is expecting an input between 1V and 4V. There is no reference voltage available other than the well regulated 5V supply voltage of the circuit. Use a 10kΩ resistor for `R _{1}`, and 100kΩ for

`R`.

_{F}Follow these steps to solve the problem:

Description | Keystrokes | Display |
---|---|---|

Use engineering notation | ENG 2 | 0.00 00 |

Enter V_{REF} |
5 a |
5.00 00 |

Enter V and _{IL}V_{IH} |
0.5 ENTER 0.7 b |
500. -03 |

Enter V and _{OL}V_{OH} |
1 ENTER 4 c |
1.00 00 |

Compute slope m |
A | 15.0 00 |

Compute offset b |
R/S | -6.50 00 |

Determine case number | B | 2.00 00 |

Enter R and _{1}R, compute _{F}R_{2} |
10 EEx 3 ENTER 100 EEx 3 C |
1.02 03 |

Compute R_{G} |
R/S | 6.21 03 |

#### Notes

It is not necessary to compute the slope and offset (by pressing `A`

) before determining the case number. Likewise, it isn’t necessary to determine the case number (by pressing `B`

) before computing resistor values (although you’ll want to know the case number in order to know which circuit to build). The program keeps track of which information is up to date, and will (re)compute anything that it needs that hasn’t already been computed.

### Reverse Solution: Finding the Effect of `R`_{2} and `R`_{G} on `V`_{OL} and `V`_{OH}

_{2}

_{G}

_{OL}

_{OH}

The closest available 5% resistor values for `R _{2}` and

`R`are 1kΩ and 6.2kΩ respectively. What effect does using these have on the solution? Follow these steps to find out:

_{G}Description | Keystrokes | Display |
---|---|---|

Enter R and _{2}R, compute _{G}V_{OL} |
1 EEx 3 ENTER 6.2 EEx 3 D |
1.13 00 |

Compute V_{OH} |
R/S | 4.15 00 |

This is within the A/D’s input range at the lower bound, but outside the range at the upper bound. What happens if we use the next available value for `R _{G}`, 6.8kΩ, instead?

Description | Keystrokes | Display |
---|---|---|

Enter R and _{2}R, compute _{G}V_{OL} |
1 EEx 3 ENTER 6.8 EEx 3 D |
1.09 00 |

Compute V_{OH} |
R/S | 3.88 00 |

This is almost centered within the desired output range, and covers 93% of it.

The only remaining concern is how component tolerances might affect the solution. This can be analyzed by trying different combinations of `R _{1}`,

`R`,

_{2}`R`, and

_{F}`R`representing resistors that are maximally out of tolerance (±5%) in each direction.

_{G}For example, to test the case where `R _{1}` and

`R`are 5% low and

_{F}`R`and

_{2}`R`are 5% high, follow these steps:

_{G}Description | Keystrokes | Display |
---|---|---|

Enter low R and _{1}R, ignore computed _{F}R_{2} |
0.95 EEx 3 ENTER 95 EEx 3 C |
920. 00 |

Enter high R and _{2}R, compute _{G}V_{OL} |
1.05 EEx 3 ENTER 7.14 EEx 3 D |
808. -03 |

Compute V_{OH} |
R/S | 3.48 00 |

The results show that in this case, the lower limit of the output is out of range, meaning either a redesign is necessary, or tighter tolerance components are needed.

### Cases where `b` = 0

In cases where the offset, `b`, is zero, the program will instead use `b` = 10^{-9} as the offset. This will avoid any division-by-zero errors. The program will then use case 1 (if `m` > 0) or case 3 (if `m` < 0) to compute the solution. In both cases, the computed value for `R _{2}` will be very large, typically around 10

^{9}times the value specified for

`R`. This indicates that

_{1}`R`and

_{2}`V`can be omitted, and that

_{REF}`R`can be replaced by a direct connection.

_{1}## Program Listing

Line | Instruction | Comments |
---|---|---|

01♦ | LBL “GOFF” | |

02♦ | LBL a | Enter available reference voltage |

03 | STO 09 | |

04 | RTN | |

05♦ | LBL b | Enter V and _{IL}V_{IH} |

06 | STO 06 | Store V_{IH} |

07 | x↔y | |

08 | STO 05 | Store V_{IL} |

09 | CF 00 | Must recompute m and b |

10 | RTN | |

11♦ | LBL c | Enter V and _{OL}V_{OH} |

12 | STO 08 | Store V_{OH} |

13 | x↔y | |

14 | STO 07 | Store V_{OL} |

15 | CF 00 | Must recompute m and b |

16 | RTN | |

17♦ | LBL A | Compute transfer function slope (m) and intercept (b) |

18 | FS? 00 | Are m and b already up to date? |

19 | GTO 05 | |

20 | RCL 08 | Compute and store m = (V – _{OH}V) ÷ (_{OL}V – _{IH}V)_{IL} |

21 | RCL 07 | |

22 | − | |

23 | RCL 06 | |

24 | RCL 05 | |

25 | − | |

26 | ÷ | |

27 | STO 20 | |

28 | RCL 05 | Compute and store b = V – _{OH}m V_{IL} |

29 | × | |

30 | RCL 07 | |

31 | − | |

32 | CHS | |

33 | x≠0? | |

34 | GTO 07 | |

35 | 1 E-9 | Make sure b is never exactly 0; |

36♦ | LBL 07 | |

37 | STO 21 | |

38 | SF 00 | Stored m and b are now up to date |

39 | CF 01 | Must recompute case number now |

40♦ | LBL 05 | Display computed or already up-to-date m and b |

41 | RCL 20 | |

42 | RTN | Return, leaving m on stack |

43 | RCL 21 | Display b if user pressed R/S after return |

44 | RTN | |

45♦ | LBL B | Compute case number and store in I |

46 | XEQ A | Make sure m and b are up to date |

47 | FS? 01 | Is case number already up to date |

48 | GTO 08 | |

49 | x<0? | |

50 | GTO 06 | Handle cases where m < 0 |

51 | 1 | |

52 | STO 25 | |

53 | RCL 21 | |

54 | x<0? | |

55 | ISZ 25 | Case 2: m positive and b negative |

56 | GTO 08 | Case 1: m positive and b positive |

57♦ | LBL 06 | Cases where m is negative |

58 | 3 | |

59 | STO 25 | |

60 | RCL 21 | |

61 | x<0? | |

62 | ISZ 25 | Case 4: m negative and b negative |

63♦ | LBL 08 | Case 3: m negative and b positive |

64 | SF 01 | Case number is now up to date (or was already up to date) |

65 | RCL 25 | Display case number and return |

66 | RTN | |

67♦ | LBL C | Compute solution using appropriate case |

68 | STO 03 | Store R_{F} |

69 | x↔y | |

70 | STO 01 | Store R_{1} |

71 | XEQ B | Get case number |

72 | GTO IND 25 | Branch to appropriate case |

73♦ | LBL 01 | Positive m and positive b |

74 | RCL 01 | Compute and store R = _{2}V _{REF}R _{1}m ÷ b |

75 | RCL 09 | |

76 | × | |

77 | RCL 20 | |

78 | × | |

79 | RCL 21 | |

80 | ÷ | |

81 | STO 02 | |

82 | R/S | Display R; 9.99e99 means “open circuit”_{2} |

83 | RCL 09 | Compute and store R = _{G}V _{REF}R ÷ (_{F}V (_{REF}m – 1) + b) |

84 | RCL 03 | |

85 | × | |

86 | XEQ 09 | Compute V (_{REF}m – 1) + b |

87 | GTO 05 | Divide, store R, and return, leaving _{G}R on stack_{G} |

88♦ | LBL 02 | Positive m and negative b |

89 | RCL 01 | Compute and store R = –_{2}R _{1}b ÷ (V (_{REF}m – 1) + b) |

90 | RCL 21 | |

91 | CHS | |

92 | × | |

93 | XEQ 09 | Compute V (_{REF}m – 1) + b, leaving V (_{REF}m – 1) in register 0 |

94 | ÷ | |

95 | STO 02 | |

96 | R/S | Display R_{2} |

97 | RCL 01 | Compute and store R = (_{G}R _{1}b + V _{REF}R) ÷ (_{F}V (_{REF}m – 1)) |

98 | RCL 21 | |

99 | × | |

100 | RCL 09 | |

101 | RCL 03 | |

102 | × | |

103 | + | |

104 | RCL 00 | Subroutine 9 left V (_{REF}m – 1) in register 0 for us |

105 | GTO 05 | Divide, store R, and return, leaving _{G}R on stack_{G} |

106♦ | LBL 03 | Negative m and positive b |

107 | RCL 01 | Compute and store R = _{2}R (_{1}V (_{REF}m – 1) + b) ÷ –b |

108 | XEQ 09 | Compute V (_{REF}m – 1) + b |

109 | × | |

110 | RCL 21 | |

111 | CHS | |

112 | ÷ | |

113 | STO 02 | |

114 | R/S | Display R_{2} |

115 | RCL 03 | Compute and store R = –_{G}R ÷ _{F}m |

116 | CHS | |

117 | RCL 20 | |

118 | GTO 05 | Divide, store R, and return, leaving _{G}R on stack_{G} |

119♦ | LBL 04 | Negative m and negative b |

120 | RCL 09 | Compute and store R = _{2}V _{REF}R ÷ –_{F}b |

121 | RCL 03 | |

122 | × | |

123 | RCL 21 | |

124 | CHS | |

125 | ÷ | |

126 | STO 02 | |

127 | R/S | Display R_{2} |

128 | RCL 03 | Compute and store R = _{G}R ÷ _{F}m |

129 | RCL 20 | |

130 | CHS | |

131♦ | LBL 05 | Common code to compute y ÷ x and store in R_{G} |

132 | ÷ | |

133 | STO 04 | |

134 | RTN | Return, leaving R on stack_{G} |

135♦ | LBL 09 | Compute V (_{REF}m – 1) + b, leaving V (_{REF}m – 1) in register 0 |

136 | RCL 20 | |

137 | 1 | |

138 | − | |

139 | RCL 09 | |

140 | × | |

141 | STO 00 | Save V (_{REF}m – 1) in register 0 for later |

142 | RCL 21 | |

143 | + | |

144 | RTN | |

145♦ | LBL D | Compute V,_{OL}V using supplied _{OH}R,_{2}R_{G} |

146 | STO 04 | Store R_{G} |

147 | x↔y | |

148 | STO 02 | Store R_{2} |

149 | XEQ B | Get case number |

150 | RCL 05 | |

151 | XEQ IND 25 | Compute V from _{OL}V_{IL} |

152 | R/S | Display computed V_{OL} |

153 | RCL 06 | |

154 | GTO IND 25 | Compute V from _{OH}V_{IH} |

155♦ | LBL 01 | Compute V for positive _{OUT}m and positive b |

156 | RCL 02 | |

157 | × | |

158♦ | LBL 00 | Entry point to compute (x + V _{REF}R) (1 + _{1}R ÷ _{F}R) ÷ (_{G}R + _{1}R)_{2} |

159 | RCL 09 | |

160 | RCL 01 | |

161 | × | |

162 | + | |

163 | RCL 03 | |

164 | RCL 04 | |

165 | ÷ | |

166 | 1 | |

167 | + | |

168 | × | |

169 | RCL 01 | |

170 | RCL 02 | |

171 | + | |

172 | ÷ | |

173 | RTN | |

174♦ | LBL 02 | Compute V for positive _{OUT}m and negative b |

175 | RCL 01 | |

176 | 1/x | |

177 | RCL 02 | |

178 | 1/x | |

179 | + | |

180 | 1/x | |

181 | RCL 04 | |

182 | + | |

183 | STO 00 | Store R + _{G}R _{1}R ÷ (_{2}R + _{1}R) for later_{2} |

184 | RCL 03 | |

185 | + | |

186 | × | |

187 | RCL 00 | |

188 | ÷ | |

189 | RCL 09 | |

190 | RCL 02 | |

191 | × | |

192 | RCL 03 | |

193 | × | |

194 | RCL 01 | |

195 | RCL 02 | |

196 | + | |

197 | RCL 00 | |

198 | × | |

199 | ÷ | |

200 | − | |

201 | RTN | |

202♦ | LBL 03 | Compute V for negative _{OUT}m and positive b |

203 | 0 | |

204 | XEQ 00 | Compute (V _{REF}R) (1 + _{1}R ÷ _{F}R) ÷ (_{G}R + _{1}R)_{2} |

205 | GTO 09 | Subtract V _{IN}R ÷ _{F}R_{G} |

206♦ | LBL 04 | Compute V for negative _{OUT}m and negative b |

207 | RCL 09 | |

208 | RCL 03 | |

209 | × | |

210 | RCL 02 | |

211 | ÷ | |

212 | CHS | |

213♦ | LBL 09 | Entry point to compute x – y R ÷ _{F}R_{G} |

214 | x↔y | |

215 | RCL 03 | |

216 | × | |

217 | RCL 04 | |

218 | ÷ | |

219 | − | |

220 | RTN |

Two interesting aspects of this program are its use of indirect addressing for branching to the forward and reverse solution subroutines for each of the four cases, and its use of repeated labels.

The forward solution for each of the four slope/offset cases is implemented by a sequence of instructions starting with the label corresponding to the case number (1 to 4). When the user presses `C`

, the case number is computed if necessary, and a `GTO (i)`

instruction then branches to the appropriate case.

Similarly, the reverse solution for each case is also labeled according to the case number. When the user presses `D`

, `V _{IL}` is recalled to the stack, after which a

`GSB (i)`

instruction causes `V`to be calculated. Then

_{OL}`V`is recalled, and

_{IH}`GTO (i)`

is used to calculate `V`and then return.

_{OH}So, there are two each of `LBL 1`

through `LBL 4`

. This works because the HP-41C/CV/CX (and other vintage HP calculators) search forwards from the current step for the matching label. Thus the `GTO (i)`

in step 73 will branch to the appropriate forward solution, and the `GSB (i)`

in step 152 and `GTO (i)`

in step 155 will branch to the appropriate reverse solution.

This program also makes use of many small subroutines to compute sub-expressions common to multiple solutions. Since there were not enough labels for all the subroutines needed, the same labels were used more than once. Had this not been done, the same sequence of steps would have been repeated several times, and the program would not have fit into the calculator’s 224 step memory.

## Registers and Flags

Register | Use |
---|---|

00 | Temporary register |

01 | R (Ohms)_{1} |

02 | R (Ohms)_{2} |

03 | R (Ohms)_{F} |

04 | R (Ohms)_{G} |

05,06 | V and _{IL}V, input voltage range_{IH} |

07,08 | V and _{OL}V, output voltage range_{OH} |

09 | V_{REF} |

11 | m, slope of transfer function |

12 | b, intercept of transfer function |

10 | Case number (1,2,3,4) |

Flag | Meaning |
---|---|

00 | m and b are up to date |

01 | Case number is up to date |

## Revision History

2009-May-26 — Initial release.

## Related Articles

If you've found this article useful, you may also be interested in:

- Op-Amp Oscillator Design with the HP-41C Programmable Calculator
- Low-Sensitivity Sallen-Key Filter Design with the HP-41C Programmable Calculator
- Low-Sensitivity Sallen-Key Filter Design with the HP-67 Programmable Calculator
- Op-Amp Gain and Offset Design with the HP-67 Programmable Calculator
- Op-Amp Oscillator Design with the HP-67 Programmable Calculator
- Resistor Network Solver for the HP-67 Programmable Calculator
- A Matrix Multi-Tool for the HP 35s Programmable Calculator
- Curve Fitting for the HP 35s Programmable Calculator

Buy Stefan a coffee! If you've found this article

useful, consider
leaving a donation to help support

stefanv.com

**Disclaimer:** Although every effort has been
made to ensure accuracy and reliability, the information on this web
page is presented without warranty of any kind, and Stefan Vorkoetter
assumes no liability for direct or consequential damages caused by its
use. It is up to you, the reader, to determine the suitability of, and
assume responsibility for, the use of this information. Links to
Amazon.com merchandise are provided in association with Amazon.com.
Links to eBay searches are provided in association with the eBay
partner network.

**Copyright:** All materials on this web site, including the
text, images, and mark-up, are Copyright © 2017 by Stefan Vorkoetter unless otherwise noted. All rights reserved.
Unauthorized duplication prohibited. You __may__ link to this site
or pages within it, but you may __not__ link directly to images on
this site, and you may __not__ copy any material from this site to
another web site or other publication without express written
permission. You may make copies for your own personal use.